From c0167cdd8eb35e7cec5eec2b8896823b711b4605 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 9 Jun 2011 17:15:23 +0200 Subject: [PATCH] QmlInspector: Implemented selection for SceneGraph The highlight items are now based on QSGPaintedItem rather than the internal QSGRectangle. Change-Id: I50222116cf5f98ec93f914298a554b3d2d901c28 --- .../qmldbg_inspector/sgselectiontool.cpp | 36 ++++++++- .../qmltooling/qmldbg_inspector/sgselectiontool.h | 12 +-- .../qmldbg_inspector/sgviewinspector.cpp | 81 ++++++++++++++++---- .../qmltooling/qmldbg_inspector/sgviewinspector.h | 6 ++ 4 files changed, 107 insertions(+), 28 deletions(-) diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp index c233cf6..a6c459a 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp +++ b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp @@ -46,8 +46,8 @@ #include #include #include +#include #include -#include namespace QmlJSDebugger { @@ -85,12 +85,28 @@ static QSGItem *itemAt(QSGItem *item, const QPointF &pos, QSGItem *overlay) } +class SGHoverHighlight : public QSGPaintedItem +{ +public: + SGHoverHighlight(QSGItem *parent) : QSGPaintedItem(parent) + { + setZ(1); // hover highlight on top of selection indicator + } + + void paint(QPainter *painter) + { + painter->setPen(QPen(QColor(0, 22, 159))); + painter->drawRect(QRect(1, 1, width() - 3, height() - 3)); + painter->setPen(QColor(158, 199, 255)); + painter->drawRect(QRect(0, 0, width() - 1, height() - 1)); + } +}; + + SGSelectionTool::SGSelectionTool(SGViewInspector *inspector) : AbstractTool(inspector), - m_hoverHighlight(new QSGRectangle(inspector->overlay())) + m_hoverHighlight(new SGHoverHighlight(inspector->overlay())) { - m_hoverHighlight->border()->setColor(QColor(64, 128, 255)); - m_hoverHighlight->setColor(Qt::transparent); } void SGSelectionTool::leaveEvent(QEvent *) @@ -98,6 +114,16 @@ void SGSelectionTool::leaveEvent(QEvent *) m_hoverHighlight->setVisible(false); } +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) + sgInspector->setSelectedItems(QList() << item); +} + void SGSelectionTool::hoverMoveEvent(QMouseEvent *event) { SGViewInspector *sgInspector = static_cast(inspector()); @@ -109,7 +135,7 @@ void SGSelectionTool::hoverMoveEvent(QMouseEvent *event) return; } - m_hoverHighlight->setSize(QSizeF(item->width() - 1, item->height() - 1)); + 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/sgselectiontool.h b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.h index f794691..e76bd20 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.h +++ b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.h @@ -46,11 +46,10 @@ #include -QT_FORWARD_DECLARE_CLASS(QSGRectangle) - namespace QmlJSDebugger { class SGViewInspector; +class SGHoverHighlight; class SGSelectionTool : public AbstractTool { @@ -60,7 +59,7 @@ public: void leaveEvent(QEvent *); - void mousePressEvent(QMouseEvent *) {} + void mousePressEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *) {} void mouseReleaseEvent(QMouseEvent *) {} void mouseDoubleClickEvent(QMouseEvent *) {} @@ -71,13 +70,8 @@ public: void keyPressEvent(QKeyEvent *) {} void keyReleaseEvent(QKeyEvent *) {} -signals: - -public slots: - private: - QList m_highlightItems; - QSGRectangle *m_hoverHighlight; + SGHoverHighlight *m_hoverHighlight; }; } // namespace QmlJSDebugger diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp index ec68ef9..4d79ac6 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp +++ b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp @@ -49,12 +49,27 @@ #include #include +#include #include #include namespace QmlJSDebugger { +class SGSelectionHighlight : public QSGPaintedItem +{ +public: + SGSelectionHighlight(QSGItem *parent) : QSGPaintedItem(parent) + { } + + void paint(QPainter *painter) + { + painter->setPen(QColor(108, 141, 221)); + painter->drawRect(QRect(0, 0, width() - 1, height() - 1)); + } +}; + + SGViewInspector::SGViewInspector(QSGView *view, QObject *parent) : AbstractViewInspector(parent), m_view(view), @@ -82,7 +97,7 @@ void SGViewInspector::changeCurrentObjects(const QList &objects) if (QSGItem *item = qobject_cast(obj)) items << item; - setSelectedItems(items); + syncSelectedItems(items); } void SGViewInspector::reloadView() @@ -147,32 +162,70 @@ QList SGViewInspector::selectedItems() const void SGViewInspector::setSelectedItems(const QList &items) { + if (!syncSelectedItems(items)) + return; + + QList objectList; + foreach (QSGItem *item, items) + objectList << item; + + sendCurrentObjects(objectList); +} + +bool SGViewInspector::syncSelectedItems(const QList &items) +{ + bool selectionChanged = false; + // Disconnect and remove items that are no longer selected foreach (const QWeakPointer &item, m_selectedItems) { - if (!item) + if (!item) // Don't see how this can happen due to handling of destroyed() + continue; + if (items.contains(item.data())) continue; - if (!items.contains(item.data())) { - QObject::disconnect(item.data(), SIGNAL(destroyed(QObject*)), - this, SLOT(removeFromSelection(QObject*))); - m_selectedItems.removeOne(item); - } + selectionChanged = true; + item.data()->disconnect(this); + m_selectedItems.removeOne(item); + delete m_highlightItems.take(item.data()); } // Connect and add newly selected items foreach (QSGItem *item, items) { - if (!m_selectedItems.contains(item)) { - QObject::connect(item, SIGNAL(destroyed(QObject*)), - this, SLOT(removeFromSelection(QObject*))); - m_selectedItems.append(item); - } + if (m_selectedItems.contains(item)) + continue; + + selectionChanged = true; + connect(item, SIGNAL(destroyed(QObject*)), this, SLOT(removeFromSelectedItems(QObject*))); + connect(item, SIGNAL(xChanged()), this, SLOT(adjustSelectionHighlight())); + connect(item, SIGNAL(yChanged()), this, SLOT(adjustSelectionHighlight())); + connect(item, SIGNAL(widthChanged()), this, SLOT(adjustSelectionHighlight())); + connect(item, SIGNAL(heightChanged()), this, SLOT(adjustSelectionHighlight())); + connect(item, SIGNAL(rotationChanged()), this, SLOT(adjustSelectionHighlight())); + m_selectedItems.append(item); + m_highlightItems.insert(item, new SGSelectionHighlight(m_overlay)); + adjustSelectionHighlight(item); } + + return selectionChanged; } void SGViewInspector::removeFromSelectedItems(QObject *object) { - if (QSGItem *item = qobject_cast(object)) - m_selectedItems.removeOne(item); + if (QSGItem *item = qobject_cast(object)) { + if (m_selectedItems.removeOne(item)) + delete m_highlightItems.take(item); + } +} + +void SGViewInspector::adjustSelectionHighlight(QSGItem *item) +{ + if (!item) + item = static_cast(sender()); + + SGSelectionHighlight *highlight = m_highlightItems.value(item); + + highlight->setSize(QSizeF(item->width(), item->height())); + highlight->setPos(m_overlay->mapFromItem(item->parentItem(), item->pos())); } bool SGViewInspector::eventFilter(QObject *obj, QEvent *event) diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h index 2a7cb6c..a54036d 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h +++ b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h @@ -45,6 +45,7 @@ #include "abstractviewinspector.h" #include +#include QT_BEGIN_NAMESPACE class QSGView; @@ -54,6 +55,7 @@ QT_END_NAMESPACE namespace QmlJSDebugger { class SGSelectionTool; +class SGSelectionHighlight; class SGViewInspector : public AbstractViewInspector { @@ -79,14 +81,18 @@ public: private slots: void removeFromSelectedItems(QObject *); + void adjustSelectionHighlight(QSGItem *item = 0); private: + bool syncSelectedItems(const QList &items); + QSGView *m_view; QSGItem *m_overlay; SGSelectionTool *m_selectionTool; QList > m_selectedItems; + QHash m_highlightItems; bool m_designMode; }; -- 1.7.2.5