10#include "third_party/pdfium/public/fpdf_doc.h"
11#include "third_party/pdfium/public/fpdf_text.h"
13#include <QtCore/qelapsedtimer.h>
14#include <QtCore/qloggingcategory.h>
15#include <QtCore/QMetaEnum>
66 m_roleNames.insert(
r, roleName);
92 return d->rowCountSoFar;
104 switch (
Role(role)) {
110 return d->searchResults[
pi.page][
pi.index].location();
112 return d->searchResults[
pi.page][
pi.index].contextBefore();
114 return d->searchResults[
pi.page][
pi.index].contextAfter();
119 const QString ret =
d->searchResults[
pi.page][
pi.index].contextBefore() +
121 d->searchResults[
pi.page][
pi.index].contextAfter();
140 return d->searchString;
163 if (
d->searchResults.size() <=
page)
165 return d->searchResults[
page];
178 return d->searchResults[
pi.page][
pi.index];
217 if (
event->timerId() !=
d->updateTimerId)
219 if (!
d->document ||
d->nextPageToUpdate >=
d->document->pageCount()) {
221 qCDebug(qLcS) <<
"done updating search results on" <<
d->searchResults.size() <<
"pages";
223 d->updateTimerId = -1;
225 d->doSearch(
d->nextPageToUpdate++);
262 double pageHeight = FPDF_GetPageHeight(pdfPage);
263 FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
266 FPDF_ClosePage(pdfPage);
272 while (FPDFText_FindNext(sh)) {
273 int idx = FPDFText_GetSchResultIndex(sh);
274 int count = FPDFText_GetSchCount(sh);
275 int rectCount = FPDFText_CountRects(textPage, idx,
count);
279 for (
int r = 0;
r < rectCount; ++
r) {
284 startIndex = FPDFText_GetCharIndexAtPos(textPage,
left,
top,
287 if (
r == rectCount - 1) {
288 endIndex = FPDFText_GetCharIndexAtPos(textPage,
right,
top,
291 qCDebug(qLcS) << rects.
last() <<
"char idx" << startIndex <<
"->" << endIndex;
293 QString contextBefore, contextAfter;
294 if (startIndex >= 0 || endIndex >= 0) {
297 int count = endIndex - startIndex + 1;
300 int len = FPDFText_GetText(textPage, startIndex,
count,
buf.data());
303 reinterpret_cast<const char16_t *
>(
buf.constData()),
len - 1);
312 contextBefore =
context.mid(0, si);
317 newSearchResults <<
QPdfLink(
page, rects, contextBefore, contextAfter);
319 FPDFText_FindClose(sh);
320 FPDFText_ClosePage(textPage);
321 FPDF_ClosePage(pdfPage);
323 << newSearchResults.
size() <<
"results on page" <<
page;
327 if (newSearchResults.
size() > 0) {
329 qCDebug(qLcS) <<
"from row" << rowsBefore <<
"rowCount" <<
rowCountSoFar <<
"increasing by" << newSearchResults.
size();
331 q->beginInsertRows(
QModelIndex(), rowsBefore, rowsBefore + newSearchResults.
size() - 1);
343 int previousTotalSoFar = 0;
348 if (totalSoFar > resultIndex)
349 return {
page, resultIndex - previousTotalSoFar};
350 previousTotalSoFar = totalSoFar;
365#include "moc_qpdfsearchmodel.cpp"
void endResetModel()
Completes a model reset operation.
void beginResetModel()
Begins a model reset operation.
QObject * parent() const
Returns a pointer to the parent object.
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
QChar toLower() const noexcept
Returns the lowercase equivalent if the character is uppercase or titlecase; otherwise returns the ch...
qsizetype size() const noexcept
bool isEmpty() const noexcept
void resize(qsizetype size)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
void killTimer(int id)
Kills the timer with timer identifier, id.
The QPdfDocument class loads a PDF document and renders pages from it.
int pageCount
This property holds the number of pages in the loaded document or 0 if no document is loaded.
void pageCountChanged(int pageCount)
The QPdfLink class defines a link between a region on a page (such as a hyperlink or a search result)...
int rowsBeforePage(int page)
QList< bool > pagesSearched
PageAndIndex pageAndIndexForResult(int resultIndex)
QList< QList< QPdfLink > > searchResults
The QPdfSearchModel class searches for a string in a PDF document and holds the results.
QString searchString
the string to search for
int rowCount(const QModelIndex &parent) const override
\reimp
QPdfLink resultAtIndex(int index) const
Returns a result found by index in the \l document, regardless of the page on which it was found.
void onDocumentPageCountChanged(int count)
void setDocument(QPdfDocument *document)
void updatePage(int page)
QHash< int, QByteArray > roleNames() const override
\reimp
~QPdfSearchModel() override
Destroys the model.
QPdfDocument * document
the document to search
void timerEvent(QTimerEvent *event) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
void setSearchString(const QString &searchString)
QVariant data(const QModelIndex &index, int role) const override
\reimp
void searchStringChanged()
Role
\value Page The page number where the search result is found (int).
QList< QPdfLink > resultsOnPage(int page) const
Returns the list of all results found on the given page.
\inmodule QtCore\reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
static QString fromUtf16(const char16_t *, qsizetype size=-1)
qsizetype size() const
Returns the number of characters in this string.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Combined button and popup list for selecting options.
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
constexpr const T & qMax(const T &a, const T &b)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLdouble GLdouble GLdouble GLdouble top
GLenum GLenum GLsizei count
GLenum GLuint GLenum GLsizei const GLchar * buf
GLdouble GLdouble GLdouble GLdouble q
static const double CharacterHitTolerance
static const int ContextChars
static QT_BEGIN_NAMESPACE const int UpdateTimerInterval
QLatin1StringView QLatin1String
#define QStringLiteral(str)
obj metaObject() -> className()
myObject disconnect()
[26]
\inmodule QtCore \reentrant
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent