Salome HOME
A new event Visual Attributes Changed is defined for performance sake.
[modules/shaper.git] / src / PartSet / PartSet_Module.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef PartSet_Module_H
21 #define PartSet_Module_H
22
23 #include "PartSet.h"
24 #include "PartSet_Tools.h"
25 #include "PartSet_OverconstraintListener.h"
26 #include "PartSet_SketcherMgr.h"
27
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_Definitions.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Attribute.h>
32 #include <ModelAPI_CompositeFeature.h>
33
34 #include <Events_Listener.h>
35
36 //#include <StdSelect_FaceFilter.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <SelectMgr_ListOfFilter.hxx>
39 #include <SelectMgr_Filter.hxx>
40
41 #include <QMap>
42 #include <QMenu>
43 #include <QObject>
44 #include <QModelIndex>
45
46 #include <map>
47 #include <memory>
48 #include <string>
49
50 class ModuleBase_Operation;
51 class ModuleBase_IViewWindow;
52 class ModuleBase_ViewerPrs;
53
54 class XGUI_Workshop;
55 class PartSet_MenuMgr;
56 class PartSet_CustomPrs;
57 class PartSet_SketcherMgr;
58 class PartSet_SketcherReentrantMgr;
59 class PartSet_RootNode;
60 class ModelAPI_Result;
61
62 class QAction;
63
64 /**
65 * \ingroup Modules
66 * Implementation of Partset module
67 */
68 class PARTSET_EXPORT PartSet_Module : public ModuleBase_IModule, public Events_Listener
69 {
70 Q_OBJECT
71
72 /// Enumeration to specify the restart operation properties.
73 enum RestartingMode {
74   RM_None, /// the operation should not be restarted
75   RM_Forbided, /// the operation should not be restarted after there is no active widget
76   RM_LastFeatureUsed, /// the operation is restarted and use the previous feature
77                       /// for own initialization
78   RM_EmptyFeatureUsed /// the operation is restarted and does not use the previous feature
79 };
80
81 public:
82
83   /// Constructor
84   /// \param theWshop a pointer to a workshop
85   PartSet_Module(ModuleBase_IWorkshop* theWshop);
86   virtual ~PartSet_Module();
87
88   // Stores the current selection
89   virtual void storeSelection();
90
91   // Restores the current selection
92   virtual void restoreSelection();
93
94   /// Creates custom widgets for property panel
95   virtual ModuleBase_ModelWidget* createWidgetByType(const std::string& theType, QWidget* theParent,
96                                                      Config_WidgetAPI* theWidgetApi);
97
98   /// Returns the active widget, by default it is the property panel active widget
99   /// If the internal edit operation is started, this is the first widget of the operation
100   virtual ModuleBase_ModelWidget* activeWidget() const;
101
102   /// Call back forlast tuning of property panel before operation performance
103   virtual void propertyPanelDefined(ModuleBase_Operation* theOperation);
104
105   /// If there is found selected attribute, widgets are created and contains
106   /// only a widget for the attribute
107   /// It is important for Property Panel filling by sketch point attribute
108   /// \param theOperation a started operation
109   /// \param theWidgets a list of created widgets
110   /// \return boolean result, false by default
111   virtual bool createWidgets(ModuleBase_Operation* theOperation,
112                              QList<ModuleBase_ModelWidget*>& theWidgets) const;
113
114   /// Launching of a edit operation on the feature
115   /// \param theFeature feature for editing
116   virtual void editFeature(FeaturePtr theFeature);
117
118   /// Returns true if the operation can be committed. Result in default implementation is true.
119   /// \return boolean value
120   virtual bool canCommitOperation() const;
121
122   /// Creates an operation and send it to loop
123   /// \param theCmdId the operation name
124   /// \param isStartAfterCommitOnly operation is launched if there is no active operation or
125   ///        it is committed
126   virtual void launchOperation(const QString& theCmdId,
127                                const bool& isStartAfterCommitOnly);
128
129   /// Realizes some functionality by an operation start
130   /// Displays all sketcher sub-Objects, hides sketcher result, appends selection filters
131   /// Activate the operation presentation
132   /// \param theOperation a started operation
133   virtual void operationStarted(ModuleBase_Operation* theOperation);
134
135   /// Realizes some functionality by an operation resume
136   /// Activate the operation presentation
137   /// \param theOperation a resumed operation
138   virtual void operationResumed(ModuleBase_Operation* theOperation);
139
140   /// Realizes some functionality by an operation commit
141   /// Restarts sketcher operation automatically of it is necessary
142   /// \param theOperation a committed operation
143   virtual void operationCommitted(ModuleBase_Operation* theOperation);
144
145   /// Realizes some functionality by an operation abort
146   /// Hides all sketcher sub-Objects, displays sketcher result and removes selection filters
147   /// \param theOperation an aborted operation
148   virtual void operationAborted(ModuleBase_Operation* theOperation);
149
150   /// Realizes some functionality by an operation stop
151   /// Hides all sketcher sub-Objects, displays sketcher result and removes selection filters
152   /// \param theOperation a stopped operation
153   virtual void operationStopped(ModuleBase_Operation* theOperation);
154
155   /// Returns current operation
156   virtual ModuleBase_Operation* currentOperation() const;
157
158   /// Returns True if there are available Undos and the sketch manager allows undo
159   /// \return the boolean result
160   virtual bool canUndo() const;
161
162   //! Returns True if there are available Redos and the sketch manager allows redo
163   /// \return the boolean result
164   virtual bool canRedo() const;
165
166   /// Returnas true if the action can be applyed to the object
167   /// \param theObject a checked object
168   /// \param theActionId an identifier of action, to be found in the menu manager like "DELETE_CMD"
169   /// \return the a booean result
170   virtual bool canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const;
171
172   /// Returns True if the current operation can be committed. Asks the sketch manager.
173   /// \return a boolean value
174   //virtual bool canCommitOperation() const;
175
176   /// Returns whether the object can be erased at the bounds of the active operation.
177   /// The sub-objects of the current operation can not be erased
178   /// \param theObject a model object
179   virtual bool canEraseObject(const ObjectPtr& theObject) const;
180
181   /// Returns whether the object can be displayed at the bounds of the active operation.
182   /// Display only current operation results for usual operation and ask the sketcher manager
183   /// if it is a sketch operation
184   /// \param theObject a model object
185   virtual bool canDisplayObject(const ObjectPtr& theObject) const;
186
187   /// Returns parent result if accepted, true if the started operation is a nested operation
188   /// of the previous operation
189   /// \param thePreviousOperationKind a kind of previous operation
190   /// \param theStartedOperationKind a kind of a new operation
191   virtual bool canUsePreselection(const QString& thePreviousOperationKind,
192                                   const QString& theStartedOperationKind);
193
194   /// Make some functionality after the objects are hidden in viewer
195   /// \param theObjects a list of hidden objects
196   //virtual void processHiddenObject(const std::list<ObjectPtr>& theObjects);
197
198   /// Returns true if selection for the object can be activate.
199   /// For sketch operation allow the selection activation if the operation is edit, for other
200   /// operation uses the default result
201   /// \param theObject a model object
202   virtual bool canActivateSelection(const ObjectPtr& theObject) const;
203
204   /// Add menu atems for object browser into the given menu
205   /// \param theMenu a popup menu to be shown in the object browser
206   virtual void addObjectBrowserMenu(QMenu* theMenu) const;
207
208   /// Add menu items for viewer into the actions map
209   /// \param theStdActions a map of standard actions
210   /// \param theParent a parent widget
211   /// \param theMenuActions map of action/menu for the desirable index in the viewer menu
212   /// \return true if items are added and there is no necessity to provide standard menu
213   virtual bool addViewerMenu(const QMap<QString, QAction*>& theStdActions,
214                              QWidget* theParent,
215                              QMap<int, QAction*>& theMenuActions) const;
216
217   /// Returns a list of modes, where the AIS objects should be activated
218   /// \param theModes a list of modes
219   virtual void activeSelectionModes(QIntList& theModes);
220
221   /// Appends specific selection modes for the module to the list of types
222   /// \param theModesType combination of available selection filters
223   /// \param theModes a selection modes to be extended
224   virtual void moduleSelectionModes(int theModesType, QIntList& theModes);
225
226   /// Appends into container of filters module filters corresponded to the modes type
227   /// \param theFilterTypes container of available selection filters
228   /// \param theSelectionFilters [out] container to be extend by elements
229   virtual void moduleSelectionFilters(const QIntList& theFilterTypes,
230                                       SelectMgr_ListOfFilter& theSelectionFilters);
231
232
233   /// Returns whether the mouse enter the viewer's window
234   /// \return true if items are added and there is no necessity to provide standard menu
235   bool isMouseOverWindow();
236
237   /// Returns sketch manager object
238   PartSet_SketcherMgr* sketchMgr() const { return mySketchMgr; }
239
240   /// Returns sketch reentrant manager
241   PartSet_SketcherReentrantMgr* sketchReentranceMgr() const { return mySketchReentrantMgr; }
242
243   /// Find object and attribute(if selected) for the given viewer selection
244   /// \param theSelected a viewer selection
245   /// \param theObject a selected model object
246   /// \param theAttribute a selected model attribute
247   virtual void getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theSelected,
248                                 ObjectPtr& theObject, AttributePtr& theAttribute);
249
250   /// Returns listener of overconstraint signal
251   /// \return the listener
252   PartSet_OverconstraintListener* overconstraintListener() { return myOverconstraintListener; }
253
254   /// Returns true if the current operation is not reentrant and the current state of the
255   /// application is not in launch operation mode
256   /// \return boolean value
257   bool isSketchNeutralPointActivated() const;
258
259   /// Performs functionality on closing document
260   virtual void closeDocument();
261
262   /// Clears specific presentations in the viewer
263   virtual void clearViewer();
264
265   /// Event Listener method
266   /// \param theMessage an event message
267   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
268
269   /// Set the object with the object results are customized
270   /// \param theFeature a feature
271   void setCustomized(const FeaturePtr& theFeature);
272
273   /// Return true if the custom presentation is activated
274   /// \param theFlag a flag of level of customization, which means that only part of sub-elements
275   /// \return boolean value
276   virtual bool isCustomPrsActivated(const ModuleBase_CustomizeFlag& theFlag) const;
277
278   /// Activate custom presentation for the object
279   /// \param theFeature a feature instance
280   /// \param theFlag a flag of level of customization, which means that only part of sub-elements
281   /// \param theUpdateViewer the parameter whether the viewer should be update immediately
282   virtual void activateCustomPrs(const FeaturePtr& theFeature,
283                                  const ModuleBase_CustomizeFlag& theFlag,
284                                  const bool theUpdateViewer);
285
286   /// Deactivate custom presentation for the object
287   /// \param theFlag a flag of level of customization, which means that only part of sub-elements
288   /// \param theUpdateViewer the parameter whether the viewer should be update immediately
289   virtual void deactivateCustomPrs(const ModuleBase_CustomizeFlag& theFlag,
290                                    const bool theUpdateViewer);
291
292   /// Modifies the given presentation in the custom way.
293   //virtual bool customisePresentation(std::shared_ptr<ModelAPI_Result> theResult,
294   //                                   AISObjectPtr thePrs,
295   //                                   std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs);
296
297   ///// Modifies the given presentation in the custom way after usual customize is performed.
298   //virtual bool afterCustomisePresentation(std::shared_ptr<ModelAPI_Result> theResult,
299   //                                        AISObjectPtr thePrs,
300   //                                        GeomCustomPrsPtr theCustomPrs);
301
302   /// Update the object presentable properties such as color, lines width and other
303   /// If the object is result with the color attribute value set, it is used,
304   /// otherwise the customize is applyed to the object's feature if it is a custom prs
305   /// \param theObject an object instance
306   /// \param theFlag a flag of level of customization, which means that only part of sub-elements
307   /// should be updated(e.g. only highlighted elements)
308   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
309   /// \returns true if the object is modified
310   virtual bool customizeFeature(ObjectPtr theObject, const ModuleBase_CustomizeFlag& theFlag,
311                                 const bool theUpdateViewer);
312
313   /// Disable displaying of custom mode
314   /// \param theMode a mode to disable
315   virtual void disableCustomMode(ModuleBase_CustomizeFlag theMode);
316
317   /// Enables disabled custom mode
318   virtual void enableCustomModes();
319
320   /// This method is called on object browser creation for customisation of module specific features
321   /// \param theObjectBrowser a pinter on Object Browser widget
322   virtual void customizeObjectBrowser(QWidget* theObjectBrowser);
323
324   /// Returns the viewer Z layer
325   int getVisualLayerId() const { return myVisualLayerId; }
326
327   /// Create specific for the module presentation
328   /// \param theResult an object for presentation
329   /// \return created presentation or NULL(default value)
330   virtual AISObjectPtr createPresentation(const ObjectPtr& theResult);
331
332   /// Customize presentation according to objects attributes
333   /// \param theObject an object for presentation
334   /// \param thePrs a presentation object
335   virtual void customizePresentation(const ObjectPtr& theObject, const AISObjectPtr& thePrs) const;
336
337   //! Returns data object by AIS
338   virtual ObjectPtr findPresentedObject(const AISObjectPtr& theAIS) const;
339
340   //! Returns true if the presentation can be shown in shading mode
341   //! \param theAIS presentation to be checked
342   //! \return boolean value
343   virtual bool canBeShaded(Handle(AIS_InteractiveObject) theAIS) const;
344
345   /// Update state of pop-up menu items in viewer
346   /// \param theStdActions - a map of standard actions
347   virtual void updateViewerMenu(const QMap<QString, QAction*>& theStdActions);
348
349   /// Returns true if the action should be always enabled
350   /// \param theActionId an action index: Accept or Accept All
351   /// \return boolean value
352   virtual bool isActionEnableStateFixed(const int theActionId) const;
353
354   //! Returns the feature error if the current state of the feature in the module is not correct
355   //! If the feature is correct, it returns an empty value
356   //! \return string value
357   virtual QString getFeatureError(const FeaturePtr& theFeature);
358
359   /// Returns list of granted operation indices
360   virtual void grantedOperationIds(ModuleBase_Operation* theOperation, QStringList& theIds) const;
361
362   /// Validates the current operation and send the state change to sketch manager
363   /// \param thePreviousState the previous widget value state
364   virtual void widgetStateChanged(int thePreviousState);
365
366   /// Returns true if the event is processed. It gives the reentrance manager to process the enter.
367   /// \param thePreviousAttributeID an index of the previous active attribute
368   virtual bool processEnter(const std::string& thePreviousAttributeID);
369
370   /// Performs some GUI actions before an operation transaction is stopped
371   /// Default realization is empty
372   virtual void beforeOperationStopped(ModuleBase_Operation* theOperation);
373
374   /// Finds a shape by attribute if it is possible
375   /// \param theAttribute an attribute
376   /// \return a geom shape
377   virtual GeomShapePtr findShape(const AttributePtr& theAttribute);
378
379   /// Finds an attribute by geom shape if it is possible
380   /// \param theObject an object of the attribute
381   /// \param theGeomShape a geom shape
382   /// \return theAttribute
383   virtual AttributePtr findAttribute(const ObjectPtr& theObject, const GeomShapePtr& theGeomShape);
384
385   /// Returns reentrant message if it was accepted
386   virtual std::shared_ptr<Events_Message> reentrantMessage();
387
388   /// Put current selection into reentrant message
389   /// \param theMessage a message of reentrant operation
390   virtual void setReentrantPreSelection(const std::shared_ptr<Events_Message>& theMessage);
391
392   /// Returns root tree node which represents a data model
393   virtual ModuleBase_ITreeNode* rootNode() const;
394
395   /// Returns the workshop
396   XGUI_Workshop* getWorkshop() const;
397
398   /// Reads description of features from XML file
399   virtual void createFeatures();
400
401 public slots:
402   /// Slolt called on object display
403   /// \param theObject a data object
404   /// \param theAIS a presentation object
405   virtual void onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
406
407   /// Slot called on before object erase
408   /// \param theObject a data object
409   /// \param theAIS a presentation object
410   virtual void onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
411
412   /// Called on transformation in current viewer
413   /// \param theTrsfType type of tranformation
414   void onViewTransformed(int theTrsfType = 2);
415
416 protected slots:
417   /// Called when previous operation is finished
418   virtual void onSelectionChanged();
419
420   /// SLOT, that is called by key release in the viewer.
421   /// \param theWnd a view window
422   /// \param theEvent the key event
423   void onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent);
424
425   /// A slot called on view window creation
426   void onViewCreated(ModuleBase_IViewWindow*);
427
428   /// A slot to change property panel title by choice type change if the title information
429   /// exists in the XML definition of this control attribute
430   /// \param theWidget a sender
431   /// \param theIndex the current choice index
432   void onChoiceChanged(ModuleBase_ModelWidget* theWidget, int theIndex);
433
434 protected:
435   /// Appends specific selection modes for the module to the list of types
436   /// \param theModes a selection modes to be extended
437   virtual void customSubShapesSelectionModes(QIntList& theModes);
438
439   /// Sets the constraints states in internal map. If the feature kind is a dimensional constraint
440   /// other dimensions are shown.
441   /// \param theFeatureKindId a feature kind
442   void storeConstraintsState(const std::string& theFeatureKindId);
443
444   /// If the feature kind is a geometrical or dimensional constraint, set visible state for
445   /// all types of constraints
446   /// \param theFeatureKindId a feature kind
447   void updateConstraintsState(const std::string& theFeatureKind);
448
449   /// Register validators for this module
450   virtual void registerValidators();
451
452   /// Connects or disconnects to the value changed signal of the property panel widgets
453   /// \param theWidget a property contol widget
454   /// \param isToConnect a boolean value whether connect or disconnect
455   virtual void connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect);
456
457   /// Updates reentrant manager state or sketcher operations for the started operation
458   /// \param theOperation the started operation
459   void updateSketcherOnStart(ModuleBase_Operation* theOperation);
460
461   /// Updates presetnations of results and arguments by operation start
462   /// \param theOperation the started operation
463   void updatePresentationsOnStart(ModuleBase_Operation* theOperation);
464
465  private slots:
466    void onTreeViewDoubleClick(const QModelIndex&);
467
468    void onActiveDocPopup(const QPoint&);
469
470  private:
471   //! Delete features
472   virtual bool deleteObjects();
473
474   void setDefaultConstraintShown();
475
476 private:
477   bool myIsOperationIsLaunched; /// state of application between launch and stop operation
478
479   PartSet_SketcherMgr* mySketchMgr;
480   PartSet_SketcherReentrantMgr* mySketchReentrantMgr;
481   PartSet_MenuMgr* myMenuMgr;
482   /// A default custom presentation, which is used for references objects of started operation
483   PartSet_CustomPrs* myCustomPrs;
484   PartSet_OverconstraintListener* myOverconstraintListener;
485   int myVisualLayerId;
486
487   /// backup of the visible state to restore them by operation stop
488   QMap<PartSet_Tools::ConstraintVisibleState, bool> myHasConstraintShown;
489
490   /// backup of selection in the viewer, it is used only to store selection before
491   /// redisplay and restore it after
492   PartSet_SketcherMgr::FeatureToSelectionMap myCurrentSelection;
493   QModelIndex myActivePartIndex;
494
495   PartSet_RootNode* myRoot;
496 };
497
498 #endif