From d50ecdbb28670412a4c541658abf297717a367f5 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 9 Dec 2023 15:41:08 +0100 Subject: [PATCH] Issue #119: Improve search bar 2 (the revenge) --- Pdf4QtLibWidgets/sources/pdfcompiler.cpp | 2 +- Pdf4QtLibWidgets/sources/pdfwidgettool.cpp | 103 ++++++++++++++------- Pdf4QtLibWidgets/sources/pdfwidgettool.h | 25 ++++- Pdf4QtViewer/pdfviewermainwindow.cpp | 40 ++++---- Pdf4QtViewer/pdfviewermainwindow.h | 6 +- Pdf4QtViewer/pdfviewermainwindowlite.cpp | 40 ++++---- Pdf4QtViewer/pdfviewermainwindowlite.h | 6 +- RELEASES.txt | 1 + 8 files changed, 140 insertions(+), 83 deletions(-) diff --git a/Pdf4QtLibWidgets/sources/pdfcompiler.cpp b/Pdf4QtLibWidgets/sources/pdfcompiler.cpp index c9b80a3..b9aa727 100644 --- a/Pdf4QtLibWidgets/sources/pdfcompiler.cpp +++ b/Pdf4QtLibWidgets/sources/pdfcompiler.cpp @@ -533,7 +533,7 @@ void PDFAsynchronousTextLayoutCompiler::makeTextLayout() m_isRunning = true; ProgressStartupInfo info; - info.showDialog = true; + info.showDialog = false; info.text = tr("Indexing document contents..."); m_proxy->getFontCache()->setCacheShrinkEnabled(this, false); diff --git a/Pdf4QtLibWidgets/sources/pdfwidgettool.cpp b/Pdf4QtLibWidgets/sources/pdfwidgettool.cpp index 65aceb5..a57e68e 100644 --- a/Pdf4QtLibWidgets/sources/pdfwidgettool.cpp +++ b/Pdf4QtLibWidgets/sources/pdfwidgettool.cpp @@ -35,6 +35,7 @@ #include #include #include + namespace pdf { @@ -222,27 +223,6 @@ void PDFWidgetTool::removeTool() m_toolStack.pop_back(); } -class PDFFindTextToolDialog : public QDialog -{ -public: - PDFFindTextToolDialog(PDFDrawWidgetProxy* proxy, QWidget* parent, Qt::WindowFlags f) : - QDialog(parent, f), - m_proxy(proxy) - { - - } - - virtual ~PDFFindTextToolDialog() override = default; - - virtual bool event(QEvent* event) override; - -protected: - virtual void paintEvent(QPaintEvent* event) override; - -private: - PDFDrawWidgetProxy* m_proxy; -}; - void PDFFindTextToolDialog::paintEvent(QPaintEvent* event) { QDialog::paintEvent(event); @@ -269,6 +249,13 @@ void PDFFindTextToolDialog::paintEvent(QPaintEvent* event) painter.drawPrimitive(QStyle::PE_Frame, frameOption); } +PDFFindTextToolDialog::PDFFindTextToolDialog(PDFDrawWidgetProxy* proxy, QWidget* parent, Qt::WindowFlags f) : + QDialog(parent, f), + m_proxy(proxy) +{ + +} + bool PDFFindTextToolDialog::event(QEvent* event) { switch (event->type()) @@ -294,6 +281,37 @@ bool PDFFindTextToolDialog::event(QEvent* event) break; } + case QEvent::KeyPress: + { + QKeyEvent* keyEvent = dynamic_cast(event); + + if (!keyEvent->modifiers() || ((keyEvent->modifiers() & Qt::KeypadModifier) && keyEvent->key() == Qt::Key_Enter)) + { + switch (keyEvent->key()) + { + case Qt::Key_Return: + case Qt::Key_Enter: + case Qt::Key_Home: + { + keyEvent->accept(); + Q_EMIT goToFirstResult(); + return true; + } + + case Qt::Key_End: + { + keyEvent->accept(); + Q_EMIT goToLastResult(); + return true; + } + + default: + break; + } + } + break; + } + default: break; } @@ -399,7 +417,7 @@ void PDFFindTextTool::setActiveImpl(bool active) m_wholeWordsCheckBox->setChecked(m_savedIsWholeWords); m_previousButton->setDefault(false); - m_nextButton->setDefault(false); + m_nextButton->setDefault(true); m_previousButton->setShortcut(m_prevAction->shortcut()); m_nextButton->setShortcut(m_nextAction->shortcut()); @@ -409,6 +427,8 @@ void PDFFindTextTool::setActiveImpl(bool active) connect(m_findTextEdit, &QLineEdit::editingFinished, this, &PDFFindTextTool::onSearchText); connect(m_caseSensitiveCheckBox, &QCheckBox::clicked, this, &PDFFindTextTool::onSearchText); connect(m_wholeWordsCheckBox, &QCheckBox::clicked, this, &PDFFindTextTool::onSearchText); + connect(m_dialog, &PDFFindTextToolDialog::goToFirstResult, this, &PDFFindTextTool::onActionFirst); + connect(m_dialog, &PDFFindTextToolDialog::goToLastResult, this, &PDFFindTextTool::onActionLast); QMargins margins = layout->contentsMargins(); margins.setTop(margins.top() + m_dialog->style()->pixelMetric(QStyle::PM_TitleBarHeight)); @@ -487,22 +507,27 @@ void PDFFindTextTool::onSearchText() } } +void PDFFindTextTool::onActionFirst() +{ + if (!m_findResults.empty()) + { + setCurrentResultIndex(0); + } +} + +void PDFFindTextTool::onActionLast() +{ + if (!m_findResults.empty()) + { + setCurrentResultIndex(m_findResults.size() - 1); + } +} + void PDFFindTextTool::onActionPrevious() { if (!m_findResults.empty()) { - if (m_selectedResultIndex == 0) - { - m_selectedResultIndex = m_findResults.size() - 1; - } - else - { - --m_selectedResultIndex; - } - m_textSelection.dirty(); - getProxy()->repaintNeeded(); - goToCurrentResult(); - updateTitle(); + setCurrentResultIndex(m_selectedResultIndex == 0 ? m_findResults.size() - 1 : m_selectedResultIndex - 1); } } @@ -510,7 +535,15 @@ void PDFFindTextTool::onActionNext() { if (!m_findResults.empty()) { - m_selectedResultIndex = (m_selectedResultIndex + 1) % m_findResults.size(); + setCurrentResultIndex((m_selectedResultIndex + 1) % m_findResults.size()); + } +} + +void PDFFindTextTool::setCurrentResultIndex(size_t index) +{ + if (!m_findResults.empty()) + { + m_selectedResultIndex = index; m_textSelection.dirty(); getProxy()->repaintNeeded(); goToCurrentResult(); diff --git a/Pdf4QtLibWidgets/sources/pdfwidgettool.h b/Pdf4QtLibWidgets/sources/pdfwidgettool.h index e70fd0b..6281ee5 100644 --- a/Pdf4QtLibWidgets/sources/pdfwidgettool.h +++ b/Pdf4QtLibWidgets/sources/pdfwidgettool.h @@ -145,6 +145,26 @@ private: std::optional m_cursor; }; +class PDFFindTextToolDialog : public QDialog +{ + Q_OBJECT +public: + PDFFindTextToolDialog(PDFDrawWidgetProxy* proxy, QWidget* parent, Qt::WindowFlags f); + + virtual ~PDFFindTextToolDialog() override = default; + virtual bool event(QEvent* event) override; + +signals: + void goToFirstResult(); + void goToLastResult(); + +protected: + virtual void paintEvent(QPaintEvent* event) override; + +private: + PDFDrawWidgetProxy* m_proxy; +}; + /// Simple tool for find text in PDF document. It is much simpler than advanced /// search and can't search using regular expressions. class PDFFindTextTool : public PDFWidgetTool @@ -176,10 +196,13 @@ protected: private: void onSearchText(); + void onActionFirst(); + void onActionLast(); void onActionPrevious(); void onActionNext(); void onDialogRejected(); + void setCurrentResultIndex(size_t index); void performSearch(); void updateResultsUI(); void updateTitle(); @@ -190,7 +213,7 @@ private: QAction* m_nextAction; QWidget* m_parentDialog; - QDialog* m_dialog; + PDFFindTextToolDialog* m_dialog; QCheckBox* m_caseSensitiveCheckBox; QCheckBox* m_wholeWordsCheckBox; QLineEdit* m_findTextEdit; diff --git a/Pdf4QtViewer/pdfviewermainwindow.cpp b/Pdf4QtViewer/pdfviewermainwindow.cpp index c38790b..3d8f231 100644 --- a/Pdf4QtViewer/pdfviewermainwindow.cpp +++ b/Pdf4QtViewer/pdfviewermainwindow.cpp @@ -89,7 +89,8 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) : m_isLoadingUI(false), m_progress(new pdf::PDFProgress(this)), m_progressTaskbarIndicator(new PDFWinTaskBarProgress(this)), - m_progressDialog(nullptr), + m_progressBarOnStatusBar(nullptr), + m_progressBarLeftLabelOnStatusBar(nullptr), m_isChangingProgressStep(false) { ui->setupUi(this); @@ -100,6 +101,14 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) : adjustToolbar(ui->mainToolBar); ui->mainToolBar->setWindowTitle(tr("Standard")); + // Initialize status bar + m_progressBarOnStatusBar = new QProgressBar(this); + m_progressBarOnStatusBar->setHidden(true); + m_progressBarLeftLabelOnStatusBar = new QLabel(this); + m_progressBarLeftLabelOnStatusBar->setHidden(true); + statusBar()->addPermanentWidget(m_progressBarLeftLabelOnStatusBar); + statusBar()->addPermanentWidget(m_progressBarOnStatusBar); + // Initialize actions m_actionManager->setAction(PDFActionManager::Open, ui->actionOpen); m_actionManager->setAction(PDFActionManager::Close, ui->actionClose); @@ -353,13 +362,12 @@ void PDFViewerMainWindow::onPageZoomSpinboxEditingFinished() void PDFViewerMainWindow::onProgressStarted(pdf::ProgressStartupInfo info) { - Q_ASSERT(!m_progressDialog); - if (info.showDialog) - { - m_progressDialog = new QProgressDialog(info.text, QString(), 0, 100, this); - m_progressDialog->setWindowModality(Qt::WindowModal); - m_progressDialog->setCancelButton(nullptr); - } + m_progressBarLeftLabelOnStatusBar->setText(info.text); + m_progressBarLeftLabelOnStatusBar->setVisible(!info.text.isEmpty()); + + m_progressBarOnStatusBar->setRange(0, 100); + m_progressBarOnStatusBar->reset(); + m_progressBarOnStatusBar->show(); m_progressTaskbarIndicator->setRange(0, 100); m_progressTaskbarIndicator->reset(); @@ -377,24 +385,14 @@ void PDFViewerMainWindow::onProgressStep(int percentage) } pdf::PDFTemporaryValueChange guard(&m_isChangingProgressStep, true); - - if (m_progressDialog) - { - m_progressDialog->setValue(percentage); - } - + m_progressBarOnStatusBar->setValue(percentage); m_progressTaskbarIndicator->setValue(percentage); } void PDFViewerMainWindow::onProgressFinished() { - if (m_progressDialog) - { - m_progressDialog->hide(); - m_progressDialog->deleteLater(); - m_progressDialog = nullptr; - } - + m_progressBarLeftLabelOnStatusBar->hide(); + m_progressBarOnStatusBar->hide(); m_progressTaskbarIndicator->hide(); m_programController->setIsBusy(false); diff --git a/Pdf4QtViewer/pdfviewermainwindow.h b/Pdf4QtViewer/pdfviewermainwindow.h index fb0eea5..6893bc2 100644 --- a/Pdf4QtViewer/pdfviewermainwindow.h +++ b/Pdf4QtViewer/pdfviewermainwindow.h @@ -41,7 +41,8 @@ #include #include #include -#include +#include +#include class QLabel; class QSpinBox; @@ -120,7 +121,8 @@ private: pdf::PDFProgress* m_progress; PDFWinTaskBarProgress* m_progressTaskbarIndicator; - QProgressDialog* m_progressDialog; + QProgressBar* m_progressBarOnStatusBar; + QLabel* m_progressBarLeftLabelOnStatusBar; bool m_isChangingProgressStep; }; diff --git a/Pdf4QtViewer/pdfviewermainwindowlite.cpp b/Pdf4QtViewer/pdfviewermainwindowlite.cpp index f9b18ad..28b6073 100644 --- a/Pdf4QtViewer/pdfviewermainwindowlite.cpp +++ b/Pdf4QtViewer/pdfviewermainwindowlite.cpp @@ -88,7 +88,8 @@ PDFViewerMainWindowLite::PDFViewerMainWindowLite(QWidget* parent) : m_isLoadingUI(false), m_progress(new pdf::PDFProgress(this)), m_progressTaskbarIndicator(new PDFWinTaskBarProgress(this)), - m_progressDialog(nullptr), + m_progressBarOnStatusBar(nullptr), + m_progressBarLeftLabelOnStatusBar(nullptr), m_isChangingProgressStep(false) { ui->setupUi(this); @@ -99,6 +100,14 @@ PDFViewerMainWindowLite::PDFViewerMainWindowLite(QWidget* parent) : adjustToolbar(ui->mainToolBar); ui->mainToolBar->setWindowTitle(tr("Standard")); + // Initialize status bar + m_progressBarOnStatusBar = new QProgressBar(this); + m_progressBarOnStatusBar->setHidden(true); + m_progressBarLeftLabelOnStatusBar = new QLabel(this); + m_progressBarLeftLabelOnStatusBar->setHidden(true); + statusBar()->addPermanentWidget(m_progressBarLeftLabelOnStatusBar); + statusBar()->addPermanentWidget(m_progressBarOnStatusBar); + // Initialize actions m_actionManager->setAction(PDFActionManager::Open, ui->actionOpen); m_actionManager->setAction(PDFActionManager::Close, ui->actionClose); @@ -270,13 +279,12 @@ void PDFViewerMainWindowLite::onPageZoomSpinboxEditingFinished() void PDFViewerMainWindowLite::onProgressStarted(pdf::ProgressStartupInfo info) { - Q_ASSERT(!m_progressDialog); - if (info.showDialog) - { - m_progressDialog = new QProgressDialog(info.text, QString(), 0, 100, this); - m_progressDialog->setWindowModality(Qt::WindowModal); - m_progressDialog->setCancelButton(nullptr); - } + m_progressBarLeftLabelOnStatusBar->setText(info.text); + m_progressBarLeftLabelOnStatusBar->setVisible(!info.text.isEmpty()); + + m_progressBarOnStatusBar->setRange(0, 100); + m_progressBarOnStatusBar->reset(); + m_progressBarOnStatusBar->show(); m_progressTaskbarIndicator->setRange(0, 100); m_progressTaskbarIndicator->reset(); @@ -294,24 +302,14 @@ void PDFViewerMainWindowLite::onProgressStep(int percentage) } pdf::PDFTemporaryValueChange guard(&m_isChangingProgressStep, true); - - if (m_progressDialog) - { - m_progressDialog->setValue(percentage); - } - + m_progressBarOnStatusBar->setValue(percentage); m_progressTaskbarIndicator->setValue(percentage); } void PDFViewerMainWindowLite::onProgressFinished() { - if (m_progressDialog) - { - m_progressDialog->hide(); - m_progressDialog->deleteLater(); - m_progressDialog = nullptr; - } - + m_progressBarLeftLabelOnStatusBar->hide(); + m_progressBarOnStatusBar->hide(); m_progressTaskbarIndicator->hide(); m_programController->setIsBusy(false); diff --git a/Pdf4QtViewer/pdfviewermainwindowlite.h b/Pdf4QtViewer/pdfviewermainwindowlite.h index 12cfd2f..cf6f36f 100644 --- a/Pdf4QtViewer/pdfviewermainwindowlite.h +++ b/Pdf4QtViewer/pdfviewermainwindowlite.h @@ -40,7 +40,8 @@ #include #include #include -#include +#include +#include class QLabel; class QSpinBox; @@ -115,7 +116,8 @@ private: pdf::PDFProgress* m_progress; PDFWinTaskBarProgress* m_progressTaskbarIndicator; - QProgressDialog* m_progressDialog; + QProgressBar* m_progressBarOnStatusBar; + QLabel* m_progressBarLeftLabelOnStatusBar; bool m_isChangingProgressStep; }; diff --git a/RELEASES.txt b/RELEASES.txt index 7980aa8..c4d0f5d 100644 --- a/RELEASES.txt +++ b/RELEASES.txt @@ -1,4 +1,5 @@ CURRENT: + - Issue #119: Improve search bar 2 (the revenge) - Issue #118: Adding CMAKE options for minimal builds - Issue #116: Improve search bar (rembember searched text, allow scroll and zoom) - Issue #115: Redesign of sidebar widget, new icons