Salome HOME
Providing Action class to have a common approach to start/finish/abort model transact...
[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
14 #include <QList>
15 #include <QObject>
16 #include <QStringList>
17
18 class QKeyEvent;
19
20 class ModuleBase_IWorkshop;
21
22 /**\class XGUI_OperationMgr
23  * \ingroup GUI
24  * \brief Operation manager. Servers to manipulate to the workshop operations. Contains a stack
25  * of started operations. In simple case, if only one operation is started, the stack contains
26  * one operation. It is possible for some kind of operations to start them above already
27  * started one. In that case, the previous active operation becomes suspended, a new one - active.
28  * The new operation is added to the top of the stack. Then it is finished, it is removed from
29  * the stack and the previous operation is activated.
30  */
31 class XGUI_EXPORT XGUI_OperationMgr : public QObject
32 {
33 Q_OBJECT
34  public:
35   /// Constructor
36   /// \param theParent the parent
37   XGUI_OperationMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop);
38   /// Destructor
39   virtual ~XGUI_OperationMgr();
40
41   void setWorkshop(ModuleBase_IWorkshop* theWorkshop)
42   { myWorkshop = theWorkshop; };
43
44   /// Returns the current operation or NULL
45   /// \return the current operation
46   ModuleBase_Operation* currentOperation() const;
47
48   /// Check if the given operation is active operation.
49   /// Also, returns false is ther is no active operation.
50   bool isCurrentOperation(ModuleBase_Operation* theOperation);
51
52   /// Returns true is operation manager has at least one non-null operation.
53   bool hasOperation() const;
54
55   /// Returns true is operation manager has an operation with given Id.
56   bool hasOperation(const QString& theId) const;
57
58   /// Returns true if the operation can be aborted
59   bool canStopOperation();
60
61   /// Find and return operation by its Id.
62   ModuleBase_Operation* findOperation(const QString& theId) const;
63
64   /// Returns number of operations in the stack
65   int operationsCount() const;
66
67   /// Returns list of all operations IDs
68   QStringList operationList() const;
69
70   /// Returns previous (parent) operation if given operation started.
71   /// else, or if there is no parent - returns NULL
72   ModuleBase_Operation* previousOperation(ModuleBase_Operation* theOperation) const;
73
74   /// Redefinition of virtual function
75   virtual bool eventFilter(QObject *theObject, QEvent *theEvent);
76
77   /// Start the operation and append it to the stack of operations
78   /// \param theOperation the started operation
79   /// \return the state whether the current operation is started
80   bool startOperation(ModuleBase_Operation* theOperation);
81
82   /// Returns whether the operation can be started. Check if there is already started operation and
83   /// the granted parameter of the launched operation
84   /// \param theId id of the operation which is going to start
85   bool canStartOperation(QString theId);
86
87   /// Aborts the parameter operation if it is current, else abort operations from the stack
88   /// of operations until the operation is found. All operations upper the parameter one are
89   /// not aborted.
90   /// \param theOperation an aborted operation
91   void abortOperation(ModuleBase_Operation* theOperation);
92
93   /// Blocking/unblocking enabling of Ok button in property panel.
94   /// It is used when operation can not be validated even all attributes are valid
95   void setLockValidating(bool toLock);
96
97   /// Returns state of validation locking
98   bool isValidationLocked() const { return myIsValidationLock; }
99
100   /// Returns enable apply state 
101   /// \return theEnabled a boolean value
102   bool isApplyEnabled() const;
103
104   /// Returns valid state of the parent operation. If the current operation is the last one
105   /// it returns the valid state of the operation
106   /// \return boolean value
107   bool isParentOperationValid() const;
108
109 public slots:
110   /// Slot that commits the current operation.
111   void onCommitOperation();
112   /// Slot that aborts the current operation.
113   void onAbortOperation();
114   /// Slot that validates the current operation using the validateOperation method.
115   void onValidateOperation();
116   /// Commit all operations
117   bool commitAllOperations();
118   /// Abort all operations
119   bool abortAllOperations();
120
121 signals:
122   /// Signal about an operation is started. It is emitted after the start() of operation is done.
123   void operationStarted(ModuleBase_Operation* theOperation);
124
125   /// Signal about an operation is stopped. It is emitted after the stop() of operation is done.
126   /// \param theOperation a stopped operation
127   void operationStopped(ModuleBase_Operation* theOperation);
128
129   /// Signal about an operation is resumed. It is emitted after the resume() of operation is done.
130   void operationResumed(ModuleBase_Operation* theOperation);
131
132   /// Emitted when current operation is Committed
133   void operationCommitted(ModuleBase_Operation* theOperation);
134
135   /// Emitted when current operation is aborted
136   void operationAborted(ModuleBase_Operation* theOperation);
137
138   /// Signal is emitted after the apply enable state changed.
139   void validationStateChanged(bool);
140
141   /// Signal is emitted after the model is modified. It is emitted for all active operations.
142   /// \param theFeatureKind a feature id
143   /// \param theState validity of the operation with the feature kind
144   void nestedStateChanged(const std::string& theFeatureKind, const bool theState);
145
146   /// Signal is emitted after the current operation is filled with existing preselection.
147   void operationActivatedByPreselection();
148
149   /// Signal is emitted after the key released click.
150   void keyEnterReleased();
151
152  protected:
153   /// Sets apply state to the value and emit signal about this state is changed
154   /// \param theEnabled the state value
155   void setApplyEnabled(const bool theEnabled);
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  public slots:
171   /// SLOT, that is called by the key in the property panel is clicked.
172   /// \param theEvent the mouse event
173   bool onKeyReleased(QKeyEvent* theEvent);
174
175   protected slots:
176   /// Slot that is called by an operation stop. Removes the stopped operation form the stack.
177   /// If there is a suspended operation, restart it.
178   void onOperationStopped();
179
180   /// Slot called on operation start
181   void onOperationStarted();
182
183   /// Slot called on operation abort
184   void onOperationAborted();
185
186   /// Slot called on operation commit
187   void onOperationCommitted();
188
189   /// Slot called on operation resume
190   void onOperationResumed();
191
192  private:
193   typedef QList<ModuleBase_Operation*> Operations;  ///< definition for a list of operations
194   Operations myOperations;  ///< a stack of started operations. The active operation is on top,
195                             // others are suspended and started by the active is finished
196
197   /// Current workshop
198   ModuleBase_IWorkshop* myWorkshop;
199
200
201   /// Lock/Unlock access to Ok button in property panel
202   bool myIsValidationLock;
203   /// Lock/Unlock access to Ok button in property panel
204   bool myIsApplyEnabled;
205 };
206
207 #endif