Advanced search - finishing, fixing bugs

This commit is contained in:
Jakub Melka
2020-01-11 16:14:38 +01:00
parent d3c0e418e7
commit 1ecc5f2441
7 changed files with 172 additions and 30 deletions

View File

@ -675,6 +675,13 @@ void PDFDrawWidgetProxy::draw(QPainter* painter, QRect rect)
painter->restore(); painter->restore();
} }
for (IDocumentDrawInterface* drawInterface : m_drawInterfaces)
{
painter->save();
drawInterface->drawPage(painter, item.pageIndex, compiledPage, layoutGetter, matrix);
painter->restore();
}
const qint64 drawTimeNS = timer.nsecsElapsed(); const qint64 drawTimeNS = timer.nsecsElapsed();
// Draw rendering times // Draw rendering times
@ -1200,4 +1207,17 @@ void PDFDrawWidgetProxy::onOptionalContentGroupStateChanged()
emit pageImageChanged(true, { }); emit pageImageChanged(true, { });
} }
void IDocumentDrawInterface::drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const
{
Q_UNUSED(painter);
Q_UNUSED(pageIndex);
Q_UNUSED(compiledPage);
Q_UNUSED(layoutGetter);
Q_UNUSED(pagePointToDevicePointMatrix);
}
} // namespace pdf } // namespace pdf

View File

@ -36,9 +36,30 @@ class PDFProgress;
class PDFWidget; class PDFWidget;
class IDrawWidget; class IDrawWidget;
class PDFCMSManager; class PDFCMSManager;
class PDFTextLayoutGetter;
class PDFAsynchronousPageCompiler; class PDFAsynchronousPageCompiler;
class PDFAsynchronousTextLayoutCompiler; class PDFAsynchronousTextLayoutCompiler;
class PDFFORQTLIBSHARED_EXPORT IDocumentDrawInterface
{
public:
explicit inline IDocumentDrawInterface() = default;
virtual ~IDocumentDrawInterface() = default;
/// Performs drawing of additional graphics onto the painter using precompiled page,
/// optionally text layout and page point to device point matrix.
/// \param painter Painter
/// \param pageIndex Page index
/// \param compiledPage Compiled page
/// \param layoutGetter Layout getter
/// \param pagePointToDevicePointMatrix Matrix mapping page space to device point space
virtual void drawPage(QPainter* painter,
pdf::PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const;
};
/// This class controls draw space - page layout. Pages are divided into blocks /// This class controls draw space - page layout. Pages are divided into blocks
/// each block can contain one or multiple pages. Units are in milimeters. /// each block can contain one or multiple pages. Units are in milimeters.
/// Pages are layouted in zoom-independent mode. /// Pages are layouted in zoom-independent mode.
@ -286,6 +307,9 @@ public:
static constexpr PDFReal getMinZoom() { return MIN_ZOOM; } static constexpr PDFReal getMinZoom() { return MIN_ZOOM; }
static constexpr PDFReal getMaxZoom() { return MAX_ZOOM; } static constexpr PDFReal getMaxZoom() { return MAX_ZOOM; }
void registerDrawInterface(IDocumentDrawInterface* drawInterface) { m_drawInterfaces.insert(drawInterface); }
void unregisterDrawInterface(IDocumentDrawInterface* drawInterface) { m_drawInterfaces.erase(drawInterface); }
signals: signals:
void drawSpaceChanged(); void drawSpaceChanged();
void pageLayoutChanged(); void pageLayoutChanged();
@ -422,6 +446,9 @@ private:
/// Progress /// Progress
PDFProgress* m_progress; PDFProgress* m_progress;
/// Additional drawing interfaces
std::set<IDocumentDrawInterface*> m_drawInterfaces;
}; };
} // namespace pdf } // namespace pdf

View File

