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