diff options
author | Alberto Mardegan <info@mardy.it> | 2015-07-26 19:43:26 +0300 |
---|---|---|
committer | Konstantin Ritt <ritt.ks@gmail.com> | 2016-03-18 19:02:13 +0000 |
commit | fb6000a74f57bd3c096f6a10142477bf2faf0ff2 (patch) | |
tree | 393d3013c4adf8fec9df787b0d06e43fc33ef832 /tests/auto/gui/text/qabstracttextdocumentlayout | |
parent | 92f9a7780e381f2b1336d965a9a5b171ae39144c (diff) |
Add QAbstractTextDocumentLayout::imageAt(), formatAt()
The new imageAt() method pairs with the existing anchorAt() method, and
allows retrieving the source link of the image under the cursor.
We also expose the common logic between these two methods as an
additional formatAt() method.
[ChangeLog][QtGui][QAbstractTextDocumentLayout] Added imageAt() and
formatAt() methods, which respectively can be used to retrieve the
source link of the image under the cursor, or the QTextFormat of the
text under the cursor.
Change-Id: If09815dde91de6616edcb19c72c462dbf7abd8ef
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'tests/auto/gui/text/qabstracttextdocumentlayout')
-rw-r--r-- | tests/auto/gui/text/qabstracttextdocumentlayout/tst_qabstracttextdocumentlayout.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/auto/gui/text/qabstracttextdocumentlayout/tst_qabstracttextdocumentlayout.cpp b/tests/auto/gui/text/qabstracttextdocumentlayout/tst_qabstracttextdocumentlayout.cpp index 15fb392b18..9542d306ba 100644 --- a/tests/auto/gui/text/qabstracttextdocumentlayout/tst_qabstracttextdocumentlayout.cpp +++ b/tests/auto/gui/text/qabstracttextdocumentlayout/tst_qabstracttextdocumentlayout.cpp @@ -48,6 +48,8 @@ private slots: void getSetCheck(); void maximumBlockCount(); void anchorAt(); + void imageAt(); + void formatAt(); }; tst_QAbstractTextDocumentLayout::tst_QAbstractTextDocumentLayout() @@ -171,5 +173,61 @@ void tst_QAbstractTextDocumentLayout::anchorAt() QCOMPARE(documentLayout->anchorAt(preeditPoint), QString()); } +void tst_QAbstractTextDocumentLayout::imageAt() +{ + QTextDocument doc; + doc.setHtml("foo<a href=\"link\"><img src=\"image\" width=\"50\" height=\"50\"/></a>"); + QAbstractTextDocumentLayout *documentLayout = doc.documentLayout(); + QTextBlock firstBlock = doc.begin(); + QTextLayout *layout = firstBlock.layout(); + layout->setPreeditArea(doc.toPlainText().length(), "xxx"); + + doc.setPageSize(QSizeF(1000, 1000)); + QFontMetrics metrics(layout->font()); + QPointF blockStart = documentLayout->blockBoundingRect(firstBlock).topLeft(); + + QRect fooBr = metrics.boundingRect("foo"); + QPointF imagePoint(fooBr.width() + blockStart.x() + 25, blockStart.y() + 25); + // imageAt on image returns source + QCOMPARE(documentLayout->imageAt(imagePoint), QString("image")); + // anchorAt on image returns link + QCOMPARE(documentLayout->anchorAt(imagePoint), QString("link")); + + // imageAt on start returns nothing (there's the "foo" text) + QPointF fooPoint(fooBr.width() + blockStart.x(), (fooBr.height() / 2) + blockStart.y()); + QCOMPARE(documentLayout->imageAt(fooPoint), QString()); +} + +void tst_QAbstractTextDocumentLayout::formatAt() +{ + QTextDocument doc; + doc.setHtml("foo<i><a href=\"link\"><img src=\"image\" width=\"50\" height=\"50\"/></a></i>"); + QAbstractTextDocumentLayout *documentLayout = doc.documentLayout(); + QTextBlock firstBlock = doc.begin(); + QTextLayout *layout = firstBlock.layout(); + layout->setPreeditArea(doc.toPlainText().length(), "xxx"); + + doc.setPageSize(QSizeF(1000, 1000)); + QFontMetrics metrics(layout->font()); + QPointF blockStart = documentLayout->blockBoundingRect(firstBlock).topLeft(); + + QRect fooBr = metrics.boundingRect("foo"); + QPointF imagePoint(fooBr.width() + blockStart.x() + 25, blockStart.y() + 25); + + QTextFormat format = documentLayout->formatAt(imagePoint); + QVERIFY(format.isCharFormat()); + QVERIFY(format.toCharFormat().isAnchor()); + QVERIFY(format.toCharFormat().fontItalic()); + QVERIFY(format.isImageFormat()); + + // move over the unformatted "foo" text) + QPointF fooPoint(fooBr.width() + blockStart.x(), (fooBr.height() / 2) + blockStart.y()); + format = documentLayout->formatAt(fooPoint); + QVERIFY(format.isCharFormat()); + QVERIFY(!format.toCharFormat().isAnchor()); + QVERIFY(!format.toCharFormat().fontItalic()); + QVERIFY(!format.isImageFormat()); +} + QTEST_MAIN(tst_QAbstractTextDocumentLayout) #include "tst_qabstracttextdocumentlayout.moc" |