@ -1041,7 +1041,7 @@ void PDFTextSelectionPainter::draw(QPainter* painter, PDFInteger pageIndex, PDFT
QMatrix transformMatrix = angleMatrix.inverted() * matrix; QMatrix transformMatrix = angleMatrix.inverted() * matrix;
path = transformMatrix.map(path); path = transformMatrix.map(path);
QColor penColor = item.color; QColor penColor = item.color.darker();
QColor brushColor = item.color; QColor brushColor = item.color;
brushColor.setAlphaF(SELECTION_ALPHA); brushColor.setAlphaF(SELECTION_ALPHA);

View File

@ -208,7 +208,7 @@ using PDFTextSelectionColoredItems = std::vector<PDFTextSelectionColoredItem>;
/// Text selection, can be used across multiple pages. Also defines color /// Text selection, can be used across multiple pages. Also defines color
/// for each text selection. /// for each text selection.
class PDFTextSelection class PDFFORQTLIBSHARED_EXPORT PDFTextSelection
{ {
public: public:
explicit PDFTextSelection() = default; explicit PDFTextSelection() = default;
@ -376,7 +376,7 @@ private:
}; };
/// Paints text selection on various pages using page to device point matrix /// Paints text selection on various pages using page to device point matrix
class PDFTextSelectionPainter class PDFFORQTLIBSHARED_EXPORT PDFTextSelectionPainter
{ {
public: public:
explicit inline PDFTextSelectionPainter(const PDFTextSelection* selection) : explicit inline PDFTextSelectionPainter(const PDFTextSelection* selection) :
@ -394,7 +394,7 @@ public:
void draw(QPainter* painter, PDFInteger pageIndex, PDFTextLayoutGetter& textLayoutGetter, const QMatrix& matrix); void draw(QPainter* painter, PDFInteger pageIndex, PDFTextLayoutGetter& textLayoutGetter, const QMatrix& matrix);
private: private:
static constexpr const PDFReal HEIGHT_INCREASE_FACTOR = 0.25; static constexpr const PDFReal HEIGHT_INCREASE_FACTOR = 0.40;
static constexpr const PDFReal SELECTION_ALPHA = 0.25; static constexpr const PDFReal SELECTION_ALPHA = 0.25;
const PDFTextSelection* m_selection; const PDFTextSelection* m_selection;

View File

@ -40,6 +40,7 @@ PDFAdvancedFindWidget::PDFAdvancedFindWidget(pdf::PDFDrawWidgetProxy* proxy, QWi
connect(ui->regularExpressionsCheckbox, &QCheckBox::clicked, this, &PDFAdvancedFindWidget::updateUI); connect(ui->regularExpressionsCheckbox, &QCheckBox::clicked, this, &PDFAdvancedFindWidget::updateUI);
connect(m_proxy, &pdf::PDFDrawWidgetProxy::textLayoutChanged, this, &PDFAdvancedFindWidget::performSearch); connect(m_proxy, &pdf::PDFDrawWidgetProxy::textLayoutChanged, this, &PDFAdvancedFindWidget::performSearch);
connect(ui->resultsTableWidget, &QTableWidget::cellDoubleClicked, this, &PDFAdvancedFindWidget::onResultItemDoubleClicked); connect(ui->resultsTableWidget, &QTableWidget::cellDoubleClicked, this, &PDFAdvancedFindWidget::onResultItemDoubleClicked);
connect(ui->resultsTableWidget, &QTableWidget::itemSelectionChanged, this, &PDFAdvancedFindWidget::onSelectionChanged);
updateUI(); updateUI();
} }
@ -105,6 +106,19 @@ void PDFAdvancedFindWidget::on_searchButton_clicked()
} }
} }
void PDFAdvancedFindWidget::on_clearButton_clicked()
{
m_parameters = SearchParameters();
m_findResults.clear();
updateResultsUI();
}
void PDFAdvancedFindWidget::onSelectionChanged()
{
m_textSelection.dirty();
m_proxy->repaintNeeded();
}
void PDFAdvancedFindWidget::onResultItemDoubleClicked(int row, int column) void PDFAdvancedFindWidget::onResultItemDoubleClicked(int row, int column)
{ {
Q_UNUSED(column); Q_UNUSED(column);
@ -144,6 +158,19 @@ void PDFAdvancedFindWidget::updateResultsUI()
} }
} }
void PDFAdvancedFindWidget::drawPage(QPainter* painter,
pdf::PDFInteger pageIndex,
const pdf::PDFPrecompiledPage* compiledPage,
pdf::PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const
{
Q_UNUSED(compiledPage);
const pdf::PDFTextSelection& textSelection = getTextSelection();
pdf::PDFTextSelectionPainter textSelectionPainter(&textSelection);
textSelectionPainter.draw(painter, pageIndex, layoutGetter, pagePointToDevicePointMatrix);
}
void PDFAdvancedFindWidget::performSearch() void PDFAdvancedFindWidget::performSearch()
{ {
if (m_parameters.isSearchFinished) if (m_parameters.isSearchFinished)
@ -215,7 +242,47 @@ void PDFAdvancedFindWidget::performSearch()
m_findResults = textLayoutStorage->find(regularExpression, flowFlags); m_findResults = textLayoutStorage->find(regularExpression, flowFlags);
} }
m_textSelection.dirty();
m_proxy->repaintNeeded();
updateResultsUI(); updateResultsUI();
} }
pdf::PDFTextSelection PDFAdvancedFindWidget::getTextSelectionImpl() const
{
pdf::PDFTextSelection result;
std::vector<size_t> selectedRowIndices;
QModelIndexList selectedRows = ui->resultsTableWidget->selectionModel()->selectedRows();
std::transform(selectedRows.cbegin(), selectedRows.cend(), std::back_inserter(selectedRowIndices), [] (const QModelIndex& index) { return index.row(); });
std::sort(selectedRowIndices.begin(), selectedRowIndices.end());
for (size_t i = 0; i < m_findResults.size(); ++i)
{
const pdf::PDFFindResult& findResult = m_findResults[i];
QColor color(Qt::blue);
if (std::binary_search(selectedRowIndices.cbegin(), selectedRowIndices.cend(), i))
{
color = QColor(Qt::yellow);
}
result.addItems(findResult.textSelectionItems, color);
}
return result;
}
void PDFAdvancedFindWidget::showEvent(QShowEvent* event)
{
BaseClass::showEvent(event);
m_proxy->registerDrawInterface(this);
}
void PDFAdvancedFindWidget::hideEvent(QHideEvent* event)
{
m_proxy->unregisterDrawInterface(this);
BaseClass::hideEvent(event);
}
} // namespace pdfviewer } // namespace pdfviewer

