OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / instances / nodeinstanceclientproxy.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
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 #include "nodeinstanceclientproxy.h"
34
35 #include <QLocalSocket>
36 #include <QVariant>
37 #include <QCoreApplication>
38 #include <QStringList>
39
40 #include "nodeinstanceserver.h"
41 #include "previewnodeinstanceserver.h"
42 #include "rendernodeinstanceserver.h"
43
44 #include "propertyabstractcontainer.h"
45 #include "propertyvaluecontainer.h"
46 #include "propertybindingcontainer.h"
47 #include "instancecontainer.h"
48 #include "createinstancescommand.h"
49 #include "createscenecommand.h"
50 #include "changevaluescommand.h"
51 #include "changebindingscommand.h"
52 #include "changefileurlcommand.h"
53 #include "removeinstancescommand.h"
54 #include "clearscenecommand.h"
55 #include "removepropertiescommand.h"
56 #include "reparentinstancescommand.h"
57 #include "changeidscommand.h"
58 #include "changestatecommand.h"
59 #include "addimportcommand.h"
60 #include "completecomponentcommand.h"
61 #include "synchronizecommand.h"
62
63 #include "informationchangedcommand.h"
64 #include "pixmapchangedcommand.h"
65 #include "valueschangedcommand.h"
66 #include "childrenchangedcommand.h"
67 #include "imagecontainer.h"
68 #include "statepreviewimagechangedcommand.h"
69 #include "componentcompletedcommand.h"
70
71 namespace QmlDesigner {
72
73 NodeInstanceClientProxy::NodeInstanceClientProxy(QObject *parent)
74     : QObject(parent),
75       m_nodeInstanceServer(0),
76       m_blockSize(0),
77       m_synchronizeId(-1)
78 {
79     if (QCoreApplication::arguments().at(2) == QLatin1String("previewmode")) {
80         m_nodeInstanceServer = new PreviewNodeInstanceServer(this);
81     } else if (QCoreApplication::arguments().at(2) == QLatin1String("editormode")) {
82         m_nodeInstanceServer = new NodeInstanceServer(this);
83     } else if (QCoreApplication::arguments().at(2) == QLatin1String("rendermode")) {
84         m_nodeInstanceServer = new RenderNodeInstanceServer(this);
85     }
86
87     m_socket = new QLocalSocket(this);
88     connect(m_socket, SIGNAL(readyRead()), this, SLOT(readDataStream()));
89     connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), QCoreApplication::instance(), SLOT(quit()));
90     connect(m_socket, SIGNAL(disconnected()), QCoreApplication::instance(), SLOT(quit()));
91     m_socket->connectToServer(QCoreApplication::arguments().at(1), QIODevice::ReadWrite | QIODevice::Unbuffered);
92     m_socket->waitForConnected(-1);
93 }
94
95 void NodeInstanceClientProxy::writeCommand(const QVariant &command)
96 {
97     QByteArray block;
98     QDataStream out(&block, QIODevice::WriteOnly);
99     out << quint32(0);
100     out << command;
101     out.device()->seek(0);
102     out << quint32(block.size() - sizeof(quint32));
103
104     m_socket->write(block);
105 }
106
107 void NodeInstanceClientProxy::informationChanged(const InformationChangedCommand &command)
108 {
109     writeCommand(QVariant::fromValue(command));
110 }
111
112 void NodeInstanceClientProxy::valuesChanged(const ValuesChangedCommand &command)
113 {
114     writeCommand(QVariant::fromValue(command));
115 }
116
117 void NodeInstanceClientProxy::pixmapChanged(const PixmapChangedCommand &command)
118 {
119     writeCommand(QVariant::fromValue(command));
120 }
121
122 void NodeInstanceClientProxy::childrenChanged(const ChildrenChangedCommand &command)
123 {
124     writeCommand(QVariant::fromValue(command));
125 }
126
127 void NodeInstanceClientProxy::statePreviewImagesChanged(const StatePreviewImageChangedCommand &command)
128 {
129     writeCommand(QVariant::fromValue(command));
130 }
131
132 void NodeInstanceClientProxy::componentCompleted(const ComponentCompletedCommand &command)
133 {
134     writeCommand(QVariant::fromValue(command));
135 }
136
137 void NodeInstanceClientProxy::flush()
138 {
139 }
140
141 void NodeInstanceClientProxy::synchronizeWithClientProcess()
142 {
143     if (m_synchronizeId >= 0) {
144         SynchronizeCommand synchronizeCommand(m_synchronizeId);
145         writeCommand(QVariant::fromValue(synchronizeCommand));
146     }
147 }
148
149 qint64 NodeInstanceClientProxy::bytesToWrite() const
150 {
151     return m_socket->bytesToWrite();
152 }
153
154 void NodeInstanceClientProxy::readDataStream()
155 {
156     QList<QVariant> commandList;
157
158     while (!m_socket->atEnd()) {
159         if (m_socket->bytesAvailable() < int(sizeof(quint32)))
160             break;
161
162         QDataStream in(m_socket);
163
164         if (m_blockSize == 0) {
165             in >> m_blockSize;
166         }
167
168         if (m_socket->bytesAvailable() < m_blockSize)
169             break;
170
171         QVariant command;
172         in >> command;
173         m_blockSize = 0;
174
175         Q_ASSERT(in.status() == QDataStream::Ok);
176
177         commandList.append(command);
178     }
179
180     foreach (const QVariant &command, commandList) {
181         dispatchCommand(command);
182     }
183 }
184
185 NodeInstanceServerInterface *NodeInstanceClientProxy::nodeInstanceServer() const
186 {
187     return m_nodeInstanceServer;
188 }
189
190 void NodeInstanceClientProxy::createInstances(const CreateInstancesCommand &command)
191 {
192     nodeInstanceServer()->createInstances(command);
193 }
194
195 void NodeInstanceClientProxy::changeFileUrl(const ChangeFileUrlCommand &command)
196 {
197     nodeInstanceServer()->changeFileUrl(command);
198 }
199
200 void NodeInstanceClientProxy::createScene(const CreateSceneCommand &command)
201 {
202     nodeInstanceServer()->createScene(command);
203 }
204
205 void NodeInstanceClientProxy::clearScene(const ClearSceneCommand &command)
206 {
207     nodeInstanceServer()->clearScene(command);
208 }
209
210 void NodeInstanceClientProxy::removeInstances(const RemoveInstancesCommand &command)
211 {
212     nodeInstanceServer()->removeInstances(command);
213 }
214
215 void NodeInstanceClientProxy::removeProperties(const RemovePropertiesCommand &command)
216 {
217     nodeInstanceServer()->removeProperties(command);
218 }
219
220 void NodeInstanceClientProxy::changePropertyBindings(const ChangeBindingsCommand &command)
221 {
222     nodeInstanceServer()->changePropertyBindings(command);
223 }
224
225 void NodeInstanceClientProxy::changePropertyValues(const ChangeValuesCommand &command)
226 {
227     nodeInstanceServer()->changePropertyValues(command);
228 }
229
230 void NodeInstanceClientProxy::reparentInstances(const ReparentInstancesCommand &command)
231 {
232     nodeInstanceServer()->reparentInstances(command);
233 }
234
235 void NodeInstanceClientProxy::changeIds(const ChangeIdsCommand &command)
236 {
237     nodeInstanceServer()->changeIds(command);
238 }
239
240 void NodeInstanceClientProxy::changeState(const ChangeStateCommand &command)
241 {
242     nodeInstanceServer()->changeState(command);
243 }
244
245 void NodeInstanceClientProxy::addImport(const AddImportCommand &command)
246 {
247     nodeInstanceServer()->addImport(command);
248 }
249
250 void NodeInstanceClientProxy::completeComponent(const CompleteComponentCommand &command)
251 {
252     nodeInstanceServer()->completeComponent(command);
253 }
254
255 void NodeInstanceClientProxy::dispatchCommand(const QVariant &command)
256 {
257     static const int createInstancesCommandType = QMetaType::type("CreateInstancesCommand");
258     static const int changeFileUrlCommandType = QMetaType::type("ChangeFileUrlCommand");
259     static const int createSceneCommandType = QMetaType::type("CreateSceneCommand");
260     static const int clearSceneCommandType = QMetaType::type("ClearSceneCommand");
261     static const int removeInstancesCommandType = QMetaType::type("RemoveInstancesCommand");
262     static const int removePropertiesCommandType = QMetaType::type("RemovePropertiesCommand");
263     static const int changeBindingsCommandType = QMetaType::type("ChangeBindingsCommand");
264     static const int changeValuesCommandType = QMetaType::type("ChangeValuesCommand");
265     static const int reparentInstancesCommandType = QMetaType::type("ReparentInstancesCommand");
266     static const int changeIdsCommandType = QMetaType::type("ChangeIdsCommand");
267     static const int changeStateCommandType = QMetaType::type("ChangeStateCommand");
268     static const int addImportCommandType = QMetaType::type("AddImportCommand");
269     static const int completeComponentCommandType = QMetaType::type("CompleteComponentCommand");
270     static const int synchronizeCommandType = QMetaType::type("SynchronizeCommand");
271
272     if (command.userType() ==  createInstancesCommandType) {
273         createInstances(command.value<CreateInstancesCommand>());
274     } else if (command.userType() ==  changeFileUrlCommandType)
275         changeFileUrl(command.value<ChangeFileUrlCommand>());
276     else if (command.userType() ==  createSceneCommandType)
277         createScene(command.value<CreateSceneCommand>());
278     else if (command.userType() ==  clearSceneCommandType)
279         clearScene(command.value<ClearSceneCommand>());
280     else if (command.userType() ==  removeInstancesCommandType)
281         removeInstances(command.value<RemoveInstancesCommand>());
282     else if (command.userType() ==  removePropertiesCommandType)
283         removeProperties(command.value<RemovePropertiesCommand>());
284     else if (command.userType() ==  changeBindingsCommandType)
285         changePropertyBindings(command.value<ChangeBindingsCommand>());
286     else if (command.userType() ==  changeValuesCommandType)
287         changePropertyValues(command.value<ChangeValuesCommand>());
288     else if (command.userType() ==  reparentInstancesCommandType)
289         reparentInstances(command.value<ReparentInstancesCommand>());
290     else if (command.userType() ==  changeIdsCommandType)
291         changeIds(command.value<ChangeIdsCommand>());
292     else if (command.userType() ==  changeStateCommandType)
293         changeState(command.value<ChangeStateCommand>());
294     else if (command.userType() ==  addImportCommandType)
295         addImport(command.value<AddImportCommand>());
296     else if (command.userType() ==  completeComponentCommandType)
297         completeComponent(command.value<CompleteComponentCommand>());
298     else if (command.userType() == synchronizeCommandType) {
299         SynchronizeCommand synchronizeCommand = command.value<SynchronizeCommand>();
300         m_synchronizeId = synchronizeCommand.synchronizeId();
301     } else
302         Q_ASSERT(false);
303 }
304 } // namespace QmlDesigner