From 0cdddb616dd870df46baad9e54c1ad2ccf5bf705 Mon Sep 17 00:00:00 2001 From: malte_langkabel Date: Thu, 19 Jan 2017 10:51:47 +0100 Subject: [PATCH] ui: added options for context menu when clicking file name in code view * copy full path * open containing folder --- .../qt/element/QtCodeFileTitleButton.cpp | 51 +++++++++++++++++++ .../qt/element/QtCodeFileTitleButton.h | 7 +++ 2 files changed, 58 insertions(+) diff --git a/src/lib_gui/qt/element/QtCodeFileTitleButton.cpp b/src/lib_gui/qt/element/QtCodeFileTitleButton.cpp index 8947b4f7..bb105388 100644 --- a/src/lib_gui/qt/element/QtCodeFileTitleButton.cpp +++ b/src/lib_gui/qt/element/QtCodeFileTitleButton.cpp @@ -1,10 +1,16 @@ #include "qt/element/QtCodeFileTitleButton.h" +#include +#include +#include +#include + #include "utility/file/FileSystem.h" #include "utility/messaging/type/MessageActivateFile.h" #include "utility/messaging/type/MessageProjectEdit.h" #include "Application.h" +#include "qt/utility/QtContextMenu.h" #include "qt/utility/utilityQt.h" #include "settings/ColorScheme.h" #include "utility/ResourcePaths.h" @@ -20,6 +26,16 @@ QtCodeFileTitleButton::QtCodeFileTitleButton(QWidget* parent) setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Fixed); connect(this, SIGNAL(clicked()), this, SLOT(clickedTitle())); + + m_copyFullPathAction = new QAction(tr("Copy Full Path"), this); + m_copyFullPathAction->setStatusTip(tr("Copies the path of this file to the clipboard")); + m_copyFullPathAction->setToolTip(tr("Copies the path of this file to the clipboard")); + connect(m_copyFullPathAction, SIGNAL(triggered()), this, SLOT(copyFullPath())); + + m_openContainingFolderAction = new QAction(tr("Open Containing Folder"), this); + m_openContainingFolderAction->setStatusTip(tr("Opens the folder that contains this file")); + m_openContainingFolderAction->setToolTip(tr("Opens the folder that contains this file")); + connect(m_openContainingFolderAction, SIGNAL(triggered()), this, SLOT(openContainingFolder())); } QtCodeFileTitleButton::~QtCodeFileTitleButton() @@ -91,6 +107,21 @@ void QtCodeFileTitleButton::checkModification() } } +void QtCodeFileTitleButton::contextMenuEvent(QContextMenuEvent* event) +{ + if (!m_filePath.empty()) + { + std::vector actions; + actions.push_back(m_copyFullPathAction); + actions.push_back(m_openContainingFolderAction); + QtContextMenu::getInstance()->showExtended(event, this, actions); + } + else if (text().size()) + { + QPushButton::contextMenuEvent(event); + } +} + void QtCodeFileTitleButton::clickedTitle() { if (!m_filePath.empty()) @@ -102,3 +133,23 @@ void QtCodeFileTitleButton::clickedTitle() MessageProjectEdit().dispatch(); } } + +void QtCodeFileTitleButton::copyFullPath() +{ + const std::string pathString = (QSysInfo::windowsVersion() != QSysInfo::WV_None) ? m_filePath.getBackslashedString() : m_filePath.str(); + QApplication::clipboard()->setText(pathString.c_str()); +} + +void QtCodeFileTitleButton::openContainingFolder() +{ + FilePath dir = m_filePath.parentDirectory(); + if (dir.exists()) + { + QDesktopServices::openUrl(QUrl(("file:///" + dir.str()).c_str(), QUrl::TolerantMode)); + } + else + { + LOG_ERROR("Unable to open directory: " + dir.str()); + } +} + diff --git a/src/lib_gui/qt/element/QtCodeFileTitleButton.h b/src/lib_gui/qt/element/QtCodeFileTitleButton.h index 0f5f4b31..502becc5 100644 --- a/src/lib_gui/qt/element/QtCodeFileTitleButton.h +++ b/src/lib_gui/qt/element/QtCodeFileTitleButton.h @@ -21,12 +21,19 @@ public: void checkModification(); +protected: + void contextMenuEvent(QContextMenuEvent* event); + private slots: void clickedTitle(); + void copyFullPath(); + void openContainingFolder(); private: FilePath m_filePath; TimePoint m_modificationTime; + QAction* m_copyFullPathAction; + QAction* m_openContainingFolderAction; }; #endif // QT_CODE_FILE_TITLE_BUTTON_H