From fb6000a74f57bd3c096f6a10142477bf2faf0ff2 Mon Sep 17 00:00:00 2001 From: Alberto Mardegan Date: Sun, 26 Jul 2015 19:43:26 +0300 Subject: 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 --- src/gui/text/qabstracttextdocumentlayout.cpp | 28 +++++++++++++++++++++++++--- src/gui/text/qabstracttextdocumentlayout.h | 3 +++ 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qabstracttextdocumentlayout.cpp b/src/gui/text/qabstracttextdocumentlayout.cpp index ff2497817e..2278378613 100644 --- a/src/gui/text/qabstracttextdocumentlayout.cpp +++ b/src/gui/text/qabstracttextdocumentlayout.cpp @@ -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(parent())->docHandle(); QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos); - QTextCharFormat fmt = pieceTable->formatCollection()->charFormat(it->format); - return fmt.anchorHref(); + return pieceTable->formatCollection()->format(it->format); } /*! diff --git a/src/gui/text/qabstracttextdocumentlayout.h b/src/gui/text/qabstracttextdocumentlayout.h index e83f4fc0c9..d363188742 100644 --- a/src/gui/text/qabstracttextdocumentlayout.h +++ b/src/gui/text/qabstracttextdocumentlayout.h @@ -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; -- cgit v1.2.3