Salome HOME
Issue #6 Extended processing of nested actions.
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // File:        XGUI_OperationMgr.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "XGUI_OperationMgr.h"
6
7 #include "ModuleBase_Operation.h"
8
9 #include <QMessageBox>
10
11 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
12 : QObject(theParent)
13 {
14 }
15
16 XGUI_OperationMgr::~XGUI_OperationMgr()
17 {
18 }
19
20 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
21 {
22   return myOperations.count() > 0 ? myOperations.last() : 0;
23 }
24
25 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
26 {
27   if (!canStartOperation(theOperation))
28     return false;
29
30   myOperations.append(theOperation);
31
32   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
33   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
34
35   theOperation->start();
36   return true;
37 }
38
39 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
40 {
41   bool aCanStart = true;
42   ModuleBase_Operation* aCurrentOp = currentOperation();
43   if (aCurrentOp && !theOperation->isGranted())
44   {
45     int anAnswer = QMessageBox::question(0, tr("Operation launch"),
46                                 tr("Previous operation is not finished and will be aborted"),
47                                 QMessageBox::Ok, QMessageBox::Cancel);
48     if (anAnswer == QMessageBox::Ok)
49       aCurrentOp->abort();
50     else
51       aCanStart = false;
52   }
53   return aCanStart;
54 }
55
56 void XGUI_OperationMgr::onOperationStopped()
57 {
58   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
59   ModuleBase_Operation* anOperation = currentOperation();
60   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
61     return;
62
63   emit operationStopped(anOperation);
64
65   myOperations.removeAll(anOperation);
66   anOperation->deleteLater();
67
68   // get last operation which can be resumed
69   ModuleBase_Operation* aResultOp = 0;
70   QListIterator<ModuleBase_Operation*> anIt(myOperations);
71   anIt.toBack();
72   while(anIt.hasPrevious())
73   {
74     ModuleBase_Operation* anOp = anIt.previous();
75     if (anOp) {
76       aResultOp = anOp;
77       break;
78     }
79   }
80   if (aResultOp)
81     startOperation(aResultOp);
82 }