From f5e5eaf9effacc7b29e2c982452cfe8b765955d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 9 Jun 2011 18:15:34 +0200 Subject: [PATCH] QmlInspector: Implement tool tip handling for SceneGraph Change-Id: I84fc21f3e5bd96290910def26cd59e7e35719149 --- .../qmldbg_inspector/sgselectiontool.cpp | 48 ++----------- .../qmldbg_inspector/sgviewinspector.cpp | 74 ++++++++++++++++++++ .../qmltooling/qmldbg_inspector/sgviewinspector.h | 7 ++ 3 files changed, 86 insertions(+), 43 deletions(-) diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp index a6c459a..2d911a8 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp +++ b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp @@ -47,44 +47,9 @@ #include #include #include -#include 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 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(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() << item); } void SGSelectionTool::hoverMoveEvent(QMouseEvent *event) { SGViewInspector *sgInspector = static_cast(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); diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp index 4d79ac6..fd9b29b 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp +++ b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp @@ -46,6 +46,7 @@ #include #include +#include #include #include @@ -56,6 +57,40 @@ 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 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 SGViewInspector::selectedItems() const { QList 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 diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h index a54036d..8cde72d 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h +++ b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h @@ -74,11 +74,16 @@ public: QSGView *view() const { return m_view; } QSGItem *overlay() const { return m_overlay; } + QSGItem *topVisibleItemAt(const QPointF &pos); + QList selectedItems() const; void setSelectedItems(const QList &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 &items); + QString titleForItem(QSGItem *item) const; + QSGView *m_view; QSGItem *m_overlay; -- 1.7.2.5