1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "qt5nodeinstanceserver.h"
29 #include "tokencommand.h"
30 #include "valueschangedcommand.h"
31 #include "changeselectioncommand.h"
32 #include "requestmodelnodepreviewimagecommand.h"
33 #include "propertybindingcontainer.h"
34 #include "propertyabstractcontainer.h"
35 
36 #include <QTimer>
37 #include <QVariant>
38 #include <QPointer>
39 #include <QImage>
40 #include <QUrl>
41 
42 QT_BEGIN_NAMESPACE
43 class QDragMoveEvent;
44 QT_END_NAMESPACE
45 
46 namespace QmlDesigner {
47 
48 class Qt5InformationNodeInstanceServer : public Qt5NodeInstanceServer
49 {
50     Q_OBJECT
51 
52 public:
53     explicit Qt5InformationNodeInstanceServer(NodeInstanceClientInterface *nodeInstanceClient);
54     ~Qt5InformationNodeInstanceServer() override;
55 
56     void reparentInstances(const ReparentInstancesCommand &command) override;
57     void clearScene(const ClearSceneCommand &command) override;
58     void update3DViewState(const Update3dViewStateCommand &command) override;
59     void createScene(const CreateSceneCommand &command) override;
60     void completeComponent(const CompleteComponentCommand &command) override;
61     void token(const TokenCommand &command) override;
62     void removeSharedMemory(const RemoveSharedMemoryCommand &command) override;
63     void changeSelection(const ChangeSelectionCommand &command) override;
64     void changePropertyValues(const ChangeValuesCommand &command) override;
65     void removeInstances(const RemoveInstancesCommand &command) override;
66     void inputEvent(const InputEventCommand &command) override;
67     void view3DAction(const View3DActionCommand &command) override;
68     void requestModelNodePreviewImage(const RequestModelNodePreviewImageCommand &command) override;
69     void changeAuxiliaryValues(const ChangeAuxiliaryCommand &command) override;
70     void changePropertyBindings(const ChangeBindingsCommand &command) override;
71     void changeIds(const ChangeIdsCommand &command) override;
72     void changeState(const ChangeStateCommand &command) override;
73     void removeProperties(const RemovePropertiesCommand &command) override;
74 
75     void handleInstanceLocked(const ServerNodeInstance &instance, bool enable, bool checkAncestors) override;
76     void handleInstanceHidden(const ServerNodeInstance &instance, bool enable, bool checkAncestors) override;
77 
78     bool isInformationServer() const override;
79 
80 private slots:
81     void handleSelectionChanged(const QVariant &objs);
82     void handleObjectPropertyCommit(const QVariant &objects, const QVariant &propNames);
83     void handleObjectPropertyChange(const QVariant &objects, const QVariant &propNames);
84     void handleActiveSceneChange();
85     void handleToolStateChanged(const QString &sceneId, const QString &tool,
86                                 const QVariant &toolState);
87     void handleView3DSizeChange();
88     void handleView3DDestroyed(QObject *obj);
89     void handleNode3DDestroyed(QObject *obj);
90 
91 protected:
92     void collectItemChangesAndSendChangeCommands() override;
93     void sendChildrenChangedCommand(const QList<ServerNodeInstance> &childList);
94     void sendTokenBack();
95     bool isDirtyRecursiveForNonInstanceItems(QQuickItem *item) const;
96     bool isDirtyRecursiveForParentInstances(QQuickItem *item) const;
97     void selectInstances(const QList<ServerNodeInstance> &instanceList);
98     void modifyProperties(const QVector<InstancePropertyValueTriple> &properties);
99     QList<ServerNodeInstance> createInstances(const QVector<InstanceContainer> &container) override;
100     void initializeAuxiliaryViews() override;
101 
102 private:
103     void handleObjectPropertyChangeTimeout();
104     void handleSelectionChangeTimeout();
105     void createEditView3D();
106     void create3DPreviewView();
107     void setup3DEditView(const QList<ServerNodeInstance> &instanceList,
108                          const QHash<QString, QVariantMap> &toolStates);
109     void createCameraAndLightGizmos(const QList<ServerNodeInstance> &instanceList) const;
110     void add3DViewPorts(const QList<ServerNodeInstance> &instanceList);
111     void add3DScenes(const QList<ServerNodeInstance> &instanceList);
112     QObject *findView3DForInstance(const ServerNodeInstance &instance) const;
113     QObject *findView3DForSceneRoot(QObject *sceneRoot) const;
114     QObject *find3DSceneRoot(const ServerNodeInstance &instance) const;
115     QObject *find3DSceneRoot(QObject *obj) const;
116     QVector<InstancePropertyValueTriple> propertyToPropertyValueTriples(
117             const ServerNodeInstance &instance,
118             const PropertyName &propertyName,
119             const QVariant &variant);
120     void modifyVariantValue(const QObjectList &objects,
121                             const QList<PropertyName> &propNames,
122                             ValuesModifiedCommand::TransactionOption option);
123     void updateView3DRect(QObject *view3D);
124     void updateActiveSceneToEditView3D();
125     void removeNode3D(QObject *node);
126     void resolveSceneRoots();
127     ServerNodeInstance active3DSceneInstance() const;
128     void updateNodesRecursive(QQuickItem *item);
129     QQuickItem *getContentItemForRendering(QQuickItem *rootItem);
130     void render3DEditView(int count = 1);
131     void doRender3DEditView();
132     void renderModelNodeImageView();
133     void doRenderModelNodeImageView();
134     void doRenderModelNode3DImageView();
135     void doRenderModelNode2DImageView();
136     void updateLockedAndHiddenStates(const QSet<ServerNodeInstance> &instances);
137     void handleInputEvents();
138     void resolveImportSupport();
139     void updateRotationBlocks(const QVector<PropertyValueContainer> &valueChanges);
140     void removeRotationBlocks(const QVector<qint32> &instanceIds);
141 
142     void createAuxiliaryQuickView(const QUrl &url, RenderViewData &viewData);
143 
144     RenderViewData m_editView3DData;
145     RenderViewData m_modelNode3DImageViewData;
146     RenderViewData m_modelNode2DImageViewData;
147 
148     bool m_editView3DSetupDone = false;
149     RequestModelNodePreviewImageCommand m_modelNodePreviewImageCommand;
150     QHash<QString, QImage> m_modelNodePreviewImageCache;
151     QSet<QObject *> m_view3Ds;
152     QMultiHash<QObject *, QObject *> m_3DSceneMap; // key: scene root, value: node
153     QObject *m_active3DView = nullptr;
154     QObject *m_active3DScene = nullptr;
155     bool m_active3DSceneUpdatePending = false;
156     QSet<ServerNodeInstance> m_parentChangedSet;
157     QList<ServerNodeInstance> m_completedComponentList;
158     QList<TokenCommand> m_tokenList;
159     QTimer m_propertyChangeTimer;
160     QTimer m_selectionChangeTimer;
161     QTimer m_render3DEditViewTimer;
162     QTimer m_renderModelNodeImageViewTimer;
163     QTimer m_inputEventTimer;
164     QObjectList m_changedNodes;
165     QList<PropertyName> m_changedProperties;
166     ChangeSelectionCommand m_lastSelectionChangeCommand;
167     QList<InputEventCommand> m_pendingInputEventCommands;
168     QObject *m_3dHelper = nullptr;
169     int m_need3DEditViewRender = 0;
170 };
171 
172 } // namespace QmlDesigner
173