1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_OperationMgr.h
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_OperationMgr.h"
9 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_IWorkshop.h"
11 #include "ModuleBase_IModule.h"
13 #include <QMessageBox>
14 #include <QApplication>
17 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
18 ModuleBase_IWorkshop* theWorkshop)
19 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
20 myWorkshop(theWorkshop)
24 XGUI_OperationMgr::~XGUI_OperationMgr()
28 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
30 return myOperations.count() > 0 ? myOperations.last() : 0;
33 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
37 return currentOperation() == theOperation;
40 bool XGUI_OperationMgr::hasOperation() const
42 return !myOperations.isEmpty() && (myOperations.last() != NULL);
45 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
47 foreach(ModuleBase_Operation* aOp, myOperations) {
48 if (aOp->id() == theId)
54 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
56 foreach(ModuleBase_Operation* aOp, myOperations) {
57 if (aOp->id() == theId)
64 int XGUI_OperationMgr::operationsCount() const
66 return myOperations.count();
69 QStringList XGUI_OperationMgr::operationList() const
72 foreach(ModuleBase_Operation* eachOperation, myOperations) {
73 FeaturePtr aFeature = eachOperation->feature();
75 result << QString::fromStdString(aFeature->getKind());
81 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
83 int idx = myOperations.lastIndexOf(theOperation);
84 if(idx == -1 || idx == 0) {
87 return myOperations.at(idx - 1);
90 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
92 if (theEvent->type() == QEvent::KeyRelease) {
93 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
95 return onKeyReleased(aKeyEvent);
98 return QObject::eventFilter(theObject, theEvent);
101 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
104 currentOperation()->postpone();
105 myOperations.append(theOperation);
107 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
108 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
109 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
110 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
111 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
112 connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
113 connect(theOperation, SIGNAL(activatedByPreselection()),
114 SIGNAL(operationActivatedByPreselection()));
116 theOperation->start();
117 onValidateOperation();
121 bool XGUI_OperationMgr::abortAllOperations()
123 if(!hasOperation()) {
125 } else if (operationsCount() == 1) {
129 QString aMessage = tr("All active operations will be aborted.");
130 int anAnswer = QMessageBox::question(qApp->activeWindow(),
131 tr("Abort operation"),
133 QMessageBox::Ok | QMessageBox::Cancel,
134 QMessageBox::Cancel);
135 bool result = anAnswer == QMessageBox::Ok;
136 while(result && hasOperation()) {
137 currentOperation()->abort();
142 bool XGUI_OperationMgr::commitAllOperations()
144 while (hasOperation()) {
145 if (isApplyEnabled()) {
148 currentOperation()->abort();
154 void XGUI_OperationMgr::onValidateOperation()
158 ModuleBase_Operation* anOperation = currentOperation();
160 bool aCanCommit = myWorkshop->module()->canCommitOperation();
161 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
165 void XGUI_OperationMgr::setLockValidating(bool toLock)
167 myIsValidationLock = toLock;
168 onValidateOperation();
171 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
173 myIsApplyEnabled = theEnabled;
174 emit validationStateChanged(theEnabled);
177 bool XGUI_OperationMgr::isApplyEnabled() const
179 return myIsApplyEnabled;
182 bool XGUI_OperationMgr::canStopOperation()
184 ModuleBase_Operation* anOperation = currentOperation();
185 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
187 if (anOperation && anOperation->isModified()) {
188 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
189 int anAnswer = QMessageBox::question(qApp->activeWindow(),
190 tr("Abort operation"),
192 QMessageBox::Ok | QMessageBox::Cancel,
193 QMessageBox::Cancel);
194 return anAnswer == QMessageBox::Ok;
199 bool XGUI_OperationMgr::commitOperation()
201 if (hasOperation() && currentOperation()->isValid()) {
208 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
210 theOperation->resume();
213 bool XGUI_OperationMgr::canStartOperation(QString theId)
215 bool aCanStart = true;
216 ModuleBase_Operation* aCurrentOp = currentOperation();
218 if (!aCurrentOp->isGranted(theId)) {
219 if (canStopOperation()) {
220 if (myIsApplyEnabled)
221 aCurrentOp->commit();
233 void XGUI_OperationMgr::onCommitOperation()
235 ModuleBase_Operation* anOperation = currentOperation();
237 anOperation->commit();
240 void XGUI_OperationMgr::onAbortOperation()
242 if (hasOperation() && canStopOperation()) {
243 currentOperation()->abort();
247 void XGUI_OperationMgr::onOperationStarted()
249 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
251 // the enable state of the parent operation of the nested one is defined by the rules that
252 // firstly there are nested operations and secondly the parent operation is valid
253 ModuleBase_Operation* aPrevOp;
254 Operations::const_iterator anIt = myOperations.end();
255 if (anIt != myOperations.begin()) { // there are items in the operations list
257 aPrevOp = *anIt; // the last top operation, the operation which is started
258 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
263 bool isNestedOk = (myOperations.count() >= 1) && aPrevOp->isValid();
264 emit nestedStateChanged(isNestedOk);
265 emit operationStarted(aSenderOperation);
268 void XGUI_OperationMgr::onOperationAborted()
270 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
271 emit operationAborted(aSenderOperation);
274 void XGUI_OperationMgr::onOperationCommitted()
276 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
277 emit nestedStateChanged(myOperations.count() >= 1);
278 emit operationCommitted(aSenderOperation);
281 void XGUI_OperationMgr::onOperationResumed()
283 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
284 emit operationResumed(aSenderOperation);
287 void XGUI_OperationMgr::onOperationStopped()
289 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
290 ModuleBase_Operation* aCurrentOperation = currentOperation();
291 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
294 myOperations.removeAll(aCurrentOperation);
295 aCurrentOperation->deleteLater();
297 emit operationStopped(aCurrentOperation);
299 // get last operation which can be resumed
300 ModuleBase_Operation* aResultOp = 0;
301 QListIterator<ModuleBase_Operation*> anIt(myOperations);
303 while (anIt.hasPrevious()) {
304 ModuleBase_Operation* anOp = anIt.previous();
311 bool isModified = aCurrentOperation->isModified();
312 aResultOp->setIsModified(isModified);
313 resumeOperation(aResultOp);
314 onValidateOperation();
318 void XGUI_OperationMgr::onOperationTriggered(bool theState)
320 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
321 if (aSenderOperation && !theState) {
322 ModuleBase_Operation* aCurrentOperation = currentOperation();
323 if (aSenderOperation == aCurrentOperation)
324 aCurrentOperation->abort();
326 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
327 // all operation from the current to triggered should also be aborted
328 while(hasOperation()) {
329 ModuleBase_Operation* aCurrentOperation = currentOperation();
330 aCurrentOperation->abort();
331 if(aSenderOperation == aCurrentOperation)
338 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
340 // Let the manager decide what to do with the given key combination.
341 ModuleBase_Operation* anOperation = currentOperation();
342 bool isAccepted = true;
343 switch (theEvent->key()) {
345 case Qt::Key_Enter: {
346 emit keyEnterReleased();
355 // anOperation->keyReleased(theEvent->key());