Salome HOME
4cf2001e1e28a50ae8ba778c624a1eea9f9de397
[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), myIsApplyEnabled(false), 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   theOperation->start();
140   onValidateOperation();
141   return true;
142 }
143
144 bool XGUI_OperationMgr::abortAllOperations()
145 {
146   bool aResult = true;
147   if(!hasOperation())
148     return aResult;
149
150   if (operationsCount() == 1) {
151     ModuleBase_Operation* aCurrentOperation = currentOperation();
152     if (canStopOperation(aCurrentOperation)) {
153       abortOperation(aCurrentOperation);
154     }
155     else
156       aResult = false;
157   }
158   else {
159     aResult = QMessageBox::question(qApp->activeWindow(),
160                                     tr("Abort operation"),
161                                     tr("All active operations will be aborted."),
162                                     QMessageBox::Ok | QMessageBox::Cancel,
163                                     QMessageBox::Cancel) == QMessageBox::Ok;
164     while(aResult && hasOperation()) {
165       abortOperation(currentOperation());
166     }
167   }
168   return aResult;
169 }
170
171 bool XGUI_OperationMgr::commitAllOperations()
172 {
173   bool isCompositeCommitted = false;
174   while (hasOperation()) {
175     ModuleBase_Operation* anOperation = currentOperation();
176     if (isApplyEnabled()) {
177       onCommitOperation();
178     } else {
179       abortOperation(anOperation);
180     }
181     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
182                                                                             (anOperation);
183     if (aFOperation) {
184       FeaturePtr aFeature = aFOperation->feature();
185       CompositeFeaturePtr aComposite = 
186           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
187       isCompositeCommitted = aComposite.get();
188       if (isCompositeCommitted)
189         break;
190     }
191   }
192   return true;
193 }
194
195 void XGUI_OperationMgr::onValidateOperation()
196 {
197   if (!hasOperation())
198     return;
199   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
200                                                                           (currentOperation());
201   if(aFOperation && aFOperation->feature().get())
202     setApplyEnabled(myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty());
203 }
204
205 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
206 {
207   myIsApplyEnabled = theEnabled;
208   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
209                                                                           (currentOperation());
210   if (aFOperation) {
211     workshop()->errorMgr()->updateActions(aFOperation->feature());
212   }
213   //emit validationStateChanged(theEnabled);
214 }
215
216 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
217 {
218   XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
219   if (theOperation) {
220     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
221     if (aFOperation)
222       anErrorMgr->updateAcceptAllAction(aFOperation->feature());
223     //emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
224     //                        theOperation->isValid());
225   }
226   else {
227     foreach(ModuleBase_Operation* anOperation, myOperations) {
228       if (anOperation)
229         updateApplyOfOperations(anOperation);
230       //emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
231       //                        anOperation->isValid());
232     }
233   }
234 }
235
236 bool XGUI_OperationMgr::isApplyEnabled() const
237 {
238   return myIsApplyEnabled;
239 }
240
241 bool XGUI_OperationMgr::isParentOperationValid() const
242 {
243   bool isValid = false;
244   // the enable state of the parent operation of the nested one is defined by the rules that
245   // firstly there are nested operations and secondly the parent operation is valid
246   ModuleBase_Operation* aPrevOp = 0;
247   Operations::const_iterator anIt = myOperations.end();
248   if (anIt != myOperations.begin()) { // there are items in the operations list
249     --anIt;
250     aPrevOp = *anIt; // the last top operation, the operation which is started
251     if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
252       --anIt;
253       aPrevOp = *anIt;
254     }
255   }
256   return aPrevOp && aPrevOp->isValid();
257 }
258
259 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
260 {
261   //in case of nested (sketch) operation no confirmation needed
262   if (isGrantedOperation(theOperation->id()))
263     return true;
264   if (theOperation && theOperation->isModified()) {
265     QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
266     int anAnswer = QMessageBox::question(qApp->activeWindow(),
267                                          tr("Abort operation"),
268                                          aMessage,
269                                          QMessageBox::Ok | QMessageBox::Cancel,
270                                          QMessageBox::Cancel);
271     return anAnswer == QMessageBox::Ok;
272   }
273   return true;
274 }
275
276 bool XGUI_OperationMgr::commitOperation()
277 {
278   if (hasOperation() && currentOperation()->isValid()) {
279     onCommitOperation();
280     return true;
281   }
282   return false;
283 }
284
285 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
286 {
287   theOperation->resume();
288 }
289
290 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
291 {
292   bool isGranted = false;
293
294   QListIterator<ModuleBase_Operation*> anIt(myOperations);
295   anIt.toBack();
296   ModuleBase_Operation* aPreviousOperation = 0;
297   while (anIt.hasPrevious() && !isGranted) {
298     ModuleBase_Operation* anOp = anIt.previous();
299     if (anOp)
300       isGranted = anOp->isGranted(theId);
301   }
302   return isGranted;
303 }
304
305 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
306 {
307   SessionPtr aMgr = ModelAPI_Session::get();
308   DocumentPtr aDoc = aMgr->activeDocument();
309   bool aIsOp = aMgr->isOperation();
310   if (!aIsOp)
311     aMgr->startOperation();
312   aDoc->setCurrentFeature(theFeature, false);
313   if (!aIsOp)
314     aMgr->finishOperation();
315 }
316
317 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
318 {
319   bool aCanStart = true;
320   ModuleBase_Operation* aCurrentOp = currentOperation();
321   if (aCurrentOp) {
322     bool aGranted = aCurrentOp->isGranted(theId);
323     // the started operation is granted for the current one,
324     // e.g. current - Sketch, started - Line
325     if (aGranted) {
326       aCanStart = true;
327     }
328     else {
329       if (!isGrantedOperation(theId)) {
330         // the operation is not granted in the current list of operations
331         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
332         aCanStart = abortAllOperations();
333       }
334       else if (canStopOperation(aCurrentOp)) {
335         // the started operation is granted in the parrent operation,
336         // e.g. current - Line in Sketch, started Circle 
337         if (myIsApplyEnabled && aCurrentOp->isModified())
338           aCurrentOp->commit();
339         else
340           abortOperation(aCurrentOp);
341       } else {
342         aCanStart = false;
343       }
344     }
345   }
346   return aCanStart;
347 }
348
349 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
350 {
351   ModuleBase_Operation* aCurrentOperation = currentOperation();
352   if (theOperation == aCurrentOperation)
353     theOperation->abort();
354   else {
355     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
356     // all operation from the current to triggered should also be aborted
357     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
358     while(hasOperation()) {
359       ModuleBase_Operation* aCurrentOperation = currentOperation();
360       aCurrentOperation->abort();
361       if(theOperation == aCurrentOperation)
362         break;
363     }
364   }
365 }
366
367 void XGUI_OperationMgr::onCommitOperation()
368 {
369   ModuleBase_Operation* anOperation = currentOperation();
370   if (anOperation)
371     anOperation->commit();
372 }
373
374 void XGUI_OperationMgr::onAbortOperation()
375 {
376   ModuleBase_Operation* aCurrentOperation = currentOperation();
377   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
378     abortOperation(aCurrentOperation);
379   }
380 }
381
382 void XGUI_OperationMgr::onBeforeOperationStarted()
383 {
384   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
385   if (!aCurrentOperation)
386     return;
387
388   /// Set current feature and remeber old current feature
389   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
390   if (aFOperation) {
391     SessionPtr aMgr = ModelAPI_Session::get();
392     DocumentPtr aDoc = aMgr->activeDocument();
393     // the parameter of current feature should be false, we should use all feature, not only visible
394     // in order to correctly save the previous feature of the nested operation, where the
395     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
396     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
397     // When sketch entity operation started, the sketch should be cashed here as the current.
398     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
399     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
400     // As a result the sketch disappears from the viewer. However after commit it is displayed back.
401     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
402
403 #ifdef DEBUG_CURRENT_FEATURE
404     FeaturePtr aFeature = aFOperation->feature();
405     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
406     qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
407             .arg(aFOperation->isEditOperation())
408             .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
409
410     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
411             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
412 #endif
413
414     if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
415       // in create operation, the current feature is changed by addFeature()
416       aDoc->setCurrentFeature(aFOperation->feature(), false);
417
418 #ifdef DEBUG_CURRENT_FEATURE
419     qDebug("\tdocument->setCurrentFeature");
420     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
421             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
422 #endif
423   }
424 }
425
426 void XGUI_OperationMgr::onOperationStarted()
427 {
428   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
429   updateApplyOfOperations(aSenderOperation);
430   emit operationStarted(aSenderOperation);
431 }
432
433 void XGUI_OperationMgr::onBeforeOperationAborted()
434 {
435   onBeforeOperationCommitted();
436 }
437
438 void XGUI_OperationMgr::onOperationAborted()
439 {
440   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
441   emit operationAborted(aSenderOperation);
442 }
443
444 void XGUI_OperationMgr::onBeforeOperationCommitted()
445 {
446   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
447   if (!aCurrentOperation)
448     return;
449
450   /// Restore the previous current feature
451   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
452   if (aFOperation) {
453 #ifdef DEBUG_CURRENT_FEATURE
454     QString aKind = aFOperation->feature()->getKind().c_str();
455     qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
456             .arg(aFOperation->isEditOperation())
457             .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
458
459     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
460             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
461 #endif
462
463     if (aFOperation->isEditOperation()) {
464       /// Restore the previous current feature
465       setCurrentFeature(aFOperation->previousCurrentFeature());
466     }
467     else { // create operation
468       // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
469       // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
470       // in order to perform it in the current transaction without opening a new one.
471       if (myOperations.front() != aFOperation)
472         setCurrentFeature(aFOperation->previousCurrentFeature());
473     }
474 #ifdef DEBUG_CURRENT_FEATURE
475     qDebug("\tdocument->setCurrentFeature");
476     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
477             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
478 #endif
479   }
480 }
481
482 void XGUI_OperationMgr::onOperationCommitted()
483 {
484   // apply state for all features from the stack of operations should be updated
485   updateApplyOfOperations();
486
487   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
488   emit operationCommitted(aSenderOperation);
489 }
490
491 void XGUI_OperationMgr::onOperationResumed()
492 {
493   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
494   emit operationResumed(aSenderOperation);
495 }
496
497 void XGUI_OperationMgr::onOperationStopped()
498 {
499   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
500   ModuleBase_Operation* aCurrentOperation = currentOperation();
501   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
502     return;
503
504   myOperations.removeAll(aCurrentOperation);
505   aCurrentOperation->deleteLater();
506
507   emit operationStopped(aCurrentOperation);
508
509   // get last operation which can be resumed
510   ModuleBase_Operation* aResultOp = 0;
511   QListIterator<ModuleBase_Operation*> anIt(myOperations);
512   anIt.toBack();
513   while (anIt.hasPrevious()) {
514     ModuleBase_Operation* anOp = anIt.previous();
515     if (anOp) {
516       aResultOp = anOp;
517       break;
518     }
519   }
520   if (aResultOp) {
521     bool isModified = aCurrentOperation->isModified();
522     aResultOp->setIsModified(aResultOp->isModified() || isModified);
523     resumeOperation(aResultOp);
524     onValidateOperation();
525   }
526 }
527
528 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
529 {
530   QObject* aSender = sender();
531
532   // Let the manager decide what to do with the given key combination.
533   ModuleBase_Operation* anOperation = currentOperation();
534   bool isAccepted = true;
535   switch (theEvent->key()) {
536     case Qt::Key_Return:
537     case Qt::Key_Enter: {
538       ModuleBase_Operation* aOperation = currentOperation();
539       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
540       ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
541       if (!aActiveWgt || !aActiveWgt->processEnter()) {
542         if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
543           ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
544           if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty()) {
545             emit keyEnterReleased();
546             commitOperation();
547           }
548           else
549             isAccepted = false;
550         }
551       }
552     }
553     break;
554     case Qt::Key_N:
555     case Qt::Key_P: {
556       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
557       if (noModifiers) {
558         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
559         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
560         if (!aContext.IsNull()) {
561           Handle(V3d_View) aView = aViewer->activeView();
562           if ((theEvent->key() == Qt::Key_N))
563             aContext->HilightNextDetected(aView);
564           else if ((theEvent->key() == Qt::Key_P))
565             aContext->HilightPreviousDetected(aView);
566         }
567       }
568     }
569
570     break;
571     default:
572       isAccepted = false;
573       break;
574   }
575   //if(anOperation) {
576   //  anOperation->keyReleased(theEvent->key());
577   //}
578   return isAccepted;
579 }
580
581 XGUI_Workshop* XGUI_OperationMgr::workshop() const
582 {
583   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
584   return aConnector->workshop();
585 }
586