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>
bb10
Alberto Mardegan 2015-07-26 19:43:26 +03:00 committed by Konstantin Ritt
parent 92f9a7780e
commit fb6000a74f
3 changed files with 86 additions and 3 deletions

View File

@ -601,10 +601,33 @@ QTextDocument *QAbstractTextDocumentLayout::document() const
string if no anchor exists at that point.
*/
QString QAbstractTextDocumentLayout::anchorAt(const QPointF& pos) const
{
QTextCharFormat fmt = formatAt(pos).toCharFormat();
return fmt.anchorHref();
}
/*!
\since 5.8
Returns the source of the image at the given position \a pos, or an empty
string if no image exists at that point.
*/
QString QAbstractTextDocumentLayout::imageAt(const QPointF &pos) const
{
QTextImageFormat fmt = formatAt(pos).toImageFormat();
return fmt.name();
}
/*!
\since 5.8
Returns the text format at the given position \a pos.
*/
QTextFormat QAbstractTextDocumentLayout::formatAt(const QPointF &pos) const
{
int cursorPos = hitTest(pos, Qt::ExactHit);
if (cursorPos == -1)
return QString();
return QTextFormat();
// compensate for preedit in the hit text block
QTextBlock block = document()->firstBlock();
@ -623,8 +646,7 @@ QString QAbstractTextDocumentLayout::anchorAt(const QPointF& pos) const
QTextDocumentPrivate *pieceTable = qobject_cast<const QTextDocument *>(parent())->docHandle();
QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos);
QTextCharFormat fmt = pieceTable->formatCollection()->charFormat(it->format);
return fmt.anchorHref();
return pieceTable->formatCollection()->format(it->format);
}
/*!

View File

@ -82,7 +82,10 @@ public:
virtual void draw(QPainter *painter, const PaintContext &context) = 0;
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const = 0;
QString anchorAt(const QPointF& pos) const;
QString imageAt(const QPointF &pos) const;
QTextFormat formatAt(const QPointF &pos) const;
virtual int pageCount() const = 0;
virtual QSizeF documentSize() const = 0;

View File

@ -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"