1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef XGUI_OperationMgr_H
22 #define XGUI_OperationMgr_H
26 #include <ModuleBase_Operation.h>
27 #include "ModelAPI_Feature.h"
31 #include <QStringList>
36 class ModuleBase_IWorkshop;
38 class XGUI_ShortCutListener;
40 /**\class XGUI_OperationMgr
42 * \brief Operation manager. Servers to manipulate to the workshop operations. Contains a stack
43 * of started operations. In simple case, if only one operation is started, the stack contains
44 * one operation. It is possible for some kind of operations to start them above already
45 * started one. In that case, the previous active operation becomes suspended, a new one - active.
46 * The new operation is added to the top of the stack. Then it is finished, it is removed from
47 * the stack and the previous operation is activated.
49 class XGUI_EXPORT XGUI_OperationMgr : public QObject
53 /// Enumeration of kind of message that is used when trying to stop the active operation
56 XGUI_AbortOperationMessage, //< warns and give possibility to abort current operation
57 XGUI_InformationMessage //< ask to apply the current operation before performing something
62 /// \param theParent the parent
63 /// \param theWorkshop a reference to workshop
64 XGUI_OperationMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop);
67 virtual ~XGUI_OperationMgr();
69 /// Switch on short cut listener
71 /// Switch off short cut listener
74 /// Set reference to workshop
75 /// \param theWorkshop reference to workshop
76 void setWorkshop(ModuleBase_IWorkshop* theWorkshop)
77 { myWorkshop = theWorkshop; };
80 ModuleBase_IWorkshop* workshop() const { return myWorkshop; }
82 /// Returns the current operation or NULL
83 /// \return the current operation
84 ModuleBase_Operation* currentOperation() const;
86 /// Check if the given operation is active operation.
87 /// Also, returns false is ther is no active operation.
88 bool isCurrentOperation(ModuleBase_Operation* theOperation);
90 /// Returns true is operation manager has at least one non-null operation.
91 bool hasOperation() const;
93 /// Returns true is operation manager has an operation with given Id.
94 bool hasOperation(const QString& theId) const;
96 /// Returns true if the operation can be aborted. If the operation is modified,
97 /// the warning message box is shown.
98 /// \param theOperation an operation which is checked on stop
99 /// \param theMessageKind a kind of message in warning message box
100 bool canStopOperation(ModuleBase_Operation* theOperation,
101 const XGUI_MessageKind& theMessageKind = XGUI_AbortOperationMessage);
103 /// Find and return operation by its Id.
104 ModuleBase_Operation* findOperation(const QString& theId) const;
106 /// Returns number of operations in the stack
107 int operationsCount() const;
109 /// Returns list of all operations IDs
110 QStringList operationList() const;
112 /// Returns previous (parent) operation if given operation started.
113 /// else, or if there is no parent - returns NULL
114 ModuleBase_Operation* previousOperation(ModuleBase_Operation* theOperation) const;
116 /// Returns an active widget of the current operation.
117 /// \return widget or NULL
118 ModuleBase_ModelWidget* activeWidget() const;
120 /// Start the operation and append it to the stack of operations
121 /// \param theOperation the started operation
122 /// \return the state whether the current operation is started
123 bool startOperation(ModuleBase_Operation* theOperation);
125 /// Returns whether the operation can be started. Check if there is already started operation and
126 /// the granted parameter of the launched operation
127 /// \param theId id of the operation which is going to start
128 /// \param isCommitted boolean value if the operation was committed otherwise it was aborted
129 bool canStartOperation(const QString& theId, bool& isCommitted);
131 /// If Apply is enabled and operation has modification, it is applyed, otherwise aborted
132 /// \param theOperation the started operation
133 /// \param isCommitted boolean value if the operation was committed otherwise it was aborted
134 void stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted);
136 /// Aborts the parameter operation if it is current, else abort operations from the stack
137 /// of operations until the operation is found. All operations upper the parameter one are
139 /// \param theOperation an aborted operation
140 void abortOperation(ModuleBase_Operation* theOperation);
142 /// Abort all operations
143 /// \param theMessageKind kind of shown warning message
144 bool abortAllOperations(const XGUI_MessageKind& theMessageKind = XGUI_AbortOperationMessage);
146 /// Commits the current operation.
147 bool commitOperation();
149 /// Returns true if SHIFT is pressed
150 /// \param thePressed new boolean state
151 void setSHIFTPressed(const bool thePressed) { mySHIFTPressed = thePressed; }
153 /// Returns true if SHIFT is pressed
154 /// \return boolean value
155 bool hasSHIFTPressed() const { return mySHIFTPressed; }
158 /// Slot that aborts the current operation.
159 void onAbortOperation();
160 /// Slot that aborts all operations. It shows aborting message
161 void onAbortAllOperation();
162 /// Slot that validates the current operation using the validateOperation method.
163 void onValidateOperation();
164 /// Commit all operations
165 bool commitAllOperations();
168 /// Signal about an operation is stopped. It is emitted after the stop() of operation is done.
169 /// \param theOperation a stopped operation
170 void operationStopped(ModuleBase_Operation* theOperation);
172 /// Signal about an operation is resumed. It is emitted after the resume() of operation is done.
173 void operationResumed(ModuleBase_Operation* theOperation);
175 /// Emitted when current operation is Committed
176 void operationCommitted(ModuleBase_Operation* theOperation);
178 /// Emitted when current operation is aborted
179 void operationAborted(ModuleBase_Operation* theOperation);
181 /// Signal is emitted after the key released click.
182 void keyEnterReleased();
184 public: // TEMPORARY, it should be protected and be performed automatically
185 /// Emits nestedStateChange for operations with an information about validity of the operation
186 /// \param theOperation the sent operation. If it is NULL, all operations in the stack are sent.
187 void updateApplyOfOperations(ModuleBase_Operation* theOperation = 0);
189 protected: // TEMPORARY
190 /// Sets the current operation or NULL
191 /// \param theOperation the started operation
192 void resumeOperation(ModuleBase_Operation* theOperation);
194 /// Returns whether the parameter operation is granted in relation to the previous operation
195 /// in a stack of started operations. It is used in canStopOperation to avoid warning message
196 /// when granted operation is aborted, e.g. SketchLine in Sketch
197 /// \param theId id of the operation which is checked
198 /// \return boolean result
199 bool isGrantedOperation(const QString& theId);
201 /// Sets the feature as a current in the document
202 /// \param theFeature a feature
203 void setCurrentFeature(const FeaturePtr& theFeature);
206 /// SLOT, that is called by the key in the property panel is clicked.
207 /// \param theObject a sender of the event
208 /// \param theEvent the mouse event
209 bool onKeyReleased(QObject *theObject, QKeyEvent* theEvent);
211 /// SLOT, that is called by the key in the property panel is clicked.
212 /// \param theObject a sender of the event
213 /// \param theEvent the mouse event
214 bool onKeyPressed(QObject *theObject, QKeyEvent* theEvent);
216 /// The functionaly, that should be done by delete click
217 /// Fistly the active widget processes it, then workshop. If no one does not
218 /// process it, do nothing
219 /// \param theObject a sender of the event
220 bool onProcessDelete(QObject* theObject);
223 /// The functionaly, that should be done by enter click
224 /// Fistly the active widget processes it, then module. If no one does not
225 /// process it, the current operation is committed
226 /// \param theObject a sender of the event
227 bool onProcessEnter(QObject *theObject);
229 /// Slot that is called by an operation stop. Removes the stopped operation form the stack.
230 /// If there is a suspended operation, restart it.
231 void onOperationStopped();
233 /// Slot called before operation started. Stores the previous current feature, set the feature
234 /// of the operation as a current in the document. The previous current feature should be restored
235 /// by the operation abort/commit
236 void onBeforeOperationStarted();
238 /// Slot called after operation started
239 void onOperationStarted();
241 /// Slot called before operation aborted. Restore the previous current operation
242 void onBeforeOperationAborted();
244 /// Slot called after operation aborted
245 void onOperationAborted();
247 /// Slot called before operation committed. Restore the previous current operation
248 void onBeforeOperationCommitted();
250 /// Slot called after operation committed
251 void onOperationCommitted();
253 /// Slot called on operation resume
254 void onOperationResumed();
257 /// Checks if the object is a parent or a child under
258 /// \param theObject an investivated object
259 /// \param theParent a candidate to be a parent
260 static bool isChildObject(const QObject* theObject, const QObject* theParent);
262 /// Creates question message box with OK/Cancel buttons, where Cancel is default button,
263 /// Escape is Null button
264 /// \param theMessage text of the message
265 /// \return message box
266 static QMessageBox* createMessageBox(const QString& theMessage);
268 /// Creates information message box with OK button,
269 /// Escape is Null button
270 /// \param theMessage text of the message
271 /// \return message box
272 static QMessageBox* createInformationBox(const QString& theMessage);
275 typedef QList<ModuleBase_Operation*> Operations; ///< definition for a list of operations
276 Operations myOperations; ///< a stack of started operations. The active operation is on top,
277 // others are suspended and started by the active is finished
280 ModuleBase_IWorkshop* myWorkshop;
281 QMessageBox* myActiveMessageBox;
282 XGUI_ShortCutListener* myShortCutListener;