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