Salome HOME
Correction tab logic.
[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 #include "XGUI_ModuleConnector.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_ErrorMgr.h"
11
12 #include <ModuleBase_IPropertyPanel.h>
13 #include <ModuleBase_ModelWidget.h>
14 #include "ModuleBase_Operation.h"
15 #include "ModuleBase_IWorkshop.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IViewer.h>
18 #include "ModuleBase_OperationDescription.h"
19 #include "ModuleBase_OperationFeature.h"
20 #include "ModuleBase_Tools.h"
21
22 #include "ModelAPI_CompositeFeature.h"
23 #include "ModelAPI_Session.h"
24
25 #include <QMessageBox>
26 #include <QApplication>
27 #include <QKeyEvent>
28
29 //#define DEBUG_CURRENT_FEATURE
30
31 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
32                                      ModuleBase_IWorkshop* theWorkshop)
33 : QObject(theParent), myWorkshop(theWorkshop)
34 {
35 }
36
37 XGUI_OperationMgr::~XGUI_OperationMgr()
38 {
39 }
40
41 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
42 {
43   return myOperations.count() > 0 ? myOperations.last() : 0;
44 }
45
46 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
47 {
48   if(!hasOperation())
49     return false;
50   return currentOperation() == theOperation;
51 }
52
53 bool XGUI_OperationMgr::hasOperation() const
54 {
55   return !myOperations.isEmpty() && (myOperations.last() != NULL);
56 }
57
58 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
59 {
60   foreach(ModuleBase_Operation* aOp, myOperations) {
61     if (aOp->id() == theId)
62       return true;
63   }
64   return false;
65 }
66
67 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
68 {
69   foreach(ModuleBase_Operation* aOp, myOperations) {
70     if (aOp->id() == theId)
71       return aOp;
72   }
73   return 0;
74 }
75
76
77 int XGUI_OperationMgr::operationsCount() const
78 {
79   return myOperations.count();
80 }
81
82 QStringList XGUI_OperationMgr::operationList() const
83 {
84   QStringList result;
85   foreach(ModuleBase_Operation* eachOperation, myOperations) {
86     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
87     if (aFOperation) {
88       FeaturePtr aFeature = aFOperation->feature();
89       if(aFeature) {
90         result << QString::fromStdString(aFeature->getKind());
91       }
92     }
93   }
94   return result;
95 }
96
97 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
98 {
99   int idx = myOperations.lastIndexOf(theOperation);
100   if(idx == -1 || idx == 0) {
101     return NULL;
102   }
103   return myOperations.at(idx - 1);
104 }
105
106 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
107 {
108   if (theEvent->type() == QEvent::KeyRelease) {
109     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
110     if(aKeyEvent) {
111       return onKeyReleased(aKeyEvent);
112     }
113   }
114   return QObject::eventFilter(theObject, theEvent);
115 }
116
117 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
118 {
119   if (hasOperation())
120     currentOperation()->postpone();
121   myOperations.append(theOperation);
122
123   connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
124   connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
125   connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
126
127   connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
128   connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
129   connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
130
131   connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
132   connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
133   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
134                                                                         (theOperation);
135   if (aFOperation)
136     connect(aFOperation, SIGNAL(activatedByPreselection()),
137             SIGNAL(operationActivatedByPreselection()));
138
139   bool isStarted = theOperation->start();
140   if (isStarted)
141     onValidateOperation();
142   return isStarted;
143 }
144
145 bool XGUI_OperationMgr::abortAllOperations()
146 {
147   bool aResult = true;
148   if(!hasOperation())
149     return aResult;
150
151   if (operationsCount() == 1) {
152     ModuleBase_Operation* aCurrentOperation = currentOperation();
153     if (canStopOperation(aCurrentOperation)) {
154       abortOperation(aCurrentOperation);
155     }
156     else
157       aResult = false;
158   }
159   else {
160     aResult = QMessageBox::question(qApp->activeWindow(),
161                                     tr("Abort operation"),
162                                     tr("All active operations will be aborted."),
163                                     QMessageBox::Ok | QMessageBox::Cancel,
164                                     QMessageBox::Cancel) == QMessageBox::Ok;
165     while(aResult && hasOperation()) {
166       abortOperation(currentOperation());
167     }
168   }
169   return aResult;
170 }
171
172 bool XGUI_OperationMgr::commitAllOperations()
173 {
174   bool isCompositeCommitted = false;
175   while (hasOperation()) {
176     ModuleBase_Operation* anOperation = currentOperation();
177     if (workshop()->errorMgr()->isApplyEnabled()) {
178       onCommitOperation();
179     } else {
180       abortOperation(anOperation);
181     }
182     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
183                                                                             (anOperation);
184     if (aFOperation) {
185       FeaturePtr aFeature = aFOperation->feature();
186       CompositeFeaturePtr aComposite = 
187           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
188       isCompositeCommitted = aComposite.get();
189       if (isCompositeCommitted)
190         break;
191     }
192   }
193   return true;
194 }
195
196 void XGUI_OperationMgr::onValidateOperation()
197 {
198   if (!hasOperation())
199     return;
200   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
201                                                                           (currentOperation());
202   if(aFOperation && aFOperation->feature().get())
203     workshop()->errorMgr()->updateActions(aFOperation->feature());
204 }
205
206 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
207 {
208   XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
209   if (theOperation) {
210     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
211     if (aFOperation)
212       anErrorMgr->updateAcceptAllAction(aFOperation->feature());
213   }
214   else {
215     foreach(ModuleBase_Operation* anOperation, myOperations) {
216       if (anOperation)
217         updateApplyOfOperations(anOperation);
218     }
219   }
220 }
221
222 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
223 {
224   //in case of nested (sketch) operation no confirmation needed
225   if (isGrantedOperation(theOperation->id()))
226     return true;
227   if (theOperation && theOperation->isModified()) {
228     QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
229     int anAnswer = QMessageBox::question(qApp->activeWindow(),
230                                          tr("Abort operation"),
231                                          aMessage,
232                                          QMessageBox::Ok | QMessageBox::Cancel,
233                                          QMessageBox::Cancel);
234     return anAnswer == QMessageBox::Ok;
235   }
236   return true;
237 }
238
239 bool XGUI_OperationMgr::commitOperation()
240 {
241   if (hasOperation() && currentOperation()->isValid()) {
242     onCommitOperation();
243     return true;
244   }
245   return false;
246 }
247
248 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
249 {
250   theOperation->resume();
251 }
252
253 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
254 {
255   bool isGranted = false;
256
257   QListIterator<ModuleBase_Operation*> anIt(myOperations);
258   anIt.toBack();
259   ModuleBase_Operation* aPreviousOperation = 0;
260   while (anIt.hasPrevious() && !isGranted) {
261     ModuleBase_Operation* anOp = anIt.previous();
262     if (anOp)
263       isGranted = anOp->isGranted(theId);
264   }
265   return isGranted;
266 }
267
268 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
269 {
270   SessionPtr aMgr = ModelAPI_Session::get();
271   DocumentPtr aDoc = aMgr->activeDocument();
272   bool aIsOp = aMgr->isOperation();
273   if (!aIsOp)
274     aMgr->startOperation();
275   aDoc->setCurrentFeature(theFeature, false);
276   if (!aIsOp)
277     aMgr->finishOperation();
278 }
279
280 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
281 {
282   bool aCanStart = true;
283   ModuleBase_Operation* aCurrentOp = currentOperation();
284   if (aCurrentOp) {
285     bool aGranted = aCurrentOp->isGranted(theId);
286     // the started operation is granted for the current one,
287     // e.g. current - Sketch, started - Line
288     if (aGranted) {
289       aCanStart = true;
290     }
291     else {
292       if (!isGrantedOperation(theId)) {
293         // the operation is not granted in the current list of operations
294         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
295         aCanStart = abortAllOperations();
296       }
297       else if (canStopOperation(aCurrentOp)) {
298         // the started operation is granted in the parrent operation,
299         // e.g. current - Line in Sketch, started Circle 
300         if (workshop()->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
301           aCurrentOp->commit();
302         else
303           abortOperation(aCurrentOp);
304       } else {
305         aCanStart = false;
306       }
307     }
308   }
309   return aCanStart;
310 }
311
312 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
313 {
314   ModuleBase_Operation* aCurrentOperation = currentOperation();
315   if (theOperation == aCurrentOperation)
316     theOperation->abort();
317   else {
318     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
319     // all operation from the current to triggered should also be aborted
320     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
321     while(hasOperation()) {
322       ModuleBase_Operation* aCurrentOperation = currentOperation();
323       aCurrentOperation->abort();
324       if(theOperation == aCurrentOperation)
325         break;
326     }
327   }
328 }
329
330 void XGUI_OperationMgr::onCommitOperation()
331 {
332   ModuleBase_Operation* anOperation = currentOperation();
333   if (anOperation)
334     anOperation->commit();
335 }
336
337 void XGUI_OperationMgr::onAbortOperation()
338 {
339   ModuleBase_Operation* aCurrentOperation = currentOperation();
340   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
341     abortOperation(aCurrentOperation);
342   }
343 }
344
345 void XGUI_OperationMgr::onBeforeOperationStarted()
346 {
347   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
348   if (!aCurrentOperation)
349     return;
350
351   /// Set current feature and remeber old current feature
352   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
353   if (aFOperation) {
354     SessionPtr aMgr = ModelAPI_Session::get();
355     DocumentPtr aDoc = aMgr->activeDocument();
356     // the parameter of current feature should be false, we should use all feature, not only visible
357     // in order to correctly save the previous feature of the nested operation, where the
358     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
359     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
360     // When sketch entity operation started, the sketch should be cashed here as the current.
361     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
362     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
363     // As a result the sketch disappears from the viewer. However after commit it is displayed back.
364     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
365
366 #ifdef DEBUG_CURRENT_FEATURE
367     FeaturePtr aFeature = aFOperation->feature();
368     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
369     qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
370             .arg(aFOperation->isEditOperation())
371             .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
372
373     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
374             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
375 #endif
376
377     if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
378       // in create operation, the current feature is changed by addFeature()
379       aDoc->setCurrentFeature(aFOperation->feature(), false);
380
381 #ifdef DEBUG_CURRENT_FEATURE
382     qDebug("\tdocument->setCurrentFeature");
383     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
384             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
385 #endif
386   ModuleBase_IModule* aModule = myWorkshop->module();
387   if (aModule)
388     aModule->beforeOperationStarted(aFOperation);
389   }
390 }
391
392 void XGUI_OperationMgr::onOperationStarted()
393 {
394   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
395   updateApplyOfOperations(aSenderOperation);
396   emit operationStarted(aSenderOperation);
397 }
398
399 void XGUI_OperationMgr::onBeforeOperationAborted()
400 {
401   onBeforeOperationCommitted();
402 }
403
404 void XGUI_OperationMgr::onOperationAborted()
405 {
406   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
407   emit operationAborted(aSenderOperation);
408 }
409
410 void XGUI_OperationMgr::onBeforeOperationCommitted()
411 {
412   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
413   if (!aCurrentOperation)
414     return;
415
416   /// Restore the previous current feature
417   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
418   if (aFOperation) {
419 #ifdef DEBUG_CURRENT_FEATURE
420     QString aKind = aFOperation->feature()->getKind().c_str();
421     qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
422             .arg(aFOperation->isEditOperation())
423             .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
424
425     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
426             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
427 #endif
428
429     if (aFOperation->isEditOperation()) {
430       /// Restore the previous current feature
431       setCurrentFeature(aFOperation->previousCurrentFeature());
432     }
433     else { // create operation
434       // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
435       // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
436       // in order to perform it in the current transaction without opening a new one.
437       if (myOperations.front() != aFOperation)
438         setCurrentFeature(aFOperation->previousCurrentFeature());
439     }
440 #ifdef DEBUG_CURRENT_FEATURE
441     qDebug("\tdocument->setCurrentFeature");
442     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
443             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
444 #endif
445     ModuleBase_IModule* aModule = myWorkshop->module();
446     if (aModule)
447       aModule->beforeOperationStopped(aFOperation);
448   }
449 }
450
451 void XGUI_OperationMgr::onOperationCommitted()
452 {
453   // apply state for all features from the stack of operations should be updated
454   updateApplyOfOperations();
455
456   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
457   emit operationCommitted(aSenderOperation);
458 }
459
460 void XGUI_OperationMgr::onOperationResumed()
461 {
462   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
463   emit operationResumed(aSenderOperation);
464 }
465
466 void XGUI_OperationMgr::onOperationStopped()
467 {
468   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
469   ModuleBase_Operation* aCurrentOperation = currentOperation();
470   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
471     return;
472
473   myOperations.removeAll(aCurrentOperation);
474   aCurrentOperation->deleteLater();
475
476   emit operationStopped(aCurrentOperation);
477
478   // get last operation which can be resumed
479   ModuleBase_Operation* aResultOp = 0;
480   QListIterator<ModuleBase_Operation*> anIt(myOperations);
481   anIt.toBack();
482   while (anIt.hasPrevious()) {
483     ModuleBase_Operation* anOp = anIt.previous();
484     if (anOp) {
485       aResultOp = anOp;
486       break;
487     }
488   }
489   if (aResultOp) {
490     bool isModified = aCurrentOperation->isModified();
491     aResultOp->setIsModified(aResultOp->isModified() || isModified);
492     resumeOperation(aResultOp);
493     onValidateOperation();
494   }
495 }
496
497 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
498 {
499   QObject* aSender = sender();
500
501   // Let the manager decide what to do with the given key combination.
502   ModuleBase_Operation* anOperation = currentOperation();
503   bool isAccepted = true;
504   switch (theEvent->key()) {
505     case Qt::Key_Return:
506     case Qt::Key_Enter: {
507       isAccepted = onProcessEnter();
508     }
509     break;
510     case Qt::Key_N:
511     case Qt::Key_P: {
512       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
513       if (noModifiers) {
514         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
515         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
516         if (!aContext.IsNull()) {
517           Handle(V3d_View) aView = aViewer->activeView();
518           if ((theEvent->key() == Qt::Key_N))
519             aContext->HilightNextDetected(aView);
520           else if ((theEvent->key() == Qt::Key_P))
521             aContext->HilightPreviousDetected(aView);
522         }
523       }
524     }
525
526     break;
527     default:
528       isAccepted = false;
529       break;
530   }
531   //if(anOperation) {
532   //  anOperation->keyReleased(theEvent->key());
533   //}
534   return isAccepted;
535 }
536
537 bool XGUI_OperationMgr::onProcessEnter()
538 {
539   bool isAccepted = true;
540   ModuleBase_Operation* aOperation = currentOperation();
541   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
542   ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
543   if (!aActiveWgt || !aActiveWgt->processEnter()) {
544     if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
545       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
546       if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
547         emit keyEnterReleased();
548         commitOperation();
549       }
550       else
551         isAccepted = false;
552     }
553   }
554   return isAccepted;
555 }
556
557 XGUI_Workshop* XGUI_OperationMgr::workshop() const
558 {
559   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
560   return aConnector->workshop();
561 }
562