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