From aa66cf5edadb3d2b1af6c6829d6d6d8a6f46d9bd Mon Sep 17 00:00:00 2001 From: Blubbz0r Date: Mon, 25 May 2015 15:33:46 +0200 Subject: [PATCH] Added context menu item Open containing folder Note: This also fixes the Copy full path context menu item (was only putting file name into clipboard) --- gui/resultstree.cpp | 46 +++++++++++++++++++++++++++++++++------------ gui/resultstree.h | 15 ++++++++++++++- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index abf10872f..d44783bab 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include "common.h" @@ -130,7 +131,7 @@ bool ResultsTree::AddErrorItem(const ErrorItem &item) line.severity = item.severity; //Create the base item for the error and ensure it has a proper //file item as a parent - QStandardItem *stditem = AddBacktraceFiles(EnsureFileItem(line.file, item.file0, hide), + QStandardItem *stditem = AddBacktraceFiles(EnsureFileItem(item.files[0], item.file0, hide), line, hide, SeverityToIcon(line.severity)); @@ -530,12 +531,13 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) } //Create an action for the application - QAction *copyfilename = new QAction(tr("Copy filename"), &menu); - QAction *copypath = new QAction(tr("Copy full path"), &menu); - QAction *copymessage = new QAction(tr("Copy message"), &menu); - QAction *copymessageid = new QAction(tr("Copy message id"), &menu); - QAction *hide = new QAction(tr("Hide"), &menu); - QAction *hideallid = new QAction(tr("Hide all with id"), &menu); + QAction *copyfilename = new QAction(tr("Copy filename"), &menu); + QAction *copypath = new QAction(tr("Copy full path"), &menu); + QAction *copymessage = new QAction(tr("Copy message"), &menu); + QAction *copymessageid = new QAction(tr("Copy message id"), &menu); + QAction *hide = new QAction(tr("Hide"), &menu); + QAction *hideallid = new QAction(tr("Hide all with id"), &menu); + QAction *opencontainingfolder = new QAction(tr("Open containing folder"), &menu); if (multipleSelection) { copyfilename->setDisabled(true); @@ -543,6 +545,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) copymessage->setDisabled(true); copymessageid->setDisabled(true); hideallid->setDisabled(true); + opencontainingfolder->setDisabled(true); } menu.addAction(copyfilename); @@ -551,6 +554,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) menu.addAction(copymessageid); menu.addAction(hide); menu.addAction(hideallid); + menu.addAction(opencontainingfolder); connect(copyfilename, SIGNAL(triggered()), this, SLOT(CopyFilename())); connect(copypath, SIGNAL(triggered()), this, SLOT(CopyFullPath())); @@ -558,6 +562,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) connect(copymessageid, SIGNAL(triggered()), this, SLOT(CopyMessageId())); connect(hide, SIGNAL(triggered()), this, SLOT(HideResult())); connect(hideallid, SIGNAL(triggered()), this, SLOT(HideAllIdResult())); + connect(opencontainingfolder, SIGNAL(triggered()), this, SLOT(OpenContainingFolder())); } //Start the menu @@ -705,12 +710,12 @@ QString ResultsTree::AskFileDir(const QString &file) void ResultsTree::CopyFilename() { - CopyPath(mContextItem, false); + CopyPathToClipboard(mContextItem, false); } void ResultsTree::CopyFullPath() { - CopyPath(mContextItem, true); + CopyPathToClipboard(mContextItem, true); } void ResultsTree::CopyMessage() @@ -810,6 +815,16 @@ void ResultsTree::HideAllIdResult() } } +void ResultsTree::OpenContainingFolder() +{ + QString filePath = GetFilePath(mContextItem, true); + if (!filePath.isEmpty()) + { + filePath = QFileInfo(filePath).absolutePath(); + QDesktopServices::openUrl(QUrl::fromLocalFile(filePath)); + } +} + void ResultsTree::Context(int application) { StartApplication(mContextItem, application); @@ -820,7 +835,13 @@ void ResultsTree::QuickStartApplication(const QModelIndex &index) StartApplication(mModel.itemFromIndex(index)); } -void ResultsTree::CopyPath(QStandardItem *target, bool fullPath) +void ResultsTree::CopyPathToClipboard(QStandardItem *target, bool fullPath) +{ + QClipboard *clipboard = QApplication::clipboard(); + clipboard->setText(GetFilePath(target, fullPath)); +} + +QString ResultsTree::GetFilePath(QStandardItem *target, bool fullPath) { if (target) { // Make sure we are working with the first column @@ -838,9 +859,10 @@ void ResultsTree::CopyPath(QStandardItem *target, bool fullPath) pathStr = fi.fileName(); } - QClipboard *clipboard = QApplication::clipboard(); - clipboard->setText(pathStr); + return pathStr; } + + return QString(); } QString ResultsTree::SeverityToIcon(Severity::SeverityType severity) const diff --git a/gui/resultstree.h b/gui/resultstree.h index 7353901c4..935356310 100644 --- a/gui/resultstree.h +++ b/gui/resultstree.h @@ -223,6 +223,11 @@ protected slots: */ void HideAllIdResult(); + /** + * @brief Slot for context menu item to open the folder containing the current file. + */ + void OpenContainingFolder(); + /** * @brief Slot for selection change in the results tree. * @@ -285,7 +290,15 @@ protected: * @param target Error tree item to open * @param fullPath Are we copying full path or only filename? */ - void CopyPath(QStandardItem *target, bool fullPath); + void CopyPathToClipboard(QStandardItem *target, bool fullPath); + + /** + * @brief Helper function returning the filename/full path of the error tree item \a target. + * + * @param target The error tree item containing the filename/full path + * @param fullPath Whether or not to retrieve the full path or only the filename. + */ + QString GetFilePath(QStandardItem *target, bool fullPath); /** * @brief Context menu event (user right clicked on the tree)