implement delete directory action to VDirectoryTree

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-05 15:19:17 +08:00
parent 09f6415536
commit 5d08a6f93d
2 changed files with 73 additions and 0 deletions

View File

@ -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) {

View File

@ -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