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