Salome HOME
0bab9abba1584de538b304c1575b67eccb662d53
[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 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
40 {
41   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
42   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
43
44   theOperation->resume();
45 }
46
47 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
48 {
49   bool aCanStart = true;
50   ModuleBase_Operation* aCurrentOp = currentOperation();
51   if (aCurrentOp && !theOperation->isGranted())
52   {
53     int anAnswer = QMessageBox::question(0, tr("Operation launch"),
54                                 tr("Previous operation is not finished and will be aborted"),
55                                 QMessageBox::Ok, QMessageBox::Cancel);
56     if (anAnswer == QMessageBox::Ok) {
57       aCurrentOp->abort();
58     } else {
59       aCanStart = false;
60     }
61   }
62   return aCanStart;
63 }
64
65 void XGUI_OperationMgr::onOperationStopped()
66 {
67   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
68   ModuleBase_Operation* anOperation = currentOperation();
69   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
70     return;
71
72   emit operationStopped(anOperation);
73
74   myOperations.removeAll(anOperation);
75   anOperation->deleteLater();
76
77   // get last operation which can be resumed
78   ModuleBase_Operation* aResultOp = 0;
79   QListIterator<ModuleBase_Operation*> anIt(myOperations);
80   anIt.toBack();
81   while(anIt.hasPrevious())
82   {
83     ModuleBase_Operation* anOp = anIt.previous();
84     if (anOp) {
85       aResultOp = anOp;
86       break;
87     }
88   }
89   if (aResultOp)
90     resumeOperation(aResultOp);
91 }