diff options
author | Eike Ziller <eike.ziller@qt.io> | 2017-11-30 16:32:55 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2017-12-12 10:50:29 +0000 |
commit | 7b6943e578f7e94493ebd76035c8c627c8c40064 (patch) | |
tree | 99808e10db6fef9eaa2012bac2e49943b7844c01 | |
parent | 7cba5fc48222b3bd34a0aa77261a7fc452006f35 (diff) | |
download | qt-creator-7b6943e578f7e94493ebd76035c8c627c8c40064.tar.gz |
Add "Remove File" to file system view
Task-number: QTCREATORBUG-19208
Change-Id: Iacf1e1468018fa8dd3426bc1325f36bb39a4b4e2
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
-rw-r--r-- | src/plugins/coreplugin/icore.cpp | 6 | ||||
-rw-r--r-- | src/plugins/coreplugin/icore.h | 1 | ||||
-rw-r--r-- | src/plugins/projectexplorer/foldernavigationwidget.cpp | 80 | ||||
-rw-r--r-- | src/plugins/projectexplorer/foldernavigationwidget.h | 1 | ||||
-rw-r--r-- | src/plugins/projectexplorer/projectexplorer.cpp | 1 | ||||
-rw-r--r-- | src/plugins/projectexplorer/projectexplorerconstants.h | 2 |
6 files changed, 80 insertions, 11 deletions
diff --git a/src/plugins/coreplugin/icore.cpp b/src/plugins/coreplugin/icore.cpp index 550321396e..99f2a2f7da 100644 --- a/src/plugins/coreplugin/icore.cpp +++ b/src/plugins/coreplugin/icore.cpp @@ -479,6 +479,12 @@ IContext *ICore::currentContextObject() return m_mainwindow->currentContextObject(); } +QWidget *ICore::currentContextWidget() +{ + IContext *context = currentContextObject(); + return context ? context->widget() : nullptr; +} + QWidget *ICore::mainWindow() { diff --git a/src/plugins/coreplugin/icore.h b/src/plugins/coreplugin/icore.h index c05db031eb..6f7991466d 100644 --- a/src/plugins/coreplugin/icore.h +++ b/src/plugins/coreplugin/icore.h @@ -106,6 +106,7 @@ public: static void raiseWindow(QWidget *widget); static IContext *currentContextObject(); + static QWidget *currentContextWidget(); // Adds and removes additional active contexts, these contexts are appended // to the currently active contexts. static void updateAdditionalContexts(const Context &remove, const Context &add, diff --git a/src/plugins/projectexplorer/foldernavigationwidget.cpp b/src/plugins/projectexplorer/foldernavigationwidget.cpp index 92fbf52b65..441b92cef1 100644 --- a/src/plugins/projectexplorer/foldernavigationwidget.cpp +++ b/src/plugins/projectexplorer/foldernavigationwidget.cpp @@ -51,6 +51,7 @@ #include <utils/hostosinfo.h> #include <utils/navigationtreeview.h> #include <utils/qtcassert.h> +#include <utils/removefiledialog.h> #include <utils/utilsicons.h> #include <QAction> @@ -161,6 +162,14 @@ static QVector<FolderNode *> renamableFolderNodes(const Utils::FileName &before, return folderNodes; } +static QStringList projectNames(const QVector<FolderNode *> &folders) +{ + const QStringList names = Utils::transform<QList>(folders, [](FolderNode *n) { + return n->managingProject()->filePath().fileName(); + }); + return Utils::filteredUnique(names); +} + bool FolderNavigationModel::setData(const QModelIndex &index, const QVariant &value, int role) { QTC_ASSERT(index.isValid() && parent(index).isValid() && index.column() == 0 @@ -186,12 +195,7 @@ bool FolderNavigationModel::setData(const QModelIndex &index, const QVariant &va failedNodes.append(folder); } if (!failedNodes.isEmpty()) { - const QString projects - = Utils::transform<QList>(failedNodes, - [](FolderNode *n) { - return n->managingProject()->filePath().fileName(); - }) - .join(", "); + const QString projects = projectNames(failedNodes).join(", "); const QString errorMessage = tr("The file \"%1\" was renamed to \"%2\", " "but the following projects could not be automatically changed: %3") @@ -395,6 +399,51 @@ void FolderNavigationWidget::editCurrentItem() m_listView->edit(current); } +static QVector<FolderNode *> removableFolderNodes(const Utils::FileName &filePath) +{ + QVector<FolderNode *> folderNodes; + ProjectTree::forEachNode([&](Node *node) { + if (node->nodeType() == NodeType::File && node->filePath() == filePath + && node->parentFolderNode() + && node->parentFolderNode()->supportsAction(RemoveFile, node)) { + folderNodes.append(node->parentFolderNode()); + } + }); + return folderNodes; +} + +void FolderNavigationWidget::removeCurrentItem() +{ + const QModelIndex current = m_listView->currentIndex(); + if (!current.isValid() || m_fileSystemModel->isDir(current)) + return; + const QString filePath = m_fileSystemModel->filePath(current); + Utils::RemoveFileDialog dialog(filePath, Core::ICore::dialogParent()); + dialog.setDeleteFileVisible(false); + if (dialog.exec() == QDialog::Accepted) { + const QVector<FolderNode *> folderNodes = removableFolderNodes( + Utils::FileName::fromString(filePath)); + const QVector<FolderNode *> failedNodes = Utils::filtered(folderNodes, + [filePath](FolderNode *folder) { + return !folder->removeFiles( + {filePath}); + }); + Core::FileChangeBlocker changeGuard(filePath); + Core::FileUtils::removeFile(filePath, true /*delete from disk*/); + if (!failedNodes.isEmpty()) { + const QString projects = projectNames(failedNodes).join(", "); + const QString errorMessage + = tr("The following projects failed to automatically remove the file: %1") + .arg(projects); + QTimer::singleShot(0, Core::ICore::instance(), [errorMessage] { + QMessageBox::warning(Core::ICore::dialogParent(), + ProjectExplorerPlugin::tr("Project Editing Failed"), + errorMessage); + }); + } + } +} + bool FolderNavigationWidget::autoSynchronization() const { return m_autoSync; @@ -559,6 +608,8 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev) Core::EditorManager::addNativeDirAndOpenWithActions(&menu, &fakeEntry); if (hasCurrentItem) { + if (!isDir) + menu.addAction(Core::ActionManager::command(Constants::REMOVEFILE)->action()); if (m_fileSystemModel->flags(current) & Qt::ItemIsEditable) menu.addAction(Core::ActionManager::command(Constants::RENAMEFILE)->action()); if (!isDir && Core::DiffService::instance()) { @@ -712,17 +763,28 @@ void FolderNavigationWidgetFactory::updateProjectsDirectoryRoot() Utils::Icons::PROJECT.icon()}); } +static FolderNavigationWidget *currentFolderNavigationWidget() +{ + return qobject_cast<FolderNavigationWidget *>(Core::ICore::currentContextWidget()); +} + void FolderNavigationWidgetFactory::registerActions() { Core::Context context(C_FOLDERNAVIGATIONWIDGET); + auto rename = new QAction(this); Core::ActionManager::registerAction(rename, Constants::RENAMEFILE, context); connect(rename, &QAction::triggered, Core::ICore::instance(), [] { - Core::IContext *context = Core::ICore::currentContextObject(); - QWidget *widget = context ? context->widget() : nullptr; - if (auto navWidget = qobject_cast<FolderNavigationWidget *>(widget)) + if (auto navWidget = currentFolderNavigationWidget()) navWidget->editCurrentItem(); }); + + auto remove = new QAction(this); + Core::ActionManager::registerAction(remove, Constants::REMOVEFILE, context); + connect(remove, &QAction::triggered, Core::ICore::instance(), [] { + if (auto navWidget = currentFolderNavigationWidget()) + navWidget->removeCurrentItem(); + }); } int DelayedFileCrumbLabel::immediateHeightForWidth(int w) const diff --git a/src/plugins/projectexplorer/foldernavigationwidget.h b/src/plugins/projectexplorer/foldernavigationwidget.h index 3ea9781eaf..ba95bd3a75 100644 --- a/src/plugins/projectexplorer/foldernavigationwidget.h +++ b/src/plugins/projectexplorer/foldernavigationwidget.h @@ -107,6 +107,7 @@ public: void removeRootDirectory(const QString &id); void editCurrentItem(); + void removeCurrentItem(); protected: void contextMenuEvent(QContextMenuEvent *ev) override; diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 31054027fb..a856856418 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -208,7 +208,6 @@ const char OPENFILE[] = "ProjectExplorer.OpenFile"; const char SEARCHONFILESYSTEM[] = "ProjectExplorer.SearchOnFileSystem"; const char SHOWINGRAPHICALSHELL[] = "ProjectExplorer.ShowInGraphicalShell"; const char OPENTERMIANLHERE[] = "ProjectExplorer.OpenTerminalHere"; -const char REMOVEFILE[] = "ProjectExplorer.RemoveFile"; const char DUPLICATEFILE[] = "ProjectExplorer.DuplicateFile"; const char DELETEFILE[] = "ProjectExplorer.DeleteFile"; const char DIFFFILE[] = "ProjectExplorer.DiffFile"; diff --git a/src/plugins/projectexplorer/projectexplorerconstants.h b/src/plugins/projectexplorer/projectexplorerconstants.h index 41e6920283..e72c964cfe 100644 --- a/src/plugins/projectexplorer/projectexplorerconstants.h +++ b/src/plugins/projectexplorer/projectexplorerconstants.h @@ -37,7 +37,7 @@ const char MODE_SESSION[] = "Project"; const char BUILD[] = "ProjectExplorer.Build"; const char STOP[] = "ProjectExplorer.Stop"; const char RENAMEFILE[] = "ProjectExplorer.RenameFile"; - +const char REMOVEFILE[] = "ProjectExplorer.RemoveFile"; // Context const char C_PROJECT_TREE[] = "ProjectExplorer.ProjectTreeContext"; |