1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "nodeinstanceclientproxy.h"
35 #include <QLocalSocket>
37 #include <QCoreApplication>
38 #include <QStringList>
40 #include "nodeinstanceserver.h"
41 #include "previewnodeinstanceserver.h"
42 #include "rendernodeinstanceserver.h"
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"
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"
71 namespace QmlDesigner {
73 NodeInstanceClientProxy::NodeInstanceClientProxy(QObject *parent)
75 m_nodeInstanceServer(0),
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);
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);
95 void NodeInstanceClientProxy::writeCommand(const QVariant &command)
98 QDataStream out(&block, QIODevice::WriteOnly);
101 out.device()->seek(0);
102 out << quint32(block.size() - sizeof(quint32));
104 m_socket->write(block);
107 void NodeInstanceClientProxy::informationChanged(const InformationChangedCommand &command)
109 writeCommand(QVariant::fromValue(command));
112 void NodeInstanceClientProxy::valuesChanged(const ValuesChangedCommand &command)
114 writeCommand(QVariant::fromValue(command));
117 void NodeInstanceClientProxy::pixmapChanged(const PixmapChangedCommand &command)
119 writeCommand(QVariant::fromValue(command));
122 void NodeInstanceClientProxy::childrenChanged(const ChildrenChangedCommand &command)
124 writeCommand(QVariant::fromValue(command));
127 void NodeInstanceClientProxy::statePreviewImagesChanged(const StatePreviewImageChangedCommand &command)
129 writeCommand(QVariant::fromValue(command));
132 void NodeInstanceClientProxy::componentCompleted(const ComponentCompletedCommand &command)
134 writeCommand(QVariant::fromValue(command));
137 void NodeInstanceClientProxy::flush()
141 void NodeInstanceClientProxy::synchronizeWithClientProcess()
143 if (m_synchronizeId >= 0) {
144 SynchronizeCommand synchronizeCommand(m_synchronizeId);
145 writeCommand(QVariant::fromValue(synchronizeCommand));
149 qint64 NodeInstanceClientProxy::bytesToWrite() const
151 return m_socket->bytesToWrite();
154 void NodeInstanceClientProxy::readDataStream()
156 QList<QVariant> commandList;
158 while (!m_socket->atEnd()) {
159 if (m_socket->bytesAvailable() < int(sizeof(quint32)))
162 QDataStream in(m_socket);
164 if (m_blockSize == 0) {
168 if (m_socket->bytesAvailable() < m_blockSize)
175 Q_ASSERT(in.status() == QDataStream::Ok);
177 commandList.append(command);
180 foreach (const QVariant &command, commandList) {
181 dispatchCommand(command);
185 NodeInstanceServerInterface *NodeInstanceClientProxy::nodeInstanceServer() const
187 return m_nodeInstanceServer;
190 void NodeInstanceClientProxy::createInstances(const CreateInstancesCommand &command)
192 nodeInstanceServer()->createInstances(command);
195 void NodeInstanceClientProxy::changeFileUrl(const ChangeFileUrlCommand &command)
197 nodeInstanceServer()->changeFileUrl(command);
200 void NodeInstanceClientProxy::createScene(const CreateSceneCommand &command)
202 nodeInstanceServer()->createScene(command);
205 void NodeInstanceClientProxy::clearScene(const ClearSceneCommand &command)
207 nodeInstanceServer()->clearScene(command);
210 void NodeInstanceClientProxy::removeInstances(const RemoveInstancesCommand &command)
212 nodeInstanceServer()->removeInstances(command);
215 void NodeInstanceClientProxy::removeProperties(const RemovePropertiesCommand &command)
217 nodeInstanceServer()->removeProperties(command);
220 void NodeInstanceClientProxy::changePropertyBindings(const ChangeBindingsCommand &command)
222 nodeInstanceServer()->changePropertyBindings(command);
225 void NodeInstanceClientProxy::changePropertyValues(const ChangeValuesCommand &command)
227 nodeInstanceServer()->changePropertyValues(command);
230 void NodeInstanceClientProxy::reparentInstances(const ReparentInstancesCommand &command)
232 nodeInstanceServer()->reparentInstances(command);
235 void NodeInstanceClientProxy::changeIds(const ChangeIdsCommand &command)
237 nodeInstanceServer()->changeIds(command);
240 void NodeInstanceClientProxy::changeState(const ChangeStateCommand &command)
242 nodeInstanceServer()->changeState(command);
245 void NodeInstanceClientProxy::addImport(const AddImportCommand &command)
247 nodeInstanceServer()->addImport(command);
250 void NodeInstanceClientProxy::completeComponent(const CompleteComponentCommand &command)
252 nodeInstanceServer()->completeComponent(command);
255 void NodeInstanceClientProxy::dispatchCommand(const QVariant &command)
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");
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();
304 } // namespace QmlDesigner