mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Advanced search (second part)
This commit is contained in:
@@ -35,6 +35,8 @@ PDFAdvancedFindWidget::PDFAdvancedFindWidget(pdf::PDFDrawWidgetProxy* proxy, QWi
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
ui->resultsTableWidget->setHorizontalHeaderLabels({ tr("Page No."), tr("Phrase"), tr("Context") });
|
||||
|
||||
connect(ui->regularExpressionsCheckbox, &QCheckBox::clicked, this, &PDFAdvancedFindWidget::updateUI);
|
||||
connect(m_proxy, &pdf::PDFDrawWidgetProxy::textLayoutChanged, this, &PDFAdvancedFindWidget::performSearch);
|
||||
updateUI();
|
||||
@@ -62,6 +64,7 @@ void PDFAdvancedFindWidget::on_searchButton_clicked()
|
||||
m_parameters.isRegularExpression = ui->regularExpressionsCheckbox->isChecked();
|
||||
m_parameters.isDotMatchingEverything = ui->dotMatchesEverythingCheckBox->isChecked();
|
||||
m_parameters.isMultiline = ui->multilineMatchingCheckBox->isChecked();
|
||||
m_parameters.isSoftHyphenRemoved = ui->removeSoftHyphenCheckBox->isChecked();
|
||||
m_parameters.isSearchFinished = m_parameters.phrase.isEmpty();
|
||||
|
||||
if (m_parameters.isSearchFinished)
|
||||
@@ -85,6 +88,9 @@ void PDFAdvancedFindWidget::on_searchButton_clicked()
|
||||
}
|
||||
}
|
||||
|
||||
m_findResults.clear();
|
||||
updateResultsUI();
|
||||
|
||||
pdf::PDFAsynchronousTextLayoutCompiler* compiler = m_proxy->getTextLayoutCompiler();
|
||||
if (compiler->isTextLayoutReady())
|
||||
{
|
||||
@@ -104,6 +110,25 @@ void PDFAdvancedFindWidget::updateUI()
|
||||
ui->regularExpressionSettingsGroupBox->setEnabled(enableRegularExpressionUI);
|
||||
}
|
||||
|
||||
void PDFAdvancedFindWidget::updateResultsUI()
|
||||
{
|
||||
ui->tabWidget->setTabText(ui->tabWidget->indexOf(ui->resultsTab), !m_findResults.empty() ? tr("Results (%1)").arg(m_findResults.size()) : tr("Results"));
|
||||
ui->resultsTableWidget->setRowCount(static_cast<int>(m_findResults.size()));
|
||||
|
||||
for (int i = 0, rowCount = int(m_findResults.size()); i < rowCount; ++i)
|
||||
{
|
||||
const pdf::PDFFindResult& findResult = m_findResults[i];
|
||||
ui->resultsTableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(findResult.textSelectionItems.front().first.pageIndex + 1)));
|
||||
ui->resultsTableWidget->setItem(i, 1, new QTableWidgetItem(findResult.matched));
|
||||
ui->resultsTableWidget->setItem(i, 2, new QTableWidgetItem(findResult.context));
|
||||
}
|
||||
|
||||
if (!m_findResults.empty())
|
||||
{
|
||||
ui->tabWidget->setCurrentWidget(ui->resultsTab);
|
||||
}
|
||||
}
|
||||
|
||||
void PDFAdvancedFindWidget::performSearch()
|
||||
{
|
||||
if (m_parameters.isSearchFinished)
|
||||
@@ -120,7 +145,62 @@ void PDFAdvancedFindWidget::performSearch()
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare string to search
|
||||
bool useRegularExpression = m_parameters.isRegularExpression;
|
||||
QString expression = m_parameters.phrase;
|
||||
|
||||
if (m_parameters.isWholeWordsOnly)
|
||||
{
|
||||
if (useRegularExpression)
|
||||
{
|
||||
expression = QString("\\b%1\\b").arg(expression);
|
||||
}
|
||||
else
|
||||
{
|
||||
expression = QString("\\b%1\\b").arg(QRegularExpression::escape(expression));
|
||||
}
|
||||
useRegularExpression = true;
|
||||
}
|
||||
|
||||
pdf::PDFTextFlow::FlowFlags flowFlags = pdf::PDFTextFlow::SeparateBlocks;
|
||||
if (m_parameters.isSoftHyphenRemoved)
|
||||
{
|
||||
flowFlags |= pdf::PDFTextFlow::RemoveSoftHyphen;
|
||||
}
|
||||
if (m_parameters.isRegularExpression)
|
||||
{
|
||||
flowFlags |= pdf::PDFTextFlow::AddLineBreaks;
|
||||
}
|
||||
|
||||
const pdf::PDFTextLayoutStorage* textLayoutStorage = compiler->getTextLayoutStorage();
|
||||
if (!useRegularExpression)
|
||||
{
|
||||
// Use simple text search
|
||||
Qt::CaseSensitivity caseSensitivity = m_parameters.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
||||
m_findResults = textLayoutStorage->find(expression, caseSensitivity, flowFlags);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use regular expression search
|
||||
QRegularExpression::PatternOptions patternOptions = QRegularExpression::UseUnicodePropertiesOption | QRegularExpression::OptimizeOnFirstUsageOption;
|
||||
if (!m_parameters.isCaseSensitive)
|
||||
{
|
||||
patternOptions |= QRegularExpression::CaseInsensitiveOption;
|
||||
}
|
||||
if (m_parameters.isDotMatchingEverything)
|
||||
{
|
||||
patternOptions |= QRegularExpression::DotMatchesEverythingOption;
|
||||
}
|
||||
if (m_parameters.isMultiline)
|
||||
{
|
||||
patternOptions |= QRegularExpression::MultilineOption;
|
||||
}
|
||||
|
||||
QRegularExpression regularExpression(expression, patternOptions);
|
||||
m_findResults = textLayoutStorage->find(regularExpression, flowFlags);
|
||||
}
|
||||
|
||||
updateResultsUI();
|
||||
}
|
||||
|
||||
} // namespace pdfviewer
|
||||
|
@@ -19,6 +19,7 @@
|
||||
#define PDFADVANCEDFINDWIDGET_H
|
||||
|
||||
#include "pdfglobal.h"
|
||||
#include "pdftextlayout.h"
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
@@ -51,6 +52,7 @@ private slots:
|
||||
|
||||
private:
|
||||
void updateUI();
|
||||
void updateResultsUI();
|
||||
void performSearch();
|
||||
|
||||
struct SearchParameters
|
||||
@@ -62,6 +64,7 @@ private:
|
||||
bool isDotMatchingEverything = false;
|
||||
bool isMultiline = false;
|
||||
bool isSearchFinished = false;
|
||||
bool isSoftHyphenRemoved = false;
|
||||
};
|
||||
|
||||
Ui::PDFAdvancedFindWidget* ui;
|
||||
@@ -69,6 +72,7 @@ private:
|
||||
pdf::PDFDrawWidgetProxy* m_proxy;
|
||||
const pdf::PDFDocument* m_document;
|
||||
SearchParameters m_parameters;
|
||||
pdf::PDFFindResults m_findResults;
|
||||
};
|
||||
|
||||
} // namespace pdfviewer
|
||||
|
@@ -30,27 +30,13 @@
|
||||
<string>Search Settings</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="searchSettingsGroupBoxLayout" columnstretch="0,1,0">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="searchLabel">
|
||||
<property name="text">
|
||||
<string>Search for:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="2">
|
||||
<widget class="QLineEdit" name="searchPhraseEdit"/>
|
||||
</item>
|
||||
<item row="4" column="2">
|
||||
<widget class="QPushButton" name="searchButton">
|
||||
<item row="3" column="1" colspan="2">
|
||||
<widget class="QCheckBox" name="regularExpressionsCheckbox">
|
||||
<property name="text">
|
||||
<string>Search</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1" colspan="2">
|
||||
<widget class="QCheckBox" name="caseSensitiveCheckBox">
|
||||
<property name="text">
|
||||
<string>Case sensitive</string>
|
||||
<string>Use regular expressions</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@@ -61,10 +47,31 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1" colspan="2">
|
||||
<widget class="QCheckBox" name="regularExpressionsCheckbox">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="searchLabel">
|
||||
<property name="text">
|
||||
<string>Use regular expressions</string>
|
||||
<string>Search for:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1" colspan="2">
|
||||
<widget class="QCheckBox" name="caseSensitiveCheckBox">
|
||||
<property name="text">
|
||||
<string>Case sensitive</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QCheckBox" name="removeSoftHyphenCheckBox">
|
||||
<property name="text">
|
||||
<string>Remove soft hyphen at end of line</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="2">
|
||||
<widget class="QPushButton" name="searchButton">
|
||||
<property name="text">
|
||||
<string>Search</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@@ -118,12 +125,28 @@
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<item>
|
||||
<widget class="QTreeWidget" name="treeWidget">
|
||||
<column>
|
||||
<property name="text">
|
||||
<string notr="true">1</string>
|
||||
</property>
|
||||
</column>
|
||||
<widget class="QTableWidget" name="resultsTableWidget">
|
||||
<property name="editTriggers">
|
||||
<set>QAbstractItemView::NoEditTriggers</set>
|
||||
</property>
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="selectionBehavior">
|
||||
<enum>QAbstractItemView::SelectRows</enum>
|
||||
</property>
|
||||
<property name="sortingEnabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="columnCount">
|
||||
<number>3</number>
|
||||
</property>
|
||||
<attribute name="horizontalHeaderStretchLastSection">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
<column/>
|
||||
<column/>
|
||||
<column/>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
Reference in New Issue
Block a user