Salome HOME
Debug of movement of part results
[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   if(!hasOperation()) {
127     return true;
128   } else if (operationsCount() == 1) {
129     onAbortOperation();
130     return true;
131   }
132   QString aMessage = tr("All active operations will be aborted.");
133   int anAnswer = QMessageBox::question(qApp->activeWindow(),
134                                        tr("Abort operation"),
135                                        aMessage,
136                                        QMessageBox::Ok | QMessageBox::Cancel,
137                                        QMessageBox::Cancel);
138   bool result = anAnswer == QMessageBox::Ok;
139   while(result && hasOperation()) {
140     currentOperation()->abort();
141   }
142   return result;
143 }
144
145 bool XGUI_OperationMgr::commitAllOperations()
146 {
147   bool isCompositeCommitted = false;
148   while (hasOperation()) {
149     ModuleBase_Operation* anOperation = currentOperation();
150     if (isApplyEnabled()) {
151       onCommitOperation();
152     } else {
153       anOperation->abort();
154     }
155     FeaturePtr aFeature = anOperation->feature();
156     CompositeFeaturePtr aComposite = 
157         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
158     isCompositeCommitted = aComposite.get();
159     if (isCompositeCommitted)
160       break;
161   }
162   return true;
163 }
164
165 void XGUI_OperationMgr::onValidateOperation()
166 {
167   if (!hasOperation())
168     return;
169   ModuleBase_Operation* anOperation = currentOperation();
170   if(anOperation) {
171     bool aCanCommit = myWorkshop->module()->canCommitOperation();
172     setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
173   }
174 }
175
176 void XGUI_OperationMgr::setLockValidating(bool toLock)
177 {
178   myIsValidationLock = toLock;
179   onValidateOperation();
180 }
181
182 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
183 {
184   myIsApplyEnabled = theEnabled;
185   emit validationStateChanged(theEnabled);
186 }
187
188 bool XGUI_OperationMgr::isApplyEnabled() const
189 {
190   return myIsApplyEnabled;
191 }
192
193 bool XGUI_OperationMgr::isParentOperationValid() const
194 {
195   bool isValid = false;
196   // the enable state of the parent operation of the nested one is defined by the rules that
197   // firstly there are nested operations and secondly the parent operation is valid
198   ModuleBase_Operation* aPrevOp = 0;
199   Operations::const_iterator anIt = myOperations.end();
200   if (anIt != myOperations.begin()) { // there are items in the operations list
201     --anIt;
202     aPrevOp = *anIt; // the last top operation, the operation which is started
203     if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
204       --anIt;
205       aPrevOp = *anIt;
206     }
207   }
208   return aPrevOp && aPrevOp->isValid();
209 }
210
211 bool XGUI_OperationMgr::canStopOperation()
212 {
213   ModuleBase_Operation* anOperation = currentOperation();
214   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
215     return true;
216   if (anOperation && anOperation->isModified()) {
217     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
218     int anAnswer = QMessageBox::question(qApp->activeWindow(),
219                                          tr("Abort operation"),
220                                          aMessage,
221                                          QMessageBox::Ok | QMessageBox::Cancel,
222                                          QMessageBox::Cancel);
223     return anAnswer == QMessageBox::Ok;
224   }
225   return true;
226 }
227
228 bool XGUI_OperationMgr::commitOperation()
229 {
230   if (hasOperation() && currentOperation()->isValid()) {
231     onCommitOperation();
232     return true;
233   }
234   return false;
235 }
236
237 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
238 {
239   theOperation->resume();
240 }
241
242 bool XGUI_OperationMgr::canStartOperation(QString theId)
243 {
244   bool aCanStart = true;
245   ModuleBase_Operation* aCurrentOp = currentOperation();
246   if (aCurrentOp) {
247     if (!aCurrentOp->isGranted(theId)) {
248       if (canStopOperation()) {
249         if (myIsApplyEnabled)
250           aCurrentOp->commit();
251         else
252           aCurrentOp->abort();
253       } else {
254         aCanStart = false;
255       }
256     }
257   }
258   return aCanStart;
259 }
260
261
262 void XGUI_OperationMgr::onCommitOperation()
263 {
264   ModuleBase_Operation* anOperation = currentOperation();
265   if (anOperation)
266     anOperation->commit();
267 }
268
269 void XGUI_OperationMgr::onAbortOperation()
270 {
271   if (hasOperation() && canStopOperation()) {
272     currentOperation()->abort();
273   }
274 }
275
276 void XGUI_OperationMgr::onOperationStarted()
277 {
278   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
279   
280   bool aParentValid = isParentOperationValid();
281   // in order to apply is enabled only if there are modifications in the model
282   // e.g. sketch can be applyed only if at least one nested element modification is finished
283   bool aCanUndo = ModelAPI_Session::get()->canUndo();
284   emit nestedStateChanged(aParentValid && aCanUndo);
285
286   emit operationStarted(aSenderOperation);
287 }
288
289 void XGUI_OperationMgr::onOperationAborted()
290 {
291   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
292   emit operationAborted(aSenderOperation);
293 }
294
295 void XGUI_OperationMgr::onOperationCommitted()
296 {
297   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
298   // in order to apply is enabled only if there are modifications in the model
299   // e.g. sketch can be applyed only if at least one nested element create is finished
300   bool aCanUndo = ModelAPI_Session::get()->canUndo();
301   emit nestedStateChanged(myOperations.count() >= 1 && aCanUndo);
302   emit operationCommitted(aSenderOperation);
303 }
304
305 void XGUI_OperationMgr::onOperationResumed()
306 {
307   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
308   emit operationResumed(aSenderOperation);
309 }
310
311 void XGUI_OperationMgr::onOperationStopped()
312 {
313   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
314   ModuleBase_Operation* aCurrentOperation = currentOperation();
315   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
316     return;
317
318   myOperations.removeAll(aCurrentOperation);
319   aCurrentOperation->deleteLater();
320
321   emit operationStopped(aCurrentOperation);
322
323   // get last operation which can be resumed
324   ModuleBase_Operation* aResultOp = 0;
325   QListIterator<ModuleBase_Operation*> anIt(myOperations);
326   anIt.toBack();
327   while (anIt.hasPrevious()) {
328     ModuleBase_Operation* anOp = anIt.previous();
329     if (anOp) {
330       aResultOp = anOp;
331       break;
332     }
333   }
334   if (aResultOp) {
335     bool isModified = aCurrentOperation->isModified();
336     aResultOp->setIsModified(aResultOp->isModified() || isModified);
337     resumeOperation(aResultOp);
338     onValidateOperation();
339   }
340 }
341
342 void XGUI_OperationMgr::onOperationTriggered(bool theState)
343 {
344   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
345   if (aSenderOperation && !theState) {
346     ModuleBase_Operation* aCurrentOperation = currentOperation();
347     if (aSenderOperation == aCurrentOperation)
348       aCurrentOperation->abort();
349     else {
350       // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
351       // all operation from the current to triggered should also be aborted
352       while(hasOperation()) {
353         ModuleBase_Operation* aCurrentOperation = currentOperation();
354         aCurrentOperation->abort();
355         if(aSenderOperation == aCurrentOperation)
356           break;
357       }
358     }
359   }
360 }
361
362 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
363 {
364   // Let the manager decide what to do with the given key combination.
365   ModuleBase_Operation* anOperation = currentOperation();
366   bool isAccepted = true;
367   switch (theEvent->key()) {
368     case Qt::Key_Return:
369     case Qt::Key_Enter: {
370       emit keyEnterReleased();
371       commitOperation();
372     }
373     break;
374     default:
375       isAccepted = false;
376       break;
377   }
378   //if(anOperation) {
379   //  anOperation->keyReleased(theEvent->key());
380   //}
381   return isAccepted;
382 }
383