OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / qdeclarativeobserverservice.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 "qdeclarativeobserverservice.h"
43
44 #include <QStringList>
45 #include <QColor>
46
47 #include <QDebug>
48
49 namespace QmlJSDebugger {
50
51 Q_GLOBAL_STATIC(QDeclarativeObserverService, serviceInstance)
52
53 QDeclarativeObserverService::QDeclarativeObserverService()
54     : QDeclarativeDebugService(QLatin1String("QDeclarativeObserverMode"))
55 {
56 }
57
58 QDeclarativeObserverService *QDeclarativeObserverService::instance()
59 {
60     return serviceInstance();
61 }
62
63 void QDeclarativeObserverService::statusChanged(Status status)
64 {
65     emit debuggingClientChanged((status == Enabled));
66 }
67
68 void QDeclarativeObserverService::messageReceived(const QByteArray &message)
69 {
70     QDataStream ds(message);
71
72     QByteArray type;
73     ds >> type;
74
75     if (type == "SET_CURRENT_OBJECTS") {
76         int itemCount = 0;
77         ds >> itemCount;
78
79         QList<QObject*> selectedObjects;
80         for(int i = 0; i < itemCount; ++i) {
81             int debugId = -1;
82             ds >> debugId;
83             QObject *obj = objectForId(debugId);
84
85             if (obj)
86                 selectedObjects << obj;
87         }
88
89         emit currentObjectsChanged(selectedObjects);
90
91     } else if (type == "RELOAD") {
92         emit reloadRequested();
93     } else if (type == "SET_ANIMATION_SPEED") {
94         qreal speed;
95         ds >> speed;
96         emit animationSpeedChangeRequested(speed);
97     } else if (type == "CHANGE_TOOL") {
98         QByteArray toolName;
99         ds >> toolName;
100         if (toolName == "COLOR_PICKER") {
101             emit colorPickerToolRequested();
102         } else if (toolName == "SELECT") {
103             emit selectToolRequested();
104         } else if (toolName == "SELECT_MARQUEE") {
105             emit selectMarqueeToolRequested();
106         } else if (toolName == "ZOOM") {
107             emit zoomToolRequested();
108         }
109     } else if (type == "SET_DESIGN_MODE") {
110         bool inDesignMode;
111         ds >> inDesignMode;
112         emit designModeBehaviorChanged(inDesignMode);
113     } else if (type == "SHOW_APP_ON_TOP") {
114         bool showOnTop;
115         ds >> showOnTop;
116         emit showAppOnTopChanged(showOnTop);
117     } else if (type == "CREATE_OBJECT") {
118         QString qml;
119         int parentId;
120         QString filename;
121         QStringList imports;
122         ds >> qml >> parentId >> imports >> filename;
123         emit objectCreationRequested(qml, objectForId(parentId), imports, filename);
124     } else if (type == "DESTROY_OBJECT") {
125         int debugId;
126         ds >> debugId;
127         if (QObject* obj = objectForId(debugId))
128             obj->deleteLater();
129     } else if (type == "MOVE_OBJECT") {
130         int debugId, newParent;
131         ds >> debugId >> newParent;
132         emit objectReparentRequested(objectForId(debugId), objectForId(newParent));
133     } else if (type == "OBJECT_ID_LIST") {
134         int itemCount;
135         ds >> itemCount;
136         m_stringIdForObjectId.clear();
137         for(int i = 0; i < itemCount; ++i) {
138             int itemDebugId;
139             QString itemIdString;
140             ds >> itemDebugId
141                >> itemIdString;
142
143             m_stringIdForObjectId.insert(itemDebugId, itemIdString);
144         }
145     } else if (type == "SET_CONTEXT_PATH_IDX") {
146         int contextPathIndex;
147         ds >> contextPathIndex;
148         emit contextPathIndexChanged(contextPathIndex);
149     } else if (type == "CLEAR_COMPONENT_CACHE") {
150         emit clearComponentCacheRequested();
151     }
152 }
153
154 void QDeclarativeObserverService::setDesignModeBehavior(bool inDesignMode)
155 {
156     QByteArray message;
157     QDataStream ds(&message, QIODevice::WriteOnly);
158
159     ds << QByteArray("SET_DESIGN_MODE")
160        << inDesignMode;
161
162     sendMessage(message);
163 }
164
165 void QDeclarativeObserverService::setCurrentObjects(QList<QObject*> objects)
166 {
167     QByteArray message;
168     QDataStream ds(&message, QIODevice::WriteOnly);
169
170     ds << QByteArray("CURRENT_OBJECTS_CHANGED")
171        << objects.length();
172
173     foreach(QObject *object, objects) {
174         int id = idForObject(object);
175         ds << id;
176     }
177
178     sendMessage(message);
179 }
180
181 void QDeclarativeObserverService::setCurrentTool(QmlJSDebugger::Constants::DesignTool toolId)
182 {
183     QByteArray message;
184     QDataStream ds(&message, QIODevice::WriteOnly);
185
186     ds << QByteArray("TOOL_CHANGED")
187        << toolId;
188
189     sendMessage(message);
190 }
191
192 void QDeclarativeObserverService::setAnimationSpeed(qreal slowdownFactor)
193 {
194
195     QByteArray message;
196     QDataStream ds(&message, QIODevice::WriteOnly);
197
198     ds << QByteArray("ANIMATION_SPEED_CHANGED")
199        << slowdownFactor;
200
201     sendMessage(message);
202 }
203
204 void QDeclarativeObserverService::reloaded()
205 {
206     QByteArray message;
207     QDataStream ds(&message, QIODevice::WriteOnly);
208
209     ds << QByteArray("RELOADED");
210
211     sendMessage(message);
212 }
213
214 void QDeclarativeObserverService::setShowAppOnTop(bool showAppOnTop)
215 {
216     QByteArray message;
217     QDataStream ds(&message, QIODevice::WriteOnly);
218
219     ds << QByteArray("SHOW_APP_ON_TOP") << showAppOnTop;
220
221     sendMessage(message);
222 }
223
224 void QDeclarativeObserverService::selectedColorChanged(const QColor &color)
225 {
226     QByteArray message;
227     QDataStream ds(&message, QIODevice::WriteOnly);
228
229     ds << QByteArray("COLOR_CHANGED")
230        << color;
231
232     sendMessage(message);
233 }
234
235 void QDeclarativeObserverService::contextPathUpdated(const QStringList &contextPath)
236 {
237     QByteArray message;
238     QDataStream ds(&message, QIODevice::WriteOnly);
239
240     ds << QByteArray("CONTEXT_PATH_UPDATED")
241        << contextPath;
242
243     sendMessage(message);
244 }
245
246 QString QDeclarativeObserverService::idStringForObject(QObject *obj) const
247 {
248     int id = idForObject(obj);
249     QString idString = m_stringIdForObjectId.value(id, QString());
250     return idString;
251 }
252
253 void QDeclarativeObserverService::sendMessage(const QByteArray &message)
254 {
255     if (status() != Enabled)
256         return;
257
258     QDeclarativeDebugService::sendMessage(message);
259 }
260
261 } // namespace QmlJSDebugger