summaryrefslogtreecommitdiffstats
path: root/src/pdf
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2020-03-10 12:57:32 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2020-03-11 08:31:44 +0100
commit5dc78ed4e2891205a7162b696b3439a87253140f (patch)
tree09778a7d19015f627be5efa813bf4730a82c1ed1 /src/pdf
parentf253884934cbdbc16fd9b783a2b115960d11af10 (diff)
PdfSelection: add selectAll() function; use in examples
The usual shortcut (control-A) now selects all text on the current page, it is highlighted, and it can be copied to the clipboard. Change-Id: I5e6d9cae675862808f8b9027cb47024ca65cf2fd Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/pdf')
-rw-r--r--src/pdf/api/qpdfdocument.h1
-rw-r--r--src/pdf/api/qpdfdocument_p.h1
-rw-r--r--src/pdf/qpdfdocument.cpp43
-rw-r--r--src/pdf/quick/qml/PdfMultiPageView.qml5
-rw-r--r--src/pdf/quick/qml/PdfPageView.qml3
-rw-r--r--src/pdf/quick/qml/PdfScrollablePageView.qml3
-rw-r--r--src/pdf/quick/qquickpdfselection.cpp16
-rw-r--r--src/pdf/quick/qquickpdfselection_p.h1
8 files changed, 67 insertions, 6 deletions
diff --git a/src/pdf/api/qpdfdocument.h b/src/pdf/api/qpdfdocument.h
index 40df181f4..027cdeb47 100644
--- a/src/pdf/api/qpdfdocument.h
+++ b/src/pdf/api/qpdfdocument.h
@@ -113,6 +113,7 @@ public:
QImage render(int page, QSize imageSize, QPdfDocumentRenderOptions options = QPdfDocumentRenderOptions());
Q_INVOKABLE QPdfSelection getSelection(int page, QPointF start, QPointF end);
+ Q_INVOKABLE QPdfSelection getAllText(int page);
Q_SIGNALS:
void passwordChanged();
diff --git a/src/pdf/api/qpdfdocument_p.h b/src/pdf/api/qpdfdocument_p.h
index 15d8b8259..2dcb70407 100644
--- a/src/pdf/api/qpdfdocument_p.h
+++ b/src/pdf/api/qpdfdocument_p.h
@@ -105,6 +105,7 @@ public:
static int fpdf_GetBlock(void* param, unsigned long position, unsigned char* pBuf, unsigned long size);
static void fpdf_AddSegment(struct _FX_DOWNLOADHINTS* pThis, size_t offset, size_t size);
void updateLastError();
+ QString getText(FPDF_TEXTPAGE textPage, int startIndex, int count);
};
QT_END_NAMESPACE
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp
index 1e8a0f527..d56edf4e9 100644
--- a/src/pdf/qpdfdocument.cpp
+++ b/src/pdf/qpdfdocument.cpp
@@ -393,6 +393,15 @@ void QPdfDocumentPrivate::fpdf_AddSegment(_FX_DOWNLOADHINTS *pThis, size_t offse
Q_UNUSED(size);
}
+QString QPdfDocumentPrivate::getText(FPDF_TEXTPAGE textPage, int startIndex, int count)
+{
+ QVector<ushort> buf(count + 1);
+ // TODO is that enough space in case one unicode character is more than one in utf-16?
+ int len = FPDFText_GetText(textPage, startIndex, count, buf.data());
+ Q_ASSERT(len - 1 <= count); // len is number of characters written, including the terminator
+ return QString::fromUtf16(buf.constData(), len - 1);
+}
+
/*!
\class QPdfDocument
\since 5.10
@@ -737,15 +746,10 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end)
int endIndex = FPDFText_GetCharIndexAtPos(textPage, end.x(), pageHeight - end.y(),
CharacterHitTolerance, CharacterHitTolerance);
if (startIndex >= 0 && endIndex != startIndex) {
- QString text;
if (startIndex > endIndex)
qSwap(startIndex, endIndex);
int count = endIndex - startIndex + 1;
- QVector<ushort> buf(count + 1);
- // TODO is that enough space in case one unicode character is more than one in utf-16?
- int len = FPDFText_GetText(textPage, startIndex, count, buf.data());
- Q_ASSERT(len - 1 <= count); // len is number of characters written, including the terminator
- text = QString::fromUtf16(buf.constData(), len - 1);
+ QString text = d->getText(textPage, startIndex, count);
QVector<QPolygonF> bounds;
int rectCount = FPDFText_CountRects(textPage, startIndex, endIndex - startIndex);
for (int i = 0; i < rectCount; ++i) {
@@ -767,6 +771,33 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end)
return QPdfSelection();
}
+QPdfSelection QPdfDocument::getAllText(int page)
+{
+ const QPdfMutexLocker lock;
+ FPDF_PAGE pdfPage = FPDF_LoadPage(d->doc, page);
+ double pageHeight = FPDF_GetPageHeight(pdfPage);
+ FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
+ int count = FPDFText_CountChars(textPage);
+ if (count < 1)
+ return QPdfSelection();
+ QString text = d->getText(textPage, 0, count);
+ QVector<QPolygonF> bounds;
+ int rectCount = FPDFText_CountRects(textPage, 0, count);
+ for (int i = 0; i < rectCount; ++i) {
+ double l, r, b, t;
+ FPDFText_GetRect(textPage, i, &l, &t, &r, &b);
+ QPolygonF poly;
+ poly << QPointF(l, pageHeight - t);
+ poly << QPointF(r, pageHeight - t);
+ poly << QPointF(r, pageHeight - b);
+ poly << QPointF(l, pageHeight - b);
+ poly << QPointF(l, pageHeight - t);
+ bounds << poly;
+ }
+ qCDebug(qLcDoc) << "on page" << page << "got" << count << "chars" << rectCount << "rects";
+ return QPdfSelection(text, bounds);
+}
+
QT_END_NAMESPACE
#include "moc_qpdfdocument.cpp"
diff --git a/src/pdf/quick/qml/PdfMultiPageView.qml b/src/pdf/quick/qml/PdfMultiPageView.qml
index 579c9a1ce..70bb5454f 100644
--- a/src/pdf/quick/qml/PdfMultiPageView.qml
+++ b/src/pdf/quick/qml/PdfMultiPageView.qml
@@ -47,6 +47,11 @@ Item {
property bool debug: false
property string selectedText
+ function selectAll() {
+ var currentItem = tableView.itemAtPos(0, tableView.contentY + root.height / 2)
+ if (currentItem !== null)
+ currentItem.selection.selectAll()
+ }
function copySelectionToClipboard() {
var currentItem = tableView.itemAtPos(0, tableView.contentY + root.height / 2)
if (debug)
diff --git a/src/pdf/quick/qml/PdfPageView.qml b/src/pdf/quick/qml/PdfPageView.qml
index dfd00a1a8..b90ad2d7f 100644
--- a/src/pdf/quick/qml/PdfPageView.qml
+++ b/src/pdf/quick/qml/PdfPageView.qml
@@ -46,6 +46,9 @@ Rectangle {
property alias status: image.status
property alias selectedText: selection.text
+ function selectAll() {
+ selection.selectAll()
+ }
function copySelectionToClipboard() {
selection.copyToClipboard()
}
diff --git a/src/pdf/quick/qml/PdfScrollablePageView.qml b/src/pdf/quick/qml/PdfScrollablePageView.qml
index 4c43972c9..6076e57df 100644
--- a/src/pdf/quick/qml/PdfScrollablePageView.qml
+++ b/src/pdf/quick/qml/PdfScrollablePageView.qml
@@ -47,6 +47,9 @@ Flickable {
property alias status: image.status
property alias selectedText: selection.text
+ function selectAll() {
+ selection.selectAll()
+ }
function copySelectionToClipboard() {
selection.copyToClipboard()
}
diff --git a/src/pdf/quick/qquickpdfselection.cpp b/src/pdf/quick/qquickpdfselection.cpp
index d313820ba..5371e85e5 100644
--- a/src/pdf/quick/qquickpdfselection.cpp
+++ b/src/pdf/quick/qquickpdfselection.cpp
@@ -124,6 +124,22 @@ QVector<QPolygonF> QQuickPdfSelection::geometry() const
return m_geometry;
}
+void QQuickPdfSelection::selectAll()
+{
+ QPdfSelection sel = m_document->m_doc.getAllText(m_page);
+ if (sel.text() != m_text) {
+ m_text = sel.text();
+ if (QGuiApplication::clipboard()->supportsSelection())
+ sel.copyToClipboard(QClipboard::Selection);
+ emit textChanged();
+ }
+
+ if (sel.bounds() != m_geometry) {
+ m_geometry = sel.bounds();
+ emit geometryChanged();
+ }
+}
+
void QQuickPdfSelection::resetPoints()
{
bool wasHolding = m_hold;
diff --git a/src/pdf/quick/qquickpdfselection_p.h b/src/pdf/quick/qquickpdfselection_p.h
index a0e6d1a8d..bb4a50fed 100644
--- a/src/pdf/quick/qquickpdfselection_p.h
+++ b/src/pdf/quick/qquickpdfselection_p.h
@@ -86,6 +86,7 @@ public:
QString text() const;
QVector<QPolygonF> geometry() const;
+ Q_INVOKABLE void selectAll();
#if QT_CONFIG(clipboard)
Q_INVOKABLE void copyToClipboard() const;
#endif