Salome HOME
Issue #2090: Line not selectable when creating an arc by its center
[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   /// Constructor
39   /// \param theParent the parent
40   /// \param theWorkshop a reference to workshop
41   XGUI_OperationMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop);
42
43   /// Destructor
44   virtual ~XGUI_OperationMgr();
45
46   /// Switch on short cut listener
47   void activate();
48   /// Switch off short cut listener
49   void deactivate();
50
51   /// Set reference to workshop
52   /// \param theWorkshop reference to workshop
53   void setWorkshop(ModuleBase_IWorkshop* theWorkshop)
54   { myWorkshop = theWorkshop; };
55
56   /// Current workshop
57   ModuleBase_IWorkshop* workshop() const { return myWorkshop; }
58
59   /// Returns the current operation or NULL
60   /// \return the current operation
61   ModuleBase_Operation* currentOperation() const;
62
63   /// Check if the given operation is active operation.
64   /// Also, returns false is ther is no active operation.
65   bool isCurrentOperation(ModuleBase_Operation* theOperation);
66
67   /// Returns true is operation manager has at least one non-null operation.
68   bool hasOperation() const;
69
70   /// Returns true is operation manager has an operation with given Id.
71   bool hasOperation(const QString& theId) const;
72
73   /// Returns true if the operation can be aborted. If the operation is modified,
74   /// the warning message box is shown.
75   /// \param theOperation an operation which is checked on stop
76   bool canStopOperation(ModuleBase_Operation* theOperation);
77
78   /// Find and return operation by its Id.
79   ModuleBase_Operation* findOperation(const QString& theId) const;
80
81   /// Returns number of operations in the stack
82   int operationsCount() const;
83
84   /// Returns list of all operations IDs
85   QStringList operationList() const;
86
87   /// Returns previous (parent) operation if given operation started.
88   /// else, or if there is no parent - returns NULL
89   ModuleBase_Operation* previousOperation(ModuleBase_Operation* theOperation) const;
90
91   /// Redefinition of virtual function
92   /// \param theObject a sender of the event
93   /// \param theEvent the event
94   virtual bool eventFilter(QObject *theObject, QEvent *theEvent);
95
96   /// Start the operation and append it to the stack of operations
97   /// \param theOperation the started operation
98   /// \return the state whether the current operation is started
99   bool startOperation(ModuleBase_Operation* theOperation);
100
101   /// Returns whether the operation can be started. Check if there is already started operation and
102   /// the granted parameter of the launched operation
103   /// \param theId id of the operation which is going to start
104   /// \param isCommitted boolean value if the operation was committed otherwise it was aborted
105   bool canStartOperation(const QString& theId, bool& isCommitted);
106
107   /// If Apply is enabled and operation has modification, it is applyed, otherwise aborted
108   /// \param theOperation the started operation
109   /// \param isCommitted boolean value if the operation was committed otherwise it was aborted
110   void stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted);
111
112   /// Aborts the parameter operation if it is current, else abort operations from the stack
113   /// of operations until the operation is found. All operations upper the parameter one are
114   /// not aborted.
115   /// \param theOperation an aborted operation
116   void abortOperation(ModuleBase_Operation* theOperation);
117
118   /// Slot that commits the current operation.
119   bool onCommitOperation();
120
121 public slots:
122   /// Slot that aborts the current operation.
123   void onAbortOperation();
124   /// Slot that validates the current operation using the validateOperation method.
125   void onValidateOperation();
126   /// Commit all operations
127   bool commitAllOperations();
128   /// Abort all operations
129   bool abortAllOperations();
130
131 signals:
132   /// Signal about an operation is stopped. It is emitted after the stop() of operation is done.
133   /// \param theOperation a stopped operation
134   void operationStopped(ModuleBase_Operation* theOperation);
135
136   /// Signal about an operation is resumed. It is emitted after the resume() of operation is done.
137   void operationResumed(ModuleBase_Operation* theOperation);
138
139   /// Emitted when current operation is Committed
140   void operationCommitted(ModuleBase_Operation* theOperation);
141
142   /// Emitted when current operation is aborted
143   void operationAborted(ModuleBase_Operation* theOperation);
144
145   /// Signal is emitted after the key released click.
146   void keyEnterReleased();
147
148 public: // TEMPORARY, it should be protected and be performed automatically
149   /// Emits nestedStateChange for operations with an information about validity of the operation
150   /// \param theOperation the sent operation. If it is NULL, all operations in the stack are sent.
151   void updateApplyOfOperations(ModuleBase_Operation* theOperation = 0);
152
153   /// Commits the current operatin if it is valid
154   bool commitOperation();
155
156 protected: // TEMPORARY
157   /// Sets the current operation or NULL
158   /// \param theOperation the started operation
159   void resumeOperation(ModuleBase_Operation* theOperation);
160
161   /// Returns whether the parameter operation is granted in relation to the previous operation
162   /// in a stack of started operations. It is used in canStopOperation to avoid warning message
163   /// when granted operation is aborted, e.g. SketchLine in Sketch
164   /// \param theId id of the operation which is checked
165   /// \return boolean result
166   bool isGrantedOperation(const QString& theId);
167
168   /// Sets the feature as a current in the document
169   /// \param theFeature a feature
170   void setCurrentFeature(const FeaturePtr& theFeature);
171
172  public slots:
173   /// SLOT, that is called by the key in the property panel is clicked.
174   /// \param theObject a sender of the event
175   /// \param theEvent the mouse event
176   bool onKeyReleased(QObject *theObject, QKeyEvent* theEvent);
177
178   /// The functionaly, that should be done by delete click
179   /// Fistly the active widget processes it, then workshop. If no one does not
180   /// process it, do nothing
181   /// \param theObject a sender of the event
182   bool onProcessDelete(QObject* theObject);
183
184   protected slots:
185   /// The functionaly, that should be done by enter click
186   /// Fistly the active widget processes it, then module. If no one does not
187   /// process it, the current operation is committed
188   /// \param theObject a sender of the event
189   bool onProcessEnter(QObject *theObject);
190
191   /// Slot that is called by an operation stop. Removes the stopped operation form the stack.
192   /// If there is a suspended operation, restart it.
193   void onOperationStopped();
194
195   /// Slot called before operation started. Stores the previous current feature, set the feature
196   /// of the operation as a current in the document. The previous current feature should be restored
197   /// by the operation abort/commit
198   void onBeforeOperationStarted();
199
200   /// Slot called after operation started
201   void onOperationStarted();
202
203   /// Slot called before operation aborted. Restore the previous current operation
204   void onBeforeOperationAborted();
205
206   /// Slot called after operation aborted
207   void onOperationAborted();
208
209   /// Slot called before operation committed. Restore the previous current operation
210   void onBeforeOperationCommitted();
211
212   /// Slot called after operation committed
213   void onOperationCommitted();
214
215   /// Slot called on operation resume
216   void onOperationResumed();
217
218 private:
219   /// Checks if the object is a parent or a child under
220   /// \param theObject an investivated object
221   /// \param theParent a candidate to be a parent
222   static bool isChildObject(const QObject* theObject, const QObject* theParent);
223
224  private:
225   typedef QList<ModuleBase_Operation*> Operations;  ///< definition for a list of operations
226   Operations myOperations;  ///< a stack of started operations. The active operation is on top,
227                             // others are suspended and started by the active is finished
228
229   /// Current workshop
230   ModuleBase_IWorkshop* myWorkshop;
231
232   XGUI_ShortCutListener* myShortCutListener;
233 };
234
235 #endif