mainwindow: add right click menu to preview labels

This commit is contained in:
Albert S. 2021-03-27 17:58:38 +01:00
parent b0dbd88293
commit 45b2dbed4d
2 changed files with 46 additions and 36 deletions

View File

@ -18,9 +18,7 @@
#include "../shared/qssgeneralexception.h" #include "../shared/qssgeneralexception.h"
#include "../shared/common.h" #include "../shared/common.h"
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
QMainWindow(parent),
ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(QCoreApplication::applicationName()); setWindowTitle(QCoreApplication::applicationName());
@ -53,7 +51,6 @@ void MainWindow::connectSignals()
this->ui->txtSearch->setEnabled(true); this->ui->txtSearch->setEnabled(true);
auto results = searchWatcher.future().result(); auto results = searchWatcher.future().result();
handleSearchResults(results); handleSearchResults(results);
} }
catch(QSSGeneralException &e) catch(QSSGeneralException &e)
{ {
@ -61,16 +58,18 @@ void MainWindow::connectSignals()
} }
}); });
connect(&pdfWorkerWatcher, &QFutureWatcher<PdfPreview>::resultReadyAt, this, [&](int index) { connect(&pdfWorkerWatcher, &QFutureWatcher<PdfPreview>::resultReadyAt, this,
pdfPreviewReceived(pdfWorkerWatcher.resultAt(index)); [&](int index) { pdfPreviewReceived(pdfWorkerWatcher.resultAt(index)); });
}); connect(&pdfWorkerWatcher, &QFutureWatcher<PdfPreview>::progressValueChanged, ui->pdfProcessBar,
connect(&pdfWorkerWatcher, &QFutureWatcher<PdfPreview>::progressValueChanged, ui->pdfProcessBar, &QProgressBar::setValue); &QProgressBar::setValue);
connect(ui->treeResultsList, &QTreeWidget::itemActivated, this, &MainWindow::treeSearchItemActivated); 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->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::tabChanged);
connect(ui->comboScale, qOverload<const QString &>(&QComboBox::currentIndexChanged), this, &MainWindow::comboScaleChanged); connect(ui->comboScale, qOverload<const QString &>(&QComboBox::currentIndexChanged), this,
connect(ui->spinPdfPreviewPage, qOverload<int>(&QSpinBox::valueChanged), this, &MainWindow::spinPdfPreviewPageValueChanged); &MainWindow::comboScaleChanged);
connect(ui->spinPdfPreviewPage, qOverload<int>(&QSpinBox::valueChanged), this,
&MainWindow::spinPdfPreviewPageValueChanged);
} }
void MainWindow::spinPdfPreviewPageValueChanged(int val) void MainWindow::spinPdfPreviewPageValueChanged(int val)
@ -91,7 +90,8 @@ bool MainWindow::pdfTabActive()
void MainWindow::keyPressEvent(QKeyEvent *event) 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) if(quit)
{ {
qApp->quit(); qApp->quit();
@ -156,6 +156,14 @@ void MainWindow::pdfPreviewReceived(PdfPreview preview)
QDesktopServices::openUrl(QUrl::fromLocalFile(docPath)); 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());
});
} }
} }
@ -176,7 +184,6 @@ void MainWindow::lineEditReturnPressed()
return searcher.search(this->currentQuery); return searcher.search(this->currentQuery);
}); });
searchWatcher.setFuture(searchFuture); searchWatcher.setFuture(searchFuture);
} }
void MainWindow::handleSearchResults(const QVector<SearchResult> &results) void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
@ -213,7 +220,6 @@ void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
{ {
makePdfPreview(1); makePdfPreview(1);
} }
} }
void MainWindow::makePdfPreview(int page) void MainWindow::makePdfPreview(int page)
@ -222,7 +228,8 @@ void MainWindow::makePdfPreview(int page)
this->pdfWorkerWatcher.cancel(); this->pdfWorkerWatcher.cancel();
this->pdfWorkerWatcher.waitForFinished(); 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()); qDeleteAll(ui->scrollAreaWidgetContents->children());
ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout()); ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout());
@ -247,25 +254,35 @@ void MainWindow::makePdfPreview(int page)
value = m.captured(2); value = m.captured(2);
} }
wordsToHighlight.append(value); wordsToHighlight.append(value);
} }
} }
} }
PdfWorker worker; PdfWorker worker;
int end = pdfPreviewsPerPage; int end = pdfPreviewsPerPage;
int begin = page * pdfPreviewsPerPage - 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->setMaximum(this->pdfWorkerWatcher.progressMaximum());
ui->pdfProcessBar->setMinimum(this->pdfWorkerWatcher.progressMinimum()); ui->pdfProcessBar->setMinimum(this->pdfWorkerWatcher.progressMinimum());
} }
void MainWindow::handleSearchError(QString error) 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) void MainWindow::treeSearchItemActivated(QTreeWidgetItem *item, int i)
{ {
QDesktopServices::openUrl(QUrl::fromLocalFile(item->text(1))); QDesktopServices::openUrl(QUrl::fromLocalFile(item->text(1)));
@ -278,21 +295,13 @@ void MainWindow::showSearchResultsContextMenu(const QPoint &point)
{ {
return; 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)); QFileInfo pathinfo(item->text(1));
QString dir = pathinfo.absolutePath(); QMenu menu("SearchResults", this);
QDesktopServices::openUrl(QUrl::fromLocalFile(dir)); createSearchResutlMenu(menu, pathinfo);
});
menu.exec(QCursor::pos()); menu.exec(QCursor::pos());
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete ui; delete ui;
} }

View File

@ -42,6 +42,7 @@ private:
void handleSearchError(QString error); void handleSearchError(QString error);
QSSQuery currentQuery; QSSQuery currentQuery;
int pdfPreviewsPerPage; int pdfPreviewsPerPage;
void createSearchResutlMenu(QMenu &menu, const QFileInfo &fileInfo);
private slots: private slots:
void lineEditReturnPressed(); void lineEditReturnPressed();
void treeSearchItemActivated(QTreeWidgetItem *item, int i); void treeSearchItemActivated(QTreeWidgetItem *item, int i);