Salome HOME
Avoid the ability to cancel the current sketch when saving.
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_OperationMgr.h
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #ifndef XGUI_OperationMgr_H
8 #define XGUI_OperationMgr_H
9
10 #include "XGUI.h"
11
12 #include <ModuleBase_Operation.h>
13 #include "ModelAPI_Feature.h"
14
15 #include <QList>
16 #include <QObject>
17 #include <QStringList>
18
19 class QKeyEvent;
20
21 class ModuleBase_IWorkshop;
22 class XGUI_Workshop;
23 class XGUI_ShortCutListener;
24
25 /**\class XGUI_OperationMgr
26  * \ingroup GUI
27  * \brief Operation manager. Servers to manipulate to the workshop operations. Contains a stack
28  * of started operations. In simple case, if only one operation is started, the stack contains
29  * one operation. It is possible for some kind of operations to start them above already
30  * started one. In that case, the previous active operation becomes suspended, a new one - active.
31  * The new operation is added to the top of the stack. Then it is finished, it is removed from
32  * the stack and the previous operation is activated.
33  */
34 class XGUI_EXPORT XGUI_OperationMgr : public QObject
35 {
36 Q_OBJECT
37 public:
38   /// Enumeration of kind of message that is used when trying to stop the active operation
39   enum XGUI_MessageKind
40   {
41     XGUI_AbortOperationMessage, //< warns and give possibility to abort current operation
42     XGUI_InformationMessage //< ask to apply the current operation before performing something
43   };
44
45 public:
46   /// Constructor
47   /// \param theParent the parent
48   /// \param theWorkshop a reference to workshop
49   XGUI_OperationMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop);
50
51   /// Destructor
52   virtual ~XGUI_OperationMgr();
53
54   /// Switch on short cut listener
55   void activate();
56   /// Switch off short cut listener
57   void deactivate();
58
59   /// Set reference to workshop
60   /// \param theWorkshop reference to workshop
61   void setWorkshop(ModuleBase_IWorkshop* theWorkshop)
62   { myWorkshop = theWorkshop; };
63
64   /// Current workshop
65   ModuleBase_IWorkshop* workshop() const { return myWorkshop; }
66
67   /// Returns the current operation or NULL
68   /// \return the current operation
69   ModuleBase_Operation* currentOperation() const;
70
71   /// Check if the given operation is active operation.
72   /// Also, returns false is ther is no active operation.
73   bool isCurrentOperation(ModuleBase_Operation* theOperation);
74
75   /// Returns true is operation manager has at least one non-null operation.
76   bool hasOperation() const;
77
78   /// Returns true is operation manager has an operation with given Id.
79   bool hasOperation(const QString& theId) const;
80
81   /// Returns true if the operation can be aborted. If the operation is modified,
82   /// the warning message box is shown.
83   /// \param theOperation an operation which is checked on stop
84   /// \param theMessageKind a kind of message in warning message box
85   bool canStopOperation(ModuleBase_Operation* theOperation,
86                         const XGUI_MessageKind& theMessageKind = XGUI_AbortOperationMessage);
87
88   /// Find and return operation by its Id.
89   ModuleBase_Operation* findOperation(const QString& theId) const;
90
91   /// Returns number of operations in the stack
92   int operationsCount() const;
93
94   /// Returns list of all operations IDs
95   QStringList operationList() const;
96
97   /// Returns previous (parent) operation if given operation started.
98   /// else, or if there is no parent - returns NULL
99   ModuleBase_Operation* previousOperation(ModuleBase_Operation* theOperation) const;
100
101   /// Redefinition of virtual function
102   /// \param theObject a sender of the event
103   /// \param theEvent the event
104   virtual bool eventFilter(QObject *theObject, QEvent *theEvent);
105
106   /// Start the operation and append it to the stack of operations
107   /// \param theOperation the started operation
108   /// \return the state whether the current operation is started
109   bool startOperation(ModuleBase_Operation* theOperation);
110
111   /// Returns whether the operation can be started. Check if there is already started operation and
112   /// the granted parameter of the launched operation
113   /// \param theId id of the operation which is going to start
114   /// \param isCommitted boolean value if the operation was committed otherwise it was aborted
115   bool canStartOperation(const QString& theId, bool& isCommitted);
116
117   /// If Apply is enabled and operation has modification, it is applyed, otherwise aborted
118   /// \param theOperation the started operation
119   /// \param isCommitted boolean value if the operation was committed otherwise it was aborted
120   void stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted);
121
122   /// Aborts the parameter operation if it is current, else abort operations from the stack
123   /// of operations until the operation is found. All operations upper the parameter one are
124   /// not aborted.
125   /// \param theOperation an aborted operation
126   void abortOperation(ModuleBase_Operation* theOperation);
127
128   bool abortAllOperations(const XGUI_MessageKind& theMessageKind = XGUI_AbortOperationMessage);
129
130 public slots:
131   /// Slot that aborts the current operation.
132   void onAbortOperation();
133   /// Slot that validates the current operation using the validateOperation method.
134   void onValidateOperation();
135   /// Commit all operations
136   bool commitAllOperations();
137   /// Abort all operations
138   void onAbortAllOperations();
139
140 protected slots:
141
142
143 signals:
144   /// Signal about an operation is stopped. It is emitted after the stop() of operation is done.
145   /// \param theOperation a stopped operation
146   void operationStopped(ModuleBase_Operation* theOperation);
147
148   /// Signal about an operation is resumed. It is emitted after the resume() of operation is done.
149   void operationResumed(ModuleBase_Operation* theOperation);
150
151   /// Emitted when current operation is Committed
152   void operationCommitted(ModuleBase_Operation* theOperation);
153
154   /// Emitted when current operation is aborted
155   void operationAborted(ModuleBase_Operation* theOperation);
156
157   /// Signal is emitted after the key released click.
158   void keyEnterReleased();
159
160 public: // TEMPORARY, it should be protected and be performed automatically
161   /// Emits nestedStateChange for operations with an information about validity of the operation
162   /// \param theOperation the sent operation. If it is NULL, all operations in the stack are sent.
163   void updateApplyOfOperations(ModuleBase_Operation* theOperation = 0);
164
165   /// Commits the current operatin if it is valid
166   bool commitOperation();
167
168 protected: // TEMPORARY
169   /// Sets the current operation or NULL
170   /// \param theOperation the started operation
171   void resumeOperation(ModuleBase_Operation* theOperation);
172
173   /// Returns whether the parameter operation is granted in relation to the previous operation
174   /// in a stack of started operations. It is used in canStopOperation to avoid warning message
175   /// when granted operation is aborted, e.g. SketchLine in Sketch
176   /// \param theId id of the operation which is checked
177   /// \return boolean result
178   bool isGrantedOperation(const QString& theId);
179
180   /// Sets the feature as a current in the document
181   /// \param theFeature a feature
182   void setCurrentFeature(const FeaturePtr& theFeature);
183
184  public slots:
185   /// SLOT, that is called by the key in the property panel is clicked.
186   /// \param theObject a sender of the event
187   /// \param theEvent the mouse event
188   bool onKeyReleased(QObject *theObject, QKeyEvent* theEvent);
189
190   /// The functionaly, that should be done by delete click
191   /// Fistly the active widget processes it, then workshop. If no one does not
192   /// process it, do nothing
193   /// \param theObject a sender of the event
194   bool onProcessDelete(QObject* theObject);
195
196   protected slots:
197   /// The functionaly, that should be done by enter click
198   /// Fistly the active widget processes it, then module. If no one does not
199   /// process it, the current operation is committed
200   /// \param theObject a sender of the event
201   bool onProcessEnter(QObject *theObject);
202
203   /// Slot that is called by an operation stop. Removes the stopped operation form the stack.
204   /// If there is a suspended operation, restart it.
205   void onOperationStopped();
206
207   /// Slot called before operation started. Stores the previous current feature, set the feature
208   /// of the operation as a current in the document. The previous current feature should be restored
209   /// by the operation abort/commit
210   void onBeforeOperationStarted();
211
212   /// Slot called after operation started
213   void onOperationStarted();
214
215   /// Slot called before operation aborted. Restore the previous current operation
216   void onBeforeOperationAborted();
217
218   /// Slot called after operation aborted
219   void onOperationAborted();
220
221   /// Slot called before operation committed. Restore the previous current operation
222   void onBeforeOperationCommitted();
223
224   /// Slot called after operation committed
225   void onOperationCommitted();
226
227   /// Slot called on operation resume
228   void onOperationResumed();
229
230 private:
231   /// Checks if the object is a parent or a child under
232   /// \param theObject an investivated object
233   /// \param theParent a candidate to be a parent
234   static bool isChildObject(const QObject* theObject, const QObject* theParent);
235
236  private:
237   typedef QList<ModuleBase_Operation*> Operations;  ///< definition for a list of operations
238   Operations myOperations;  ///< a stack of started operations. The active operation is on top,
239                             // others are suspended and started by the active is finished
240
241   /// Current workshop
242   ModuleBase_IWorkshop* myWorkshop;
243
244   XGUI_ShortCutListener* myShortCutListener;
245 };
246
247 #endif