From: Yann Bodson Date: Tue, 27 Sep 2011 02:56:17 +0000 (+1000) Subject: Add support for onLinkActivated with Text.StyledText X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=8469bc9cf1a4ef4ff6f51775569002135f739686;p=konrad%2Fqtdeclarative.git Add support for onLinkActivated with Text.StyledText Change-Id: If7efa09e0e42970c6cb6ca8725713eb4a6f97ac8 Reviewed-by: Michael Brasser Reviewed-on: http://codereview.qt-project.org/5665 Reviewed-by: Qt Sanity Bot Reviewed-by: Yann Bodson --- diff --git a/src/declarative/items/qsgtext.cpp b/src/declarative/items/qsgtext.cpp index f936c3f..3cad9b3 100644 --- a/src/declarative/items/qsgtext.cpp +++ b/src/declarative/items/qsgtext.cpp @@ -1683,18 +1683,49 @@ void QSGText::componentComplete() } } + +QString QSGTextPrivate::anchorAt(const QPointF &mousePos) +{ + if (format == QSGText::StyledText) { + for (int i = 0; i < layout.lineCount(); ++i) { + QTextLine line = layout.lineAt(i); + if (line.naturalTextRect().contains(mousePos)) { + int charPos = line.xToCursor(mousePos.x()); + foreach (const QTextLayout::FormatRange &formatRange, layout.additionalFormats()) { + if (formatRange.format.isAnchor() + && charPos >= formatRange.start + && charPos <= formatRange.start + formatRange.length) { + return formatRange.format.anchorHref(); + } + } + break; + } + } + } + return QString(); +} + +bool QSGTextPrivate::isLinkActivatedConnected() +{ + static int idx = this->signalIndex("linkActivated(QString)"); + return this->isSignalConnected(idx); +} + /*! \internal */ void QSGText::mousePressEvent(QMouseEvent *event) { Q_D(QSGText); - if (!d->richText || !d->doc || d->doc->documentLayout()->anchorAt(event->localPos()).isEmpty()) { - event->setAccepted(false); - d->activeLink.clear(); - } else { - d->activeLink = d->doc->documentLayout()->anchorAt(event->localPos()); + if (d->isLinkActivatedConnected()) { + if (d->format == QSGText::StyledText) + d->activeLink = d->anchorAt(event->localPos()); + else if (d->richText && d->doc) + d->activeLink = d->doc->documentLayout()->anchorAt(event->localPos()); } + if (d->activeLink.isEmpty()) + event->setAccepted(false); + // ### may malfunction if two of the same links are clicked & dragged onto each other) if (!event->isAccepted()) @@ -1707,8 +1738,17 @@ void QSGText::mouseReleaseEvent(QMouseEvent *event) { Q_D(QSGText); - // ### confirm the link, and send a signal out - if (d->richText && d->doc && d->activeLink == d->doc->documentLayout()->anchorAt(event->localPos())) + // ### confirm the link, and send a signal out + + QString link; + if (d->isLinkActivatedConnected()) { + if (d->format == QSGText::StyledText) + link = d->anchorAt(event->localPos()); + else if (d->richText && d->doc) + link = d->doc->documentLayout()->anchorAt(event->localPos()); + } + + if (!link.isEmpty() && d->activeLink == link) emit linkActivated(d->activeLink); else event->setAccepted(false); diff --git a/src/declarative/items/qsgtext_p_p.h b/src/declarative/items/qsgtext_p_p.h index 40c9861..2e99997 100644 --- a/src/declarative/items/qsgtext_p_p.h +++ b/src/declarative/items/qsgtext_p_p.h @@ -133,6 +133,8 @@ public: QRect setupTextLayout(); QPixmap textLayoutImage(bool drawStyle); void drawTextLayout(QPainter *p, const QPointF &pos, bool drawStyle); + bool isLinkActivatedConnected(); + QString anchorAt(const QPointF &pos); QTextLayout layout; static QPixmap drawOutline(const QPixmap &source, const QPixmap &styleSource);