View File

@ -19,6 +19,7 @@
#define PDFADVANCEDFINDWIDGET_H #define PDFADVANCEDFINDWIDGET_H
#include "pdfglobal.h" #include "pdfglobal.h"
#include "pdfdrawspacecontroller.h"
#include "pdftextlayout.h" #include "pdftextlayout.h"
#include <QWidget> #include <QWidget>
@ -37,25 +38,44 @@ class PDFDrawWidgetProxy;
namespace pdfviewer namespace pdfviewer
{ {
class PDFAdvancedFindWidget : public QWidget class PDFAdvancedFindWidget : public QWidget, public pdf::IDocumentDrawInterface
{ {
Q_OBJECT Q_OBJECT
private:
using BaseClass = QWidget;
public: public:
explicit PDFAdvancedFindWidget(pdf::PDFDrawWidgetProxy* proxy, QWidget* parent = nullptr); explicit PDFAdvancedFindWidget(pdf::PDFDrawWidgetProxy* proxy, QWidget* parent = nullptr);
virtual ~PDFAdvancedFindWidget() override; virtual ~PDFAdvancedFindWidget() override;
virtual void drawPage(QPainter* painter,
pdf::PDFInteger pageIndex,
const pdf::PDFPrecompiledPage* compiledPage,
pdf::PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const override;
void setDocument(const pdf::PDFDocument* document); void setDocument(const pdf::PDFDocument* document);
protected:
virtual void showEvent(QShowEvent* event) override;
virtual void hideEvent(QHideEvent* event) override;
private slots: private slots:
void on_searchButton_clicked(); void on_searchButton_clicked();
void onSelectionChanged();
void onResultItemDoubleClicked(int row, int column); void onResultItemDoubleClicked(int row, int column);
void on_clearButton_clicked();
private: private:
void updateUI(); void updateUI();
void updateResultsUI(); void updateResultsUI();
void performSearch(); void performSearch();
pdf::PDFTextSelection getTextSelection() const { return m_textSelection.get(this, &PDFAdvancedFindWidget::getTextSelectionImpl); }
pdf::PDFTextSelection getTextSelectionImpl() const;
struct SearchParameters struct SearchParameters
{ {
QString phrase; QString phrase;
@ -74,6 +94,7 @@ private:
const pdf::PDFDocument* m_document; const pdf::PDFDocument* m_document;
SearchParameters m_parameters; SearchParameters m_parameters;
pdf::PDFFindResults m_findResults; pdf::PDFFindResults m_findResults;
mutable pdf::PDFCachedItem<pdf::PDFTextSelection> m_textSelection;
}; };
} // namespace pdfviewer } // namespace pdfviewer

View File

@ -29,38 +29,21 @@
<property name="title"> <property name="title">
<string>Search Settings</string> <string>Search Settings</string>
</property> </property>
<layout class="QGridLayout" name="searchSettingsGroupBoxLayout" columnstretch="0,1,0"> <layout class="QGridLayout" name="searchSettingsGroupBoxLayout" columnstretch="0,1,0,0">
<item row="0" column="1" colspan="2"> <item row="5" column="3">
<widget class="QLineEdit" name="searchPhraseEdit"/> <widget class="QPushButton" name="searchButton">
</item>
<item row="3" column="1" colspan="2">
<widget class="QCheckBox" name="regularExpressionsCheckbox">
<property name="text"> <property name="text">
<string>Use regular expressions</string> <string>Search</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1" colspan="2"> <item row="2" column="1" colspan="3">
<widget class="QCheckBox" name="wholeWordsOnlyCheckBox"> <widget class="QCheckBox" name="wholeWordsOnlyCheckBox">
<property name="text"> <property name="text">
<string>Whole words only</string> <string>Whole words only</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0">
<widget class="QLabel" name="searchLabel">
<property name="text">
<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"> <item row="4" column="1">
<widget class="QCheckBox" name="removeSoftHyphenCheckBox"> <widget class="QCheckBox" name="removeSoftHyphenCheckBox">
<property name="text"> <property name="text">
@ -68,10 +51,34 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="2"> <item row="0" column="0">
<widget class="QPushButton" name="searchButton"> <widget class="QLabel" name="searchLabel">
<property name="text"> <property name="text">
<string>Search</string> <string>Search for:</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="3">
<widget class="QCheckBox" name="caseSensitiveCheckBox">
<property name="text">
<string>Case sensitive</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="3">
<widget class="QLineEdit" name="searchPhraseEdit"/>
</item>
<item row="3" column="1" colspan="3">
<widget class="QCheckBox" name="regularExpressionsCheckbox">
<property name="text">
<string>Use regular expressions</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QPushButton" name="clearButton">
<property name="text">
<string>Clear</string>
</property> </property>
</widget> </widget>
</item> </item>