From 4196ff229b3385e5097699c159e9226bd86a95a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Tue, 21 Jun 2011 10:58:37 +0200 Subject: [PATCH] QmlInspector: Introduced more self-contained selection highlight The SGHighlight will update itself when its item changes position or size. Previously, the hover highlight wasn't updating at all. The highlight now also works correctly for rotated items (but not yet for children of rotated items). Change-Id: Idb14f356d779aef8e2d3e16a496685f0507a7060 --- .../qmldbg_inspector/qmldbg_inspector.pro | 6 +- .../qmltooling/qmldbg_inspector/sghighlight.cpp | 98 ++++++++++++++++++++++ .../qmltooling/qmldbg_inspector/sghighlight.h | 97 +++++++++++++++++++++ .../qmldbg_inspector/sgselectiontool.cpp | 33 ++------ .../qmldbg_inspector/sgviewinspector.cpp | 35 +------- .../qmltooling/qmldbg_inspector/sgviewinspector.h | 1 - 6 files changed, 206 insertions(+), 64 deletions(-) create mode 100644 src/plugins/qmltooling/qmldbg_inspector/sghighlight.cpp create mode 100644 src/plugins/qmltooling/qmldbg_inspector/sghighlight.h diff --git a/src/plugins/qmltooling/qmldbg_inspector/qmldbg_inspector.pro b/src/plugins/qmltooling/qmldbg_inspector/qmldbg_inspector.pro index 355395c..427a177 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/qmldbg_inspector.pro +++ b/src/plugins/qmltooling/qmldbg_inspector/qmldbg_inspector.pro @@ -27,7 +27,8 @@ SOURCES += \ editor/toolbarcolorbox.cpp \ abstracttool.cpp \ sgviewinspector.cpp \ - sgselectiontool.cpp + sgselectiontool.cpp \ + sghighlight.cpp HEADERS += \ abstractviewinspector.h \ @@ -51,7 +52,8 @@ HEADERS += \ editor/toolbarcolorbox.h \ abstracttool.h \ sgviewinspector.h \ - sgselectiontool.h + sgselectiontool.h \ + sghighlight.h RESOURCES += editor/editor.qrc diff --git a/src/plugins/qmltooling/qmldbg_inspector/sghighlight.cpp b/src/plugins/qmltooling/qmldbg_inspector/sghighlight.cpp new file mode 100644 index 0000000..4488214 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_inspector/sghighlight.cpp @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "sghighlight.h" + +#include + +namespace QmlJSDebugger { + +SGHighlight::SGHighlight(QSGItem *item, QSGItem *parent) + : QSGPaintedItem(parent) +{ + setItem(item); +} + +void SGHighlight::setItem(QSGItem *item) +{ + if (m_item) + m_item.data()->disconnect(this); + + if (item) { + connect(item, SIGNAL(xChanged()), SLOT(adjust())); + connect(item, SIGNAL(yChanged()), SLOT(adjust())); + connect(item, SIGNAL(widthChanged()), SLOT(adjust())); + connect(item, SIGNAL(heightChanged()), SLOT(adjust())); + connect(item, SIGNAL(rotationChanged()), SLOT(adjust())); + connect(item, SIGNAL(transformOriginChanged(TransformOrigin)), + SLOT(adjust())); + } + + m_item = item; + adjust(); +} + +void SGHighlight::adjust() +{ + const QSGItem *item = m_item.data(); + setSize(QSizeF(item->width(), item->height())); + setPos(parentItem()->mapFromItem(item->parentItem(), item->pos())); + setRotation(item->rotation()); + setTransformOrigin(item->transformOrigin()); +} + + +void SGSelectionHighlight::paint(QPainter *painter) +{ + painter->setPen(QColor(108, 141, 221)); + painter->drawRect(QRect(0, 0, width() - 1, height() - 1)); +} + + +void SGHoverHighlight::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)); +} + +} // namespace QmlJSDebugger diff --git a/src/plugins/qmltooling/qmldbg_inspector/sghighlight.h b/src/plugins/qmltooling/qmldbg_inspector/sghighlight.h new file mode 100644 index 0000000..f3e8edd --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_inspector/sghighlight.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SGHIGHLIGHT_H +#define SGHIGHLIGHT_H + +#include +#include + +namespace QmlJSDebugger { + +class SGHighlight : public QSGPaintedItem +{ + Q_OBJECT + +public: + SGHighlight(QSGItem *parent) : QSGPaintedItem(parent) {} + SGHighlight(QSGItem *item, QSGItem *parent); + + void setItem(QSGItem *item); + +private slots: + void adjust(); + +private: + QWeakPointer m_item; +}; + +/** + * A highlight suitable for indicating selection. + */ +class SGSelectionHighlight : public SGHighlight +{ +public: + SGSelectionHighlight(QSGItem *item, QSGItem *parent) + : SGHighlight(item, parent) + {} + + void paint(QPainter *painter); +}; + +/** + * A highlight suitable for indicating hover. + */ +class SGHoverHighlight : public SGHighlight +{ +public: + SGHoverHighlight(QSGItem *parent) + : SGHighlight(parent) + { + setZ(1); // hover highlight on top of selection highlight + } + + void paint(QPainter *painter); +}; + +} // namespace QmlJSDebugger + +#endif // SGHIGHLIGHT_H diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp index 7260933..36e2818 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp +++ b/src/plugins/qmltooling/qmldbg_inspector/sgselectiontool.cpp @@ -41,41 +41,16 @@ #include "sgselectiontool.h" +#include "sghighlight.h" #include "sgviewinspector.h" #include #include #include #include -#include namespace QmlJSDebugger { -class SGHoverHighlight : public QSGPaintedItem -{ -public: - SGHoverHighlight(QSGItem *parent) : QSGPaintedItem(parent) - { - setZ(1); // hover highlight on top of selection indicator - } - - void setItem(QSGItem *item) - { - setSize(QSizeF(item->width(), item->height())); - setPos(parentItem()->mapFromItem(item, QPointF())); - setVisible(true); - } - - 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 SGHoverHighlight(inspector->overlay())) @@ -101,10 +76,12 @@ void SGSelectionTool::mousePressEvent(QMouseEvent *event) void SGSelectionTool::hoverMoveEvent(QMouseEvent *event) { QSGItem *item = inspector()->topVisibleItemAt(event->pos()); - if (!item) + if (!item) { m_hoverHighlight->setVisible(false); - else + } else { m_hoverHighlight->setItem(item); + m_hoverHighlight->setVisible(true); + } } void SGSelectionTool::createContextMenu(const QList &items, QPoint pos) diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp index b1d1918..bb9ad52 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp +++ b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.cpp @@ -42,6 +42,7 @@ #include "sgviewinspector.h" #include "qdeclarativeinspectorprotocol.h" +#include "sghighlight.h" #include "sgselectiontool.h" #include @@ -50,7 +51,6 @@ #include #include -#include #include #include @@ -117,20 +117,6 @@ static QSGItem *itemAt(QSGItem *item, const QPointF &pos, QSGItem *overlay) } -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), @@ -271,14 +257,8 @@ bool SGViewInspector::syncSelectedItems(const QList &items) 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); + m_highlightItems.insert(item, new SGSelectionHighlight(item, m_overlay)); } return selectionChanged; @@ -292,17 +272,6 @@ void SGViewInspector::removeFromSelectedItems(QObject *object) } } -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) { if (obj != m_view) diff --git a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h index d1f6499..14a5bb1 100644 --- a/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h +++ b/src/plugins/qmltooling/qmldbg_inspector/sgviewinspector.h @@ -89,7 +89,6 @@ protected: private slots: void removeFromSelectedItems(QObject *); - void adjustSelectionHighlight(QSGItem *item = 0); private: bool syncSelectedItems(const QList &items); -- 2.7.4