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