]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Isssue #149 #147 Restart operation issue resolved
[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 #include <QApplication>
11 #include <QKeyEvent>
12
13 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
14     : QObject(theParent)
15 {
16   // listen to Escape signal to stop the current operation
17   qApp->installEventFilter(this);
18 }
19
20 XGUI_OperationMgr::~XGUI_OperationMgr()
21 {
22 }
23
24 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
25 {
26   return myOperations.count() > 0 ? myOperations.last() : 0;
27 }
28
29 bool XGUI_OperationMgr::hasOperation() const
30 {
31   return (myOperations.count() > 0) && (myOperations.last() != NULL);
32 }
33
34 int XGUI_OperationMgr::operationsCount() const
35 {
36   return myOperations.count();
37 }
38
39 QStringList XGUI_OperationMgr::operationList() const
40 {
41   QStringList result;
42   foreach(ModuleBase_Operation* eachOperation, myOperations) {
43     FeaturePtr aFeature = eachOperation->feature();
44     if(aFeature) {
45       result << QString::fromStdString(aFeature->getKind());
46     }
47   }
48   return result;
49 }
50
51 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
52 {
53   int idx = myOperations.lastIndexOf(theOperation);
54   if(idx == -1 || idx == 0) {
55     return NULL;
56   }
57   return myOperations.at(idx - 1);
58 }
59
60 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
61 {
62   if (theEvent->type() == QEvent::KeyRelease) {
63     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
64     if(aKeyEvent) {
65       onKeyReleased(aKeyEvent);
66       return true;
67     }
68   }
69   return QObject::eventFilter(theObject, theEvent);
70 }
71
72 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
73 {
74   if (!canStartOperation(theOperation))
75     return false;
76
77   myOperations.append(theOperation);
78
79   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
80   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
81   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
82   connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
83           SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
84
85   theOperation->start();
86   onValidateOperation();
87   return true;
88 }
89
90 bool XGUI_OperationMgr::abortAllOperations()
91 {
92   if(!hasOperation()) {
93     return true;
94   } else if (operationsCount() == 1) {
95     onAbortOperation();
96     return true;
97   }
98   QString aMessage = tr("All active operations will be aborted.");
99   int anAnswer = QMessageBox::question(qApp->activeWindow(),
100                                        tr("Abort operation"),
101                                        aMessage,
102                                        QMessageBox::Ok | QMessageBox::Cancel,
103                                        QMessageBox::Cancel);
104   bool result = anAnswer == QMessageBox::Ok;
105   while(result && hasOperation()) {
106     currentOperation()->abort();
107   }
108   return result;
109 }
110
111 void XGUI_OperationMgr::onValidateOperation()
112 {
113   if (!hasOperation())
114     return;
115   ModuleBase_Operation* anOperation = currentOperation();
116   if(anOperation) {
117     bool isValid = anOperation->isValid();
118     emit operationValidated(isValid);
119   }
120 }
121
122 bool XGUI_OperationMgr::commitOperation()
123 {
124   if (hasOperation() && currentOperation()->isValid()) {
125     onCommitOperation();
126     return true;
127   }
128   return false;
129 }
130
131 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
132 {
133   theOperation->resume();
134 }
135
136 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
137 {
138   bool aCanStart = true;
139   ModuleBase_Operation* aCurrentOp = currentOperation();
140   if (aCurrentOp) {
141     if (!aCurrentOp->isGranted(theOperation)) {
142       if (canAbortOperation()) {
143         aCurrentOp->abort();
144       } else {
145         aCanStart = false;
146       }
147     }
148   }
149   return aCanStart;
150 }
151
152
153 void XGUI_OperationMgr::onCommitOperation()
154 {
155   ModuleBase_Operation* anOperation = currentOperation();
156   if (anOperation)
157     anOperation->commit();
158 }
159
160 void XGUI_OperationMgr::onAbortOperation()
161 {
162   if (hasOperation() && canAbortOperation()) {
163     currentOperation()->abort();
164   }
165 }
166
167 bool XGUI_OperationMgr::canAbortOperation()
168 {
169   ModuleBase_Operation* anOperation = currentOperation();
170   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
171     return true;
172   if (anOperation && anOperation->isModified()) {
173     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
174     int anAnswer = QMessageBox::question(qApp->activeWindow(),
175                                          tr("Abort operation"),
176                                          aMessage,
177                                          QMessageBox::Ok | QMessageBox::Cancel,
178                                          QMessageBox::Cancel);
179     return anAnswer == QMessageBox::Ok;
180   }
181   return true;
182 }
183
184 void XGUI_OperationMgr::onOperationStopped()
185 {
186   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
187   ModuleBase_Operation* anOperation = currentOperation();
188   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
189     return;
190
191   myOperations.removeAll(anOperation);
192   anOperation->deleteLater();
193
194   emit operationStopped(anOperation);
195
196   // get last operation which can be resumed
197   ModuleBase_Operation* aResultOp = 0;
198   QListIterator<ModuleBase_Operation*> anIt(myOperations);
199   anIt.toBack();
200   while (anIt.hasPrevious()) {
201     ModuleBase_Operation* anOp = anIt.previous();
202     if (anOp) {
203       aResultOp = anOp;
204       break;
205     }
206   }
207   if (aResultOp) {
208     resumeOperation(aResultOp);
209     onValidateOperation();
210   }
211 }
212
213 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
214 {
215   // Let the manager decide what to do with the given key combination.
216   ModuleBase_Operation* anOperation = currentOperation();
217   bool isRestart = false;
218   switch (theEvent->key()) {
219     case Qt::Key_Escape: {
220       onAbortOperation();
221     }
222       break;
223     case Qt::Key_Return:
224     case Qt::Key_Enter: {
225       if(anOperation) {
226          anOperation->activateNextToCurrentWidget();
227       }
228       commitOperation();
229     }
230       break;
231     default:
232       break;
233   }
234   if(anOperation)
235     anOperation->keyReleased(theEvent->key());
236 }
237
238 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
239 {
240   ModuleBase_Operation* anOperation = currentOperation();
241   if (anOperation)
242     anOperation->onWidgetActivated(theWidget);
243 }