aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYann Bodson <yann.bodson@nokia.com>2011-09-27 12:56:17 +1000
committerQt by Nokia <qt-info@nokia.com>2011-09-28 10:30:57 +0200
commit8469bc9cf1a4ef4ff6f51775569002135f739686 (patch)
tree88e126b4b5a9f183a9a0c87af46f383bb26ba7e3 /src
parent8dd9eddb55663b83ca07bcedecf5ad2c93a14e52 (diff)
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 <qt_sanity_bot@ovi.com> Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/declarative/items/qsgtext.cpp54
-rw-r--r--src/declarative/items/qsgtext_p_p.h2
2 files changed, 49 insertions, 7 deletions
diff --git a/src/declarative/items/qsgtext.cpp b/src/declarative/items/qsgtext.cpp
index f936c3f32b..3cad9b3bac 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 40c986142c..2e999972b4 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);