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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qmljsobserverclient.h"
43 #include "qmljsclientproxy.h"
44 #include "qmljsinspectorconstants.h"
48 namespace QmlJSInspector {
51 QmlJSObserverClient::QmlJSObserverClient(QDeclarativeDebugConnection *client,
53 : QDeclarativeDebugClient(QLatin1String("QDeclarativeObserverMode"), client) ,
58 void QmlJSObserverClient::statusChanged(Status status)
60 emit connectedStatusChanged(status);
63 void QmlJSObserverClient::messageReceived(const QByteArray &message)
65 QDataStream ds(message);
70 if (type == "CURRENT_OBJECTS_CHANGED") {
74 log(LogReceive, QString("%1 %2 [list of debug ids]").arg(QString(type),
75 QString::number(objectCount)));
77 m_currentDebugIds.clear();
79 for(int i = 0; i < objectCount; ++i) {
83 m_currentDebugIds << debugId;
87 emit currentObjectsChanged(m_currentDebugIds);
88 } else if (type == "TOOL_CHANGED") {
92 log(LogReceive, QString("%1 %2").arg(QString(type), QString::number(toolId)));
94 if (toolId == Constants::ColorPickerMode) {
95 emit colorPickerActivated();
96 } else if (toolId == Constants::ZoomMode) {
97 emit zoomToolActivated();
98 } else if (toolId == Constants::SelectionToolMode) {
99 emit selectToolActivated();
100 } else if (toolId == Constants::MarqueeSelectionToolMode) {
101 emit selectMarqueeToolActivated();
103 } else if (type == "ANIMATION_SPEED_CHANGED") {
104 qreal slowdownFactor;
105 ds >> slowdownFactor;
107 log(LogReceive, QString("%1 %2").arg(QString(type), QString::number(slowdownFactor)));
109 emit animationSpeedChanged(slowdownFactor);
110 } else if (type == "SET_DESIGN_MODE") {
114 log(LogReceive, QString("%1 %2").arg(QString(type), inDesignMode ? "true" : "false"));
116 emit designModeBehaviorChanged(inDesignMode);
117 } else if (type == "SHOW_APP_ON_TOP") {
121 log(LogReceive, QString("%1 %2").arg(QString(type), showAppOnTop ? "true" : "false"));
123 emit showAppOnTopChanged(showAppOnTop);
124 } else if (type == "RELOADED") {
126 log(LogReceive, type);
129 } else if (type == "COLOR_CHANGED") {
133 log(LogReceive, QString("%1 %2").arg(QString(type), col.name()));
135 emit selectedColorChanged(col);
136 } else if (type == "CONTEXT_PATH_UPDATED") {
137 QStringList contextPath;
140 log(LogReceive, QString("%1 %2").arg(QString(type), contextPath.join(", ")));
142 emit contextPathUpdated(contextPath);
146 QList<int> QmlJSObserverClient::currentObjects() const
148 return m_currentDebugIds;
151 void QmlJSObserverClient::setCurrentObjects(const QList<int> &debugIds) {
152 if (!m_connection || !m_connection->isConnected())
155 if (debugIds == m_currentDebugIds)
158 m_currentDebugIds = debugIds;
161 QDataStream ds(&message, QIODevice::WriteOnly);
163 QByteArray cmd = "SET_CURRENT_OBJECTS";
165 << debugIds.length();
167 foreach (int id, debugIds) {
171 log(LogSend, QString("%1 %2 [list of ids]").arg(QString(cmd),
172 QString::number(debugIds.length())));
174 sendMessage(message);
177 void recurseObjectIdList(const QDeclarativeDebugObjectReference &ref, QList<int> &debugIds, QList<QString> &objectIds)
179 debugIds << ref.debugId();
180 objectIds << ref.idString();
181 foreach(const QDeclarativeDebugObjectReference &child, ref.children()) {
182 recurseObjectIdList(child, debugIds, objectIds);
186 void QmlJSObserverClient::setObjectIdList(const QList<QDeclarativeDebugObjectReference> &objectRoots)
189 QDataStream ds(&message, QIODevice::WriteOnly);
192 QList<QString> objectIds;
194 foreach(const QDeclarativeDebugObjectReference &ref, objectRoots) {
195 recurseObjectIdList(ref, debugIds, objectIds);
198 QByteArray cmd = "OBJECT_ID_LIST";
200 << debugIds.length();
202 Q_ASSERT(debugIds.length() == objectIds.length());
204 for(int i = 0; i < debugIds.length(); ++i) {
205 ds << debugIds[i] << objectIds[i];
208 log(LogSend, QString("%1 %2 [list of debug / object ids]").arg(QString(cmd),
209 QString::number(debugIds.length())));
211 sendMessage(message);
214 void QmlJSObserverClient::setContextPathIndex(int contextPathIndex)
216 if (!m_connection || !m_connection->isConnected())
220 QDataStream ds(&message, QIODevice::WriteOnly);
222 QByteArray cmd = "SET_CONTEXT_PATH_IDX";
226 log(LogSend, QString("%1 %2").arg(QString(cmd), contextPathIndex));
228 sendMessage(message);
231 void QmlJSObserverClient::clearComponentCache()
233 if (!m_connection || !m_connection->isConnected())
237 QDataStream ds(&message, QIODevice::WriteOnly);
239 QByteArray cmd = "CLEAR_COMPONENT_CACHE";
244 sendMessage(message);
247 void QmlJSObserverClient::reloadViewer()
249 if (!m_connection || !m_connection->isConnected())
253 QDataStream ds(&message, QIODevice::WriteOnly);
255 QByteArray cmd = "RELOAD";
260 sendMessage(message);
263 void QmlJSObserverClient::setDesignModeBehavior(bool inDesignMode)
265 if (!m_connection || !m_connection->isConnected())
269 QDataStream ds(&message, QIODevice::WriteOnly);
271 QByteArray cmd = "SET_DESIGN_MODE";
275 log(LogSend, QString("%1 %2").arg(QString(cmd), inDesignMode ? "true" : "false"));
277 sendMessage(message);
280 void QmlJSObserverClient::setAnimationSpeed(qreal slowdownFactor)
282 if (!m_connection || !m_connection->isConnected())
286 QDataStream ds(&message, QIODevice::WriteOnly);
288 QByteArray cmd = "SET_ANIMATION_SPEED";
293 log(LogSend, QString("%1 %2").arg(QString(cmd), QString::number(slowdownFactor)));
295 sendMessage(message);
298 void QmlJSObserverClient::changeToColorPickerTool()
300 if (!m_connection || !m_connection->isConnected())
304 QDataStream ds(&message, QIODevice::WriteOnly);
306 QByteArray cmd = "CHANGE_TOOL";
307 QByteArray tool = "COLOR_PICKER";
311 log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
313 sendMessage(message);
316 void QmlJSObserverClient::changeToSelectTool()
318 if (!m_connection || !m_connection->isConnected())
322 QDataStream ds(&message, QIODevice::WriteOnly);
324 QByteArray cmd = "CHANGE_TOOL";
325 QByteArray tool = "SELECT";
329 log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
331 sendMessage(message);
334 void QmlJSObserverClient::changeToSelectMarqueeTool()
336 if (!m_connection || !m_connection->isConnected())
340 QDataStream ds(&message, QIODevice::WriteOnly);
342 QByteArray cmd = "CHANGE_TOOL";
343 QByteArray tool = "SELECT_MARQUEE";
347 log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
349 sendMessage(message);
352 void QmlJSObserverClient::changeToZoomTool()
354 if (!m_connection || !m_connection->isConnected())
358 QDataStream ds(&message, QIODevice::WriteOnly);
360 QByteArray cmd = "CHANGE_TOOL";
361 QByteArray tool = "ZOOM";
365 log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
367 sendMessage(message);
370 void QmlJSObserverClient::showAppOnTop(bool showOnTop)
372 if (!m_connection || !m_connection->isConnected())
376 QDataStream ds(&message, QIODevice::WriteOnly);
378 QByteArray cmd = "SHOW_APP_ON_TOP";
381 log(LogSend, QString("%1 %2").arg(QString(cmd), showOnTop ? "true" : "false"));
383 sendMessage(message);
386 void QmlJSObserverClient::createQmlObject(const QString &qmlText, int parentDebugId,
387 const QStringList &imports, const QString &filename)
389 if (!m_connection || !m_connection->isConnected())
393 QDataStream ds(&message, QIODevice::WriteOnly);
395 QByteArray cmd = "CREATE_OBJECT";
402 log(LogSend, QString("%1 %2 %3 [%4] %5").arg(QString(cmd), qmlText, QString::number(parentDebugId),
403 imports.join(","), filename));
405 sendMessage(message);
408 void QmlJSObserverClient::destroyQmlObject(int debugId)
410 if (!m_connection || !m_connection->isConnected())
413 QDataStream ds(&message, QIODevice::WriteOnly);
415 QByteArray cmd = "DESTROY_OBJECT";
416 ds << cmd << debugId;
418 log(LogSend, QString("%1 %2").arg(QString(cmd), debugId));
420 sendMessage(message);
423 void QmlJSObserverClient::reparentQmlObject(int debugId, int newParent)
425 if (!m_connection || !m_connection->isConnected())
428 QDataStream ds(&message, QIODevice::WriteOnly);
430 QByteArray cmd = "MOVE_OBJECT";
435 log(LogSend, QString("%1 %2 %3").arg(QString(cmd), QString::number(debugId),
436 QString::number(newParent)));
438 sendMessage(message);
442 void QmlJSObserverClient::applyChangesToQmlFile()
444 if (!m_connection || !m_connection->isConnected())
450 void QmlJSObserverClient::applyChangesFromQmlFile()
452 if (!m_connection || !m_connection->isConnected())
458 void QmlJSObserverClient::log(LogDirection direction, const QString &message)
461 if (direction == LogSend) {
464 msg += " receiving ";
467 emit logActivity(name(), msg);
470 } // namespace Internal
471 } // namespace QmlJSInspector