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