-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
ViewProvider.h
557 lines (499 loc) · 22 KB
/
ViewProvider.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/***************************************************************************
* Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef GUI_VIEWPROVIDER_H
#define GUI_VIEWPROVIDER_H
#include <map>
#include <vector>
#include <string>
#include <bitset>
#include <QIcon>
#include <boost/signals2.hpp>
#include <boost/intrusive_ptr.hpp>
#include <App/TransactionalObject.h>
#include <App/Material.h>
#include <Base/Vector3D.h>
#include <Base/BoundBox.h>
class SbVec2s;
class SbVec3f;
class SoNode;
class SoPath;
class SoSeparator;
class SoEvent;
class SoSwitch;
class SoTransform;
class SbMatrix;
class SoEventCallback;
class SoPickedPoint;
class SoDetail;
class SoFullPath;
class QString;
class QMenu;
class QObject;
namespace Base {
class Matrix4D;
}
namespace App {
class Color;
}
class SoGroup;
namespace Gui {
namespace TaskView {
class TaskContent;
}
class View3DInventorViewer;
class ViewProviderPy;
class ObjectItem;
class MDIView;
enum ViewStatus {
UpdateData = 0,
Detach = 1,
isRestoring = 2,
UpdatingView = 3,
};
/** Convenience smart pointer to wrap coin node.
*
* It is basically boost::intrusive plus implicit pointer conversion to save the
* trouble of typing get() all the time.
*/
template<class T>
class CoinPtr: public boost::intrusive_ptr<T> {
public:
// Too bad, VC2013 does not support constructor inheritance
//using boost::intrusive_ptr<T>::intrusive_ptr;
typedef boost::intrusive_ptr<T> inherited;
CoinPtr() {}
CoinPtr(T *p, bool add_ref=true):inherited(p,add_ref){}
template<class Y> CoinPtr(CoinPtr<Y> const &r):inherited(r){}
operator T *() const {
return this->get();
}
};
/** Helper function to deal with bug in SoNode::removeAllChildren()
*
* @sa https://bitbucket.org/Coin3D/coin/pull-requests/119/fix-sochildlist-auditing/diff
*/
void GuiExport coinRemoveAllChildren(SoGroup *node);
/** General interface for all visual stuff in FreeCAD
* This class is used to generate and handle all around
* visualizing and presenting objects from the FreeCAD
* App layer to the user. This class and its descendents
* have to be implemented for any object type in order to
* show them in the 3DView and TreeView.
*/
class GuiExport ViewProvider : public App::TransactionalObject
{
PROPERTY_HEADER(Gui::ViewProvider);
public:
/// constructor.
ViewProvider();
/// destructor.
virtual ~ViewProvider();
// returns the root node of the Provider (3D)
virtual SoSeparator* getRoot(void){return pcRoot;}
// return the mode switch node of the Provider (3D)
SoSwitch *getModeSwitch(void){return pcModeSwitch;}
SoTransform *getTransformNode(){return pcTransform;}
// returns the root for the Annotations.
SoSeparator* getAnnotation(void);
// returns the root node of the Provider (3D)
virtual SoSeparator* getFrontRoot(void) const;
// returns the root node where the children gets collected(3D)
virtual SoGroup* getChildRoot(void) const;
// returns the root node of the Provider (3D)
virtual SoSeparator* getBackRoot(void) const;
///Indicate whether to be added to scene graph or not
virtual bool canAddToSceneGraph() const {return true;}
/** deliver the children belonging to this object
* this method is used to deliver the objects to
* the 3DView which should be grouped under its
* scene graph. This affects the visibility and the 3D
* position of the object.
*/
virtual std::vector<App::DocumentObject*> claimChildren3D(void) const;
/** @name Selection handling
* This group of methods do the selection handling.
* Here you can define how the selection for your ViewProfider
* works.
*/
//@{
/// indicates if the ViewProvider use the new Selection model
virtual bool useNewSelectionModel(void) const;
virtual bool isSelectable(void) const {return true;}
/// return a hit element given the picked point which contains the full node path
virtual bool getElementPicked(const SoPickedPoint *, std::string &subname) const;
/// return a hit element to the selection path or 0
virtual std::string getElement(const SoDetail *) const { return std::string(); }
/// return the coin node detail of the subelement
virtual SoDetail* getDetail(const char *) const { return 0; }
/** return the coin node detail and path to the node of the subelement
*
* @param subname: dot separated string reference to the sub element
* @param pPath: output coin path leading to the returned element detail
* @param append: If true, pPath will be first appended with the root node and
* the mode switch node of this view provider.
*
* @return the coint detail of the subelement
*
* If this view provider links to other view provider, then the
* implementation of getDetailPath() shall also append all intermediate
* nodes starting just after the mode switch node up till the mode switch of
* the linked view provider.
*/
virtual bool getDetailPath(const char *subname, SoFullPath *pPath, bool append, SoDetail *&det) const;
/** partial rendering setup
*
* @param subelements: a list of dot separated string refer to the sub element
* @param clear: if true, remove the the subelement from partial rendering.
* If else, add the subelement for rendering.
*
* @return Return the number of subelement found
*
* Partial rendering only works if there is at least one SoFCSelectRoot node
* in this view provider
*/
int partialRender(const std::vector<std::string> &subelements, bool clear);
virtual std::vector<Base::Vector3d> getModelPoints(const SoPickedPoint *) const;
/// return the highlight lines for a given element or the whole shape
virtual std::vector<Base::Vector3d> getSelectionShape(const char* Element) const {
(void)Element;
return std::vector<Base::Vector3d>();
}
/** Return the bound box of this view object
*
* This method shall work regardless whether the current view object is
* visible or not.
*/
Base::BoundBox3d getBoundingBox(const char *subname=0, bool transform=true, MDIView *view=0) const;
/**
* Get called if the object is about to get deleted.
* Here you can delete other objects, switch their visibility or prevent the deletion of the object.
* @param subNames list of selected subelements
* @return true if the deletion is approved by the view provider.
*/
virtual bool onDelete(const std::vector<std::string> &subNames);
/** Called before deletion
*
* Unlike onDelete(), this function is guaranteed to be called before
* deletion, either by Document::remObject(), or on document deletion.
*/
virtual void beforeDelete();
/**
* @brief Asks the view provider if the given object that is part of its
* outlist can be removed from there without breaking it.
* @param obj is part of the outlist of the object associated to the view provider
* @return true if the removal is approved by the view provider.
*/
virtual bool canDelete(App::DocumentObject* obj) const;
//@}
/** @name Methods used by the Tree
* If you want to take control over the
* appearance of your object in the tree you
* can reimplemnt these methods.
*/
//@{
/// deliver the icon shown in the tree view
virtual QIcon getIcon(void) const;
/** deliver the children belonging to this object
* this method is used to deliver the objects to
* the tree framework which should be grouped under its
* label. Obvious is the usage in the group but it can
* be used for any kind of grouping needed for a special
* purpose.
*/
virtual std::vector<App::DocumentObject*> claimChildren(void) const;
//@}
/** @name Drag and drop
* To enable drag and drop you have to re-implement \ref canDragObjects() and
* \ref canDropObjects() to return true. For finer control you can also re-implement
* \ref canDragObject() or \ref canDropObject() to filter certain object types, by
* default these methods don't filter any types.
* To take action of drag and drop the method \ref dragObject() and \ref dropObject()
* must be re-implemented, too.
*/
//@{
/** Check whether children can be removed from the view provider by drag and drop */
virtual bool canDragObjects() const;
/** Check whether the object can be removed from the view provider by drag and drop */
virtual bool canDragObject(App::DocumentObject*) const;
/** Remove a child from the view provider by drag and drop */
virtual void dragObject(App::DocumentObject*);
/** Check whether objects can be added to the view provider by drag and drop or drop only */
virtual bool canDropObjects() const;
/** Check whether the object can be dropped to the view provider by drag and drop or drop only*/
virtual bool canDropObject(App::DocumentObject*) const;
/** Return false to force drop only operation for a given object*/
virtual bool canDragAndDropObject(App::DocumentObject*) const;
/** Add an object to the view provider by drag and drop */
virtual void dropObject(App::DocumentObject*);
/** Query object dropping with full quanlified name
*
* Tree view now calls this function instead of canDropObject(), and may
* query for objects from other document. The default implementation
* (actually in ViewProviderDocumentObject) inhibites cross document
* dropping, and calls canDropObject(obj) for the rest. Override this
* function to enable cross document linking.
*
* @param obj: the object being dropped
*
* @param owner: the (grand)parent object of the dropping object. Maybe
* null. This may not be the top parent object, as tree view will try to
* find a parent of the dropping object realtive to this object to avoid
* cyclic dependency
*
* @param subname: subname reference to the dropping object
*
* @param elements: non-object sub-elements, e.g. Faces, Edges, selected
* when the object is being dropped
*
* @return Return whether the dropping action is allowed.
* */
virtual bool canDropObjectEx(App::DocumentObject *obj, App::DocumentObject *owner,
const char *subname, const std::vector<std::string> &elements) const;
/// return a subname referencing the sub-object holding the dropped objects
virtual std::string getDropPrefix() const { return std::string(); }
/** Add an object with full quanlified name to the view provider by drag and drop
*
* @param obj: the object being dropped
*
* @param owner: the (grand)parent object of the dropping object. Maybe
* null. This may not be the top parent object, as tree view will try to
* find a parent of the dropping object realtive to this object to avoid
* cyclic dependency
*
* @param subname: subname reference to the dropping object
*
* @param elements: non-object sub-elements, e.g. Faces, Edges, selected
* when the object is being dropped
*
* @return Optionally returns a subname reference locating the dropped
* object, which may or may not be the actual dropped object, e.g. it may be
* a link.
*/
virtual std::string dropObjectEx(App::DocumentObject *obj, App::DocumentObject *owner,
const char *subname, const std::vector<std::string> &elements);
/** Replace an object to the view provider by drag and drop
*
* @param oldObj: object to be replaced
* @param newObj: object to replace with
*
* @return Returns 0 if not found, 1 if succeed, -1 if not supported
*/
virtual int replaceObject(App::DocumentObject *oldObj, App::DocumentObject *newObj);
//@}
/** Tell the tree view if this object should apear there */
virtual bool showInTree() const { return true; }
/** Tell the tree view to remove children items from the tree root*/
virtual bool canRemoveChildrenFromRoot() const {return true;}
/** @name Signals of the view provider */
//@{
/// signal on icon change
boost::signals2::signal<void ()> signalChangeIcon;
/// signal on tooltip change
boost::signals2::signal<void (const QString&)> signalChangeToolTip;
/// signal on status tip change
boost::signals2::signal<void (const QString&)> signalChangeStatusTip;
//@}
/** update the content of the ViewProvider
* this method have to implement the recalculation
* of the ViewProvider. There are different reasons to
* update. E.g. only the view attribute has changed, or
* the data has manipulated.
*/
virtual void update(const App::Property*);
virtual void updateData(const App::Property*);
bool isUpdatesEnabled () const;
void setUpdatesEnabled (bool enable);
/// return the status bits
unsigned long getStatus() const {return StatusBits.to_ulong();}
bool testStatus(ViewStatus pos) const {return StatusBits.test((size_t)pos);}
void setStatus(ViewStatus pos, bool on) {StatusBits.set((size_t)pos, on);}
std::string toString() const;
PyObject* getPyObject();
/** @name Display mode methods
*/
//@{
std::string getActiveDisplayMode(void) const;
/// set the display mode
virtual void setDisplayMode(const char* ModeName);
/// get the default display mode
virtual const char* getDefaultDisplayMode() const;
/// returns a list of all possible display modes
virtual std::vector<std::string> getDisplayModes(void) const;
/// Hides the view provider
virtual void hide(void);
/// Shows the view provider
virtual void show(void);
/// checks whether the view provider is visible or not
virtual bool isShow(void) const;
void setVisible(bool);
bool isVisible() const;
void setLinkVisible(bool);
bool isLinkVisible() const;
/// Overrides the display mode with mode.
virtual void setOverrideMode(const std::string &mode);
const std::string getOverrideMode();
//@}
/** @name Color mangement methods
*/
//@{
virtual std::map<std::string, App::Color> getElementColors(const char *element=0) const {
(void)element;
return {};
}
virtual void setElementColors(const std::map<std::string, App::Color> &colors) {
(void)colors;
}
static const std::string &hiddenMarker();
static const char *hasHiddenMarker(const char *subname);
//@}
/** @name Edit methods
* if the Viewprovider goes in edit mode
* you can handle most of the events in the viewer by yourself
*/
//@{
enum EditMode {Default = 0,
Transform,
Cutting,
Color,
};
protected:
/// is called by the document when the provider goes in edit mode
virtual bool setEdit(int ModNum);
/// is called when you lose the edit mode
virtual void unsetEdit(int ModNum);
/// return the edit mode or -1 if nothing is being edited
int getEditingMode() const;
public:
virtual ViewProvider *startEditing(int ModNum=0);
bool isEditing() const;
void finishEditing();
/// adjust viewer settings when editing a view provider
virtual void setEditViewer(View3DInventorViewer*, int ModNum);
/// restores viewer settings when leaving editing mode
virtual void unsetEditViewer(View3DInventorViewer*);
//@}
/** @name Task panel
* With this interface the ViewProvider can steer the
* appearance of widgets in the task view
*/
//@{
/// get a list of TaskBoxes associated with this object
virtual void getTaskViewContent(std::vector<Gui::TaskView::TaskContent*>&) const {}
//@}
/// is called when the provider is in edit and a key event occurs. Only ESC ends edit.
virtual bool keyPressed(bool pressed, int key);
/// is called by the tree if the user double click on the object
virtual bool doubleClicked(void) { return false; }
/// is called when the provider is in edit and the mouse is moved
virtual bool mouseMove(const SbVec2s &cursorPos, View3DInventorViewer* viewer);
/// is called when the Provider is in edit and the mouse is clicked
virtual bool mouseButtonPressed(int button, bool pressed, const SbVec2s &cursorPos,
const View3DInventorViewer* viewer);
/// set up the context-menu with the supported edit modes
virtual void setupContextMenu(QMenu*, QObject*, const char*) {}
/** @name direct handling methods
* This group of methods is to direct influence the
* appearance of the viewed content. It's only for fast
* interactions! If you want to set the visual parameters
* you have to do it on the object viewed by this provider!
*/
//@{
/// set the viewing transformation of the provider
virtual void setTransformation(const Base::Matrix4D &rcMatrix);
virtual void setTransformation(const SbMatrix &rcMatrix);
static SbMatrix convert(const Base::Matrix4D &rcMatrix);
static Base::Matrix4D convert(const SbMatrix &sbMat);
//@}
virtual MDIView *getMDIView() {return 0;}
public:
// this method is called by the viewer when the ViewProvider is in edit
static void eventCallback(void * ud, SoEventCallback * node);
//restoring the object from document:
//this may be of interest to extensions, hence call them
virtual void Restore(Base::XMLReader& reader);
bool isRestoring() {return testStatus(Gui::isRestoring);}
/** @name Display mask modes
* Mainly controls an SoSwitch node which selects the display mask modes.
* The number of display mask modes doesn't necessarily match with the number
* of display modes.
* E.g. various display modes like Gaussian curvature, mean curvature or gray
* values are displayed by one display mask mode that handles color values.
*/
//@{
/// Adds a new display mask mode
void addDisplayMaskMode( SoNode *node, const char* type );
/// Activates the display mask mode \a type
void setDisplayMaskMode( const char* type );
/// Get the node to the display mask mode \a type
SoNode* getDisplayMaskMode(const char* type) const;
/// Returns a list of added display mask modes
std::vector<std::string> getDisplayMaskModes() const;
void setDefaultMode(int);
int getDefaultMode() const;
//@}
virtual void setRenderCacheMode(int);
protected:
/** Helper method to check that the node is valid, i.e. it must not cause
* and infinite recursion.
*/
bool checkRecursion(SoNode*);
/** Helper method to get picked entities while editing.
* It's in the responsibility of the caller to delete the returned instance.
*/
SoPickedPoint* getPointOnRay(const SbVec2s& pos,
const View3DInventorViewer* viewer) const;
/** Helper method to get picked entities while editing.
* It's in the responsibility of the caller to delete the returned instance.
*/
SoPickedPoint* getPointOnRay(const SbVec3f& pos, const SbVec3f& dir,
const View3DInventorViewer* viewer) const;
/// Reimplemented from subclass
void onChanged(const App::Property* prop);
/** @name Methods used by the Tree
* If you want to take control over the
* viewprovider specific overlay icons, such as status, you
* can reimplement this method.
*/
virtual QIcon mergeOverlayIcons (const QIcon & orig) const;
protected:
/// The root Separator of the ViewProvider
SoSeparator *pcRoot;
/// this is transformation for the provider
SoTransform *pcTransform;
const char* sPixmap;
/// this is the mode switch, all the different viewing modes are collected here
SoSwitch *pcModeSwitch;
/// The root separator for annotations
SoSeparator *pcAnnotation;
ViewProviderPy* pyViewObject;
std::string overrideMode;
std::bitset<32> StatusBits;
private:
void setModeSwitch();
int _iActualMode;
int _iEditMode;
int viewOverrideMode;
std::string _sCurrentMode;
std::map<std::string, int> _sDisplayMaskModes;
};
} // namespace Gui
#endif // GUI_VIEWPROVIDER_H