Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 #include "XGUI_OperationMgr.h"
2
3 #include "ModuleBase_Operation.h"
4
5 #include <QMessageBox>
6
7 /*!
8  \brief Constructor
9  */
10 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
11 : QObject(theParent)
12 {
13 }
14
15 /*!
16  \brief Destructor
17  */
18 XGUI_OperationMgr::~XGUI_OperationMgr()
19 {
20 }
21
22 /*!
23  \brief Returns the current operation or NULL
24  * \return the current operation
25  */
26 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
27 {
28   return myOperations.count() > 0 ? myOperations.last() : 0;
29 }
30
31 /*!
32  \brief Sets the current operation or NULL
33  * \return the current operation
34  */
35 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
36 {
37   if (!canStartOperation(theOperation))
38     return false;
39
40   myOperations.append(theOperation);
41   emit beforeOperationStart();
42
43   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
44   theOperation->start();
45
46   emit afterOperationStart();
47   return true;
48 }
49
50 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
51 {
52   bool aCanStart = true;
53   ModuleBase_Operation* aCurrentOp = currentOperation();
54   if (aCurrentOp && !theOperation->isGranted())
55   {
56     int anAnswer = QMessageBox::question(0, tr("Operation launch"),
57                                 tr("Previous operation is not finished and will be aborted"),
58                                 QMessageBox::Ok, QMessageBox::Cancel);
59     if (anAnswer == QMessageBox::Ok)
60       aCurrentOp->abort();
61     else
62       aCanStart = false;
63   }
64   return aCanStart;
65 }
66
67 void XGUI_OperationMgr::commitCurrentOperation()
68 {
69   ModuleBase_Operation* anOperation = currentOperation();
70   if (!anOperation)
71     return;
72
73   anOperation->commit();
74 }
75
76 void XGUI_OperationMgr::onOperationStopped()
77 {
78   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
79   ModuleBase_Operation* anOperation = currentOperation();
80   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
81     return;
82
83   myOperations.removeAll(anOperation);
84
85   // get last operation which can be resumed
86   ModuleBase_Operation* aResultOp = 0;
87   QListIterator<ModuleBase_Operation*> anIt(myOperations);
88   anIt.toBack();
89   while(anIt.hasPrevious())
90   {
91     ModuleBase_Operation* anOp = anIt.previous();
92     if (anOp) {
93       aResultOp = anOp;
94       break;
95     }
96   }
97   if (aResultOp)
98     startOperation(aResultOp);
99 }