Add support for onLinkActivated with Text.StyledText
authorYann Bodson <yann.bodson@nokia.com>
Tue, 27 Sep 2011 02:56:17 +0000 (12:56 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 28 Sep 2011 08:30:57 +0000 (10:30 +0200)
Change-Id: If7efa09e0e42970c6cb6ca8725713eb4a6f97ac8
Reviewed-by: Michael Brasser
Reviewed-on: http://codereview.qt-project.org/5665
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
src/declarative/items/qsgtext.cpp
src/declarative/items/qsgtext_p_p.h

index f936c3f..3cad9b3 100644 (file)
@@ -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);
index 40c9861..2e99997 100644 (file)
@@ -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);