diff options
author | Kai Koehne <kai.koehne@nokia.com> | 2011-10-26 15:52:48 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-01 17:03:29 +0100 |
commit | 1695f0622b4673c49ccb6b127f1a7d9b24611d80 (patch) | |
tree | 011ef84395cc5440fc537ae9b21cb6891b55db45 /src/plugins/qmltooling/qmldbg_qtquick2 | |
parent | 3c8ea4c9151045d95dd0b0df72f6f680ce57c1fc (diff) |
Debugger: Split inspector plugin into a qtquick1 and a qtquick2 plugin
This allows the inspector to be used also when e.g. qtquick1 and widgets libraries
are not available.
Change-Id: Id8510ea2a1a9c2a776d67e6d7732a4e40363d5a3
Reviewed-by: Aurindam Jana <aurindam.jana@nokia.com>
Diffstat (limited to 'src/plugins/qmltooling/qmldbg_qtquick2')
9 files changed, 1069 insertions, 0 deletions
diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/qmldbg_qtquick2.pro b/src/plugins/qmltooling/qmldbg_qtquick2/qmldbg_qtquick2.pro new file mode 100644 index 0000000000..a06c1bdc10 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/qmldbg_qtquick2.pro @@ -0,0 +1,31 @@ +load(qt_module) + +TARGET = qmldbg_qtquick2 +QT += declarative-private core-private gui-private opengl-private v8-private + +load(qt_plugin) + +DESTDIR = $$QT.declarative.plugins/qmltooling + +INCLUDEPATH *= $$PWD $$PWD/../shared + +SOURCES += \ + qtquick2plugin.cpp \ + sghighlight.cpp \ + sgselectiontool.cpp \ + sgviewinspector.cpp \ + ../shared/abstracttool.cpp \ + ../shared/abstractviewinspector.cpp + +HEADERS += \ + qtquick2plugin.h \ + sghighlight.h \ + sgselectiontool.h \ + sgviewinspector.h \ + ../shared/abstracttool.h \ + ../shared/abstractviewinspector.h \ + ../shared/qdeclarativeinspectorprotocol.h \ + ../shared/qmlinspectorconstants.h + +target.path += $$[QT_INSTALL_PLUGINS]/qmltooling +INSTALLS += target diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/qtquick2plugin.cpp b/src/plugins/qmltooling/qmldbg_qtquick2/qtquick2plugin.cpp new file mode 100644 index 0000000000..5ed919c87c --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/qtquick2plugin.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** 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 "qtquick2plugin.h" +#include "sgviewinspector.h" + +#include <QtCore/qplugin.h> +#include <QtDeclarative/private/qdeclarativeinspectorservice_p.h> +#include <QtDeclarative/QQuickView> + +namespace QmlJSDebugger { +namespace QtQuick2 { + +QtQuick2Plugin::QtQuick2Plugin() : + m_inspector(0) +{ +} + +QtQuick2Plugin::~QtQuick2Plugin() +{ + delete m_inspector; +} + +bool QtQuick2Plugin::canHandleView(QObject *view) +{ + return qobject_cast<QQuickView*>(view); +} + +void QtQuick2Plugin::activate(QObject *view) +{ + QQuickView *qtQuickView = qobject_cast<QQuickView*>(view); + Q_ASSERT(qtQuickView); + m_inspector = new SGViewInspector(qtQuickView, qtQuickView); +} + +void QtQuick2Plugin::deactivate() +{ + delete m_inspector; +} + +void QtQuick2Plugin::clientMessage(const QByteArray &message) +{ + if (m_inspector) + m_inspector->handleMessage(message); +} + +} // namespace QtQuick2 +} // namespace QmlJSDebugger + +Q_EXPORT_PLUGIN2(qmldbg_qtquick2, QmlJSDebugger::QtQuick2::QtQuick2Plugin) diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/qtquick2plugin.h b/src/plugins/qmltooling/qmldbg_qtquick2/qtquick2plugin.h new file mode 100644 index 0000000000..89edcad6b7 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/qtquick2plugin.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** 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 QDECLARATIVEINSPECTORPLUGIN_H +#define QDECLARATIVEINSPECTORPLUGIN_H + +#include <QtCore/QPointer> +#include <QtDeclarative/private/qdeclarativeinspectorinterface_p.h> + +namespace QmlJSDebugger { + +class AbstractViewInspector; + +namespace QtQuick2 { + +class QtQuick2Plugin : public QObject, public QDeclarativeInspectorInterface +{ + Q_OBJECT + Q_DISABLE_COPY(QtQuick2Plugin) + Q_INTERFACES(QDeclarativeInspectorInterface) + +public: + QtQuick2Plugin(); + ~QtQuick2Plugin(); + + // QDeclarativeInspectorInterface + bool canHandleView(QObject *view); + void activate(QObject *view); + void deactivate(); + void clientMessage(const QByteArray &message); + +private: + QPointer<AbstractViewInspector> m_inspector; +}; + +} // namespace QtQuick2 +} // namespace QmlJSDebugger + +#endif // QDECLARATIVEINSPECTORPLUGIN_H diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/sghighlight.cpp b/src/plugins/qmltooling/qmldbg_qtquick2/sghighlight.cpp new file mode 100644 index 0000000000..ab254a3fdd --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/sghighlight.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** 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 <QtGui/QPainter> + +namespace QmlJSDebugger { +namespace QtQuick2 { + +SGHighlight::SGHighlight(QQuickItem *item, QQuickItem *parent) + : QQuickPaintedItem(parent) +{ + setItem(item); +} + +void SGHighlight::setItem(QQuickItem *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 QQuickItem *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 QtQuick2 +} // namespace QmlJSDebugger diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/sghighlight.h b/src/plugins/qmltooling/qmldbg_qtquick2/sghighlight.h new file mode 100644 index 0000000000..8ba1af6576 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/sghighlight.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** 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 <QtCore/QWeakPointer> +#include <QtDeclarative/QQuickPaintedItem> + +namespace QmlJSDebugger { +namespace QtQuick2 { + +class SGHighlight : public QQuickPaintedItem +{ + Q_OBJECT + +public: + SGHighlight(QQuickItem *parent) : QQuickPaintedItem(parent) {} + SGHighlight(QQuickItem *item, QQuickItem *parent); + + void setItem(QQuickItem *item); + +private slots: + void adjust(); + +private: + QWeakPointer<QQuickItem> m_item; +}; + +/** + * A highlight suitable for indicating selection. + */ +class SGSelectionHighlight : public SGHighlight +{ +public: + SGSelectionHighlight(QQuickItem *item, QQuickItem *parent) + : SGHighlight(item, parent) + {} + + void paint(QPainter *painter); +}; + +/** + * A highlight suitable for indicating hover. + */ +class SGHoverHighlight : public SGHighlight +{ +public: + SGHoverHighlight(QQuickItem *parent) + : SGHighlight(parent) + { + setZ(1); // hover highlight on top of selection highlight + } + + void paint(QPainter *painter); +}; + +} // namespace QtQuick2 +} // namespace QmlJSDebugger + +#endif // SGHIGHLIGHT_H diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/sgselectiontool.cpp b/src/plugins/qmltooling/qmldbg_qtquick2/sgselectiontool.cpp new file mode 100644 index 0000000000..025aa2d603 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/sgselectiontool.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** 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 "sgselectiontool.h" + +#include "sghighlight.h" +#include "sgviewinspector.h" + +#include <QtWidgets/QMenu> +#include <QtGui/QMouseEvent> +#include <QtDeclarative/QQuickView> +#include <QtDeclarative/QQuickItem> + +namespace QmlJSDebugger { +namespace QtQuick2 { + +SGSelectionTool::SGSelectionTool(SGViewInspector *inspector) : + AbstractTool(inspector), + m_hoverHighlight(new SGHoverHighlight(inspector->overlay())) +{ +} + +void SGSelectionTool::leaveEvent(QEvent *) +{ + m_hoverHighlight->setVisible(false); +} + +void SGSelectionTool::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + if (QQuickItem *item = inspector()->topVisibleItemAt(event->pos())) + inspector()->setSelectedItems(QList<QQuickItem*>() << item); + } else if (event->button() == Qt::RightButton) { + QList<QQuickItem*> items = inspector()->itemsAt(event->pos()); + createContextMenu(items, event->globalPos()); + } +} + +void SGSelectionTool::hoverMoveEvent(QMouseEvent *event) +{ + QQuickItem *item = inspector()->topVisibleItemAt(event->pos()); + if (!item) { + m_hoverHighlight->setVisible(false); + } else { + m_hoverHighlight->setItem(item); + m_hoverHighlight->setVisible(true); + } +} + +void SGSelectionTool::createContextMenu(const QList<QQuickItem *> &items, QPoint pos) +{ + QMenu contextMenu; + connect(&contextMenu, SIGNAL(hovered(QAction*)), + this, SLOT(contextMenuElementHovered(QAction*))); + + const QList<QQuickItem*> selectedItems = inspector()->selectedItems(); + int shortcutKey = Qt::Key_1; + + foreach (QQuickItem *item, items) { + const QString title = inspector()->titleForItem(item); + QAction *elementAction = contextMenu.addAction(title); + elementAction->setData(QVariant::fromValue(item)); + + connect(elementAction, SIGNAL(triggered()), this, SLOT(contextMenuElementSelected())); + + if (selectedItems.contains(item)) { + QFont font = elementAction->font(); + font.setBold(true); + elementAction->setFont(font); + } + + if (shortcutKey <= Qt::Key_9) { + elementAction->setShortcut(QKeySequence(shortcutKey)); + shortcutKey++; + } + } + + contextMenu.exec(pos); +} + +void SGSelectionTool::contextMenuElementHovered(QAction *action) +{ + if (QQuickItem *item = action->data().value<QQuickItem*>()) + m_hoverHighlight->setItem(item); +} + +void SGSelectionTool::contextMenuElementSelected() +{ + if (QQuickItem *item = static_cast<QAction*>(sender())->data().value<QQuickItem*>()) + inspector()->setSelectedItems(QList<QQuickItem*>() << item); +} + +SGViewInspector *SGSelectionTool::inspector() const +{ + return static_cast<SGViewInspector*>(AbstractTool::inspector()); +} + +} // namespace QtQuick2 +} // namespace QmlJSDebugger diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/sgselectiontool.h b/src/plugins/qmltooling/qmldbg_qtquick2/sgselectiontool.h new file mode 100644 index 0000000000..fe338bd85a --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/sgselectiontool.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** 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 SGSELECTIONTOOL_H +#define SGSELECTIONTOOL_H + +#include "abstracttool.h" + +#include <QtCore/QList> +#include <QtCore/QPoint> + +QT_FORWARD_DECLARE_CLASS(QAction) +QT_FORWARD_DECLARE_CLASS(QQuickItem) + +namespace QmlJSDebugger { +namespace QtQuick2 { + +class SGViewInspector; +class SGHoverHighlight; + +class SGSelectionTool : public AbstractTool +{ + Q_OBJECT +public: + explicit SGSelectionTool(SGViewInspector *inspector); + + void leaveEvent(QEvent *); + + void mousePressEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *) {} + void mouseReleaseEvent(QMouseEvent *) {} + void mouseDoubleClickEvent(QMouseEvent *) {} + + void hoverMoveEvent(QMouseEvent *); + void wheelEvent(QWheelEvent *) {} + + void keyPressEvent(QKeyEvent *) {} + void keyReleaseEvent(QKeyEvent *) {} + +private slots: + void contextMenuElementHovered(QAction *action); + void contextMenuElementSelected(); + +private: + void createContextMenu(const QList<QQuickItem*> &items, QPoint pos); + + SGViewInspector *inspector() const; + + SGHoverHighlight *m_hoverHighlight; +}; + +} // namespace QtQuick2 +} // namespace QmlJSDebugger + +#endif // SGSELECTIONTOOL_H diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/sgviewinspector.cpp b/src/plugins/qmltooling/qmldbg_qtquick2/sgviewinspector.cpp new file mode 100644 index 0000000000..06eb6eac97 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/sgviewinspector.cpp @@ -0,0 +1,332 @@ +/**************************************************************************** +** +** 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 "sgviewinspector.h" + +#include "qdeclarativeinspectorprotocol.h" +#include "sghighlight.h" +#include "sgselectiontool.h" + +#include <QtDeclarative/private/qquickitem_p.h> + +#include <QtDeclarative/QQuickView> +#include <QtDeclarative/QQuickItem> + +#include <cfloat> + +namespace QmlJSDebugger { +namespace QtQuick2 { + +/* + * Collects all the items at the given position, from top to bottom. + */ +static void collectItemsAt(QQuickItem *item, const QPointF &pos, QQuickItem *overlay, + QList<QQuickItem *> &resultList) +{ + if (item == overlay) + return; + + if (item->flags() & QQuickItem::ItemClipsChildrenToShape) { + if (!QRectF(0, 0, item->width(), item->height()).contains(pos)) + return; + } + + QList<QQuickItem *> children = QQuickItemPrivate::get(item)->paintOrderChildItems(); + for (int i = children.count() - 1; i >= 0; --i) { + QQuickItem *child = children.at(i); + collectItemsAt(child, item->mapToItem(child, pos), overlay, resultList); + } + + if (!QRectF(0, 0, item->width(), item->height()).contains(pos)) + return; + + resultList.append(item); +} + +/* + * Returns the first visible item at the given position, or 0 when no such + * child exists. + */ +static QQuickItem *itemAt(QQuickItem *item, const QPointF &pos, QQuickItem *overlay) +{ + if (item == overlay) + return 0; + + if (!item->isVisible() || item->opacity() == 0.0) + return 0; + + if (item->flags() & QQuickItem::ItemClipsChildrenToShape) { + if (!QRectF(0, 0, item->width(), item->height()).contains(pos)) + return 0; + } + + QList<QQuickItem *> children = QQuickItemPrivate::get(item)->paintOrderChildItems(); + for (int i = children.count() - 1; i >= 0; --i) { + QQuickItem *child = children.at(i); + if (QQuickItem *betterCandidate = itemAt(child, item->mapToItem(child, pos), overlay)) + return betterCandidate; + } + + if (!(item->flags() & QQuickItem::ItemHasContents)) + return 0; + + if (!QRectF(0, 0, item->width(), item->height()).contains(pos)) + return 0; + + return item; +} + + +SGViewInspector::SGViewInspector(QQuickView *view, QObject *parent) : + AbstractViewInspector(parent), + m_view(view), + m_overlay(new QQuickItem), + m_selectionTool(new SGSelectionTool(this)), + m_designMode(true) +{ + // Try to make sure the overlay is always on top + m_overlay->setZ(FLT_MAX); + + if (QQuickItem *root = view->rootItem()) + m_overlay->setParentItem(root); + + view->installEventFilter(this); + setCurrentTool(m_selectionTool); +} + +void SGViewInspector::changeCurrentObjects(const QList<QObject*> &objects) +{ + QList<QQuickItem*> items; + foreach (QObject *obj, objects) + if (QQuickItem *item = qobject_cast<QQuickItem*>(obj)) + items << item; + + syncSelectedItems(items); +} + +void SGViewInspector::reloadView() +{ + // TODO + emit reloadRequested(); +} + +void SGViewInspector::reparentQmlObject(QObject *object, QObject *newParent) +{ + if (!newParent) + return; + + object->setParent(newParent); + QQuickItem *newParentItem = qobject_cast<QQuickItem*>(newParent); + QQuickItem *item = qobject_cast<QQuickItem*>(object); + if (newParentItem && item) + item->setParentItem(newParentItem); +} + +void SGViewInspector::changeTool(InspectorProtocol::Tool tool) +{ + switch (tool) { + case InspectorProtocol::ColorPickerTool: + // TODO + emit colorPickerActivated(); + break; + case InspectorProtocol::SelectMarqueeTool: + // TODO + emit marqueeSelectToolActivated(); + break; + case InspectorProtocol::SelectTool: + setCurrentTool(m_selectionTool); + emit selectToolActivated(); + break; + case InspectorProtocol::ZoomTool: + // TODO + emit zoomToolActivated(); + break; + } +} + +QWindow *getMasterWindow(QWindow *w) +{ + QWindow *p = w->parent(); + while (p) { + w = p; + p = p->parent(); + } + return w; +} + +Qt::WindowFlags SGViewInspector::windowFlags() const +{ + return getMasterWindow(m_view)->windowFlags(); +} + +void SGViewInspector::setWindowFlags(Qt::WindowFlags flags) +{ + QWindow *w = getMasterWindow(m_view); + w->setWindowFlags(flags); + // make flags are applied + w->setVisible(false); + w->setVisible(true); +} + +QDeclarativeEngine *SGViewInspector::declarativeEngine() const +{ + return m_view->engine(); +} + +QQuickItem *SGViewInspector::topVisibleItemAt(const QPointF &pos) const +{ + QQuickItem *root = m_view->rootItem(); + return itemAt(root, root->mapFromScene(pos), m_overlay); +} + +QList<QQuickItem *> SGViewInspector::itemsAt(const QPointF &pos) const +{ + QQuickItem *root = m_view->rootItem(); + QList<QQuickItem *> resultList; + collectItemsAt(root, root->mapFromScene(pos), m_overlay, resultList); + return resultList; +} + +QList<QQuickItem*> SGViewInspector::selectedItems() const +{ + QList<QQuickItem *> selection; + foreach (const QWeakPointer<QQuickItem> &selectedItem, m_selectedItems) { + if (selectedItem) + selection << selectedItem.data(); + } + return selection; +} + +void SGViewInspector::setSelectedItems(const QList<QQuickItem *> &items) +{ + if (!syncSelectedItems(items)) + return; + + QList<QObject*> objectList; + foreach (QQuickItem *item, items) + objectList << item; + + sendCurrentObjects(objectList); +} + +bool SGViewInspector::syncSelectedItems(const QList<QQuickItem *> &items) +{ + bool selectionChanged = false; + + // Disconnect and remove items that are no longer selected + foreach (const QWeakPointer<QQuickItem> &item, m_selectedItems) { + if (!item) // Don't see how this can happen due to handling of destroyed() + continue; + if (items.contains(item.data())) + continue; + + selectionChanged = true; + item.data()->disconnect(this); + m_selectedItems.removeOne(item); + delete m_highlightItems.take(item.data()); + } + + // Connect and add newly selected items + foreach (QQuickItem *item, items) { + if (m_selectedItems.contains(item)) + continue; + + selectionChanged = true; + connect(item, SIGNAL(destroyed(QObject*)), this, SLOT(removeFromSelectedItems(QObject*))); + m_selectedItems.append(item); + m_highlightItems.insert(item, new SGSelectionHighlight(item, m_overlay)); + } + + return selectionChanged; +} + +void SGViewInspector::removeFromSelectedItems(QObject *object) +{ + if (QQuickItem *item = qobject_cast<QQuickItem*>(object)) { + if (m_selectedItems.removeOne(item)) + delete m_highlightItems.take(item); + } +} + +bool SGViewInspector::eventFilter(QObject *obj, QEvent *event) +{ + if (obj != m_view) + return QObject::eventFilter(obj, event); + + return AbstractViewInspector::eventFilter(obj, event); +} + +bool SGViewInspector::mouseMoveEvent(QMouseEvent *event) +{ + // TODO +// if (QQuickItem *item = topVisibleItemAt(event->pos())) +// m_view->setToolTip(titleForItem(item)); +// else +// m_view->setToolTip(QString()); + + return AbstractViewInspector::mouseMoveEvent(event); +} + +QString SGViewInspector::titleForItem(QQuickItem *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("QQuick"))) + className = className.mid(6); + + 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 QtQuick2 +} // namespace QmlJSDebugger diff --git a/src/plugins/qmltooling/qmldbg_qtquick2/sgviewinspector.h b/src/plugins/qmltooling/qmldbg_qtquick2/sgviewinspector.h new file mode 100644 index 0000000000..fa404a538a --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_qtquick2/sgviewinspector.h @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** 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 QSGVIEWINSPECTOR_H +#define QSGVIEWINSPECTOR_H + +#include "abstractviewinspector.h" + +#include <QtCore/QWeakPointer> +#include <QtCore/QHash> + +QT_BEGIN_NAMESPACE +class QQuickView; +class QQuickItem; +QT_END_NAMESPACE + +namespace QmlJSDebugger { +namespace QtQuick2 { + +class SGSelectionTool; +class SGSelectionHighlight; + +class SGViewInspector : public AbstractViewInspector +{ + Q_OBJECT +public: + explicit SGViewInspector(QQuickView *view, QObject *parent = 0); + + // AbstractViewInspector + void changeCurrentObjects(const QList<QObject*> &objects); + void reloadView(); + void reparentQmlObject(QObject *object, QObject *newParent); + void changeTool(InspectorProtocol::Tool tool); + Qt::WindowFlags windowFlags() const; + void setWindowFlags(Qt::WindowFlags flags); + QDeclarativeEngine *declarativeEngine() const; + + QQuickView *view() const { return m_view; } + QQuickItem *overlay() const { return m_overlay; } + + QQuickItem *topVisibleItemAt(const QPointF &pos) const; + QList<QQuickItem *> itemsAt(const QPointF &pos) const; + + QList<QQuickItem *> selectedItems() const; + void setSelectedItems(const QList<QQuickItem*> &items); + + QString titleForItem(QQuickItem *item) const; + +protected: + bool eventFilter(QObject *obj, QEvent *event); + + bool mouseMoveEvent(QMouseEvent *); + +private slots: + void removeFromSelectedItems(QObject *); + +private: + bool syncSelectedItems(const QList<QQuickItem*> &items); + + QQuickView *m_view; + QQuickItem *m_overlay; + + SGSelectionTool *m_selectionTool; + + QList<QWeakPointer<QQuickItem> > m_selectedItems; + QHash<QQuickItem*, SGSelectionHighlight*> m_highlightItems; + + bool m_designMode; +}; + +} // namespace QtQuick2 +} // namespace QmlJSDebugger + +#endif // QSGVIEWINSPECTOR_H |