]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
7119a8fa19cbf726c364190dd736e9d70cef6f74
[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 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
50 {
51   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
52   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
53
54   theOperation->resume();
55 }
56
57 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
58 {
59   bool aCanStart = true;
60   ModuleBase_Operation* aCurrentOp = currentOperation();
61   if (aCurrentOp && !theOperation->isGranted())
62   {
63     int anAnswer = QMessageBox::question(0, tr("Operation launch"),
64                                 tr("Previous operation is not finished and will be aborted"),
65                                 QMessageBox::Ok, QMessageBox::Cancel);
66     if (anAnswer == QMessageBox::Ok) {
67       aCurrentOp->abort();
68     } else {
69       aCanStart = false;
70     }
71   }
72   return aCanStart;
73 }
74
75 void XGUI_OperationMgr::onOperationStopped()
76 {
77   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
78   ModuleBase_Operation* anOperation = currentOperation();
79   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
80     return;
81
82   emit operationStopped(anOperation);
83
84   myOperations.removeAll(anOperation);
85   anOperation->deleteLater();
86
87   // get last operation which can be resumed
88   ModuleBase_Operation* aResultOp = 0;
89   QListIterator<ModuleBase_Operation*> anIt(myOperations);
90   anIt.toBack();
91   while(anIt.hasPrevious())
92   {
93     ModuleBase_Operation* anOp = anIt.previous();
94     if (anOp) {
95       aResultOp = anOp;
96       break;
97     }
98   }
99   if (aResultOp)
100     resumeOperation(aResultOp);
101 }