From 45b2dbed4de62501cdf8fee5358fa0fdb5682777 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 27 Mar 2021 17:58:38 +0100 Subject: [PATCH] mainwindow: add right click menu to preview labels --- gui/mainwindow.cpp | 81 +++++++++++++++++++++++++--------------------- gui/mainwindow.h | 1 + 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index f7c0526..03878ea 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -18,9 +18,7 @@ #include "../shared/qssgeneralexception.h" #include "../shared/common.h" -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) +MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle(QCoreApplication::applicationName()); @@ -47,13 +45,12 @@ MainWindow::MainWindow(QWidget *parent) : void MainWindow::connectSignals() { connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed); - connect(&searchWatcher, &QFutureWatcher::finished, this, [&]{ + connect(&searchWatcher, &QFutureWatcher::finished, this, [&] { try { this->ui->txtSearch->setEnabled(true); auto results = searchWatcher.future().result(); handleSearchResults(results); - } catch(QSSGeneralException &e) { @@ -61,16 +58,18 @@ void MainWindow::connectSignals() } }); - connect(&pdfWorkerWatcher, &QFutureWatcher::resultReadyAt, this, [&](int index) { - pdfPreviewReceived(pdfWorkerWatcher.resultAt(index)); - }); - connect(&pdfWorkerWatcher, &QFutureWatcher::progressValueChanged, ui->pdfProcessBar, &QProgressBar::setValue); + connect(&pdfWorkerWatcher, &QFutureWatcher::resultReadyAt, this, + [&](int index) { pdfPreviewReceived(pdfWorkerWatcher.resultAt(index)); }); + connect(&pdfWorkerWatcher, &QFutureWatcher::progressValueChanged, ui->pdfProcessBar, + &QProgressBar::setValue); connect(ui->treeResultsList, &QTreeWidget::itemActivated, this, &MainWindow::treeSearchItemActivated); - connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this, &MainWindow::showSearchResultsContextMenu); + connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this, + &MainWindow::showSearchResultsContextMenu); connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::tabChanged); - connect(ui->comboScale, qOverload(&QComboBox::currentIndexChanged), this, &MainWindow::comboScaleChanged); - connect(ui->spinPdfPreviewPage, qOverload(&QSpinBox::valueChanged), this, &MainWindow::spinPdfPreviewPageValueChanged); - + connect(ui->comboScale, qOverload(&QComboBox::currentIndexChanged), this, + &MainWindow::comboScaleChanged); + connect(ui->spinPdfPreviewPage, qOverload(&QSpinBox::valueChanged), this, + &MainWindow::spinPdfPreviewPageValueChanged); } void MainWindow::spinPdfPreviewPageValueChanged(int val) @@ -91,7 +90,8 @@ bool MainWindow::pdfTabActive() void MainWindow::keyPressEvent(QKeyEvent *event) { - bool quit = ((event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_Q) || event->key() == Qt::Key_Escape); + bool quit = + ((event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_Q) || event->key() == Qt::Key_Escape); if(quit) { qApp->quit(); @@ -156,6 +156,14 @@ void MainWindow::pdfPreviewReceived(PdfPreview preview) QDesktopServices::openUrl(QUrl::fromLocalFile(docPath)); } }); + connect(label, &ClickLabel::rightClick, [this, docPath, previewPage]() { + QFileInfo fileInfo{docPath}; + QMenu menu("labeRightClick", this); + createSearchResutlMenu(menu, fileInfo); + menu.addAction("Copy page number", + [previewPage] { QGuiApplication::clipboard()->setText(QString::number(previewPage)); }); + menu.exec(QCursor::pos()); + }); } } @@ -167,7 +175,7 @@ void MainWindow::lineEditReturnPressed() ui->lblSearchResults->setText("Invalid paranthesis"); return; } - //TODO: validate q; + // TODO: validate q; ui->lblSearchResults->setText("Searching..."); this->ui->txtSearch->setEnabled(false); QFuture> searchFuture = QtConcurrent::run([&, q]() { @@ -176,7 +184,6 @@ void MainWindow::lineEditReturnPressed() return searcher.search(this->currentQuery); }); searchWatcher.setFuture(searchFuture); - } void MainWindow::handleSearchResults(const QVector &results) @@ -187,7 +194,7 @@ void MainWindow::handleSearchResults(const QVector &results) for(const SearchResult &result : results) { QFileInfo pathInfo(result.fileData.absPath); - QString fileName =pathInfo.fileName(); + QString fileName = pathInfo.fileName(); QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeResultsList); QDateTime dt = QDateTime::fromSecsSinceEpoch(result.fileData.mtime); @@ -203,7 +210,7 @@ void MainWindow::handleSearchResults(const QVector &results) } ui->treeResultsList->resizeColumnToContents(0); ui->treeResultsList->resizeColumnToContents(1); - pdfDirty = ! this->pdfSearchResults.empty(); + pdfDirty = !this->pdfSearchResults.empty(); int numpages = ceil(static_cast(this->pdfSearchResults.size()) / pdfPreviewsPerPage); ui->spinPdfPreviewPage->setMinimum(1); @@ -213,7 +220,6 @@ void MainWindow::handleSearchResults(const QVector &results) { makePdfPreview(1); } - } void MainWindow::makePdfPreview(int page) @@ -222,7 +228,8 @@ void MainWindow::makePdfPreview(int page) this->pdfWorkerWatcher.cancel(); this->pdfWorkerWatcher.waitForFinished(); - QCoreApplication::processEvents(); //Maybe not necessary anymore, depends on whether it's possible that a slot is still to be fired. + QCoreApplication::processEvents(); // Maybe not necessary anymore, depends on whether it's possible that a slot is + // still to be fired. qDeleteAll(ui->scrollAreaWidgetContents->children()); ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout()); @@ -247,23 +254,33 @@ void MainWindow::makePdfPreview(int page) value = m.captured(2); } wordsToHighlight.append(value); - } } } PdfWorker worker; int end = pdfPreviewsPerPage; int begin = page * pdfPreviewsPerPage - pdfPreviewsPerPage; - this->pdfWorkerWatcher.setFuture(worker.generatePreviews(this->pdfSearchResults.mid(begin, end), wordsToHighlight, scaleText.toInt() / 100.)); + this->pdfWorkerWatcher.setFuture( + worker.generatePreviews(this->pdfSearchResults.mid(begin, end), wordsToHighlight, scaleText.toInt() / 100.)); ui->pdfProcessBar->setMaximum(this->pdfWorkerWatcher.progressMaximum()); ui->pdfProcessBar->setMinimum(this->pdfWorkerWatcher.progressMinimum()); - } - void MainWindow::handleSearchError(QString error) { - ui->lblSearchResults->setText("Error:" + error); + ui->lblSearchResults->setText("Error:" + error); +} + +void MainWindow::createSearchResutlMenu(QMenu &menu, const QFileInfo &fileInfo) +{ + menu.addAction("Copy filename to clipboard", + [&fileInfo] { QGuiApplication::clipboard()->setText(fileInfo.fileName()); }); + menu.addAction("Copy full path to clipboard", + [&fileInfo] { QGuiApplication::clipboard()->setText(fileInfo.absoluteFilePath()); }); + menu.addAction("Open containing folder", [&fileInfo] { + QString dir = fileInfo.absolutePath(); + QDesktopServices::openUrl(QUrl::fromLocalFile(dir)); + }); } void MainWindow::treeSearchItemActivated(QTreeWidgetItem *item, int i) @@ -278,21 +295,13 @@ void MainWindow::showSearchResultsContextMenu(const QPoint &point) { return; } - QMenu menu("SearchResult", 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)); - - }); + QFileInfo pathinfo(item->text(1)); + QMenu menu("SearchResults", this); + createSearchResutlMenu(menu, pathinfo); menu.exec(QCursor::pos()); } - MainWindow::~MainWindow() { delete ui; } - diff --git a/gui/mainwindow.h b/gui/mainwindow.h index ae5b5aa..5743cd9 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -42,6 +42,7 @@ private: void handleSearchError(QString error); QSSQuery currentQuery; int pdfPreviewsPerPage; + void createSearchResutlMenu(QMenu &menu, const QFileInfo &fileInfo); private slots: void lineEditReturnPressed(); void treeSearchItemActivated(QTreeWidgetItem *item, int i);