QmlInspector: Implement tool tip handling for SceneGraph
authorThorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
Thu, 9 Jun 2011 16:15:34 +0000 (18:15 +0200)
committerThorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
Thu, 23 Jun 2011 14:26:11 +0000 (16:26 +0200)
Change-Id: I84fc21f3e5bd96290910def26cd59e7e35719149

src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp
src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp
src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h

index a6c459a..2d911a8 100644 (file)
 #include <QtDeclarative/QSGView>
 #include <QtDeclarative/QSGItem>
 #include <QtDeclarative/QSGPaintedItem>
-#include <QtDeclarative/private/qsgitem_p.h>
 
 namespace QmlJSDebugger {
 
-/*
- * Returns the first visible item at the given position, or 0 when no such
- * child exists.
- */
-static QSGItem *itemAt(QSGItem *item, const QPointF &pos, QSGItem *overlay)
-{
-    if (item == overlay)
-        return 0;
-
-    if (!item->isVisible() || item->opacity() == 0.0)
-        return 0;
-
-    if (item->flags() & QSGItem::ItemClipsChildrenToShape) {
-        if (!QRectF(0, 0, item->width(), item->height()).contains(pos))
-            return 0;
-    }
-
-    QList<QSGItem *> children = QSGItemPrivate::get(item)->paintOrderChildItems();
-    for (int i = children.count() - 1; i >= 0; --i) {
-        QSGItem *child = children.at(i);
-        if (QSGItem *betterCandidate = itemAt(child, item->mapToItem(child, pos), overlay))
-            return betterCandidate;
-    }
-
-    if (!(item->flags() & QSGItem::ItemHasContents))
-        return 0;
-
-    if (!QRectF(0, 0, item->width(), item->height()).contains(pos))
-        return 0;
-
-    return item;
-}
-
-
 class SGHoverHighlight : public QSGPaintedItem
 {
 public:
@@ -117,24 +82,21 @@ void SGSelectionTool::leaveEvent(QEvent *)
 void SGSelectionTool::mousePressEvent(QMouseEvent *event)
 {
     SGViewInspector *sgInspector = static_cast<SGViewInspector*>(inspector());
-    QSGItem *root = sgInspector->view()->rootItem();
-    QPointF mappedPos = root->mapFromScene(event->pos());
-    QSGItem *item = itemAt(root, mappedPos, sgInspector->overlay());
-    if (item && item != root)
+    QSGItem *item = sgInspector->topVisibleItemAt(event->pos());
+    if (item)
         sgInspector->setSelectedItems(QList<QSGItem*>() << item);
 }
 
 void SGSelectionTool::hoverMoveEvent(QMouseEvent *event)
 {
     SGViewInspector *sgInspector = static_cast<SGViewInspector*>(inspector());
-    QSGItem *root = sgInspector->view()->rootItem();
-    QPointF mappedPos = root->mapFromScene(event->pos());
-    QSGItem *item = itemAt(root, mappedPos, sgInspector->overlay());
-    if (!item || item == root) {
+    QSGItem *item = sgInspector->topVisibleItemAt(event->pos());
+    if (!item) {
         m_hoverHighlight->setVisible(false);
         return;
     }
 
+    QSGItem *root = sgInspector->view()->rootItem();
     m_hoverHighlight->setSize(QSizeF(item->width(), item->height()));
     m_hoverHighlight->setPos(root->mapFromItem(item->parentItem(), item->pos()));
     m_hoverHighlight->setVisible(true);
index 4d79ac6..fd9b29b 100644 (file)
@@ -46,6 +46,7 @@
 
 #include <QtDeclarative/private/qdeclarativeinspectorservice_p.h>
 #include <QtDeclarative/private/qdeclarativedebughelper_p.h>
+#include <QtDeclarative/private/qsgitem_p.h>
 
 #include <QtDeclarative/QSGView>
 #include <QtDeclarative/QSGItem>
 
 namespace QmlJSDebugger {
 
+/*
+ * Returns the first visible item at the given position, or 0 when no such
+ * child exists.
+ */
+static QSGItem *itemAt(QSGItem *item, const QPointF &pos, QSGItem *overlay)
+{
+    if (item == overlay)
+        return 0;
+
+    if (!item->isVisible() || item->opacity() == 0.0)
+        return 0;
+
+    if (item->flags() & QSGItem::ItemClipsChildrenToShape) {
+        if (!QRectF(0, 0, item->width(), item->height()).contains(pos))
+            return 0;
+    }
+
+    QList<QSGItem *> children = QSGItemPrivate::get(item)->paintOrderChildItems();
+    for (int i = children.count() - 1; i >= 0; --i) {
+        QSGItem *child = children.at(i);
+        if (QSGItem *betterCandidate = itemAt(child, item->mapToItem(child, pos), overlay))
+            return betterCandidate;
+    }
+
+    if (!(item->flags() & QSGItem::ItemHasContents))
+        return 0;
+
+    if (!QRectF(0, 0, item->width(), item->height()).contains(pos))
+        return 0;
+
+    return item;
+}
+
+
 class SGSelectionHighlight : public QSGPaintedItem
 {
 public:
@@ -150,6 +185,12 @@ QWidget *SGViewInspector::viewWidget() const
     return m_view;
 }
 
+QSGItem *SGViewInspector::topVisibleItemAt(const QPointF &pos)
+{
+    QSGItem *root = m_view->rootItem();
+    return itemAt(root, root->mapFromScene(pos), m_overlay);
+}
+
 QList<QSGItem*> SGViewInspector::selectedItems() const
 {
     QList<QSGItem *> selection;
@@ -236,4 +277,37 @@ bool SGViewInspector::eventFilter(QObject *obj, QEvent *event)
     return AbstractViewInspector::eventFilter(obj, event);
 }
 
+bool SGViewInspector::mouseMoveEvent(QMouseEvent *event)
+{
+    if (QSGItem *item = topVisibleItemAt(event->pos()))
+        m_view->setToolTip(titleForItem(item));
+    else
+        m_view->setToolTip(QString());
+
+    return AbstractViewInspector::mouseMoveEvent(event);
+}
+
+QString SGViewInspector::titleForItem(QSGItem *item) const
+{
+    QString className = QLatin1String(item->metaObject()->className());
+    QString objectStringId = idStringForObject(item);
+
+    className.remove(QRegExp(QLatin1String("_QMLTYPE_\\d+")));
+    className.remove(QRegExp(QLatin1String("_QML_\\d+")));
+    if (className.startsWith(QLatin1String("QSG")))
+        className = className.mid(3);
+
+    QString constructedName;
+
+    if (!objectStringId.isEmpty()) {
+        constructedName = objectStringId + QLatin1String(" (") + className + QLatin1Char(')');
+    } else if (!item->objectName().isEmpty()) {
+        constructedName = item->objectName() + QLatin1String(" (") + className + QLatin1Char(')');
+    } else {
+        constructedName = className;
+    }
+
+    return constructedName;
+}
+
 } // namespace QmlJSDebugger
index a54036d..8cde72d 100644 (file)
@@ -74,11 +74,16 @@ public:
     QSGView *view() const { return m_view; }
     QSGItem *overlay() const { return m_overlay; }
 
+    QSGItem *topVisibleItemAt(const QPointF &pos);
+
     QList<QSGItem *> selectedItems() const;
     void setSelectedItems(const QList<QSGItem*> &items);
 
+protected:
     bool eventFilter(QObject *obj, QEvent *event);
 
+    bool mouseMoveEvent(QMouseEvent *);
+
 private slots:
     void removeFromSelectedItems(QObject *);
     void adjustSelectionHighlight(QSGItem *item = 0);
@@ -86,6 +91,8 @@ private slots:
 private:
     bool syncSelectedItems(const QList<QSGItem*> &items);
 
+    QString titleForItem(QSGItem *item) const;
+
     QSGView *m_view;
     QSGItem *m_overlay;