From 5d08a6f93d853512b6e7dbc992c976bf1efeb6c8 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 5 Oct 2016 15:19:17 +0800 Subject: [PATCH] implement delete directory action to VDirectoryTree Signed-off-by: Le Tan --- vdirectorytree.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++ vdirectorytree.h | 4 +++ 2 files changed, 73 insertions(+) diff --git a/vdirectorytree.cpp b/vdirectorytree.cpp index 69209844..a7ab44cc 100644 --- a/vdirectorytree.cpp +++ b/vdirectorytree.cpp @@ -33,6 +33,11 @@ void VDirectoryTree::initialActions() newSubDirAct->setStatusTip(tr("Create a new sub-directory")); connect(newSubDirAct, &QAction::triggered, this, &VDirectoryTree::newSubDirectory); + + deleteDirAct = new QAction(tr("&Delete"), this); + deleteDirAct->setStatusTip(tr("Delete selected directory")); + connect(deleteDirAct, &QAction::triggered, + this, &VDirectoryTree::deleteDirectory); } void VDirectoryTree::setTreePath(const QString& path) @@ -116,6 +121,11 @@ QTreeWidgetItem* VDirectoryTree::insertDirectoryTreeItem(QTreeWidgetItem *parent return item; } +void VDirectoryTree::removeDirectoryTreeItem(QTreeWidgetItem *item) +{ + delete item; +} + void VDirectoryTree::updateDirectoryTreeTopLevel() { const QString &path = treePath; @@ -299,6 +309,7 @@ void VDirectoryTree::contextMenuRequested(QPoint pos) menu.addAction(newRootDirAct); menu.addAction(newSubDirAct); } + menu.addAction(deleteDirAct); } menu.exec(mapToGlobal(pos)); } @@ -390,6 +401,22 @@ void VDirectoryTree::newRootDirectory() } while (true); } +void VDirectoryTree::deleteDirectory() +{ + QTreeWidgetItem *curItem = currentItem(); + QJsonObject curItemJson = curItem->data(0, Qt::UserRole).toJsonObject(); + QString curItemName = curItemJson["name"].toString(); + + QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete directory \"%1\"?") + .arg(curItemName)); + msgBox.setInformativeText(tr("This will delete any files under this directory.")); + msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Ok); + if (msgBox.exec() == QMessageBox::Ok) { + deleteDirectoryAndUpdateTree(curItem); + } +} + QTreeWidgetItem* VDirectoryTree::createDirectoryAndUpdateTree(QTreeWidgetItem *parent, const QString &name, const QString &description) { @@ -437,6 +464,48 @@ QTreeWidgetItem* VDirectoryTree::createDirectoryAndUpdateTree(QTreeWidgetItem *p return insertDirectoryTreeItem(parent, NULL, itemJson); } +void VDirectoryTree::deleteDirectoryAndUpdateTree(QTreeWidgetItem *item) +{ + QJsonObject itemJson = item->data(0, Qt::UserRole).toJsonObject(); + QString itemName = itemJson["name"].toString(); + QString relativePath = itemJson["relative_path"].toString(); + + // Update parent's config file to exclude this directory + QString path = QDir(treePath).filePath(relativePath); + QJsonObject configJson = readDirectoryConfig(path); + QJsonArray subDirArray = configJson["sub_directories"].toArray(); + bool deleted = false; + for (int i = 0; i < subDirArray.size(); ++i) { + QJsonObject ele = subDirArray[i].toObject(); + if (ele["name"].toString() == itemName) { + subDirArray.removeAt(i); + deleted = true; + break; + } + } + if (!deleted) { + qWarning() << "error: fail to find" << itemName << "to delete in its parent's configuration file"; + return; + } + configJson["sub_directories"] = subDirArray; + if (!writeDirectoryConfig(path, configJson)) { + qWarning() << "error: fail to update parent's configuration file to delete" << itemName; + return; + } + + // Delete the entire directory + QString dirName = QDir(path).filePath(itemName); + QDir dir(dirName); + if (!dir.removeRecursively()) { + qWarning() << "error: fail to delete" << dirName << "recursively"; + } else { + qDebug() << "delete" << dirName << "recursively"; + } + + // Update the tree + removeDirectoryTreeItem(item); +} + bool VDirectoryTree::isConflictNameWithChildren(const QTreeWidgetItem *parent, const QString &name) { if (parent) { diff --git a/vdirectorytree.h b/vdirectorytree.h index d195122c..f17e520b 100644 --- a/vdirectorytree.h +++ b/vdirectorytree.h @@ -26,6 +26,7 @@ private slots: void newSiblingDirectory(); void newSubDirectory(); void newRootDirectory(); + void deleteDirectory(); private: // Clean and pdate the TreeWidget according to treePath @@ -45,6 +46,7 @@ private: void initialActions(); QTreeWidgetItem* createDirectoryAndUpdateTree(QTreeWidgetItem *parent, const QString &name, const QString &description); + void deleteDirectoryAndUpdateTree(QTreeWidgetItem *item); // If @name conflict with the children's names of @parent. bool isConflictNameWithChildren(const QTreeWidgetItem *parent, const QString &name); // Read config from the directory config json file into a QJsonObject @@ -53,6 +55,7 @@ private: bool deleteDirectoryConfig(const QString &path); QTreeWidgetItem* insertDirectoryTreeItem(QTreeWidgetItem *parent, QTreeWidgetItem *preceding, const QJsonObject &newItem); + void removeDirectoryTreeItem(QTreeWidgetItem *item); // The path of the directory tree root QString treePath; @@ -63,6 +66,7 @@ private: QAction *newRootDirAct; QAction *newSiblingDirAct; QAction *newSubDirAct; + QAction *deleteDirAct; }; #endif // VDIRECTORYTREE_H