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