Salome HOME
Issue #394 Undo-ing a Sketch element
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_Operation.h
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #ifndef ModuleBase_Operation_H
11 #define ModuleBase_Operation_H
12
13 #include <ModuleBase.h>
14 #include <ModuleBase_ViewerPrs.h>
15
16 #include <ModelAPI_CompositeFeature.h>
17 #include <ModelAPI_Document.h>
18
19 #include <QObject>
20 #include <QString>
21 #include <QStringList>
22
23 class ModuleBase_ModelWidget;
24 class ModuleBase_OperationDescription;
25 class ModuleBase_IPropertyPanel;
26 class ModuleBase_ISelection;
27 class ModuleBase_IViewer;
28
29 class QKeyEvent;
30
31 /*!
32  * \class ModuleBase_Operation
33  * \ingroup GUI
34  * \brief Base class for all operations
35  *
36  *  Base class for all operations. If you perform an action it is reasonable to create
37  *  operation intended for this. This is a base class for all operations which provides
38  *  mechanism for correct starting operations, starting operations above already started
39  *  ones, committing operations and so on. To create own operation it is reasonable to
40  *  inherit it from this class and redefines virtual methods to provide own behavior
41  *  Main virtual methods are
42  *  - virtual bool      isReadyToStart();
43  *  - virtual void      startOperation();
44  *  - virtual void      abortOperation();
45  *  - virtual void      commitOperation();
46  */
47
48 class MODULEBASE_EXPORT ModuleBase_Operation : public QObject
49 {
50 Q_OBJECT
51
52  public:
53   /// Constructor
54   /// \param theId the operation identifier
55   /// \param theParent the QObject parent
56   ModuleBase_Operation(const QString& theId = "", QObject* theParent = 0);
57   /// Destructor
58   virtual ~ModuleBase_Operation();
59
60   /// Returns the operation description
61   /// /returns the instance of the description class
62   ModuleBase_OperationDescription* getDescription() const { return myDescription; }
63
64   /**
65   * Must return true if this operation can be launched as nested for any current operation
66   * and it is not necessary to check this operation on validity. By default 
67   * the operation is not granted.
68   * The method has to be redefined for granted operations.
69   */
70   virtual bool isGranted(QString theId) const;
71
72
73   /// Returns True if data of its feature was modified during operation
74   virtual bool isModified() const { return myIsModified; }
75
76   /// Returns True id the current operation is launched in editing mode
77   bool isEditOperation() const { return myIsEditing; }
78
79   /// Returns list of nested features
80   QStringList nestedFeatures() const { return myNestedFeatures; }
81
82   /// Sets list of nested features
83   void setNestedFeatures(const QStringList& theList) { myNestedFeatures = theList; }
84
85
86   /// Returns operations Id from it's description
87   QString id() const;
88
89   /// Returns the operation feature
90   /// \return the feature
91   FeaturePtr feature() const;
92
93   /**
94   * Must return True if the operation's feature is valid.
95   * Since IOperation does not have any feature returns false.
96   */
97   virtual bool isValid() const;
98
99   /// Sets the operation feature
100   void setFeature(FeaturePtr theFeature);
101
102   /// Returns True if the current operation works with the given object (feature or result)
103   virtual bool hasObject(ObjectPtr theObj) const;
104
105   /// Initialisation of operation with preliminary selection
106   /// \param theSelection an instance of Selection class
107   /// \param theViewer a viewer to have the viewer the eye position
108   virtual void initSelection(ModuleBase_ISelection* theSelection,
109                              ModuleBase_IViewer* theViewer);
110
111   /// \brief Set property pane to the operation
112   /// \param theProp a property panel instance
113   virtual void setPropertyPanel(ModuleBase_IPropertyPanel* theProp);
114
115   /// \return Currently installed property panel
116   ModuleBase_IPropertyPanel* propertyPanel() const { return myPropertyPanel; }
117
118   /// Activates widgets by preselection if it is accepted. Emits signal if the activation is correct
119   virtual void activateByPreselection();
120
121   /// If the operation works with feature which is sub-feature of another one
122   /// then this variable has to be initialised by parent feature 
123   /// before operation feature creating
124   void setParentFeature(CompositeFeaturePtr theParent);
125
126   /// \return Installed parent feature (can be NULL)
127   CompositeFeaturePtr parentFeature() const;
128
129 signals:
130   /// The operation is started
131   void started();  
132
133   /// The operation is aborted
134   void aborted();  
135
136   /// The operation is committed
137   void committed();  
138
139   /// The operation is aborted or committed
140   void stopped();  
141
142   /// The operation is resumed
143   void resumed();  
144
145   /// The operation is postponed
146   void postponed();  
147
148   /// The operation is filled with existing preselection
149   void activatedByPreselection(); 
150
151  public slots:
152   /// Starts operation
153   /// Public slot. Verifies whether operation can be started and starts operation.
154   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
155   /// to change behavior of operation. There is no point in using this method. It would
156   /// be better to inherit own operator from base one and redefine startOperation method
157   /// instead.
158   void start();
159
160   /// Deactivates current operation which can be resumed later.
161   void postpone();
162
163   /// Resumes operation
164   /// Public slot. Verifies whether operation can be started and starts operation.
165   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
166   /// to change behavior of operation. There is no point in using this method. It would
167   /// be better to inherit own operator from base one and redefine startOperation method
168   /// instead.
169   void resume();
170
171   /// Aborts operation
172   /// Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
173   /// Redefine abortOperation method to change behavior of operation instead
174   void abort();
175
176   /// Commits operation
177   /// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
178   /// Redefine commitOperation method to change behavior of operation instead
179   bool commit();
180
181   /// Alias for start/abort slots
182   /// Public slot. Aborts operation if false, else does nothing.
183   /// Provided for S/S compatibility with QAction's toggle(bool)
184   /// \param theState th flag to abort, if it is true, do nothing, overwise abort
185   void setRunning(bool theState);
186
187  protected:
188   /// Virtual method called when operation started (see start() method for more description)
189   /// Default impl calls corresponding slot and commits immediately.
190    virtual void startOperation() {}
191
192   /// Implementation of specific steps on postpone operation
193   virtual void postponeOperation() {}
194
195   /// Virtual method called when operation stopped - committed or aborted.
196   virtual void stopOperation() {}
197
198   /// Virtual method called when operation aborted (see abort() method for more description)
199   virtual void abortOperation() {}
200
201   /// Virtual method called when operation committed (see commit() method for more description)
202   virtual void commitOperation() {}
203
204   /// Virtual method called after operation committed (see commit() method for more description)
205   virtual void afterCommitOperation() {}
206
207   /// Virtual method called after operation resume (see resume() method for more description)
208   virtual void resumeOperation() {}
209
210   /// Send update message by loop
211   void flushUpdated();
212
213   /// Send created message by loop
214   void flushCreated();
215
216   /// Creates an operation new feature
217   /// \param theFlushMessage the flag whether the create message should be flushed
218   /// \returns the created feature
219   virtual FeaturePtr createFeature(const bool theFlushMessage = true);
220
221   /// Verifies whether this operator can be commited.
222   /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
223   virtual bool canBeCommitted() const;
224
225   /// Returns pointer to the root document.
226   std::shared_ptr<ModelAPI_Document> document() const;
227
228   /// Return a widget value point by the selection and the viewer position
229   /// The default realization returns false
230   /// \param thePrs the presentation
231   /// \param theViewer a viewer to have the viewer the eye position
232   /// \param theX the horizontal coordinate
233   /// \param theY the vertical coordinate
234   /// \return true if the point exits in the selection
235   virtual bool getViewerPoint(ModuleBase_ViewerPrs thePrs,
236                                    ModuleBase_IViewer* theViewer,
237                                    double& theX, double& theY);
238
239   /// Removes the preselection information and clears the map of preselection
240   void clearPreselection();
241
242  protected:
243    /// The operation feature to be handled
244   FeaturePtr myFeature;  
245
246   /// the container to have the operation description
247   ModuleBase_OperationDescription* myDescription;  
248
249   /// Editing feature flag
250   bool myIsEditing;
251
252   /// Modified feature flag
253   bool myIsModified;
254
255   /// List of nested operations IDs
256   QStringList myNestedFeatures;
257
258   /// List of pre-selected object 
259   QList<ModuleBase_ViewerPrs> myPreSelection;
260
261   /// Access to property panel
262   ModuleBase_IPropertyPanel* myPropertyPanel;
263
264   /// If the operation works with feature which is sub-feature of another one
265   /// then this variable has to be initialised by parent feature 
266   /// before operation feature creating
267   CompositeFeaturePtr myParentFeature;  
268
269 };
270
271 #endif