1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "abstractviewinspector.h"
44 #include "abstracttool.h"
45 #include "qdeclarativeinspectorprotocol.h"
47 #include <QtDeclarative/QDeclarativeEngine>
48 #include <QtDeclarative/QDeclarativeComponent>
49 #include <QtCore/private/qabstractanimation_p.h>
50 #include <QtDeclarative/private/qdeclarativeinspectorservice_p.h>
52 #include <QtGui/QMouseEvent>
54 namespace QmlJSDebugger {
57 AbstractViewInspector::AbstractViewInspector(QObject *parent) :
60 m_showAppOnTop(false),
61 m_designModeBehavior(false),
62 m_animationPaused(false),
63 m_slowDownFactor(1.0),
66 m_debugService = QDeclarativeInspectorService::instance();
67 connect(m_debugService, SIGNAL(gotMessage(QByteArray)),
68 this, SLOT(handleMessage(QByteArray)));
71 void AbstractViewInspector::createQmlObject(const QString &qml, QObject *parent,
72 const QStringList &importList,
73 const QString &filename)
79 foreach (const QString &s, importList) {
81 imports += QLatin1Char('\n');
84 QDeclarativeContext *parentContext = declarativeEngine()->contextForObject(parent);
85 QDeclarativeComponent component(declarativeEngine());
86 QByteArray constructedQml = QString(imports + qml).toLatin1();
88 component.setData(constructedQml, QUrl::fromLocalFile(filename));
89 QObject *newObject = component.create(parentContext);
91 reparentQmlObject(newObject, parent);
94 void AbstractViewInspector::clearComponentCache()
96 declarativeEngine()->clearComponentCache();
99 void AbstractViewInspector::setDesignModeBehavior(bool value)
101 if (m_designModeBehavior == value)
104 m_designModeBehavior = value;
105 emit designModeBehaviorChanged(value);
106 sendDesignModeBehavior(value);
109 void AbstractViewInspector::setAnimationSpeed(qreal slowDownFactor)
111 Q_ASSERT(slowDownFactor > 0);
112 if (m_slowDownFactor == slowDownFactor)
115 animationSpeedChangeRequested(slowDownFactor);
116 sendAnimationSpeed(slowDownFactor);
119 void AbstractViewInspector::setAnimationPaused(bool paused)
121 if (m_animationPaused == paused)
124 animationPausedChangeRequested(paused);
125 sendAnimationPaused(paused);
128 void AbstractViewInspector::animationSpeedChangeRequested(qreal factor)
130 if (m_slowDownFactor != factor) {
131 m_slowDownFactor = factor;
132 emit animationSpeedChanged(factor);
135 const float effectiveFactor = m_animationPaused ? 0 : factor;
136 QUnifiedTimer::instance()->setSlowModeEnabled(effectiveFactor != 1.0);
137 QUnifiedTimer::instance()->setSlowdownFactor(effectiveFactor);
140 void AbstractViewInspector::animationPausedChangeRequested(bool paused)
142 if (m_animationPaused != paused) {
143 m_animationPaused = paused;
144 emit animationPausedChanged(paused);
147 const float effectiveFactor = paused ? 0 : m_slowDownFactor;
148 QUnifiedTimer::instance()->setSlowModeEnabled(effectiveFactor != 1.0);
149 QUnifiedTimer::instance()->setSlowdownFactor(effectiveFactor);
152 void AbstractViewInspector::setShowAppOnTop(bool appOnTop)
154 Qt::WindowFlags flags = windowFlags();
156 flags |= Qt::WindowStaysOnTopHint;
158 flags &= ~Qt::WindowStaysOnTopHint;
160 setWindowFlags(flags);
162 m_showAppOnTop = appOnTop;
163 sendShowAppOnTop(appOnTop);
165 emit showAppOnTopChanged(appOnTop);
168 void AbstractViewInspector::changeToColorPickerTool()
170 changeTool(InspectorProtocol::ColorPickerTool);
173 void AbstractViewInspector::changeToZoomTool()
175 changeTool(InspectorProtocol::ZoomTool);
178 void AbstractViewInspector::changeToSingleSelectTool()
180 changeTool(InspectorProtocol::SelectTool);
183 void AbstractViewInspector::changeToMarqueeSelectTool()
185 changeTool(InspectorProtocol::SelectMarqueeTool);
188 bool AbstractViewInspector::eventFilter(QObject *obj, QEvent *event)
190 if (!designModeBehavior())
191 return QObject::eventFilter(obj, event);
193 switch (event->type()) {
195 if (leaveEvent(event))
198 case QEvent::MouseButtonPress:
199 if (mousePressEvent(static_cast<QMouseEvent*>(event)))
202 case QEvent::MouseMove:
203 if (mouseMoveEvent(static_cast<QMouseEvent*>(event)))
206 case QEvent::MouseButtonRelease:
207 if (mouseReleaseEvent(static_cast<QMouseEvent*>(event)))
210 case QEvent::KeyPress:
211 if (keyPressEvent(static_cast<QKeyEvent*>(event)))
214 case QEvent::KeyRelease:
215 if (keyReleaseEvent(static_cast<QKeyEvent*>(event)))
218 case QEvent::MouseButtonDblClick:
219 if (mouseDoubleClickEvent(static_cast<QMouseEvent*>(event)))
223 if (wheelEvent(static_cast<QWheelEvent*>(event)))
230 return QObject::eventFilter(obj, event);
233 bool AbstractViewInspector::leaveEvent(QEvent *event)
235 m_currentTool->leaveEvent(event);
239 bool AbstractViewInspector::mousePressEvent(QMouseEvent *event)
241 m_currentTool->mousePressEvent(event);
245 bool AbstractViewInspector::mouseMoveEvent(QMouseEvent *event)
247 if (event->buttons()) {
248 m_currentTool->mouseMoveEvent(event);
250 m_currentTool->hoverMoveEvent(event);
255 bool AbstractViewInspector::mouseReleaseEvent(QMouseEvent *event)
257 m_currentTool->mouseReleaseEvent(event);
261 bool AbstractViewInspector::keyPressEvent(QKeyEvent *event)
263 m_currentTool->keyPressEvent(event);
267 bool AbstractViewInspector::keyReleaseEvent(QKeyEvent *event)
269 switch (event->key()) {
271 changeTool(InspectorProtocol::SelectTool);
273 // disabled because multiselection does not do anything useful without design mode
275 // changeTool(InspectorProtocol::SelectMarqueeTool);
278 changeTool(InspectorProtocol::ColorPickerTool);
281 changeTool(InspectorProtocol::ZoomTool);
284 setAnimationPaused(!animationPaused());
290 m_currentTool->keyReleaseEvent(event);
294 bool AbstractViewInspector::mouseDoubleClickEvent(QMouseEvent *event)
296 m_currentTool->mouseDoubleClickEvent(event);
300 bool AbstractViewInspector::wheelEvent(QWheelEvent *event)
302 m_currentTool->wheelEvent(event);
306 void AbstractViewInspector::handleMessage(const QByteArray &message)
308 QDataStream ds(message);
310 InspectorProtocol::Message type;
314 case InspectorProtocol::SetCurrentObjects: {
318 QList<QObject*> selectedObjects;
319 for (int i = 0; i < itemCount; ++i) {
322 if (QObject *obj = QDeclarativeDebugService::objectForId(debugId))
323 selectedObjects << obj;
326 changeCurrentObjects(selectedObjects);
329 case InspectorProtocol::Reload: {
333 case InspectorProtocol::SetAnimationSpeed: {
336 animationSpeedChangeRequested(speed);
339 case InspectorProtocol::SetAnimationPaused: {
342 animationPausedChangeRequested(paused);
345 case InspectorProtocol::ChangeTool: {
346 InspectorProtocol::Tool tool;
351 case InspectorProtocol::SetDesignMode: {
354 setDesignModeBehavior(inDesignMode);
357 case InspectorProtocol::ShowAppOnTop: {
360 setShowAppOnTop(showOnTop);
363 case InspectorProtocol::CreateObject: {
368 ds >> qml >> parentId >> imports >> filename;
369 createQmlObject(qml, QDeclarativeDebugService::objectForId(parentId),
373 case InspectorProtocol::DestroyObject: {
376 if (QObject *obj = QDeclarativeDebugService::objectForId(debugId))
380 case InspectorProtocol::MoveObject: {
381 int debugId, newParent;
382 ds >> debugId >> newParent;
383 reparentQmlObject(QDeclarativeDebugService::objectForId(debugId),
384 QDeclarativeDebugService::objectForId(newParent));
387 case InspectorProtocol::ObjectIdList: {
390 m_stringIdForObjectId.clear();
391 for (int i = 0; i < itemCount; ++i) {
393 QString itemIdString;
397 m_stringIdForObjectId.insert(itemDebugId, itemIdString);
401 case InspectorProtocol::ClearComponentCache: {
402 clearComponentCache();
406 qWarning() << "Warning: Not handling message:" << type;
410 void AbstractViewInspector::sendDesignModeBehavior(bool inDesignMode)
413 QDataStream ds(&message, QIODevice::WriteOnly);
415 ds << InspectorProtocol::SetDesignMode
418 m_debugService->sendMessage(message);
421 void AbstractViewInspector::sendCurrentObjects(const QList<QObject*> &objects)
424 QDataStream ds(&message, QIODevice::WriteOnly);
426 ds << InspectorProtocol::CurrentObjectsChanged
429 foreach (QObject *object, objects) {
430 int id = QDeclarativeDebugService::idForObject(object);
434 m_debugService->sendMessage(message);
437 void AbstractViewInspector::sendCurrentTool(Constants::DesignTool toolId)
440 QDataStream ds(&message, QIODevice::WriteOnly);
442 ds << InspectorProtocol::ToolChanged
445 m_debugService->sendMessage(message);
448 void AbstractViewInspector::sendAnimationSpeed(qreal slowDownFactor)
451 QDataStream ds(&message, QIODevice::WriteOnly);
453 ds << InspectorProtocol::AnimationSpeedChanged
456 m_debugService->sendMessage(message);
459 void AbstractViewInspector::sendAnimationPaused(bool paused)
462 QDataStream ds(&message, QIODevice::WriteOnly);
464 ds << InspectorProtocol::AnimationPausedChanged
467 m_debugService->sendMessage(message);
470 void AbstractViewInspector::sendReloaded()
473 QDataStream ds(&message, QIODevice::WriteOnly);
475 ds << InspectorProtocol::Reloaded;
477 m_debugService->sendMessage(message);
480 void AbstractViewInspector::sendShowAppOnTop(bool showAppOnTop)
483 QDataStream ds(&message, QIODevice::WriteOnly);
485 ds << InspectorProtocol::ShowAppOnTop << showAppOnTop;
487 m_debugService->sendMessage(message);
490 void AbstractViewInspector::sendColorChanged(const QColor &color)
493 QDataStream ds(&message, QIODevice::WriteOnly);
495 ds << InspectorProtocol::ColorChanged
498 m_debugService->sendMessage(message);
501 QString AbstractViewInspector::idStringForObject(QObject *obj) const
503 const int id = QDeclarativeDebugService::idForObject(obj);
504 return m_stringIdForObjectId.value(id);
507 } // namespace QmlJSDebugger