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());
@ -47,13 +45,12 @@ MainWindow::MainWindow(QWidget *parent) :
void MainWindow::connectSignals() void MainWindow::connectSignals()
{ {
connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed); connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed);
connect(&searchWatcher, &QFutureWatcher<SearchResult>::finished, this, [&]{ connect(&searchWatcher, &QFutureWatcher<SearchResult>::finished, this, [&] {
try try
{ {
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());
});
} }
} }
@ -167,7 +175,7 @@ void MainWindow::lineEditReturnPressed()
ui->lblSearchResults->setText("Invalid paranthesis"); ui->lblSearchResults->setText("Invalid paranthesis");
return; return;
} }
//TODO: validate q; // TODO: validate q;
ui->lblSearchResults->setText("Searching..."); ui->lblSearchResults->setText("Searching...");
this->ui->txtSearch->setEnabled(false); this->ui->txtSearch->setEnabled(false);
QFuture<QVector<SearchResult>> searchFuture = QtConcurrent::run([&, q]() { QFuture<QVector<SearchResult>> searchFuture = QtConcurrent::run([&, q]() {
@ -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)
@ -187,7 +194,7 @@ void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
for(const SearchResult &result : results) for(const SearchResult &result : results)
{ {
QFileInfo pathInfo(result.fileData.absPath); QFileInfo pathInfo(result.fileData.absPath);
QString fileName =pathInfo.fileName(); QString fileName = pathInfo.fileName();
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeResultsList); QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeResultsList);
QDateTime dt = QDateTime::fromSecsSinceEpoch(result.fileData.mtime); QDateTime dt = QDateTime::fromSecsSinceEpoch(result.fileData.mtime);
@ -203,7 +210,7 @@ void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
} }
ui->treeResultsList->resizeColumnToContents(0); ui->treeResultsList->resizeColumnToContents(0);
ui->treeResultsList->resizeColumnToContents(1); ui->treeResultsList->resizeColumnToContents(1);
pdfDirty = ! this->pdfSearchResults.empty(); pdfDirty = !this->pdfSearchResults.empty();
int numpages = ceil(static_cast<double>(this->pdfSearchResults.size()) / pdfPreviewsPerPage); int numpages = ceil(static_cast<double>(this->pdfSearchResults.size()) / pdfPreviewsPerPage);
ui->spinPdfPreviewPage->setMinimum(1); ui->spinPdfPreviewPage->setMinimum(1);
@ -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,23 +254,33 @@ 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)
@ -278,21 +295,13 @@ void MainWindow::showSearchResultsContextMenu(const QPoint &point)
{ {
return; return;
} }
QMenu menu("SearchResult", this); QFileInfo pathinfo(item->text(1));
menu.addAction("Copy filename to clipboard", [&] { QGuiApplication::clipboard()->setText(item->text(0));}); QMenu menu("SearchResults", this);
menu.addAction("Copy full path to clipboard", [&] { QGuiApplication::clipboard()->setText(item->text(1)); }); createSearchResutlMenu(menu, pathinfo);
menu.addAction("Open containing folder", [&] {
QFileInfo pathinfo(item->text(1));
QString dir = pathinfo.absolutePath();
QDesktopServices::openUrl(QUrl::fromLocalFile(dir));
});
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);