diff options
Diffstat (limited to 'src/pdfquick/qquickpdfsearchmodel.cpp')
-rw-r--r-- | src/pdfquick/qquickpdfsearchmodel.cpp | 168 |
1 files changed, 74 insertions, 94 deletions
diff --git a/src/pdfquick/qquickpdfsearchmodel.cpp b/src/pdfquick/qquickpdfsearchmodel.cpp index 85003cd0d..896584ad7 100644 --- a/src/pdfquick/qquickpdfsearchmodel.cpp +++ b/src/pdfquick/qquickpdfsearchmodel.cpp @@ -1,38 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtPDF module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qquickpdfsearchmodel_p.h" #include <QtCore/qloggingcategory.h> @@ -60,6 +27,11 @@ QQuickPdfSearchModel::QQuickPdfSearchModel(QObject *parent) this, &QQuickPdfSearchModel::onResultsChanged); } +/*! + \internal +*/ +QQuickPdfSearchModel::~QQuickPdfSearchModel() = default; + QQuickPdfDocument *QQuickPdfSearchModel::document() const { return m_quickDocument; @@ -71,7 +43,7 @@ void QQuickPdfSearchModel::setDocument(QQuickPdfDocument *document) return; m_quickDocument = document; - QPdfSearchModel::setDocument(&document->m_doc); + QPdfSearchModel::setDocument(document->document()); } /*! @@ -102,15 +74,16 @@ void QQuickPdfSearchModel::setDocument(QQuickPdfDocument *document) } \endqml + It becomes empty whenever \c {currentPage != currentResultLink.page}. + \sa PathMultiline */ QList<QPolygonF> QQuickPdfSearchModel::currentResultBoundingPolygons() const { QList<QPolygonF> ret; - const auto &results = const_cast<QQuickPdfSearchModel *>(this)->resultsOnPage(m_currentPage); - if (m_currentResult < 0 || m_currentResult >= results.count()) + const auto result = currentResultLink(); + if (result.page() != m_currentPage) return ret; - const auto result = results[m_currentResult]; for (auto rect : result.rectangles()) ret << QPolygonF(rect); return ret; @@ -119,23 +92,21 @@ QList<QPolygonF> QQuickPdfSearchModel::currentResultBoundingPolygons() const /*! \qmlproperty point PdfSearchModel::currentResultBoundingRect - The bounding box containing all \l currentResultBoundingPolygons. - - When this property changes, a scrollable view should automatically scroll - itself in such a way as to ensure that this region is visible; for example, - it could try to position the upper-left corner near the upper-left of its - own viewport, subject to the constraints of the scrollable area. + The bounding box containing all \l currentResultBoundingPolygons, + if \c {currentPage == currentResultLink.page}; otherwise, an invalid rectangle. */ QRectF QQuickPdfSearchModel::currentResultBoundingRect() const { QRectF ret; - const auto &results = const_cast<QQuickPdfSearchModel *>(this)->resultsOnPage(m_currentPage); - if (m_currentResult < 0 || m_currentResult >= results.count()) + const auto result = currentResultLink(); + if (result.page() != m_currentPage) return ret; - auto rects = results[m_currentResult].rectangles(); - ret = rects.takeFirst(); - for (auto rect : rects) - ret = ret.united(rect); + auto rects = result.rectangles(); + if (!rects.isEmpty()) { + ret = rects.takeFirst(); + for (auto rect : rects) + ret = ret.united(rect); + } return ret; } @@ -205,15 +176,15 @@ QList<QPolygonF> QQuickPdfSearchModel::currentPageBoundingPolygons() const */ QList<QPolygonF> QQuickPdfSearchModel::boundingPolygonsOnPage(int page) { - if (!document() || searchString().isEmpty() || page < 0 || page > document()->pageCount()) + if (!document() || searchString().isEmpty() || page < 0 || page > document()->document()->pageCount()) return {}; updatePage(page); QList<QPolygonF> ret; - auto m = QPdfSearchModel::resultsOnPage(page); - for (auto result : m) { - for (auto rect : result.rectangles()) + const auto m = QPdfSearchModel::resultsOnPage(page); + for (const auto &result : m) { + for (const auto &rect : result.rectangles()) ret << QPolygonF(rect); } @@ -228,12 +199,13 @@ QList<QPolygonF> QQuickPdfSearchModel::boundingPolygonsOnPage(int page) */ void QQuickPdfSearchModel::setCurrentPage(int currentPage) { - if (m_currentPage == currentPage) + if (m_currentPage == currentPage || !document()) return; + const auto pageCount = document()->document()->pageCount(); if (currentPage < 0) - currentPage = document()->pageCount() - 1; - else if (currentPage >= document()->pageCount()) + currentPage = pageCount - 1; + else if (currentPage >= pageCount) currentPage = 0; m_currentPage = currentPage; @@ -246,51 +218,50 @@ void QQuickPdfSearchModel::setCurrentPage(int currentPage) /*! \qmlproperty int PdfSearchModel::currentResult - The result index on \l currentPage for which \l currentResultBoundingPolygons - should provide the regions to highlight. + The result index within the whole set of search results, for which + \l currentResultBoundingPolygons should provide the regions to highlight + if currentPage matches \c currentResultLink.page. */ void QQuickPdfSearchModel::setCurrentResult(int currentResult) { if (m_currentResult == currentResult) return; - int currentResultWas = currentResult; - int currentPageWas = m_currentPage; - if (currentResult < 0) { - setCurrentPage(m_currentPage - 1); - while (resultsOnPage(m_currentPage).count() == 0 && m_currentPage != currentPageWas) { - m_suspendSignals = true; - setCurrentPage(m_currentPage - 1); - } - if (m_suspendSignals) { - emit currentPageChanged(); - m_suspendSignals = false; - } - const auto results = resultsOnPage(m_currentPage); - currentResult = results.count() - 1; + const int currentResultWas = m_currentResult; + const int currentPageWas = m_currentPage; + const int resultCount = rowCount({}); + + // wrap around at the ends + if (currentResult >= resultCount) { + currentResult = 0; + } else if (currentResult < 0) { + currentResult = resultCount - 1; + } + + const QPdfLink link = resultAtIndex(currentResult); + if (link.isValid()) { + setCurrentPage(link.page()); + m_currentResult = currentResult; + emit currentResultChanged(); + emit currentResultLinkChanged(); + emit currentResultBoundingPolygonsChanged(); + emit currentResultBoundingRectChanged(); + qCDebug(qLcSearch) << "currentResult was" << currentResultWas + << "requested" << currentResult << "on page" << currentPageWas + << "->" << m_currentResult << "on page" << m_currentPage; } else { - const auto results = resultsOnPage(m_currentPage); - if (currentResult >= results.count()) { - setCurrentPage(m_currentPage + 1); - while (resultsOnPage(m_currentPage).count() == 0 && m_currentPage != currentPageWas) { - m_suspendSignals = true; - setCurrentPage(m_currentPage + 1); - } - if (m_suspendSignals) { - emit currentPageChanged(); - m_suspendSignals = false; - } - currentResult = 0; - } + qWarning() << "failed to find result" << currentResult << "in range 0 ->" << resultCount; } - qCDebug(qLcSearch) << "currentResult was" << m_currentResult - << "requested" << currentResultWas << "on page" << currentPageWas - << "->" << currentResult << "on page" << m_currentPage; +} - m_currentResult = currentResult; - emit currentResultChanged(); - emit currentResultBoundingPolygonsChanged(); - emit currentResultBoundingRectChanged(); +/*! + \qmlproperty QPdfLink PdfSearchModel::currentResultLink + + The result at index \l currentResult. +*/ +QPdfLink QQuickPdfSearchModel::currentResultLink() const +{ + return resultAtIndex(m_currentResult); } /*! @@ -299,4 +270,13 @@ void QQuickPdfSearchModel::setCurrentResult(int currentResult) The string to search for. */ +/*! + \since 6.8 + \qmlproperty int PdfSearchModel::count + + The number of search results found. +*/ + QT_END_NAMESPACE + +#include "moc_qquickpdfsearchmodel_p.cpp" |