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