From da8d91169282ed241e7e729ad8c6c472ebc59dc5 Mon Sep 17 00:00:00 2001
From: Jens Bache-Wiig <jensb.bache-wiig@nokia.com>
Date: Wed, 21 Sep 2011 16:06:47 +0200
Subject: [PATCH] Move tooltip functionality into TooltipArea

---
 components/Button.qml          | 13 +++++------
 src/styleitem/qstyleitem.cpp   | 17 --------------
 src/styleitem/qstyleitem.h     |  1 -
 src/styleitem/qstyleplugin.cpp |  2 ++
 src/styleitem/qtooltiparea.cpp | 41 ++++++++++++++++++++++++++++++++++
 src/styleitem/qtooltiparea.h   | 32 ++++++++++++++++++++++++++
 src/styleitem/styleitem.pro    |  8 +++++--
 7 files changed, 86 insertions(+), 28 deletions(-)
 create mode 100644 src/styleitem/qtooltiparea.cpp
 create mode 100644 src/styleitem/qtooltiparea.h

diff --git a/components/Button.qml b/components/Button.qml
index 1a978be45..885a9f0e8 100644
--- a/components/Button.qml
+++ b/components/Button.qml
@@ -2,7 +2,7 @@ import QtQuick 1.0
 import "custom" as Components
 
 Components.Button {
-    id:button
+    id: button
 
     width: Math.max(80, sizehint.width)
     height: Math.max(22, sizehint.height)
@@ -11,6 +11,10 @@ Components.Button {
     property bool defaultbutton
     property string hint
 
+    TooltipArea {
+        anchors.fill: parent
+        text: button.tooltip
+    }
 
     background: StyleItem {
         id: styleitem
@@ -25,13 +29,6 @@ Components.Button {
 
         // If no icon, let the style do the drawing
         activeControl: focus ? "default" : ""
-        Connections{
-            target: button
-            onToolTipTriggered: styleitem.showTip()
-        }
-        function showTip(){
-            showToolTip(tooltip);
-        }
     }
 
     label: Item {
diff --git a/src/styleitem/qstyleitem.cpp b/src/styleitem/qstyleitem.cpp
index 0fa139889..1bdb9f774 100644
--- a/src/styleitem/qstyleitem.cpp
+++ b/src/styleitem/qstyleitem.cpp
@@ -831,23 +831,6 @@ bool QStyleItem::eventFilter(QObject *o, QEvent *e) {
     return QObject::eventFilter(o, e);
 }
 
-void QStyleItem::showToolTip(const QString &str)
-{
-    QPoint global;
-    QPointF scenePos = mapToScene(width() - 20, 0);
-    QGraphicsScene *scene = QGraphicsItem::scene();
-    QObject *parent = scene->parent();
-    if (parent) {
-        QGraphicsView *view = qobject_cast<QGraphicsView*>(parent);
-        if (view) {
-            QPoint p = view->mapFromScene(scenePos);
-            global = view->mapToGlobal(p);
-        }
-    }
-
-    QToolTip::showText(QPoint(global.x(),global.y()), str);
-}
-
 QRect QStyleItem::subControlRect(const QString &subcontrolString)
 {
     QStyle::SubControl subcontrol = QStyle::SC_None;
diff --git a/src/styleitem/qstyleitem.h b/src/styleitem/qstyleitem.h
index 105f90fe3..6f6daf270 100644
--- a/src/styleitem/qstyleitem.h
+++ b/src/styleitem/qstyleitem.h
@@ -174,7 +174,6 @@ public Q_SLOTS:
     void updateItem(){update();}
     QString hitTest(int x, int y);
     QRect subControlRect(const QString &subcontrolString);
-    void showToolTip(const QString &str);
     int textWidth(const QString &);
 
 Q_SIGNALS:
diff --git a/src/styleitem/qstyleplugin.cpp b/src/styleitem/qstyleplugin.cpp
index b1cd386d0..ef7c630f2 100644
--- a/src/styleitem/qstyleplugin.cpp
+++ b/src/styleitem/qstyleplugin.cpp
@@ -47,6 +47,7 @@
 #include "qdesktopitem.h"
 #include "qwheelarea.h"
 #include "qcursorarea.h"
+#include "qtooltiparea.h"
 #include <qdeclarativeextensionplugin.h>
 
 #include <qdeclarativeengine.h>
@@ -82,6 +83,7 @@ void StylePlugin::registerTypes(const char *uri)
 {
     qmlRegisterType<QStyleItem>(uri, 0, 1, "StyleItem");
     qmlRegisterType<QCursorArea>(uri, 0, 1, "CursorArea");
+    qmlRegisterType<QTooltipArea>(uri, 0, 1, "TooltipArea");
     qmlRegisterType<QRangeModel>(uri, 0, 1, "RangeModel");
     qmlRegisterType<QGraphicsDropShadowEffect>(uri, 0, 1, "DropShadow");
     qmlRegisterType<QDeclarativeFolderListModel>(uri, 0, 1, "FileSystemModel");
diff --git a/src/styleitem/qtooltiparea.cpp b/src/styleitem/qtooltiparea.cpp
new file mode 100644
index 000000000..2a1cb3e2a
--- /dev/null
+++ b/src/styleitem/qtooltiparea.cpp
@@ -0,0 +1,41 @@
+#include "qtooltiparea.h"
+#include <QGraphicsView>
+#include <QToolTip>
+#include <QApplication>
+
+QTooltipArea::QTooltipArea(QDeclarativeItem *parent) :
+    QDeclarativeItem(parent)
+{
+    setAcceptHoverEvents(true);
+    connect(&m_tiptimer, SIGNAL(timeout()), this, SLOT(timeout()));
+}
+
+void QTooltipArea::setText(const QString &t)
+{
+    if (t != m_text) {
+        m_text = t;
+        emit textChanged();
+    }
+}
+
+void QTooltipArea::showToolTip(const QString &str) const
+{
+    QToolTip::showText(cursor().pos(), str);
+}
+
+void QTooltipArea::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
+{
+    m_tiptimer.start(1000);
+    return QDeclarativeItem::hoverEnterEvent(event);
+}
+
+void QTooltipArea::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
+{
+    m_tiptimer.stop();
+    return QDeclarativeItem::hoverLeaveEvent(event);
+}
+
+void QTooltipArea::timeout()
+{
+    showToolTip(m_text);
+}
diff --git a/src/styleitem/qtooltiparea.h b/src/styleitem/qtooltiparea.h
new file mode 100644
index 000000000..9a2c68e6d
--- /dev/null
+++ b/src/styleitem/qtooltiparea.h
@@ -0,0 +1,32 @@
+#ifndef QTOOLTIPAREA_H
+#define QTOOLTIPAREA_H
+
+#include <QDeclarativeItem>
+#include <QTimer>
+
+class QTooltipArea : public QDeclarativeItem
+{
+    Q_OBJECT
+    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
+
+public:
+    QTooltipArea(QDeclarativeItem *parent = 0);
+    void setText(const QString &t);
+    QString text() const {return m_text;}
+    void showToolTip(const QString &str) const;
+    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
+    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
+
+public slots:
+    void timeout();
+
+signals:
+    void textChanged();
+
+private:
+
+    QTimer m_tiptimer;
+    QString m_text;
+};
+
+#endif // QTOOLTIPAREA_H
diff --git a/src/styleitem/styleitem.pro b/src/styleitem/styleitem.pro
index 68c6d57a4..21aba3fa4 100644
--- a/src/styleitem/styleitem.pro
+++ b/src/styleitem/styleitem.pro
@@ -21,7 +21,8 @@ HEADERS += qtmenu.h \
            qwindowitem.h \
            qdesktopitem.h \
            qtoplevelwindow.h \
-           qcursorarea.h
+           qcursorarea.h \
+           qtooltiparea.h
 
 SOURCES += qtmenu.cpp \
            qtmenubar.cpp \
@@ -34,7 +35,8 @@ SOURCES += qtmenu.cpp \
            qwindowitem.cpp \
            qdesktopitem.cpp \
            qtoplevelwindow.cpp \
-           qcursorarea.cpp
+           qcursorarea.cpp \
+           qtooltiparea.cpp
 
 TARGETPATH = QtDesktop/plugin
 
@@ -57,3 +59,5 @@ symbian {
 
 
 
+
+
-- 
GitLab