1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
9 ** 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 file.
14 ** Please review the following information to ensure the GNU Lesser General
15 ** 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.
24 ** Alternatively, this file may be used in accordance with the terms and
25 ** conditions contained in a signed written agreement between you and Nokia.
27 ** If you have questions regarding the use of this file, please contact
28 ** Nokia at qt-info@nokia.com.
30 **************************************************************************/
32 #include "qmljsobserverclient.h"
33 #include "qmljsclientproxy.h"
34 #include "qmljsinspectorconstants.h"
36 #include <QtGui/QColor>
38 using namespace QmlJSDebugger;
40 namespace QmlJSInspector {
43 QmlJSObserverClient::QmlJSObserverClient(QDeclarativeDebugConnection *client,
45 : QDeclarativeDebugClient(QLatin1String("QDeclarativeObserverMode"), client) ,
50 void QmlJSObserverClient::statusChanged(Status status)
52 emit connectedStatusChanged(status);
55 void QmlJSObserverClient::messageReceived(const QByteArray &message)
57 QDataStream ds(message);
59 ObserverProtocol::Message type;
63 case ObserverProtocol::CurrentObjectsChanged: {
67 log(LogReceive, type, QString("%1 [list of debug ids]").arg(objectCount));
69 m_currentDebugIds.clear();
71 for (int i = 0; i < objectCount; ++i) {
75 m_currentDebugIds << debugId;
78 emit currentObjectsChanged(m_currentDebugIds);
81 case ObserverProtocol::ToolChanged: {
85 log(LogReceive, type, QString::number(toolId));
87 if (toolId == Constants::ColorPickerMode) {
88 emit colorPickerActivated();
89 } else if (toolId == Constants::ZoomMode) {
90 emit zoomToolActivated();
91 } else if (toolId == Constants::SelectionToolMode) {
92 emit selectToolActivated();
93 } else if (toolId == Constants::MarqueeSelectionToolMode) {
94 emit selectMarqueeToolActivated();
98 case ObserverProtocol::AnimationSpeedChanged: {
100 ds >> slowDownFactor;
102 log(LogReceive, type, QString::number(slowDownFactor));
104 emit animationSpeedChanged(slowDownFactor);
107 case ObserverProtocol::AnimationPausedChanged: {
111 log(LogReceive, type, paused ? QLatin1String("true") : QLatin1String("false"));
113 emit animationPausedChanged(paused);
116 case ObserverProtocol::SetDesignMode: {
120 log(LogReceive, type, QLatin1String(inDesignMode ? "true" : "false"));
122 emit designModeBehaviorChanged(inDesignMode);
125 case ObserverProtocol::ShowAppOnTop: {
129 log(LogReceive, type, QLatin1String(showAppOnTop ? "true" : "false"));
131 emit showAppOnTopChanged(showAppOnTop);
134 case ObserverProtocol::Reloaded: {
135 log(LogReceive, type);
139 case ObserverProtocol::ColorChanged: {
143 log(LogReceive, type, col.name());
145 emit selectedColorChanged(col);
148 case ObserverProtocol::ContextPathUpdated: {
149 QStringList contextPath;
152 log(LogReceive, type, contextPath.join(", "));
154 emit contextPathUpdated(contextPath);
158 qWarning() << "Warning: Not handling message:" << type;
162 QList<int> QmlJSObserverClient::currentObjects() const
164 return m_currentDebugIds;
167 void QmlJSObserverClient::setCurrentObjects(const QList<int> &debugIds)
169 if (!m_connection || !m_connection->isConnected())
172 if (debugIds == m_currentDebugIds)
175 m_currentDebugIds = debugIds;
178 QDataStream ds(&message, QIODevice::WriteOnly);
180 ObserverProtocol::Message cmd = ObserverProtocol::SetCurrentObjects;
182 << debugIds.length();
184 foreach (int id, debugIds) {
188 log(LogSend, cmd, QString("%1 [list of ids]").arg(debugIds.length()));
190 sendMessage(message);
193 void recurseObjectIdList(const QDeclarativeDebugObjectReference &ref, QList<int> &debugIds, QList<QString> &objectIds)
195 debugIds << ref.debugId();
196 objectIds << ref.idString();
197 foreach (const QDeclarativeDebugObjectReference &child, ref.children())
198 recurseObjectIdList(child, debugIds, objectIds);
201 void QmlJSObserverClient::setObjectIdList(const QList<QDeclarativeDebugObjectReference> &objectRoots)
204 QDataStream ds(&message, QIODevice::WriteOnly);
207 QList<QString> objectIds;
209 foreach (const QDeclarativeDebugObjectReference &ref, objectRoots)
210 recurseObjectIdList(ref, debugIds, objectIds);
212 ObserverProtocol::Message cmd = ObserverProtocol::ObjectIdList;
214 << debugIds.length();
216 Q_ASSERT(debugIds.length() == objectIds.length());
218 for(int i = 0; i < debugIds.length(); ++i) {
219 ds << debugIds[i] << objectIds[i];
222 log(LogSend, cmd, QString("%1 %2 [list of debug / object ids]").arg(debugIds.length()));
224 sendMessage(message);
227 void QmlJSObserverClient::setContextPathIndex(int contextPathIndex)
229 if (!m_connection || !m_connection->isConnected())
233 QDataStream ds(&message, QIODevice::WriteOnly);
235 ObserverProtocol::Message cmd = ObserverProtocol::SetContextPathIdx;
239 log(LogSend, cmd, QString::number(contextPathIndex));
241 sendMessage(message);
244 void QmlJSObserverClient::clearComponentCache()
246 if (!m_connection || !m_connection->isConnected())
250 QDataStream ds(&message, QIODevice::WriteOnly);
252 ObserverProtocol::Message cmd = ObserverProtocol::ClearComponentCache;
257 sendMessage(message);
260 void QmlJSObserverClient::reloadViewer()
262 if (!m_connection || !m_connection->isConnected())
266 QDataStream ds(&message, QIODevice::WriteOnly);
268 ObserverProtocol::Message cmd = ObserverProtocol::Reload;
273 sendMessage(message);
276 void QmlJSObserverClient::setDesignModeBehavior(bool inDesignMode)
278 if (!m_connection || !m_connection->isConnected())
282 QDataStream ds(&message, QIODevice::WriteOnly);
284 ObserverProtocol::Message cmd = ObserverProtocol::SetDesignMode;
288 log(LogSend, cmd, QLatin1String(inDesignMode ? "true" : "false"));
290 sendMessage(message);
293 void QmlJSObserverClient::setAnimationSpeed(qreal slowDownFactor)
295 if (!m_connection || !m_connection->isConnected())
299 QDataStream ds(&message, QIODevice::WriteOnly);
301 ObserverProtocol::Message cmd = ObserverProtocol::SetAnimationSpeed;
306 log(LogSend, cmd, QString::number(slowDownFactor));
308 sendMessage(message);
311 void QmlJSObserverClient::setAnimationPaused(bool paused)
313 if (!m_connection || !m_connection->isConnected())
317 QDataStream ds(&message, QIODevice::WriteOnly);
319 ObserverProtocol::Message cmd = ObserverProtocol::SetAnimationPaused;
323 log(LogSend, cmd, paused ? QLatin1String("true") : QLatin1String("false"));
325 sendMessage(message);
328 void QmlJSObserverClient::changeToColorPickerTool()
330 if (!m_connection || !m_connection->isConnected())
334 QDataStream ds(&message, QIODevice::WriteOnly);
336 ObserverProtocol::Message cmd = ObserverProtocol::ChangeTool;
337 ObserverProtocol::Tool tool = ObserverProtocol::ColorPickerTool;
341 log(LogSend, cmd, ObserverProtocol::toString(tool));
343 sendMessage(message);
346 void QmlJSObserverClient::changeToSelectTool()
348 if (!m_connection || !m_connection->isConnected())
352 QDataStream ds(&message, QIODevice::WriteOnly);
354 ObserverProtocol::Message cmd = ObserverProtocol::ChangeTool;
355 ObserverProtocol::Tool tool = ObserverProtocol::SelectTool;
359 log(LogSend, cmd, ObserverProtocol::toString(tool));
361 sendMessage(message);
364 void QmlJSObserverClient::changeToSelectMarqueeTool()
366 if (!m_connection || !m_connection->isConnected())
370 QDataStream ds(&message, QIODevice::WriteOnly);
372 ObserverProtocol::Message cmd = ObserverProtocol::ChangeTool;
373 ObserverProtocol::Tool tool = ObserverProtocol::SelectMarqueeTool;
377 log(LogSend, cmd, ObserverProtocol::toString(tool));
379 sendMessage(message);
382 void QmlJSObserverClient::changeToZoomTool()
384 if (!m_connection || !m_connection->isConnected())
388 QDataStream ds(&message, QIODevice::WriteOnly);
390 ObserverProtocol::Message cmd = ObserverProtocol::ChangeTool;
391 ObserverProtocol::Tool tool = ObserverProtocol::ZoomTool;
395 log(LogSend, cmd, ObserverProtocol::toString(tool));
397 sendMessage(message);
400 void QmlJSObserverClient::showAppOnTop(bool showOnTop)
402 if (!m_connection || !m_connection->isConnected())
406 QDataStream ds(&message, QIODevice::WriteOnly);
408 ObserverProtocol::Message cmd = ObserverProtocol::ShowAppOnTop;
409 ds << cmd << showOnTop;
411 log(LogSend, cmd, QLatin1String(showOnTop ? "true" : "false"));
413 sendMessage(message);
416 void QmlJSObserverClient::createQmlObject(const QString &qmlText, int parentDebugId,
417 const QStringList &imports, const QString &filename)
419 if (!m_connection || !m_connection->isConnected())
423 QDataStream ds(&message, QIODevice::WriteOnly);
425 ObserverProtocol::Message cmd = ObserverProtocol::CreateObject;
432 log(LogSend, cmd, QString("%1 %2 [%3] %4").arg(qmlText, QString::number(parentDebugId),
433 imports.join(","), filename));
435 sendMessage(message);
438 void QmlJSObserverClient::destroyQmlObject(int debugId)
440 if (!m_connection || !m_connection->isConnected())
443 QDataStream ds(&message, QIODevice::WriteOnly);
445 ObserverProtocol::Message cmd = ObserverProtocol::DestroyObject;
446 ds << cmd << debugId;
448 log(LogSend, cmd, QString::number(debugId));
450 sendMessage(message);
453 void QmlJSObserverClient::reparentQmlObject(int debugId, int newParent)
455 if (!m_connection || !m_connection->isConnected())
458 QDataStream ds(&message, QIODevice::WriteOnly);
460 ObserverProtocol::Message cmd = ObserverProtocol::MoveObject;
465 log(LogSend, cmd, QString("%1 %2").arg(QString::number(debugId),
466 QString::number(newParent)));
468 sendMessage(message);
472 void QmlJSObserverClient::applyChangesToQmlFile()
474 if (!m_connection || !m_connection->isConnected())
480 void QmlJSObserverClient::applyChangesFromQmlFile()
482 if (!m_connection || !m_connection->isConnected())
488 void QmlJSObserverClient::log(LogDirection direction, ObserverProtocol::Message message,
489 const QString &extra)
492 if (direction == LogSend)
493 msg += QLatin1String(" sending ");
495 msg += QLatin1String(" receiving ");
497 msg += ObserverProtocol::toString(message);
498 msg += QLatin1Char(' ');
500 emit logActivity(name(), msg);
503 } // namespace Internal
504 } // namespace QmlJSInspector