]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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::hasOperation() const
26 {
27   return (myOperations.count() > 0) && (myOperations.last() != NULL);
28 }
29
30 int XGUI_OperationMgr::operationsCount() const
31 {
32   return myOperations.count();
33 }
34
35 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
36 {
37   if (!canStartOperation(theOperation))
38     return false;
39
40   myOperations.append(theOperation);
41
42   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
43   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
44
45   theOperation->start();
46   return true;
47 }
48
49 bool XGUI_OperationMgr::abortOperation()
50 {
51   ModuleBase_Operation* aCurrentOp = currentOperation();
52   if (!aCurrentOp || !canStopOperation())
53     return false; 
54
55   aCurrentOp->abort();
56   return true;
57 }
58
59 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
60 {
61   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
62   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
63
64   theOperation->resume();
65 }
66
67 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
68 {
69   bool aCanStart = true;
70   ModuleBase_Operation* aCurrentOp = currentOperation();
71   if (aCurrentOp && !theOperation->isGranted())
72   {
73     if (canStopOperation()) {
74       aCurrentOp->abort();
75     } else {
76       aCanStart = false;
77     }
78   }
79   return aCanStart;
80 }
81
82 bool XGUI_OperationMgr::canStopOperation()
83 {
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;
88 }
89
90 void XGUI_OperationMgr::onCommitOperation()
91 {
92   ModuleBase_Operation* anOperation = currentOperation();
93   if (anOperation)
94     anOperation->commit();
95 }
96
97 void XGUI_OperationMgr::onAbortOperation()
98 {
99   ModuleBase_Operation* anOperation = currentOperation();
100   if (anOperation)
101     anOperation->abort();
102 }
103
104 void XGUI_OperationMgr::onOperationStopped()
105 {
106   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
107   ModuleBase_Operation* anOperation = currentOperation();
108   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
109     return;
110
111   emit operationStopped(anOperation);
112
113   myOperations.removeAll(anOperation);
114   anOperation->deleteLater();
115
116   // get last operation which can be resumed
117   ModuleBase_Operation* aResultOp = 0;
118   QListIterator<ModuleBase_Operation*> anIt(myOperations);
119   anIt.toBack();
120   while(anIt.hasPrevious())
121   {
122     ModuleBase_Operation* anOp = anIt.previous();
123     if (anOp) {
124       aResultOp = anOp;
125       break;
126     }
127   }
128   if (aResultOp)
129     resumeOperation(aResultOp);
130 }