Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[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   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
45
46   theOperation->start();
47   return true;
48 }
49
50 bool XGUI_OperationMgr::abortOperation()
51 {
52   ModuleBase_Operation* aCurrentOp = currentOperation();
53   if (!aCurrentOp || !canStopOperation())
54     return false; 
55
56   aCurrentOp->abort();
57   return true;
58 }
59
60 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
61 {
62   theOperation->resume();
63 }
64
65 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
66 {
67   bool aCanStart = true;
68   ModuleBase_Operation* aCurrentOp = currentOperation();
69   if (aCurrentOp && !theOperation->isGranted(aCurrentOp))
70   {
71     if (canStopOperation()) {
72       aCurrentOp->abort();
73     } else {
74       aCanStart = false;
75     }
76   }
77   return aCanStart;
78 }
79
80 bool XGUI_OperationMgr::canStopOperation()
81 {
82   int anAnswer = QMessageBox::question(0, tr("Operation launch"),
83                               tr("Previous operation is not finished and will be aborted"),
84                               QMessageBox::Ok, QMessageBox::Cancel);
85   return anAnswer == QMessageBox::Ok;
86 }
87
88 void XGUI_OperationMgr::onCommitOperation()
89 {
90   ModuleBase_Operation* anOperation = currentOperation();
91   if (anOperation)
92     anOperation->commit();
93 }
94
95 void XGUI_OperationMgr::onAbortOperation()
96 {
97   ModuleBase_Operation* anOperation = currentOperation();
98   if (anOperation)
99     anOperation->abort();
100 }
101
102 void XGUI_OperationMgr::onOperationStopped()
103 {
104   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
105   ModuleBase_Operation* anOperation = currentOperation();
106   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
107     return;
108
109   emit operationStopped(anOperation);
110
111   myOperations.removeAll(anOperation);
112   anOperation->deleteLater();
113
114   // get last operation which can be resumed
115   ModuleBase_Operation* aResultOp = 0;
116   QListIterator<ModuleBase_Operation*> anIt(myOperations);
117   anIt.toBack();
118   while(anIt.hasPrevious())
119   {
120     ModuleBase_Operation* anOp = anIt.previous();
121     if (anOp) {
122       aResultOp = anOp;
123       break;
124     }
125   }
126   if (aResultOp)
127     resumeOperation(aResultOp);
128 }