Debian Codegraph @

src/Gui/ViewProviderLink.h

raw
1 /****************************************************************************
2 * Copyright (c) 2017 Zheng Lei (realthunder) <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ****************************************************************************/
22
23 #ifndef SRC_GUI_VIEWPROVIDER_LINK_H_
24 #define SRC_GUI_VIEWPROVIDER_LINK_H_
25
26 #include <App/Link.h>
27 #include <unordered_map>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <memory>
32
33 #include "Selection/SoFCUnifiedSelection.h"
34 #include "ViewProviderDocumentObject.h"
35 #include "ViewProviderExtension.h"
36 #include "ViewProviderFeaturePython.h"
37
38
39 class SoBase;
40 class SoDragger;
41 class SoMaterialBinding;
42 class SoPickStyle;
43
44 namespace Gui
45 {
46
47 class LinkInfo;
48 using LinkInfoPtr = boost::intrusive_ptr<LinkInfo>;
49
50 #if defined(_MSC_VER)
51 // forward declaration to please MSVC
52 void intrusive_ptr_add_ref(Gui::LinkInfo* px);
53 void intrusive_ptr_release(Gui::LinkInfo* px);
54 #endif
55
56 class GuiExport ViewProviderLinkObserver: public ViewProviderExtension
57 {
58 EXTENSION_TYPESYSTEM_HEADER_WITH_OVERRIDE();
59
60 public:
61 ViewProviderLinkObserver();
62 ~ViewProviderLinkObserver() override;
63 void extensionReattach(App::DocumentObject*) override;
64 void extensionBeforeDelete() override;
65 void extensionOnChanged(const App::Property*) override;
66 void extensionUpdateData(const App::Property*) override;
67 void extensionFinishRestoring() override;
68 bool extensionCanDragObject(App::DocumentObject*) const override
69 {
70 return false;
71 }
72 bool extensionCanDropObject(App::DocumentObject*) const override
73 {
74 return false;
75 }
76 void extensionModeSwitchChange() override;
77
78 bool isLinkVisible() const;
79 void setLinkVisible(bool);
80
81 LinkInfoPtr linkInfo;
82 };
83
84 class GuiExport LinkOwner
85 {
86 public:
87 virtual void unlink(LinkInfoPtr)
88 {}
89 virtual void onLinkedIconChange(LinkInfoPtr)
90 {}
91 virtual void onLinkedUpdateData(LinkInfoPtr, const App::Property*)
92 {}
93
94 protected:
95 virtual ~LinkOwner() = default;
96 };
97
98 class GuiExport LinkView: public Base::BaseClass, public LinkOwner
99 {
100 TYPESYSTEM_HEADER_WITH_OVERRIDE();
101
102 public:
103 LinkView();
104 ~LinkView() override;
105 LinkView& operator=(const LinkView&) = delete;
106 LinkView(const LinkView&) = delete;
107
108 PyObject* getPyObject() override;
109
110 void unlink(LinkInfoPtr) override;
111 void onLinkedIconChange(LinkInfoPtr) override;
112 void onLinkedUpdateData(LinkInfoPtr, const App::Property*) override;
113
114 bool isLinked() const;
115
116 SoFCSelectionRoot* getLinkRoot() const
117 {
118 return pcLinkRoot;
119 }
120
121 QIcon getLinkedIcon(QPixmap overlay) const;
122
123 void updateLink();
124
125 void setLink(
126 App::DocumentObject* obj,
127 const std::vector<std::string>& subs = std::vector<std::string>()
128 );
129
130 void setLinkViewObject(
131 ViewProviderDocumentObject* vpd,
132 const std::vector<std::string>& subs = std::vector<std::string>()
133 );
134
135 std::vector<ViewProviderDocumentObject*> getChildren() const;
136
137 void setMaterial(int index, const App::Material* material);
138 void setDrawStyle(int linePattern, double lineWidth = 0, double pointSize = 0);
139 void setTransform(int index, const Base::Matrix4D& mat);
140 void renderDoubleSide(bool);
141 void setSize(int size);
142
143 int getSize() const
144 {
145 return nodeArray.size();
146 }
147
148 static void setTransform(SoTransform* pcTransform, const Base::Matrix4D& mat);
149
150 enum SnapshotType
151 {
152 // three type of snapshot to override linked root node:
153
154 // override transform and visibility
155 SnapshotTransform = 0,
156 // override visibility
157 SnapshotVisible = 1,
158 // override none (for child objects of a container)
159 SnapshotChild = 2,
160
161 SnapshotMax,
162
163 // special type for sub object linking
164 SnapshotContainer = -1,
165 // sub object linking with transform override
166 SnapshotContainerTransform = -2,
167 };
168 void setNodeType(SnapshotType type, bool sublink = true);
169
170 void setChildren(
171 const std::vector<App::DocumentObject*>& children,
172 const boost::dynamic_bitset<>& vis,
173 SnapshotType type = SnapshotVisible
174 );
175
176 bool linkGetDetailPath(const char*, SoFullPath*, SoDetail*&) const;
177 bool linkGetElementPicked(const SoPickedPoint*, std::string&) const;
178
179 void setElementVisible(int index, bool visible);
180 bool isElementVisible(int index) const;
181
182 ViewProviderDocumentObject* getOwner() const;
183 void setOwner(ViewProviderDocumentObject* vpd);
184
185 bool hasSubs() const;
186
187 std::vector<std::string> getSubNames() const;
188 ViewProviderDocumentObject* getLinkedView() const;
189
190 Base::BoundBox3d getBoundBox(ViewProviderDocumentObject* vpd = nullptr) const;
191
192 void setInvalid();
193
194 protected:
195 void replaceLinkedRoot(SoSeparator*);
196 void resetRoot();
197 bool getGroupHierarchy(int index, SoFullPath* path) const;
198
199 protected:
200 LinkInfoPtr linkOwner;
201 LinkInfoPtr linkInfo;
202 CoinPtr<SoFCSelectionRoot> pcLinkRoot;
203 CoinPtr<SoTransform> pcTransform;
204 CoinPtr<SoSeparator> pcLinkedRoot;
205 CoinPtr<SoDrawStyle> pcDrawStyle; // for override line width and point size
206 CoinPtr<SoShapeHints> pcShapeHints; // for override double side rendering for mirror
207 SnapshotType nodeType;
208 SnapshotType childType;
209 bool autoSubLink; // auto delegate to linked sub object if there is only one sub object
210
211 class SubInfo;
212 friend class SubInfo;
213 std::map<std::string, std::unique_ptr<SubInfo>> subInfo;
214
215 class Element;
216 std::vector<std::unique_ptr<Element>> nodeArray;
217 std::unordered_map<SoNode*, int> nodeMap;
218
219 Py::Object PythonObject;
220 };
221
222 class GuiExport ViewProviderLink: public ViewProviderDragger
223 {
224 PROPERTY_HEADER_WITH_OVERRIDE(Gui::ViewProviderLink);
225 using inherited = ViewProviderDragger;
226
227 public:
228 App::PropertyBool OverrideMaterial;
229 App::PropertyMaterial ShapeMaterial;
230 App::PropertyEnumeration DrawStyle;
231 App::PropertyFloatConstraint LineWidth;
232 App::PropertyFloatConstraint PointSize;
233 App::PropertyMaterialList MaterialList;
234 App::PropertyBoolList OverrideMaterialList;
235 App::PropertyBool Selectable;
236 App::PropertyColorList OverrideColorList;
237 App::PropertyPersistentObject ChildViewProvider;
238
239 ViewProviderLink();
240 ~ViewProviderLink() override;
241
242 void attach(App::DocumentObject* pcObj) override;
243 void reattach(App::DocumentObject* pcObj) override;
244
245 bool isSelectable() const override;
246
247 bool useNewSelectionModel() const override
248 {
249 return true;
250 }
251
252 void updateData(const App::Property*) override;
253 void onChanged(const App::Property* prop) override;
254 std::vector<App::DocumentObject*> claimChildren() const override;
255 bool getElementPicked(const SoPickedPoint*, std::string&) const override;
256 bool getDetailPath(const char*, SoFullPath*, bool, SoDetail*&) const override;
257
258 void finishRestoring() override;
259
260 QIcon getIcon() const override;
261
262 bool canDragObjects() const override;
263 bool canDragObject(App::DocumentObject*) const override;
264 void dragObject(App::DocumentObject*) override;
265 bool canDropObjects() const override;
266 bool canDragAndDropObject(App::DocumentObject*) const override;
267 bool canDropObjectEx(
268 App::DocumentObject* obj,
269 App::DocumentObject* owner,
270 const char* subname,
271 const std::vector<std::string>& subElements
272 ) const override;
273 std::string dropObjectEx(
274 App::DocumentObject*,
275 App::DocumentObject*,
276 const char* subname,
277 const std::vector<std::string>& subElements
278 ) override;
279
280 bool onDelete(const std::vector<std::string>&) override;
281 bool canDelete(App::DocumentObject* obj) const override;
282
283 std::vector<std::string> getDisplayModes() const override;
284
285 void setupContextMenu(QMenu*, QObject*, const char*) override;
286
287 virtual QPixmap getOverlayPixmap() const;
288
289 ViewProvider* startEditing(int ModNum) override;
290 bool doubleClicked() override;
291
292 PyObject* getPyObject() override;
293 PyObject* getPyLinkView();
294
295 static void updateLinks(ViewProvider* vp);
296
297 std::map<std::string, Base::Color> getElementColors(const char* subname = nullptr) const override;
298 void setElementColors(const std::map<std::string, Base::Color>& colors) override;
299
300 void setOverrideMode(const std::string& mode) override;
301
302 void onBeforeChange(const App::Property*) override;
303 ViewProviderDocumentObject* getChildViewProvider() const
304 {
305 return childVp;
306 }
307
308 App::Property* getPropertyByName(const char* name) const override;
309 void getPropertyMap(std::map<std::string, App::Property*>& Map) const override;
310 /// See PropertyContainer::visitProperties for semantics
311 void visitProperties(const std::function<void(App::Property*)>& visitor) const override;
312 void getPropertyList(std::vector<App::Property*>& List) const override;
313
314 ViewProviderDocumentObject* getLinkedViewProvider(
315 std::string* subname = nullptr,
316 bool recursive = false
317 ) const override;
318
319 bool allowOverride(const App::DocumentObject&) const override
320 {
321 return true;
322 }
323
324 void setTransformation(const Base::Matrix4D& rcMatrix) override;
325 void setTransformation(const SbMatrix& rcMatrix) override;
326
327 protected:
328 bool setEdit(int ModNum) override;
329 void setEditViewer(View3DInventorViewer*, int ModNum) override;
330 void unsetEditViewer(View3DInventorViewer*) override;
331 bool linkEdit(const App::LinkBaseExtension* ext = nullptr) const;
332 void _setupContextMenu(App::LinkBaseExtension* ext, QMenu*, QObject*, const char*);
333
334 enum LinkType
335 {
336 LinkTypeNone,
337 LinkTypeNormal,
338 LinkTypeSubs,
339 };
340
341 bool hasElements(const App::LinkBaseExtension* ext = nullptr) const;
342 bool isGroup(const App::LinkBaseExtension* ext = nullptr, bool plainGroup = false) const;
343 const App::LinkBaseExtension* getLinkExtension() const;
344 App::LinkBaseExtension* getLinkExtension();
345
346 void updateDataPrivate(App::LinkBaseExtension* ext, const App::Property*);
347 void updateElementList(App::LinkBaseExtension* ext);
348
349 bool setLinkType(App::LinkBaseExtension* ext);
350
351 void onChangeIcon() const;
352 std::vector<App::DocumentObject*> claimChildrenPrivate() const;
353
354 void applyMaterial();
355 void applyColors();
356
357 void checkIcon(const App::LinkBaseExtension* ext = nullptr);
358
359 ViewProvider* getLinkedView(bool real, const App::LinkBaseExtension* ext = nullptr) const;
360
361 bool initDraggingPlacement();
362 bool callDraggerProxy(const char* fname);
363
364 private:
365 static void dragStartCallback(void* data, SoDragger* d);
366 static void dragFinishCallback(void* data, SoDragger* d);
367 static void dragMotionCallback(void* data, SoDragger* d);
368
369 protected:
370 LinkView* linkView;
371 LinkType linkType;
372 bool hasSubName;
373 bool hasSubElement;
374
375 struct DraggerContext
376 {
377 Base::Matrix4D preTransform;
378 Base::Placement initialPlacement;
379 Base::Matrix4D mat;
380 Base::BoundBox3d bbox;
381 bool cmdPending;
382 };
383 std::unique_ptr<DraggerContext> dragCtx;
384 ViewProviderDocumentObject* childVp;
385 LinkInfoPtr childVpLink;
386 mutable qint64 overlayCacheKey;
387
388 private:
389 SoPickStyle* pcPickStyle {nullptr};
390 };
391
392 using ViewProviderLinkPython = ViewProviderFeaturePythonT<ViewProviderLink>;
393
394 } // namespace Gui
395
396
397 #endif // SRC_GUI_VIEWPROVIDER_LINK_H_