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