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