OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / qdeclarativeobserverservice.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 "qdeclarativeobserverservice.h"
33
34 #include <observerprotocol.h>
35
36 #include <QStringList>
37 #include <QColor>
38
39 namespace QmlJSDebugger {
40
41 Q_GLOBAL_STATIC(QDeclarativeObserverService, serviceInstance)
42
43 QDeclarativeObserverService::QDeclarativeObserverService()
44     : QDeclarativeDebugService(QLatin1String("QDeclarativeObserverMode"))
45 {
46 }
47
48 QDeclarativeObserverService *QDeclarativeObserverService::instance()
49 {
50     return serviceInstance();
51 }
52
53 void QDeclarativeObserverService::statusChanged(Status status)
54 {
55     emit debuggingClientChanged((status == Enabled));
56 }
57
58 void QDeclarativeObserverService::messageReceived(const QByteArray &message)
59 {
60     QDataStream ds(message);
61
62     ObserverProtocol::Message type;
63     ds >> type;
64
65     switch (type) {
66     case ObserverProtocol::SetCurrentObjects: {
67         int itemCount = 0;
68         ds >> itemCount;
69
70         QList<QObject*> selectedObjects;
71         for (int i = 0; i < itemCount; ++i) {
72             int debugId = -1;
73             ds >> debugId;
74             QObject *obj = objectForId(debugId);
75
76             if (obj)
77                 selectedObjects << obj;
78         }
79
80         emit currentObjectsChanged(selectedObjects);
81         break;
82     }
83     case ObserverProtocol::Reload: {
84         emit reloadRequested();
85         break;
86     }
87     case ObserverProtocol::SetAnimationSpeed: {
88         qreal speed;
89         ds >> speed;
90         emit animationSpeedChangeRequested(speed);
91         break;
92     }
93     case ObserverProtocol::SetAnimationPaused: {
94         bool paused;
95         ds >> paused;
96         emit executionPauseChangeRequested(paused);
97         break;
98     }
99     case ObserverProtocol::ChangeTool: {
100         ObserverProtocol::Tool tool;
101         ds >> tool;
102         switch (tool) {
103         case ObserverProtocol::ColorPickerTool:
104             emit colorPickerToolRequested();
105             break;
106         case ObserverProtocol::SelectTool:
107             emit selectToolRequested();
108             break;
109         case ObserverProtocol::SelectMarqueeTool:
110             emit selectMarqueeToolRequested();
111             break;
112         case ObserverProtocol::ZoomTool:
113             emit zoomToolRequested();
114             break;
115         default:
116             qWarning() << "Warning: Unhandled tool:" << tool;
117         }
118         break;
119     }
120     case ObserverProtocol::SetDesignMode: {
121         bool inDesignMode;
122         ds >> inDesignMode;
123         emit designModeBehaviorChanged(inDesignMode);
124         break;
125     }
126     case ObserverProtocol::ShowAppOnTop: {
127         bool showOnTop;
128         ds >> showOnTop;
129         emit showAppOnTopChanged(showOnTop);
130         break;
131     }
132     case ObserverProtocol::CreateObject: {
133         QString qml;
134         int parentId;
135         QString filename;
136         QStringList imports;
137         ds >> qml >> parentId >> imports >> filename;
138         emit objectCreationRequested(qml, objectForId(parentId), imports, filename);
139         break;
140     }
141     case ObserverProtocol::DestroyObject: {
142         int debugId;
143         ds >> debugId;
144         if (QObject* obj = objectForId(debugId))
145             obj->deleteLater();
146         break;
147     }
148     case ObserverProtocol::MoveObject: {
149         int debugId, newParent;
150         ds >> debugId >> newParent;
151         emit objectReparentRequested(objectForId(debugId), objectForId(newParent));
152         break;
153     }
154     case ObserverProtocol::ObjectIdList: {
155         int itemCount;
156         ds >> itemCount;
157         m_stringIdForObjectId.clear();
158         for (int i = 0; i < itemCount; ++i) {
159             int itemDebugId;
160             QString itemIdString;
161             ds >> itemDebugId
162                >> itemIdString;
163
164             m_stringIdForObjectId.insert(itemDebugId, itemIdString);
165         }
166         break;
167     }
168     case ObserverProtocol::SetContextPathIdx: {
169         int contextPathIndex;
170         ds >> contextPathIndex;
171         emit contextPathIndexChanged(contextPathIndex);
172         break;
173     }
174     case ObserverProtocol::ClearComponentCache: {
175         emit clearComponentCacheRequested();
176         break;
177     }
178     default:
179         qWarning() << "Warning: Not handling message:" << type;
180     }
181 }
182
183 void QDeclarativeObserverService::setDesignModeBehavior(bool inDesignMode)
184 {
185     QByteArray message;
186     QDataStream ds(&message, QIODevice::WriteOnly);
187
188     ds << ObserverProtocol::SetDesignMode
189        << inDesignMode;
190
191     sendMessage(message);
192 }
193
194 void QDeclarativeObserverService::setCurrentObjects(QList<QObject*> objects)
195 {
196     QByteArray message;
197     QDataStream ds(&message, QIODevice::WriteOnly);
198
199     ds << ObserverProtocol::CurrentObjectsChanged
200        << objects.length();
201
202     foreach (QObject *object, objects) {
203         int id = idForObject(object);
204         ds << id;
205     }
206
207     sendMessage(message);
208 }
209
210 void QDeclarativeObserverService::setCurrentTool(QmlJSDebugger::Constants::DesignTool toolId)
211 {
212     QByteArray message;
213     QDataStream ds(&message, QIODevice::WriteOnly);
214
215     ds << ObserverProtocol::ToolChanged
216        << toolId;
217
218     sendMessage(message);
219 }
220
221 void QDeclarativeObserverService::setAnimationSpeed(qreal slowDownFactor)
222 {
223     QByteArray message;
224     QDataStream ds(&message, QIODevice::WriteOnly);
225
226     ds << ObserverProtocol::AnimationSpeedChanged
227        << slowDownFactor;
228
229     sendMessage(message);
230 }
231
232 void QDeclarativeObserverService::setAnimationPaused(bool paused)
233 {
234     QByteArray message;
235     QDataStream ds(&message, QIODevice::WriteOnly);
236
237     ds << ObserverProtocol::AnimationPausedChanged
238        << paused;
239
240     sendMessage(message);
241 }
242
243 void QDeclarativeObserverService::reloaded()
244 {
245     QByteArray message;
246     QDataStream ds(&message, QIODevice::WriteOnly);
247
248     ds << ObserverProtocol::Reloaded;
249
250     sendMessage(message);
251 }
252
253 void QDeclarativeObserverService::setShowAppOnTop(bool showAppOnTop)
254 {
255     QByteArray message;
256     QDataStream ds(&message, QIODevice::WriteOnly);
257
258     ds << ObserverProtocol::ShowAppOnTop << showAppOnTop;
259
260     sendMessage(message);
261 }
262
263 void QDeclarativeObserverService::selectedColorChanged(const QColor &color)
264 {
265     QByteArray message;
266     QDataStream ds(&message, QIODevice::WriteOnly);
267
268     ds << ObserverProtocol::ColorChanged
269        << color;
270
271     sendMessage(message);
272 }
273
274 void QDeclarativeObserverService::contextPathUpdated(const QStringList &contextPath)
275 {
276     QByteArray message;
277     QDataStream ds(&message, QIODevice::WriteOnly);
278
279     ds << ObserverProtocol::ContextPathUpdated
280        << contextPath;
281
282     sendMessage(message);
283 }
284
285 QString QDeclarativeObserverService::idStringForObject(QObject *obj) const
286 {
287     int id = idForObject(obj);
288     QString idString = m_stringIdForObjectId.value(id, QString());
289     return idString;
290 }
291
292 void QDeclarativeObserverService::sendMessage(const QByteArray &message)
293 {
294     if (status() != Enabled)
295         return;
296
297     QDeclarativeDebugService::sendMessage(message);
298 }
299
300 } // namespace QmlJSDebugger