]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
e0139d8f511cb460a2e64661f4d1ea1f045a00ce
[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     QString anError = myWorkshop->module()->getFeatureError(aFOperation->feature());
203     if (anError.isEmpty()) {
204       ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
205       if (aPanel) {
206         ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
207         if (anActiveWidget)
208           anError = myWorkshop->module()->getWidgetError(anActiveWidget);
209       }
210     }
211     setApplyEnabled(anError.isEmpty());
212   }
213 }
214
215 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
216 {
217   myIsApplyEnabled = theEnabled;
218   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
219                                                                           (currentOperation());
220   if (aFOperation) {
221     workshop()->errorMgr()->updateActions(aFOperation->feature());
222   }
223   //emit validationStateChanged(theEnabled);
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   }
434 }
435
436 void XGUI_OperationMgr::onOperationStarted()
437 {
438   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
439   updateApplyOfOperations(aSenderOperation);
440   emit operationStarted(aSenderOperation);
441 }
442
443 void XGUI_OperationMgr::onBeforeOperationAborted()
444 {
445   onBeforeOperationCommitted();
446 }
447
448 void XGUI_OperationMgr::onOperationAborted()
449 {
450   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
451   emit operationAborted(aSenderOperation);
452 }
453
454 void XGUI_OperationMgr::onBeforeOperationCommitted()
455 {
456   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
457   if (!aCurrentOperation)
458     return;
459
460   /// Restore the previous current feature
461   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
462   if (aFOperation) {
463 #ifdef DEBUG_CURRENT_FEATURE
464     QString aKind = aFOperation->feature()->getKind().c_str();
465     qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
466             .arg(aFOperation->isEditOperation())
467             .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
468
469     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
470             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
471 #endif
472
473     if (aFOperation->isEditOperation()) {
474       /// Restore the previous current feature
475       setCurrentFeature(aFOperation->previousCurrentFeature());
476     }
477     else { // create operation
478       // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
479       // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
480       // in order to perform it in the current transaction without opening a new one.
481       if (myOperations.front() != aFOperation)
482         setCurrentFeature(aFOperation->previousCurrentFeature());
483     }
484 #ifdef DEBUG_CURRENT_FEATURE
485     qDebug("\tdocument->setCurrentFeature");
486     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
487             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
488 #endif
489   }
490 }
491
492 void XGUI_OperationMgr::onOperationCommitted()
493 {
494   // apply state for all features from the stack of operations should be updated
495   updateApplyOfOperations();
496
497   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
498   emit operationCommitted(aSenderOperation);
499 }
500
501 void XGUI_OperationMgr::onOperationResumed()
502 {
503   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
504   emit operationResumed(aSenderOperation);
505 }
506
507 void XGUI_OperationMgr::onOperationStopped()
508 {
509   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
510   ModuleBase_Operation* aCurrentOperation = currentOperation();
511   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
512     return;
513
514   myOperations.removeAll(aCurrentOperation);
515   aCurrentOperation->deleteLater();
516
517   emit operationStopped(aCurrentOperation);
518
519   // get last operation which can be resumed
520   ModuleBase_Operation* aResultOp = 0;
521   QListIterator<ModuleBase_Operation*> anIt(myOperations);
522   anIt.toBack();
523   while (anIt.hasPrevious()) {
524     ModuleBase_Operation* anOp = anIt.previous();
525     if (anOp) {
526       aResultOp = anOp;
527       break;
528     }
529   }
530   if (aResultOp) {
531     bool isModified = aCurrentOperation->isModified();
532     aResultOp->setIsModified(aResultOp->isModified() || isModified);
533     resumeOperation(aResultOp);
534     onValidateOperation();
535   }
536 }
537
538 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
539 {
540   QObject* aSender = sender();
541
542   // Let the manager decide what to do with the given key combination.
543   ModuleBase_Operation* anOperation = currentOperation();
544   bool isAccepted = true;
545   switch (theEvent->key()) {
546     case Qt::Key_Return:
547     case Qt::Key_Enter: {
548       ModuleBase_Operation* aOperation = currentOperation();
549       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
550       ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
551       if (!aActiveWgt || !aActiveWgt->processEnter()) {
552         if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
553           ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
554           if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
555             emit keyEnterReleased();
556             commitOperation();
557           }
558           else
559             isAccepted = false;
560         }
561       }
562     }
563     break;
564     case Qt::Key_N:
565     case Qt::Key_P: {
566       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
567       if (noModifiers) {
568         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
569         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
570         if (!aContext.IsNull()) {
571           Handle(V3d_View) aView = aViewer->activeView();
572           if ((theEvent->key() == Qt::Key_N))
573             aContext->HilightNextDetected(aView);
574           else if ((theEvent->key() == Qt::Key_P))
575             aContext->HilightPreviousDetected(aView);
576         }
577       }
578     }
579
580     break;
581     default:
582       isAccepted = false;
583       break;
584   }
585   //if(anOperation) {
586   //  anOperation->keyReleased(theEvent->key());
587   //}
588   return isAccepted;
589 }
590
591 XGUI_Workshop* XGUI_OperationMgr::workshop() const
592 {
593   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
594   return aConnector->workshop();
595 }
596