From fe26454f8520295db35f61822ad29f23ab1e1fcf Mon Sep 17 00:00:00 2001 From: Albert S Date: Fri, 29 Jun 2018 18:16:14 +0200 Subject: [PATCH] Added context menu to copy or open paths --- window.cpp | 24 ++++++++++++++++++++++++ window.h | 1 + 2 files changed, 25 insertions(+) diff --git a/window.cpp b/window.cpp index 662a66b..3fc50b0 100644 --- a/window.cpp +++ b/window.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include Window::Window(const QVector &configs, const QString &dbpath) { this->userEntryButtons = generateEntryButtons(configs); @@ -57,6 +59,28 @@ void Window::initTreeWidgets() treeFileSearchResults.setHeaderLabels(headers); treeFileSearchResults.header()->setSectionResizeMode(QHeaderView::ResizeToContents); connect(&treeFileSearchResults, &QTreeWidget::itemActivated, this, &Window::treeSearchItemActivated); + treeFileSearchResults.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); + connect(&treeFileSearchResults, &QTreeWidget::customContextMenuRequested, this, &Window::showSearchResultsContextMenu); +} + + +void Window::showSearchResultsContextMenu(const QPoint &point) +{ + QTreeWidgetItem *item = treeFileSearchResults.itemAt(point); + if(item == nullptr) + { + return; + } + QMenu menu("Test", this); + menu.addAction("Copy filename to clipboard", [&] { QGuiApplication::clipboard()->setText(item->text(0));}); + menu.addAction("Copy full path to clipboard", [&] { QGuiApplication::clipboard()->setText(item->text(1)); }); + menu.addAction("Open containing folder", [&] { + QFileInfo pathinfo(item->text(1)); + QString dir = pathinfo.absolutePath(); + QDesktopServices::openUrl(QUrl::fromLocalFile(dir)); + + }); + menu.exec(QCursor::pos()); } QVector Window::generateEntryButtons(const QVector &configs) diff --git a/window.h b/window.h index 1484fc5..963b3e6 100644 --- a/window.h +++ b/window.h @@ -69,6 +69,7 @@ class Window : public QWidget void handleSearchResults(const QVector &results); void handleCancelledSearch(); void treeSearchItemActivated(QTreeWidgetItem *item, int i); + void showSearchResultsContextMenu(const QPoint &point); signals: void beginFileSearch(const QString &query); void beginContentSearch(const QString &query);