mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Issue #116: Improve search bar (rembember searched text, allow scroll and zoom)
This commit is contained in:
@@ -46,6 +46,9 @@ public:
|
|||||||
|
|
||||||
/// Returns page indices, which are currently displayed in the widget
|
/// Returns page indices, which are currently displayed in the widget
|
||||||
virtual std::vector<PDFInteger> getCurrentPages() const = 0;
|
virtual std::vector<PDFInteger> getCurrentPages() const = 0;
|
||||||
|
|
||||||
|
/// Runs event on this widget
|
||||||
|
virtual bool doEvent(QEvent* event) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PDF4QTLIBSHARED_EXPORT PDFWidget : public QWidget
|
class PDF4QTLIBSHARED_EXPORT PDFWidget : public QWidget
|
||||||
@@ -138,6 +141,7 @@ public:
|
|||||||
|
|
||||||
virtual QSize minimumSizeHint() const override;
|
virtual QSize minimumSizeHint() const override;
|
||||||
virtual QWidget* getWidget() override { return this; }
|
virtual QWidget* getWidget() override { return this; }
|
||||||
|
virtual bool doEvent(QEvent* event) override { return this->event(event); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool event(QEvent* event) override;
|
virtual bool event(QEvent* event) override;
|
||||||
|
@@ -221,6 +221,56 @@ void PDFWidgetTool::removeTool()
|
|||||||
m_toolStack.pop_back();
|
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;
|
||||||
|
|
||||||
|
private:
|
||||||
|
PDFDrawWidgetProxy* m_proxy;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool PDFFindTextToolDialog::event(QEvent* event)
|
||||||
|
{
|
||||||
|
switch (event->type())
|
||||||
|
{
|
||||||
|
case QEvent::Wheel:
|
||||||
|
{
|
||||||
|
PDFWidget* widget = m_proxy->getWidget();
|
||||||
|
|
||||||
|
QWheelEvent* oldEvent = dynamic_cast<QWheelEvent*>(event);
|
||||||
|
|
||||||
|
if (!rect().contains(oldEvent->position().toPoint(), false))
|
||||||
|
{
|
||||||
|
IDrawWidget* pdfDrawWidget = widget->getDrawWidget();
|
||||||
|
QPointF position = pdfDrawWidget->getWidget()->mapFromGlobal(oldEvent->globalPosition());
|
||||||
|
|
||||||
|
QWheelEvent wheelEvent(position, oldEvent->globalPosition(),
|
||||||
|
oldEvent->pixelDelta(), oldEvent->angleDelta(), oldEvent->buttons(),
|
||||||
|
oldEvent->modifiers(), oldEvent->phase(), oldEvent->isInverted(),
|
||||||
|
oldEvent->source(), oldEvent->pointingDevice());
|
||||||
|
return pdfDrawWidget->doEvent(&wheelEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return QDialog::event(event);
|
||||||
|
}
|
||||||
|
|
||||||
PDFFindTextTool::PDFFindTextTool(PDFDrawWidgetProxy* proxy, QAction* prevAction, QAction* nextAction, QObject* parent, QWidget* parentDialog) :
|
PDFFindTextTool::PDFFindTextTool(PDFDrawWidgetProxy* proxy, QAction* prevAction, QAction* nextAction, QObject* parent, QWidget* parentDialog) :
|
||||||
BaseClass(proxy, parent),
|
BaseClass(proxy, parent),
|
||||||
m_prevAction(prevAction),
|
m_prevAction(prevAction),
|
||||||
@@ -262,6 +312,7 @@ void PDFFindTextTool::clearResults()
|
|||||||
m_findResults.clear();
|
m_findResults.clear();
|
||||||
m_selectedResultIndex = 0;
|
m_selectedResultIndex = 0;
|
||||||
m_textSelection.dirty();
|
m_textSelection.dirty();
|
||||||
|
getProxy()->repaintNeeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PDFFindTextTool::setActiveImpl(bool active)
|
void PDFFindTextTool::setActiveImpl(bool active)
|
||||||
@@ -276,7 +327,7 @@ void PDFFindTextTool::setActiveImpl(bool active)
|
|||||||
getProxy()->getTextLayoutCompiler()->makeTextLayout();
|
getProxy()->getTextLayoutCompiler()->makeTextLayout();
|
||||||
|
|
||||||
// Create dialog
|
// Create dialog
|
||||||
m_dialog = new QDialog(m_parentDialog, Qt::Popup | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
|
m_dialog = new PDFFindTextToolDialog(getProxy(), m_parentDialog, Qt::Popup | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
|
||||||
m_dialog->setWindowTitle(tr("Find"));
|
m_dialog->setWindowTitle(tr("Find"));
|
||||||
|
|
||||||
QGridLayout* layout = new QGridLayout(m_dialog);
|
QGridLayout* layout = new QGridLayout(m_dialog);
|
||||||
@@ -294,6 +345,10 @@ void PDFFindTextTool::setActiveImpl(bool active)
|
|||||||
m_previousButton = new QPushButton(tr("Previous"), m_dialog);
|
m_previousButton = new QPushButton(tr("Previous"), m_dialog);
|
||||||
m_nextButton = new QPushButton(tr("Next"), m_dialog);
|
m_nextButton = new QPushButton(tr("Next"), m_dialog);
|
||||||
|
|
||||||
|
m_findTextEdit->setText(m_savedText);
|
||||||
|
m_caseSensitiveCheckBox->setChecked(m_savedIsCaseSensitive);
|
||||||
|
m_wholeWordsCheckBox->setChecked(m_savedIsWholeWords);
|
||||||
|
|
||||||
m_previousButton->setDefault(false);
|
m_previousButton->setDefault(false);
|
||||||
m_nextButton->setDefault(false);
|
m_nextButton->setDefault(false);
|
||||||
|
|
||||||
@@ -322,11 +377,19 @@ void PDFFindTextTool::setActiveImpl(bool active)
|
|||||||
m_dialog->move(topRightParent - QPoint(m_dialog->width() * 1.1, 0));
|
m_dialog->move(topRightParent - QPoint(m_dialog->width() * 1.1, 0));
|
||||||
m_dialog->setFocus();
|
m_dialog->setFocus();
|
||||||
m_findTextEdit->setFocus();
|
m_findTextEdit->setFocus();
|
||||||
connect(m_dialog, &QDialog::rejected, this, [this] { setActive(false); });
|
m_findTextEdit->selectAll();
|
||||||
|
connect(m_dialog, &QDialog::rejected, this, &PDFFindTextTool::onDialogRejected);
|
||||||
|
|
||||||
|
onSearchText();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Q_ASSERT(m_dialog);
|
Q_ASSERT(m_dialog);
|
||||||
|
|
||||||
|
m_savedText = m_findTextEdit->text();
|
||||||
|
m_savedIsCaseSensitive = m_caseSensitiveCheckBox->isChecked();
|
||||||
|
m_savedIsWholeWords = m_wholeWordsCheckBox->isChecked();
|
||||||
|
|
||||||
m_dialog->deleteLater();
|
m_dialog->deleteLater();
|
||||||
m_dialog = nullptr;
|
m_dialog = nullptr;
|
||||||
m_caseSensitiveCheckBox = nullptr;
|
m_caseSensitiveCheckBox = nullptr;
|
||||||
@@ -351,8 +414,7 @@ void PDFFindTextTool::onSearchText()
|
|||||||
m_parameters.isWholeWordsOnly = m_wholeWordsCheckBox->isChecked();
|
m_parameters.isWholeWordsOnly = m_wholeWordsCheckBox->isChecked();
|
||||||
m_parameters.isSearchFinished = m_parameters.phrase.isEmpty();
|
m_parameters.isSearchFinished = m_parameters.phrase.isEmpty();
|
||||||
|
|
||||||
m_findResults.clear();
|
clearResults();
|
||||||
m_textSelection.dirty();
|
|
||||||
updateResultsUI();
|
updateResultsUI();
|
||||||
|
|
||||||
if (m_parameters.isSearchFinished)
|
if (m_parameters.isSearchFinished)
|
||||||
@@ -403,6 +465,11 @@ void PDFFindTextTool::onActionNext()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PDFFindTextTool::onDialogRejected()
|
||||||
|
{
|
||||||
|
setActive(false);
|
||||||
|
}
|
||||||
|
|
||||||
void PDFFindTextTool::performSearch()
|
void PDFFindTextTool::performSearch()
|
||||||
{
|
{
|
||||||
if (m_parameters.isSearchFinished)
|
if (m_parameters.isSearchFinished)
|
||||||
|
@@ -177,6 +177,7 @@ private:
|
|||||||
void onSearchText();
|
void onSearchText();
|
||||||
void onActionPrevious();
|
void onActionPrevious();
|
||||||
void onActionNext();
|
void onActionNext();
|
||||||
|
void onDialogRejected();
|
||||||
|
|
||||||
void performSearch();
|
void performSearch();
|
||||||
void updateResultsUI();
|
void updateResultsUI();
|
||||||
@@ -194,6 +195,10 @@ private:
|
|||||||
QPushButton* m_previousButton;
|
QPushButton* m_previousButton;
|
||||||
QPushButton* m_nextButton;
|
QPushButton* m_nextButton;
|
||||||
|
|
||||||
|
QString m_savedText;
|
||||||
|
bool m_savedIsCaseSensitive = false;
|
||||||
|
bool m_savedIsWholeWords = false;
|
||||||
|
|
||||||
pdf::PDFTextSelection getTextSelection() const { return m_textSelection.get(this, &PDFFindTextTool::getTextSelectionImpl); }
|
pdf::PDFTextSelection getTextSelection() const { return m_textSelection.get(this, &PDFFindTextTool::getTextSelectionImpl); }
|
||||||
pdf::PDFTextSelection getTextSelectionImpl() const;
|
pdf::PDFTextSelection getTextSelectionImpl() const;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user