OSDN Git Service

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