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