From bb142016b82a9c4b0e3d67771c7fa01eb3d72ef3 Mon Sep 17 00:00:00 2001 From: cutefishd Date: Wed, 31 Mar 2021 10:40:38 +0800 Subject: [PATCH] Add new folder dialog and terminal opening --- CMakeLists.txt | 1 + dialogs/createfolderdialog.cpp | 33 +++++++++++++ dialogs/createfolderdialog.h | 22 +++++++++ model/foldermodel.cpp | 22 ++++++++- model/foldermodel.h | 1 + qml/Dialogs/CreateFolderDialog.qml | 62 ++++++++++++++++++++++-- qml/FolderPage.qml | 4 -- translations/zh_CN.ts | 77 +++++++++++++++++++----------- 8 files changed, 186 insertions(+), 36 deletions(-) create mode 100644 dialogs/createfolderdialog.cpp create mode 100644 dialogs/createfolderdialog.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c0e2182..82c642a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ add_executable(cutefish-filemanager model/positioner.cpp dialogs/propertiesdialog.cpp + dialogs/createfolderdialog.cpp widgets/rubberband.cpp widgets/itemviewadapter.cpp diff --git a/dialogs/createfolderdialog.cpp b/dialogs/createfolderdialog.cpp new file mode 100644 index 0000000..9caad21 --- /dev/null +++ b/dialogs/createfolderdialog.cpp @@ -0,0 +1,33 @@ +#include "createfolderdialog.h" +#include +#include +#include + +#include + +CreateFolderDialog::CreateFolderDialog(QObject *parent) + : QObject(parent) +{ + +} + +void CreateFolderDialog::setPath(const QString &path) +{ + m_path = path; +} + +void CreateFolderDialog::show() +{ + QQmlApplicationEngine *engine = new QQmlApplicationEngine; + engine->rootContext()->setContextProperty("main", this); + engine->load(QUrl("qrc:/qml/Dialogs/CreateFolderDialog.qml")); +} + +void CreateFolderDialog::newFolder(const QString &folderName) +{ + if (m_path.isEmpty() || folderName.isEmpty()) + return; + + auto job = KIO::mkdir(QUrl(m_path + "/" + folderName)); + job->start(); +} diff --git a/dialogs/createfolderdialog.h b/dialogs/createfolderdialog.h new file mode 100644 index 0000000..8676e45 --- /dev/null +++ b/dialogs/createfolderdialog.h @@ -0,0 +1,22 @@ +#ifndef CREATEFOLDERDIALOG_H +#define CREATEFOLDERDIALOG_H + +#include + +class CreateFolderDialog : public QObject +{ + Q_OBJECT + +public: + explicit CreateFolderDialog(QObject *parent = nullptr); + + void setPath(const QString &path); + void show(); + + Q_INVOKABLE void newFolder(const QString &folderName); + +private: + QString m_path; +}; + +#endif // CREATEFOLDERDIALOG_H diff --git a/model/foldermodel.cpp b/model/foldermodel.cpp index 1dbdfce..0328d2b 100644 --- a/model/foldermodel.cpp +++ b/model/foldermodel.cpp @@ -2,6 +2,7 @@ #include "dirlister.h" #include "../dialogs/propertiesdialog.h" +#include "../dialogs/createfolderdialog.h" // Qt #include @@ -36,6 +37,7 @@ #include #include #include +#include FolderModel::FolderModel(QObject *parent) : QSortFilterProxyModel(parent) @@ -596,6 +598,13 @@ void FolderModel::unpinSelection() m_pinnedSelection = QItemSelection(); } +void FolderModel::newFolder() +{ + CreateFolderDialog *dlg = new CreateFolderDialog; + dlg->setPath(rootItem().url().toString()); + dlg->show(); +} + void FolderModel::rename(int row, const QString &name) { if (row < 0) @@ -852,7 +861,17 @@ void FolderModel::openPropertiesDialog() void FolderModel::openInTerminal() { - qDebug() << "TODO"; + QString url; + if (m_selectionModel->hasSelection()) { + KFileItem item = itemForIndex(m_selectionModel->selectedIndexes().first()); + if (item.isDir()) { + url = item.url().toLocalFile(); + } + } else { + url = rootItem().url().toLocalFile(); + } + + KToolInvocation::invokeTerminal(QString(), url); } void FolderModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) @@ -986,6 +1005,7 @@ void FolderModel::createActions() connect(paste, &QAction::triggered, this, &FolderModel::paste); QAction *newFolder = new QAction(tr("New Folder"), this); + connect(newFolder, &QAction::triggered, this, &FolderModel::newFolder); QAction *trash = new QAction(tr("Move To Trash"), this); connect(trash, &QAction::triggered, this, &FolderModel::moveSelectedToTrash); diff --git a/model/foldermodel.h b/model/foldermodel.h index d58ca1c..2760e3b 100644 --- a/model/foldermodel.h +++ b/model/foldermodel.h @@ -128,6 +128,7 @@ public: Q_INVOKABLE void pinSelection(); Q_INVOKABLE void unpinSelection(); + Q_INVOKABLE void newFolder(); Q_INVOKABLE void rename(int row, const QString &name); Q_INVOKABLE void copy(); Q_INVOKABLE void paste(); diff --git a/qml/Dialogs/CreateFolderDialog.qml b/qml/Dialogs/CreateFolderDialog.qml index ad7209f..971225f 100644 --- a/qml/Dialogs/CreateFolderDialog.qml +++ b/qml/Dialogs/CreateFolderDialog.qml @@ -1,12 +1,66 @@ import QtQuick 2.12 import QtQuick.Controls 2.12 +import QtQuick.Window 2.12 import QtQuick.Layouts 1.12 import MeuiKit 1.0 as Meui -Dialog { +Window { id: control - modal: true - x: (parent.width - control.width) / 2 - y: (parent.height - control.height) / 2 + title: qsTr("New Folder") + flags: Qt.Dialog + visible: true + + width: 400 + Meui.Units.largeSpacing * 2 + height: _mainLayout.implicitHeight + Meui.Units.largeSpacing * 4 + + minimumWidth: width + minimumHeight: height + maximumWidth: width + maximumHeight: height + + Rectangle { + anchors.fill: parent + color: Meui.Theme.backgroundColor + } + + ColumnLayout { + id: _mainLayout + anchors.fill: parent + anchors.leftMargin: Meui.Units.largeSpacing + anchors.rightMargin: Meui.Units.largeSpacing + spacing: 0 + + RowLayout { + Label { + text: qsTr("Name") + } + + TextField { + id: _textField + Layout.fillWidth: true + Keys.onEscapePressed: control.close() + focus: true + } + } + + RowLayout { + Button { + text: qsTr("Cancel") + Layout.fillWidth: true + onClicked: control.close() + } + + Button { + text: qsTr("OK") + Layout.fillWidth: true + onClicked: { + main.newFolder(_textField.text) + control.close() + } + enabled: _textField.text + flat: true + } + } + } } diff --git a/qml/FolderPage.qml b/qml/FolderPage.qml index 660af43..48482bd 100644 --- a/qml/FolderPage.qml +++ b/qml/FolderPage.qml @@ -129,10 +129,6 @@ Item { onCountChanged: { _fileTips.visible = count === 0 } - -// Component.onCompleted: { -// folderModel.requestRename.connect(rename) -// } } } diff --git a/translations/zh_CN.ts b/translations/zh_CN.ts index 823db24..f1dfda1 100644 --- a/translations/zh_CN.ts +++ b/translations/zh_CN.ts @@ -1,10 +1,33 @@ + + CreateFolderDialog + + + New Folder + 新建文件夹 + + + + Name + 名称 + + + + Cancel + 取消 + + + + OK + 确定 + + DesktopView - + Desktop 桌面 @@ -12,82 +35,82 @@ FolderModel - + %1 selected 选中了 %1 项 - + %1 item %1 项 - + %1 items %1 项 - + Select All 全选 - + Open 打开 - + Cut 剪切 - + Copy 复制 - + Paste 粘贴 - + New Folder 新建文件夹 - + Move To Trash 移动到回收站 - + Empty Trash 清空回收站 - + Delete 删除 - + Rename 重命名 - + Open in Terminal 在终端中打开 - + Set as Wallpaper 设置为壁纸 - + Properties 属性 @@ -95,12 +118,12 @@ FolderPage - + No files 无文件 - + Empty Trash 清空回收站 @@ -108,42 +131,42 @@ PlacesModel - + Home 主文件夹 - + Desktop 桌面 - + Documents 文档 - + Downloads 下载 - + Music 音乐 - + Pictures 图片 - + Videos 视频 - + Trash 回收站