From d733c9d55c19df434c5b98b6595a5f4c56364798 Mon Sep 17 00:00:00 2001 From: kateleet Date: Mon, 27 Dec 2021 01:32:21 +0800 Subject: [PATCH] feat: add new documents option --- model/foldermodel.cpp | 42 ++++++++++++++++++++++++++++++ model/foldermodel.h | 1 + qml.qrc | 1 + templates/TextFile.txt | 1 + translations/en_US.ts | 50 ++++++++++++++++++++++-------------- translations/zh_CN.ts | 58 ++++++++++++++++++++++-------------------- 6 files changed, 107 insertions(+), 46 deletions(-) create mode 100644 templates/TextFile.txt diff --git a/model/foldermodel.cpp b/model/foldermodel.cpp index cc34e97..8d0a28a 100644 --- a/model/foldermodel.cpp +++ b/model/foldermodel.cpp @@ -914,6 +914,33 @@ void FolderModel::newFolder() job->start(); } +void FolderModel::newTextFile() +{ + QString rootPath = rootItem().url().toString(); + QString baseName = tr("New Text"); + QString newName = baseName; + + int i = 0; + while (true) { + if (QFile::exists(rootItem().url().toLocalFile() + "/" + newName)) { + ++i; + newName = QString("%1%2").arg(baseName).arg(QString::number(i)); + } else { + break; + } + } + + m_newDocumentUrl = QUrl(rootItem().url().toString() + "/" + newName); + + QFile srcFile(":/templates/TextFile.txt"); + + if (!srcFile.open(QIODevice::ReadOnly)) { + return; + } + + srcFile.copy(m_newDocumentUrl.toLocalFile()); +} + void FolderModel::rename(int row, const QString &name) { if (row < 0) @@ -1229,7 +1256,11 @@ void FolderModel::openContextMenu(QQuickItem *visualParent, Qt::KeyboardModifier QAction *selectAll = new QAction(tr("Select All"), this); connect(selectAll, &QAction::triggered, this, &FolderModel::selectAll); + QMenu *newMenu = new QMenu(tr("New Documents")); + newMenu->addAction(m_actionCollection.action("newTextFile")); + menu->addAction(m_actionCollection.action("newFolder")); + menu->addMenu(newMenu); menu->addSeparator(); menu->addAction(m_actionCollection.action("paste")); menu->addAction(selectAll); @@ -1675,6 +1706,11 @@ void FolderModel::createActions() QAction *newFolder = new QAction(tr("New Folder"), this); connect(newFolder, &QAction::triggered, this, &FolderModel::newFolder); + QMenu *newDocuments = new QMenu(tr("New Documents")); + QAction *newTextFile = new QAction(tr("New Text"), this); + connect(newTextFile, &QAction::triggered, this, &FolderModel::newTextFile); + newDocuments->addAction(newTextFile); + QAction *trash = new QAction(tr("Move To Trash"), this); connect(trash, &QAction::triggered, this, &FolderModel::moveSelectedToTrash); @@ -1716,6 +1752,7 @@ void FolderModel::createActions() m_actionCollection.addAction(QStringLiteral("copy"), copy); m_actionCollection.addAction(QStringLiteral("paste"), paste); m_actionCollection.addAction(QStringLiteral("newFolder"), newFolder); + m_actionCollection.addAction(QStringLiteral("newTextFile"), newTextFile); m_actionCollection.addAction(QStringLiteral("trash"), trash); m_actionCollection.addAction(QStringLiteral("emptyTrash"), emptyTrash); m_actionCollection.addAction(QStringLiteral("del"), del); @@ -1792,6 +1829,11 @@ void FolderModel::updateActions() newFolder->setEnabled(rootItem().isWritable()); } + if (QAction *newTextFile = m_actionCollection.action(QStringLiteral("newTextFile"))) { + newTextFile->setVisible(!isTrash); + newTextFile->setEnabled(rootItem().isWritable()); + } + if (QAction *paste = m_actionCollection.action(QStringLiteral("paste"))) { bool enable = false; diff --git a/model/foldermodel.h b/model/foldermodel.h index ce1811c..6920ec8 100644 --- a/model/foldermodel.h +++ b/model/foldermodel.h @@ -188,6 +188,7 @@ public: Q_INVOKABLE void unpinSelection(); Q_INVOKABLE void newFolder(); + Q_INVOKABLE void newTextFile(); Q_INVOKABLE void rename(int row, const QString &name); Q_INVOKABLE void copy(); Q_INVOKABLE void paste(); diff --git a/qml.qrc b/qml.qrc index 1c76bec..66f91a2 100644 --- a/qml.qrc +++ b/qml.qrc @@ -72,5 +72,6 @@ images/drive-harddisk.svg images/drive-optical.svg images/drive-removable-media-usb.svg + templates/TextFile.txt diff --git a/templates/TextFile.txt b/templates/TextFile.txt new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/templates/TextFile.txt @@ -0,0 +1 @@ + diff --git a/translations/en_US.ts b/translations/en_US.ts index 1353d83..1299640 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -142,98 +142,110 @@ The file or folder %1 does not exist. - + Select All Select All - + File Manager File Manager - + Open Open - + Open with Open with - + Cut Cut - + Copy Copy - + Paste Paste - + New Folder New Folder - + + + New Text + + + + + + New Documents + + + + Move To Trash Move To Trash - + Empty Trash Empty Trash - + Delete Delete - + Rename Rename - + Open in Terminal Open in Terminal - + Set as Wallpaper Set as Wallpaper - + Properties Properties - + Change background Change background - + Restore Restore - + Show hidden files Show hidden files - + Open in new window diff --git a/translations/zh_CN.ts b/translations/zh_CN.ts index 5a52b3a..21abaaa 100644 --- a/translations/zh_CN.ts +++ b/translations/zh_CN.ts @@ -142,98 +142,110 @@ 文件或文件夹 %1 不存在 - + Select All 全选 - + File Manager 文件管理器 - + Open 打开 - + Open with 打开方式 - + Cut 剪切 - + Copy 复制 - + Paste 粘贴 - + New Folder 新建文件夹 - + + + New Text + 新建文本 + + + + + New Documents + 新建文档 + + + Move To Trash 移动到回收站 - + Empty Trash 清空回收站 - + Delete 删除 - + Rename 重命名 - + Open in Terminal 在终端中打开 - + Set as Wallpaper 设置为壁纸 - + Properties 属性 - + Change background 更改桌面背景 - + Restore 恢复 - + Show hidden files 显示隐藏文件 - + Open in new window 在新窗口中打开 @@ -454,10 +466,6 @@ PropertiesDialog - - Properties - 属性 - Type: @@ -503,10 +511,6 @@ OK 确定 - - %1 files - %1 项 - SideBar