Salome HOME
Merge remote-tracking branch 'remotes/origin/SolveSpace'
[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::onOperationStopped()
91 {
92   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
93   ModuleBase_Operation* anOperation = currentOperation();
94   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
95     return;
96
97   emit operationStopped(anOperation);
98
99   myOperations.removeAll(anOperation);
100   anOperation->deleteLater();
101
102   // get last operation which can be resumed
103   ModuleBase_Operation* aResultOp = 0;
104   QListIterator<ModuleBase_Operation*> anIt(myOperations);
105   anIt.toBack();
106   while(anIt.hasPrevious())
107   {
108     ModuleBase_Operation* anOp = anIt.previous();
109     if (anOp) {
110       aResultOp = anOp;
111       break;
112     }
113   }
114   if (aResultOp)
115     resumeOperation(aResultOp);
116 }