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