1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_OperationMgr.cpp
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_OperationMgr.h"
8 #include "XGUI_ModuleConnector.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_ErrorMgr.h"
11 #include "XGUI_Tools.h"
12 #include "XGUI_ObjectsBrowser.h"
13 #include "XGUI_ContextMenuMgr.h"
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_ModelWidget.h>
17 #include "ModuleBase_Operation.h"
18 #include "ModuleBase_IWorkshop.h"
19 #include "ModuleBase_IModule.h"
20 #include <ModuleBase_IViewer.h>
21 #include "ModuleBase_OperationDescription.h"
22 #include "ModuleBase_OperationFeature.h"
23 #include "ModuleBase_Tools.h"
25 #include "ModelAPI_CompositeFeature.h"
26 #include "ModelAPI_Session.h"
28 #include <XGUI_PropertyPanel.h>
29 #include <QToolButton>
31 #include <QMessageBox>
32 #include <QApplication>
35 //#define DEBUG_CURRENT_FEATURE
37 /// Processes "Delete" key event of application. This key is used by several application actions.
38 /// There is a logical order of the actions processing. So the key can not be set for actions
39 /// as a shortcut. The class listens the key event and call operation manager processor.
40 class XGUI_ShortCutListener : public QObject
44 /// \param theParent the parent to be deleted when the parent is deleted
45 /// \param theOperationMgr the class to perform deletion
46 XGUI_ShortCutListener(QObject* theParent, XGUI_OperationMgr* theOperationMgr)
47 : QObject(theParent), myOperationMgr(theOperationMgr)
49 qApp->installEventFilter(this);
51 ~XGUI_ShortCutListener() {}
53 /// Switch on short cut listener
54 void setActive(const bool theIsActive) { myIsActive = theIsActive; }
56 /// Redefinition of virtual function to process Delete key release
57 virtual bool eventFilter(QObject *theObject, QEvent *theEvent)
59 bool isAccepted = false;
60 if (myIsActive && theEvent->type() == QEvent::KeyRelease) {
61 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
63 switch (aKeyEvent->key()) {
64 case Qt::Key_Delete: {
65 isAccepted = myOperationMgr->onProcessDelete(theObject);
71 isAccepted = QObject::eventFilter(theObject, theEvent);
76 XGUI_OperationMgr* myOperationMgr; /// processor for key event
77 bool myIsActive; /// boolean state whether the event filter perform own signal processing
80 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
81 ModuleBase_IWorkshop* theWorkshop)
82 : QObject(theParent), myWorkshop(theWorkshop)
84 /// we need to install filter to the application in order to react to 'Delete' key button
85 /// this key can not be a short cut for a corresponded action because we need to set
86 /// the actions priority
87 myShortCutListener = new XGUI_ShortCutListener(theParent, this);
90 XGUI_OperationMgr::~XGUI_OperationMgr()
94 void XGUI_OperationMgr::activate()
96 myShortCutListener->setActive(true);
99 void XGUI_OperationMgr::deactivate()
101 myShortCutListener->setActive(false);
104 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
106 return myOperations.count() > 0 ? myOperations.last() : 0;
109 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
113 return currentOperation() == theOperation;
116 bool XGUI_OperationMgr::hasOperation() const
118 return !myOperations.isEmpty() && (myOperations.last() != NULL);
121 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
123 foreach(ModuleBase_Operation* aOp, myOperations) {
124 if (aOp->id() == theId)
130 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
132 foreach(ModuleBase_Operation* aOp, myOperations) {
133 if (aOp->id() == theId)
140 int XGUI_OperationMgr::operationsCount() const
142 return myOperations.count();
145 QStringList XGUI_OperationMgr::operationList() const
148 foreach(ModuleBase_Operation* eachOperation, myOperations) {
149 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
151 FeaturePtr aFeature = aFOperation->feature();
153 result << QString::fromStdString(aFeature->getKind());
160 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
162 int idx = myOperations.lastIndexOf(theOperation);
163 if(idx == -1 || idx == 0) {
166 return myOperations.at(idx - 1);
169 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
171 bool isAccepted = false;
172 if (theEvent->type() == QEvent::KeyRelease) {
173 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
175 isAccepted = onKeyReleased(theObject, aKeyEvent);
178 isAccepted = QObject::eventFilter(theObject, theEvent);
183 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
186 currentOperation()->postpone();
187 myOperations.append(theOperation);
189 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
190 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
191 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
193 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
194 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
195 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
197 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
198 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
200 bool isStarted = theOperation->start();
202 onValidateOperation();
206 bool XGUI_OperationMgr::abortAllOperations()
212 if (operationsCount() == 1) {
213 ModuleBase_Operation* aCurrentOperation = currentOperation();
214 if (canStopOperation(aCurrentOperation)) {
215 abortOperation(aCurrentOperation);
221 aResult = QMessageBox::question(qApp->activeWindow(),
222 tr("Abort operation"),
223 tr("All active operations will be aborted."),
224 QMessageBox::Ok | QMessageBox::Cancel,
225 QMessageBox::Cancel) == QMessageBox::Ok;
226 while(aResult && hasOperation()) {
227 abortOperation(currentOperation());
233 bool XGUI_OperationMgr::commitAllOperations()
235 bool isCompositeCommitted = false, anOperationProcessed = false;
236 while (hasOperation()) {
237 ModuleBase_Operation* anOperation = currentOperation();
238 if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled()) {
239 anOperationProcessed = onCommitOperation();
241 abortOperation(anOperation);
242 anOperationProcessed = true;
244 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
247 FeaturePtr aFeature = aFOperation->feature();
248 CompositeFeaturePtr aComposite =
249 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
250 isCompositeCommitted = aComposite.get();
251 if (isCompositeCommitted)
254 // not processed[committed] operation might be used in composite feature,
255 // so the while will be stopped by the previous check.
256 // this code is not necessary, but logically should be done when the processing will not
257 // be done for not composite feature by some reasons
258 if (!anOperationProcessed)
264 void XGUI_OperationMgr::onValidateOperation()
268 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
269 (currentOperation());
270 if(aFOperation && aFOperation->feature().get())
271 XGUI_Tools::workshop(myWorkshop)->errorMgr()->updateActions(aFOperation->feature());
274 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
276 XGUI_ErrorMgr* anErrorMgr = XGUI_Tools::workshop(myWorkshop)->errorMgr();
278 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
280 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
283 foreach(ModuleBase_Operation* anOperation, myOperations) {
285 updateApplyOfOperations(anOperation);
288 // Apply button of the current operation should also be updated
289 onValidateOperation();
292 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
294 //in case of nested (sketch) operation no confirmation needed
295 if (isGrantedOperation(theOperation->id()))
297 if (theOperation && theOperation->isModified()) {
298 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
299 int anAnswer = QMessageBox::question(qApp->activeWindow(),
300 tr("Abort operation"),
302 QMessageBox::Ok | QMessageBox::Cancel,
303 QMessageBox::Cancel);
304 return anAnswer == QMessageBox::Ok;
309 bool XGUI_OperationMgr::commitOperation()
311 //if (hasOperation() && currentOperation()->isValid()) {
312 // onCommitOperation();
316 return onCommitOperation();
319 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
321 theOperation->resume();
324 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
326 bool isGranted = false;
328 QListIterator<ModuleBase_Operation*> anIt(myOperations);
330 ModuleBase_Operation* aPreviousOperation = 0;
331 while (anIt.hasPrevious() && !isGranted) {
332 ModuleBase_Operation* anOp = anIt.previous();
334 isGranted = anOp->isGranted(theId);
339 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
341 SessionPtr aMgr = ModelAPI_Session::get();
342 DocumentPtr aDoc = aMgr->activeDocument();
343 bool aIsOp = aMgr->isOperation();
345 aMgr->startOperation(QString("Set current feature: %1").arg(theFeature->getKind().c_str()).toStdString());
346 aDoc->setCurrentFeature(theFeature, false);
348 aMgr->finishOperation();
351 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
353 bool aCanStart = true;
354 ModuleBase_Operation* aCurrentOp = currentOperation();
356 bool aGranted = aCurrentOp->isGranted(theId);
357 // the started operation is granted for the current one,
358 // e.g. current - Sketch, started - Line
363 if (!isGrantedOperation(theId)) {
364 // the operation is not granted in the current list of operations
365 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
366 aCanStart = abortAllOperations();
368 else if (canStopOperation(aCurrentOp)) {
369 // the started operation is granted in the parrent operation,
370 // e.g. current - Line in Sketch, started Circle
371 stopOperation(aCurrentOp);
380 void XGUI_OperationMgr::stopOperation(ModuleBase_Operation* theOperation)
382 if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() && theOperation->isModified())
383 theOperation->commit();
385 abortOperation(theOperation);
388 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
390 ModuleBase_Operation* aCurrentOperation = currentOperation();
391 if (theOperation == aCurrentOperation)
392 theOperation->abort();
394 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
395 // all operation from the current to triggered should also be aborted
396 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
397 while(hasOperation()) {
398 ModuleBase_Operation* aCurrentOperation = currentOperation();
399 aCurrentOperation->abort();
400 if(theOperation == aCurrentOperation)
406 bool XGUI_OperationMgr::onCommitOperation()
408 bool isCommitted = false;
409 ModuleBase_Operation* anOperation = currentOperation();
410 if (anOperation && myWorkshop->module()->canCommitOperation())
411 isCommitted = anOperation->commit();
415 void XGUI_OperationMgr::onAbortOperation()
417 ModuleBase_Operation* aCurrentOperation = currentOperation();
418 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
419 abortOperation(aCurrentOperation);
423 void XGUI_OperationMgr::onBeforeOperationStarted()
425 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
426 if (!aCurrentOperation)
429 /// Set current feature and remeber old current feature
430 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
432 SessionPtr aMgr = ModelAPI_Session::get();
433 DocumentPtr aDoc = aMgr->activeDocument();
434 // the parameter of current feature should be false, we should use all feature, not only visible
435 // in order to correctly save the previous feature of the nested operation, where the
436 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
437 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
438 // When sketch entity operation started, the sketch should be cashed here as the current.
439 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
440 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
441 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
442 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
444 #ifdef DEBUG_CURRENT_FEATURE
445 FeaturePtr aFeature = aFOperation->feature();
446 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
447 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
448 .arg(aFOperation->isEditOperation())
449 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
451 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
452 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
455 if (aFOperation->isEditOperation()) {// it should be performed by the feature edit only
456 // in create operation, the current feature is changed by addFeature()
457 aDoc->setCurrentFeature(aFOperation->feature(), false);
458 // this is the only place where flushes must be called after setCurrentFeature for the current
459 // moment: after this the opertion is not finished, so, the ObjectBrowser state may be corrupted
461 static Events_Loop* aLoop = Events_Loop::loop();
462 static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
463 aLoop->flush(aCreateEvent);
464 static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
465 aLoop->flush(aDeleteEvent);
468 #ifdef DEBUG_CURRENT_FEATURE
469 qDebug("\tdocument->setCurrentFeature");
470 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
471 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
473 ModuleBase_IModule* aModule = myWorkshop->module();
475 aModule->beforeOperationStarted(aFOperation);
479 void XGUI_OperationMgr::onOperationStarted()
481 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
482 updateApplyOfOperations(aSenderOperation);
483 emit operationStarted(aSenderOperation);
486 void XGUI_OperationMgr::onBeforeOperationAborted()
488 onBeforeOperationCommitted();
491 void XGUI_OperationMgr::onOperationAborted()
493 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
494 emit operationAborted(aSenderOperation);
497 void XGUI_OperationMgr::onBeforeOperationCommitted()
499 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
500 if (!aCurrentOperation)
503 /// Restore the previous current feature
504 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
506 #ifdef DEBUG_CURRENT_FEATURE
507 QString aKind = aFOperation->feature()->getKind().c_str();
508 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
509 .arg(aFOperation->isEditOperation())
510 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
512 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
513 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
516 if (aFOperation->isEditOperation()) {
517 /// Restore the previous current feature
518 setCurrentFeature(aFOperation->previousCurrentFeature());
520 else { // create operation
521 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
522 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
523 // in order to perform it in the current transaction without opening a new one.
524 if (myOperations.front() != aFOperation)
525 setCurrentFeature(aFOperation->previousCurrentFeature());
527 #ifdef DEBUG_CURRENT_FEATURE
528 qDebug("\tdocument->setCurrentFeature");
529 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
530 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
532 ModuleBase_IModule* aModule = myWorkshop->module();
534 aModule->beforeOperationStopped(aFOperation);
538 void XGUI_OperationMgr::onOperationCommitted()
540 // apply state for all features from the stack of operations should be updated
541 updateApplyOfOperations();
543 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
544 emit operationCommitted(aSenderOperation);
547 void XGUI_OperationMgr::onOperationResumed()
549 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
550 emit operationResumed(aSenderOperation);
553 void XGUI_OperationMgr::onOperationStopped()
555 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
556 ModuleBase_Operation* aCurrentOperation = currentOperation();
557 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
560 myOperations.removeAll(aCurrentOperation);
561 aCurrentOperation->deleteLater();
563 emit operationStopped(aCurrentOperation);
565 // get last operation which can be resumed
566 ModuleBase_Operation* aResultOp = 0;
567 QListIterator<ModuleBase_Operation*> anIt(myOperations);
569 while (anIt.hasPrevious()) {
570 ModuleBase_Operation* anOp = anIt.previous();
577 bool isModified = aCurrentOperation->isModified();
578 aResultOp->setIsModified(aResultOp->isModified() || isModified);
579 resumeOperation(aResultOp);
580 onValidateOperation();
584 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
586 // Let the manager decide what to do with the given key combination.
587 ModuleBase_Operation* anOperation = currentOperation();
588 bool isAccepted = false;
589 switch (theEvent->key()) {
591 case Qt::Key_Enter: {
592 isAccepted = onProcessEnter(theObject);
597 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
599 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
600 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
601 if (!aContext.IsNull()) {
602 Handle(V3d_View) aView = aViewer->activeView();
603 if ((theEvent->key() == Qt::Key_N))
604 aContext->HilightNextDetected(aView);
605 else if ((theEvent->key() == Qt::Key_P))
606 aContext->HilightPreviousDetected(aView);
617 // anOperation->keyReleased(theEvent->key());
622 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
624 bool isAccepted = false;
625 ModuleBase_Operation* aOperation = currentOperation();
626 // to avoid enter processing when operation has not been started yet
629 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
630 // only property panel enter is processed in order to do not process enter in application dialogs
631 bool isPPChild = isChildObject(theObject, aPanel);
635 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
636 bool isAborted = false;
638 QWidget* aFocusWidget = aPanel->focusWidget();
639 QToolButton* aCancelBtn = dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
640 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
641 abortOperation(aOperation);
647 isAccepted = anActiveWgt && anActiveWgt->processEnter();
649 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
651 /// functionality is similar to Apply click
652 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
653 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
654 // key released is emitted to apply the current value to the model if it was modified in PP
655 emit keyEnterReleased();
667 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
669 bool isAccepted = false;
670 ModuleBase_Operation* aOperation = currentOperation();
671 ModuleBase_ModelWidget* anActiveWgt = 0;
672 // firstly the widget should process Delete action
673 ModuleBase_IPropertyPanel* aPanel;
674 bool isPPChildObject = false;
676 aPanel = aOperation->propertyPanel();
678 isPPChildObject = isChildObject(theObject, aPanel);
679 // process delete in active widget only if delete sender is child of property panel
680 // it is necessary for the case when OB is shown, user perform selection and click Delete
681 if (isPPChildObject) {
682 anActiveWgt = aPanel->activeWidget();
684 isAccepted = anActiveWgt->processDelete();
690 // after widget, object browser and viewer should process delete
691 /// other widgets such as line edit controls should not lead to
692 /// processing delete by workshop
693 XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
694 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
695 bool isToDeleteObject = false;
696 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
697 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
698 if (theObject == aBrowser->treeView()) {
699 aContextMenuMgr->updateObjectBrowserMenu();
700 isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
702 else if (isChildObject(theObject, aViewPort)) {
703 aContextMenuMgr->updateViewerMenu();
704 isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
706 else if (isPPChildObject) {
707 // property panel child object is processed to process delete performed on Apply button of PP
708 isToDeleteObject = true;
710 if (isToDeleteObject) {
711 aWorkshop->deleteObjects();
719 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
721 bool isPPChild = false;
722 if (theParent && theObject) {
723 QObject* aParent = (QObject*)theObject;
725 isPPChild = aParent == theParent;
728 aParent = aParent->parent();