Salome HOME
Abort operation correction, Cases are: 1. Deselect feature button in ToolBar, 2....
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_OperationMgr.cpp
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "XGUI_OperationMgr.h"
8
9 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_IWorkshop.h"
11 #include "ModuleBase_IModule.h"
12
13 #include "ModelAPI_CompositeFeature.h"
14 #include "ModelAPI_Session.h"
15
16 #include <QMessageBox>
17 #include <QApplication>
18 #include <QKeyEvent>
19
20 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
21                                      ModuleBase_IWorkshop* theWorkshop)
22 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
23   myWorkshop(theWorkshop)
24 {
25 }
26
27 XGUI_OperationMgr::~XGUI_OperationMgr()
28 {
29 }
30
31 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
32 {
33   return myOperations.count() > 0 ? myOperations.last() : 0;
34 }
35
36 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
37 {
38   if(!hasOperation())
39     return false;
40   return currentOperation() == theOperation;
41 }
42
43 bool XGUI_OperationMgr::hasOperation() const
44 {
45   return !myOperations.isEmpty() && (myOperations.last() != NULL);
46 }
47
48 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
49 {
50   foreach(ModuleBase_Operation* aOp, myOperations) {
51     if (aOp->id() == theId)
52       return true;
53   }
54   return false;
55 }
56
57 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
58 {
59   foreach(ModuleBase_Operation* aOp, myOperations) {
60     if (aOp->id() == theId)
61       return aOp;
62   }
63   return 0;
64 }
65
66
67 int XGUI_OperationMgr::operationsCount() const
68 {
69   return myOperations.count();
70 }
71
72 QStringList XGUI_OperationMgr::operationList() const
73 {
74   QStringList result;
75   foreach(ModuleBase_Operation* eachOperation, myOperations) {
76     FeaturePtr aFeature = eachOperation->feature();
77     if(aFeature) {
78       result << QString::fromStdString(aFeature->getKind());
79     }
80   }
81   return result;
82 }
83
84 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
85 {
86   int idx = myOperations.lastIndexOf(theOperation);
87   if(idx == -1 || idx == 0) {
88     return NULL;
89   }
90   return myOperations.at(idx - 1);
91 }
92
93 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
94 {
95   if (theEvent->type() == QEvent::KeyRelease) {
96     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
97     if(aKeyEvent) {
98       return onKeyReleased(aKeyEvent);
99     }
100   }
101   return QObject::eventFilter(theObject, theEvent);
102 }
103
104 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
105 {
106   if (hasOperation())
107     currentOperation()->postpone();
108   myOperations.append(theOperation);
109
110   connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
111   connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
112   connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
113   connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
114   connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
115   connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
116   connect(theOperation, SIGNAL(activatedByPreselection()),
117           SIGNAL(operationActivatedByPreselection()));
118
119   theOperation->start();
120   onValidateOperation();
121   return true;
122 }
123
124 bool XGUI_OperationMgr::abortAllOperations()
125 {
126   bool aResult = true;
127   if(!hasOperation())
128     return aResult;
129
130   if (operationsCount() == 1) {
131     if (canStopOperation()) {
132       abortOperation(currentOperation());
133     }
134     else
135       aResult = false;
136   }
137   else {
138     aResult = QMessageBox::question(qApp->activeWindow(),
139                                     tr("Abort operation"),
140                                     tr("All active operations will be aborted."),
141                                     QMessageBox::Ok | QMessageBox::Cancel,
142                                     QMessageBox::Cancel) == QMessageBox::Ok;
143     while(aResult && hasOperation()) {
144       abortOperation(currentOperation());
145     }
146   }
147   return aResult;
148 }
149
150 bool XGUI_OperationMgr::commitAllOperations()
151 {
152   bool isCompositeCommitted = false;
153   while (hasOperation()) {
154     ModuleBase_Operation* anOperation = currentOperation();
155     if (isApplyEnabled()) {
156       onCommitOperation();
157     } else {
158       abortOperation(anOperation);
159     }
160     FeaturePtr aFeature = anOperation->feature();
161     CompositeFeaturePtr aComposite = 
162         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
163     isCompositeCommitted = aComposite.get();
164     if (isCompositeCommitted)
165       break;
166   }
167   return true;
168 }
169
170 void XGUI_OperationMgr::onValidateOperation()
171 {
172   if (!hasOperation())
173     return;
174   ModuleBase_Operation* anOperation = currentOperation();
175   if(anOperation) {
176     bool aCanCommit = myWorkshop->module()->canCommitOperation();
177     setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
178   }
179 }
180
181 void XGUI_OperationMgr::setLockValidating(bool toLock)
182 {
183   myIsValidationLock = toLock;
184   onValidateOperation();
185 }
186
187 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
188 {
189   myIsApplyEnabled = theEnabled;
190   emit validationStateChanged(theEnabled);
191 }
192
193 bool XGUI_OperationMgr::isApplyEnabled() const
194 {
195   return myIsApplyEnabled;
196 }
197
198 bool XGUI_OperationMgr::isParentOperationValid() const
199 {
200   bool isValid = false;
201   // the enable state of the parent operation of the nested one is defined by the rules that
202   // firstly there are nested operations and secondly the parent operation is valid
203   ModuleBase_Operation* aPrevOp = 0;
204   Operations::const_iterator anIt = myOperations.end();
205   if (anIt != myOperations.begin()) { // there are items in the operations list
206     --anIt;
207     aPrevOp = *anIt; // the last top operation, the operation which is started
208     if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
209       --anIt;
210       aPrevOp = *anIt;
211     }
212   }
213   return aPrevOp && aPrevOp->isValid();
214 }
215
216 bool XGUI_OperationMgr::canStopOperation()
217 {
218   ModuleBase_Operation* anOperation = currentOperation();
219   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
220     return true;
221   if (anOperation && anOperation->isModified()) {
222     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
223     int anAnswer = QMessageBox::question(qApp->activeWindow(),
224                                          tr("Abort operation"),
225                                          aMessage,
226                                          QMessageBox::Ok | QMessageBox::Cancel,
227                                          QMessageBox::Cancel);
228     return anAnswer == QMessageBox::Ok;
229   }
230   return true;
231 }
232
233 bool XGUI_OperationMgr::commitOperation()
234 {
235   if (hasOperation() && currentOperation()->isValid()) {
236     onCommitOperation();
237     return true;
238   }
239   return false;
240 }
241
242 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
243 {
244   theOperation->resume();
245 }
246
247 bool XGUI_OperationMgr::canStartOperation(QString theId)
248 {
249   bool aCanStart = true;
250   ModuleBase_Operation* aCurrentOp = currentOperation();
251   if (aCurrentOp) {
252     if (!aCurrentOp->isGranted(theId)) {
253       if (canStopOperation()) {
254         if (myIsApplyEnabled)
255           aCurrentOp->commit();
256         else
257           abortOperation(aCurrentOp);
258       } else {
259         aCanStart = false;
260       }
261     }
262   }
263   return aCanStart;
264 }
265
266 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
267 {
268   ModuleBase_Operation* aCurrentOperation = currentOperation();
269   if (theOperation == aCurrentOperation)
270     theOperation->abort();
271   else {
272     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
273     // all operation from the current to triggered should also be aborted
274     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
275     while(hasOperation()) {
276       ModuleBase_Operation* aCurrentOperation = currentOperation();
277       aCurrentOperation->abort();
278       if(theOperation == aCurrentOperation)
279         break;
280     }
281   }
282 }
283
284 void XGUI_OperationMgr::onCommitOperation()
285 {
286   ModuleBase_Operation* anOperation = currentOperation();
287   if (anOperation)
288     anOperation->commit();
289 }
290
291 void XGUI_OperationMgr::onAbortOperation()
292 {
293   if (hasOperation() && canStopOperation()) {
294     abortOperation(currentOperation());
295   }
296 }
297
298 void XGUI_OperationMgr::onOperationStarted()
299 {
300   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
301   
302   bool aParentValid = isParentOperationValid();
303   // in order to apply is enabled only if there are modifications in the model
304   // e.g. sketch can be applyed only if at least one nested element modification is finished
305   bool aCanUndo = ModelAPI_Session::get()->canUndo();
306   emit nestedStateChanged(aParentValid && aCanUndo);
307
308   emit operationStarted(aSenderOperation);
309 }
310
311 void XGUI_OperationMgr::onOperationAborted()
312 {
313   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
314   emit operationAborted(aSenderOperation);
315 }
316
317 void XGUI_OperationMgr::onOperationCommitted()
318 {
319   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
320   // in order to apply is enabled only if there are modifications in the model
321   // e.g. sketch can be applyed only if at least one nested element create is finished
322   bool aCanUndo = ModelAPI_Session::get()->canUndo();
323   emit nestedStateChanged(myOperations.count() >= 1 && aCanUndo);
324   emit operationCommitted(aSenderOperation);
325 }
326
327 void XGUI_OperationMgr::onOperationResumed()
328 {
329   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
330   emit operationResumed(aSenderOperation);
331 }
332
333 void XGUI_OperationMgr::onOperationStopped()
334 {
335   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
336   ModuleBase_Operation* aCurrentOperation = currentOperation();
337   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
338     return;
339
340   myOperations.removeAll(aCurrentOperation);
341   aCurrentOperation->deleteLater();
342
343   emit operationStopped(aCurrentOperation);
344
345   // get last operation which can be resumed
346   ModuleBase_Operation* aResultOp = 0;
347   QListIterator<ModuleBase_Operation*> anIt(myOperations);
348   anIt.toBack();
349   while (anIt.hasPrevious()) {
350     ModuleBase_Operation* anOp = anIt.previous();
351     if (anOp) {
352       aResultOp = anOp;
353       break;
354     }
355   }
356   if (aResultOp) {
357     bool isModified = aCurrentOperation->isModified();
358     aResultOp->setIsModified(aResultOp->isModified() || isModified);
359     resumeOperation(aResultOp);
360     onValidateOperation();
361   }
362 }
363
364 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
365 {
366   // Let the manager decide what to do with the given key combination.
367   ModuleBase_Operation* anOperation = currentOperation();
368   bool isAccepted = true;
369   switch (theEvent->key()) {
370     case Qt::Key_Return:
371     case Qt::Key_Enter: {
372       emit keyEnterReleased();
373       commitOperation();
374     }
375     break;
376     default:
377       isAccepted = false;
378       break;
379   }
380   //if(anOperation) {
381   //  anOperation->keyReleased(theEvent->key());
382   //}
383   return isAccepted;
384 }
385