1 // File: XGUI_OperationMgr.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_OperationMgr.h"
7 #include "ModuleBase_Operation.h"
11 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16 XGUI_OperationMgr::~XGUI_OperationMgr()
20 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
22 return myOperations.count() > 0 ? myOperations.last() : 0;
25 bool XGUI_OperationMgr::hasOperation() const
27 return (myOperations.count() > 0) && (myOperations.last() != NULL);
30 int XGUI_OperationMgr::operationsCount() const
32 return myOperations.count();
35 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
37 if (!canStartOperation(theOperation))
40 myOperations.append(theOperation);
42 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
43 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
45 theOperation->start();
49 bool XGUI_OperationMgr::abortOperation()
51 ModuleBase_Operation* aCurrentOp = currentOperation();
52 if (!aCurrentOp || !canStopOperation())
59 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
61 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
62 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
64 theOperation->resume();
67 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
69 bool aCanStart = true;
70 ModuleBase_Operation* aCurrentOp = currentOperation();
71 if (aCurrentOp && !theOperation->isGranted())
73 if (canStopOperation()) {
82 bool XGUI_OperationMgr::canStopOperation()
84 int anAnswer = QMessageBox::question(0, tr("Operation launch"),
85 tr("Previous operation is not finished and will be aborted"),
86 QMessageBox::Ok, QMessageBox::Cancel);
87 return anAnswer == QMessageBox::Ok;
90 void XGUI_OperationMgr::onCommitOperation()
92 ModuleBase_Operation* anOperation = currentOperation();
94 anOperation->commit();
97 void XGUI_OperationMgr::onAbortOperation()
99 ModuleBase_Operation* anOperation = currentOperation();
101 anOperation->abort();
104 void XGUI_OperationMgr::onOperationStopped()
106 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
107 ModuleBase_Operation* anOperation = currentOperation();
108 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
111 emit operationStopped(anOperation);
113 myOperations.removeAll(anOperation);
114 anOperation->deleteLater();
116 // get last operation which can be resumed
117 ModuleBase_Operation* aResultOp = 0;
118 QListIterator<ModuleBase_Operation*> anIt(myOperations);
120 while(anIt.hasPrevious())
122 ModuleBase_Operation* anOp = anIt.previous();
129 resumeOperation(aResultOp);