From 7c89a8078fdc91c37d3e9c3ce7d8f071b9cd13d4 Mon Sep 17 00:00:00 2001 From: kateleet Date: Sat, 25 Dec 2021 01:46:16 +0800 Subject: [PATCH] feat: support drop files --- CMakeLists.txt | 1 + qml/TextEditor.qml | 10 ++++++++-- qml/main.qml | 33 ++++++++++++++++++++++++++++++--- src/highlightmodel.cpp | 2 -- src/main.cpp | 2 ++ src/texteditor.cpp | 12 ++++++++++++ src/texteditor.h | 19 +++++++++++++++++++ 7 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/texteditor.cpp create mode 100644 src/texteditor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 386859e..45df845 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(PROJECT_SOURCES src/main.cpp src/documenthandler.cpp src/highlightmodel.cpp + src/texteditor.cpp qml.qrc ) diff --git a/qml/TextEditor.qml b/qml/TextEditor.qml index a1e0335..8513c65 100644 --- a/qml/TextEditor.qml +++ b/qml/TextEditor.qml @@ -14,8 +14,8 @@ Item { property bool showLineNumbers: true property int characterCount: body.text.length - height: ListView.view.height - width: ListView.view.width + height: ListView.view ? ListView.view.height : 0 + width: ListView.view ? ListView.view.width : 0 DocumentHandler { id: document @@ -25,6 +25,7 @@ Item { selectionEnd: body.selectionEnd backgroundColor: FishUI.Theme.backgroundColor enableSyntaxHighlighting: true + theme: FishUI.Theme.darkMode ? "Breeze Dark" : "Breeze Light" onSearchFound: { body.select(start, end) @@ -47,6 +48,11 @@ Item { Flickable { id: _flickable + FishUI.WheelHandler { + id: wheelHandler + target: _flickable + } + boundsBehavior: Flickable.StopAtBounds boundsMovement: Flickable.StopAtBounds diff --git a/qml/main.qml b/qml/main.qml index ca1fef3..150e6c1 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -3,6 +3,7 @@ import QtQuick.Window 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 1.15 import FishUI 1.0 as FishUI +import Cutefish.TextEditor 1.0 FishUI.Window { id: root @@ -13,6 +14,14 @@ FishUI.Window { visible: true title: qsTr("Text Editor") + FileHelper { + id: fileHelper + + onNewPath: { + _tabView.addTab(textEditorCompeont, { fileUrl: path }) + } + } + headerItem: Item { Rectangle { anchors.fill: parent @@ -23,7 +32,7 @@ FishUI.Window { id: _tabbar anchors.fill: parent anchors.margins: FishUI.Units.smallSpacing / 2 - anchors.rightMargin: FishUI.Units.largeSpacing * 2 + anchors.rightMargin: FishUI.Units.largeSpacing * 4 model: _tabView.count currentIndex : _tabView.currentIndex @@ -59,6 +68,19 @@ FishUI.Window { } } + DropArea { + id: _dropArea + anchors.fill: parent + + onDropped: { + if (drop.hasUrls) { + for (var i = 0; i < drop.urls.length; ++i) { + fileHelper.addPath(drop.urls[i]) + } + } + } + } + ColumnLayout { anchors.fill: parent spacing: 0 @@ -87,12 +109,17 @@ FishUI.Window { anchors.bottomMargin: FishUI.Units.smallSpacing Label { - text: qsTr("Characters %1").arg(_tabView.currentItem.characterCount) + text: _tabView.currentItem ? qsTr("Characters %1").arg(_tabView.currentItem.characterCount) + : "" } } } } + function addPath(path) { + _tabView.addTab(textEditorCompeont, { fileUrl: path }) + } + function addTab() { _tabView.addTab(textEditorCompeont, {}) } @@ -106,6 +133,6 @@ FishUI.Window { } Component.onCompleted: { - addTab() + // addTab() } } diff --git a/src/highlightmodel.cpp b/src/highlightmodel.cpp index 615ab0e..eb82f89 100644 --- a/src/highlightmodel.cpp +++ b/src/highlightmodel.cpp @@ -7,7 +7,5 @@ HighlightModel::HighlightModel(QObject *parent) for (KSyntaxHighlighting::Definition def : m_repository.definitions()) { if (def.isHidden()) continue; - - qDebug() << def.translatedName(); } } diff --git a/src/main.cpp b/src/main.cpp index 5990811..e583821 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include "documenthandler.h" #include "highlightmodel.h" +#include "texteditor.h" int main(int argc, char *argv[]) { @@ -12,6 +13,7 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); qmlRegisterType("Cutefish.TextEditor", 1, 0, "DocumentHandler"); + qmlRegisterType("Cutefish.TextEditor", 1, 0, "FileHelper"); HighlightModel m; diff --git a/src/texteditor.cpp b/src/texteditor.cpp new file mode 100644 index 0000000..57cbed7 --- /dev/null +++ b/src/texteditor.cpp @@ -0,0 +1,12 @@ +#include "texteditor.h" + +FileHelper::FileHelper(QObject *parent) + : QObject(parent) +{ + +} + +void FileHelper::addPath(const QString &path) +{ + emit newPath(path); +} diff --git a/src/texteditor.h b/src/texteditor.h new file mode 100644 index 0000000..e7657d0 --- /dev/null +++ b/src/texteditor.h @@ -0,0 +1,19 @@ +#ifndef TEXTEDITOR_H +#define TEXTEDITOR_H + +#include + +class FileHelper : public QObject +{ + Q_OBJECT + +public: + explicit FileHelper(QObject *parent = nullptr); + + Q_INVOKABLE void addPath(const QString &path); + +signals: + void newPath(const QString &path); +}; + +#endif // TEXTEDITOR_H