OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljsinspector / qmljsobserverclient.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qmljsobserverclient.h"
43 #include "qmljsclientproxy.h"
44 #include "qmljsinspectorconstants.h"
45
46 #include <QColor>
47
48 namespace QmlJSInspector {
49 namespace Internal {
50
51 QmlJSObserverClient::QmlJSObserverClient(QDeclarativeDebugConnection *client,
52                                                              QObject * /*parent*/)
53     : QDeclarativeDebugClient(QLatin1String("QDeclarativeObserverMode"), client) ,
54     m_connection(client)
55 {
56 }
57
58 void QmlJSObserverClient::statusChanged(Status status)
59 {
60     emit connectedStatusChanged(status);
61 }
62
63 void QmlJSObserverClient::messageReceived(const QByteArray &message)
64 {
65     QDataStream ds(message);
66
67     QByteArray type;
68     ds >> type;
69
70     if (type == "CURRENT_OBJECTS_CHANGED") {
71         int objectCount;
72         ds >> objectCount;
73
74         log(LogReceive, QString("%1 %2 [list of debug ids]").arg(QString(type),
75                                                                  QString::number(objectCount)));
76
77         m_currentDebugIds.clear();
78
79         for(int i = 0; i < objectCount; ++i) {
80             int debugId;
81             ds >> debugId;
82             if (debugId != -1) {
83                 m_currentDebugIds << debugId;
84             }
85         }
86
87         emit currentObjectsChanged(m_currentDebugIds);
88     } else if (type == "TOOL_CHANGED") {
89         int toolId;
90         ds >> toolId;
91
92         log(LogReceive, QString("%1 %2").arg(QString(type), QString::number(toolId)));
93
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();
102         }
103     } else if (type == "ANIMATION_SPEED_CHANGED") {
104         qreal slowdownFactor;
105         ds >> slowdownFactor;
106
107         log(LogReceive, QString("%1 %2").arg(QString(type), QString::number(slowdownFactor)));
108
109         emit animationSpeedChanged(slowdownFactor);
110     } else if (type == "SET_DESIGN_MODE") {
111         bool inDesignMode;
112         ds >> inDesignMode;
113
114         log(LogReceive, QString("%1 %2").arg(QString(type), inDesignMode ? "true" : "false"));
115
116         emit designModeBehaviorChanged(inDesignMode);
117     } else if (type == "SHOW_APP_ON_TOP") {
118         bool showAppOnTop;
119         ds >> showAppOnTop;
120
121         log(LogReceive, QString("%1 %2").arg(QString(type), showAppOnTop ? "true" : "false"));
122
123         emit showAppOnTopChanged(showAppOnTop);
124     } else if (type == "RELOADED") {
125
126         log(LogReceive, type);
127
128         emit reloaded();
129     } else if (type == "COLOR_CHANGED") {
130         QColor col;
131         ds >> col;
132
133         log(LogReceive, QString("%1 %2").arg(QString(type), col.name()));
134
135         emit selectedColorChanged(col);
136     } else if (type == "CONTEXT_PATH_UPDATED") {
137         QStringList contextPath;
138         ds >> contextPath;
139
140         log(LogReceive, QString("%1 %2").arg(QString(type), contextPath.join(", ")));
141
142         emit contextPathUpdated(contextPath);
143     }
144 }
145
146 QList<int> QmlJSObserverClient::currentObjects() const
147 {
148     return m_currentDebugIds;
149 }
150
151 void QmlJSObserverClient::setCurrentObjects(const QList<int> &debugIds) {
152     if (!m_connection || !m_connection->isConnected())
153         return;
154
155     if (debugIds == m_currentDebugIds)
156         return;
157
158     m_currentDebugIds = debugIds;
159
160     QByteArray message;
161     QDataStream ds(&message, QIODevice::WriteOnly);
162
163     QByteArray cmd = "SET_CURRENT_OBJECTS";
164     ds << cmd
165        << debugIds.length();
166
167     foreach (int id, debugIds) {
168         ds << id;
169     }
170
171     log(LogSend, QString("%1 %2 [list of ids]").arg(QString(cmd),
172                                                     QString::number(debugIds.length())));
173
174     sendMessage(message);
175 }
176
177 void recurseObjectIdList(const QDeclarativeDebugObjectReference &ref, QList<int> &debugIds, QList<QString> &objectIds)
178 {
179     debugIds << ref.debugId();
180     objectIds << ref.idString();
181     foreach(const QDeclarativeDebugObjectReference &child, ref.children()) {
182         recurseObjectIdList(child, debugIds, objectIds);
183     }
184 }
185
186 void QmlJSObserverClient::setObjectIdList(const QList<QDeclarativeDebugObjectReference> &objectRoots)
187 {
188     QByteArray message;
189     QDataStream ds(&message, QIODevice::WriteOnly);
190
191     QList<int> debugIds;
192     QList<QString> objectIds;
193
194     foreach(const QDeclarativeDebugObjectReference &ref, objectRoots) {
195         recurseObjectIdList(ref, debugIds, objectIds);
196     }
197
198     QByteArray cmd = "OBJECT_ID_LIST";
199     ds << cmd
200        << debugIds.length();
201
202     Q_ASSERT(debugIds.length() == objectIds.length());
203
204     for(int i = 0; i < debugIds.length(); ++i) {
205         ds << debugIds[i] << objectIds[i];
206     }
207
208     log(LogSend, QString("%1 %2 [list of debug / object ids]").arg(QString(cmd),
209                                                                    QString::number(debugIds.length())));
210
211     sendMessage(message);
212 }
213
214 void QmlJSObserverClient::setContextPathIndex(int contextPathIndex)
215 {
216     if (!m_connection || !m_connection->isConnected())
217         return;
218
219     QByteArray message;
220     QDataStream ds(&message, QIODevice::WriteOnly);
221
222     QByteArray cmd = "SET_CONTEXT_PATH_IDX";
223     ds << cmd
224        << contextPathIndex;
225
226     log(LogSend, QString("%1 %2").arg(QString(cmd), contextPathIndex));
227
228     sendMessage(message);
229 }
230
231 void QmlJSObserverClient::clearComponentCache()
232 {
233     if (!m_connection || !m_connection->isConnected())
234         return;
235
236     QByteArray message;
237     QDataStream ds(&message, QIODevice::WriteOnly);
238
239     QByteArray cmd = "CLEAR_COMPONENT_CACHE";
240     ds << cmd;
241
242     log(LogSend, cmd);
243
244     sendMessage(message);
245 }
246
247 void QmlJSObserverClient::reloadViewer()
248 {
249     if (!m_connection || !m_connection->isConnected())
250         return;
251
252     QByteArray message;
253     QDataStream ds(&message, QIODevice::WriteOnly);
254
255     QByteArray cmd = "RELOAD";
256     ds << cmd;
257
258     log(LogSend, cmd);
259
260     sendMessage(message);
261 }
262
263 void QmlJSObserverClient::setDesignModeBehavior(bool inDesignMode)
264 {
265     if (!m_connection || !m_connection->isConnected())
266         return;
267
268     QByteArray message;
269     QDataStream ds(&message, QIODevice::WriteOnly);
270
271     QByteArray cmd = "SET_DESIGN_MODE";
272     ds << cmd
273        << inDesignMode;
274
275     log(LogSend, QString("%1 %2").arg(QString(cmd), inDesignMode ? "true" : "false"));
276
277     sendMessage(message);
278 }
279
280 void QmlJSObserverClient::setAnimationSpeed(qreal slowdownFactor)
281 {
282     if (!m_connection || !m_connection->isConnected())
283         return;
284
285     QByteArray message;
286     QDataStream ds(&message, QIODevice::WriteOnly);
287
288     QByteArray cmd = "SET_ANIMATION_SPEED";
289     ds << cmd
290        << slowdownFactor;
291
292
293     log(LogSend, QString("%1 %2").arg(QString(cmd),  QString::number(slowdownFactor)));
294
295     sendMessage(message);
296 }
297
298 void QmlJSObserverClient::changeToColorPickerTool()
299 {
300     if (!m_connection || !m_connection->isConnected())
301         return;
302
303     QByteArray message;
304     QDataStream ds(&message, QIODevice::WriteOnly);
305
306     QByteArray cmd = "CHANGE_TOOL";
307     QByteArray tool = "COLOR_PICKER";
308     ds << cmd
309        << tool;
310
311     log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
312
313     sendMessage(message);
314 }
315
316 void QmlJSObserverClient::changeToSelectTool()
317 {
318     if (!m_connection || !m_connection->isConnected())
319         return;
320
321     QByteArray message;
322     QDataStream ds(&message, QIODevice::WriteOnly);
323
324     QByteArray cmd = "CHANGE_TOOL";
325     QByteArray tool = "SELECT";
326     ds << cmd
327        << tool;
328
329     log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
330
331     sendMessage(message);
332 }
333
334 void QmlJSObserverClient::changeToSelectMarqueeTool()
335 {
336     if (!m_connection || !m_connection->isConnected())
337         return;
338
339     QByteArray message;
340     QDataStream ds(&message, QIODevice::WriteOnly);
341
342     QByteArray cmd = "CHANGE_TOOL";
343     QByteArray tool = "SELECT_MARQUEE";
344     ds << cmd
345        << tool;
346
347     log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
348
349     sendMessage(message);
350 }
351
352 void QmlJSObserverClient::changeToZoomTool()
353 {
354     if (!m_connection || !m_connection->isConnected())
355         return;
356
357     QByteArray message;
358     QDataStream ds(&message, QIODevice::WriteOnly);
359
360     QByteArray cmd = "CHANGE_TOOL";
361     QByteArray tool = "ZOOM";
362     ds << cmd
363        << tool;
364
365     log(LogSend, QString("%1 %2").arg(QString(cmd), QString(tool)));
366
367     sendMessage(message);
368 }
369
370 void QmlJSObserverClient::showAppOnTop(bool showOnTop)
371 {
372     if (!m_connection || !m_connection->isConnected())
373         return;
374
375     QByteArray message;
376     QDataStream ds(&message, QIODevice::WriteOnly);
377
378     QByteArray cmd = "SHOW_APP_ON_TOP";
379     ds << showOnTop;
380
381     log(LogSend, QString("%1 %2").arg(QString(cmd), showOnTop ? "true" : "false"));
382
383     sendMessage(message);
384 }
385
386 void QmlJSObserverClient::createQmlObject(const QString &qmlText, int parentDebugId,
387                                              const QStringList &imports, const QString &filename)
388 {
389     if (!m_connection || !m_connection->isConnected())
390         return;
391
392     QByteArray message;
393     QDataStream ds(&message, QIODevice::WriteOnly);
394
395     QByteArray cmd = "CREATE_OBJECT";
396     ds << cmd
397        << qmlText
398        << parentDebugId
399        << imports
400        << filename;
401
402     log(LogSend, QString("%1 %2 %3 [%4] %5").arg(QString(cmd), qmlText, QString::number(parentDebugId),
403                                                imports.join(","), filename));
404
405     sendMessage(message);
406 }
407
408 void QmlJSObserverClient::destroyQmlObject(int debugId)
409 {
410     if (!m_connection || !m_connection->isConnected())
411         return;
412     QByteArray message;
413     QDataStream ds(&message, QIODevice::WriteOnly);
414
415     QByteArray cmd = "DESTROY_OBJECT";
416     ds << cmd << debugId;
417
418     log(LogSend, QString("%1 %2").arg(QString(cmd), debugId));
419
420     sendMessage(message);
421 }
422
423 void QmlJSObserverClient::reparentQmlObject(int debugId, int newParent)
424 {
425     if (!m_connection || !m_connection->isConnected())
426         return;
427     QByteArray message;
428     QDataStream ds(&message, QIODevice::WriteOnly);
429
430     QByteArray cmd = "MOVE_OBJECT";
431     ds << cmd
432        << debugId
433        << newParent;
434
435     log(LogSend, QString("%1 %2 %3").arg(QString(cmd), QString::number(debugId),
436                                          QString::number(newParent)));
437
438     sendMessage(message);
439 }
440
441
442 void QmlJSObserverClient::applyChangesToQmlFile()
443 {
444     if (!m_connection || !m_connection->isConnected())
445         return;
446
447     // TODO
448 }
449
450 void QmlJSObserverClient::applyChangesFromQmlFile()
451 {
452     if (!m_connection || !m_connection->isConnected())
453         return;
454
455     // TODO
456 }
457
458 void QmlJSObserverClient::log(LogDirection direction, const QString &message)
459 {
460     QString msg;
461     if (direction == LogSend) {
462         msg += " sending ";
463     } else {
464         msg += " receiving ";
465     }
466     msg += message;
467     emit logActivity(name(), msg);
468 }
469
470 } // namespace Internal
471 } // namespace QmlJSInspector