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