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_ObjectsBrowser.h>
13 #include <ModuleBase_IPropertyPanel.h>
14 #include <ModuleBase_ModelWidget.h>
15 #include "ModuleBase_Operation.h"
16 #include "ModuleBase_IWorkshop.h"
17 #include "ModuleBase_IModule.h"
18 #include <ModuleBase_IViewer.h>
19 #include "ModuleBase_OperationDescription.h"
20 #include "ModuleBase_OperationFeature.h"
21 #include "ModuleBase_Tools.h"
23 #include "ModelAPI_CompositeFeature.h"
24 #include "ModelAPI_Session.h"
26 #include <XGUI_PropertyPanel.h>
27 #include <QToolButton>
29 #include <QMessageBox>
30 #include <QApplication>
33 //#define DEBUG_CURRENT_FEATURE
35 /// Processes "Delete" key event of application. This key is used by several application actions.
36 /// There is a logical order of the actions processing. So the key can not be set for actions
37 /// as a shortcut. The class listens the key event and call operation manager processor.
38 class XGUI_ShortCutListener : public QObject
42 /// \param theParent the parent to be deleted when the parent is deleted
43 /// \param theOperationMgr the class to perform deletion
44 XGUI_ShortCutListener(QObject* theParent, XGUI_OperationMgr* theOperationMgr)
45 : QObject(theParent), myOperationMgr(theOperationMgr)
47 qApp->installEventFilter(this);
49 ~XGUI_ShortCutListener() {}
51 /// Switch on short cut listener
52 void setActive(const bool theIsActive) { myIsActive = theIsActive; }
54 /// Redefinition of virtual function to process Delete key release
55 virtual bool eventFilter(QObject *theObject, QEvent *theEvent)
57 bool isAccepted = false;
58 if (myIsActive && theEvent->type() == QEvent::KeyRelease) {
59 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
61 switch (aKeyEvent->key()) {
62 case Qt::Key_Delete: {
63 isAccepted = myOperationMgr->onProcessDelete(theObject);
69 isAccepted = QObject::eventFilter(theObject, theEvent);
74 XGUI_OperationMgr* myOperationMgr; /// processor for key event
75 bool myIsActive; /// boolean state whether the event filter perform own signal processing
78 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
79 ModuleBase_IWorkshop* theWorkshop)
80 : QObject(theParent), myWorkshop(theWorkshop)
82 /// we need to install filter to the application in order to react to 'Delete' key button
83 /// this key can not be a short cut for a corresponded action because we need to set
84 /// the actions priority
85 myShortCutListener = new XGUI_ShortCutListener(theParent, this);
88 XGUI_OperationMgr::~XGUI_OperationMgr()
92 void XGUI_OperationMgr::activate()
94 myShortCutListener->setActive(true);
97 void XGUI_OperationMgr::deactivate()
99 myShortCutListener->setActive(false);
102 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
104 return myOperations.count() > 0 ? myOperations.last() : 0;
107 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
111 return currentOperation() == theOperation;
114 bool XGUI_OperationMgr::hasOperation() const
116 return !myOperations.isEmpty() && (myOperations.last() != NULL);
119 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
121 foreach(ModuleBase_Operation* aOp, myOperations) {
122 if (aOp->id() == theId)
128 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
130 foreach(ModuleBase_Operation* aOp, myOperations) {
131 if (aOp->id() == theId)
138 int XGUI_OperationMgr::operationsCount() const
140 return myOperations.count();
143 QStringList XGUI_OperationMgr::operationList() const
146 foreach(ModuleBase_Operation* eachOperation, myOperations) {
147 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
149 FeaturePtr aFeature = aFOperation->feature();
151 result << QString::fromStdString(aFeature->getKind());
158 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
160 int idx = myOperations.lastIndexOf(theOperation);
161 if(idx == -1 || idx == 0) {
164 return myOperations.at(idx - 1);
167 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
169 bool isAccepted = false;
170 if (theEvent->type() == QEvent::KeyRelease) {
171 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
173 isAccepted = onKeyReleased(theObject, aKeyEvent);
176 isAccepted = QObject::eventFilter(theObject, theEvent);
181 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
184 currentOperation()->postpone();
185 myOperations.append(theOperation);
187 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
188 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
189 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
191 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
192 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
193 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
195 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
196 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
197 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
200 connect(aFOperation, SIGNAL(activatedByPreselection()),
201 SIGNAL(operationActivatedByPreselection()));
203 bool isStarted = theOperation->start();
205 onValidateOperation();
209 bool XGUI_OperationMgr::abortAllOperations()
215 if (operationsCount() == 1) {
216 ModuleBase_Operation* aCurrentOperation = currentOperation();
217 if (canStopOperation(aCurrentOperation)) {
218 abortOperation(aCurrentOperation);
224 aResult = QMessageBox::question(qApp->activeWindow(),
225 tr("Abort operation"),
226 tr("All active operations will be aborted."),
227 QMessageBox::Ok | QMessageBox::Cancel,
228 QMessageBox::Cancel) == QMessageBox::Ok;
229 while(aResult && hasOperation()) {
230 abortOperation(currentOperation());
236 bool XGUI_OperationMgr::commitAllOperations()
238 bool isCompositeCommitted = false;
239 while (hasOperation()) {
240 ModuleBase_Operation* anOperation = currentOperation();
241 if (workshop()->errorMgr()->isApplyEnabled()) {
244 abortOperation(anOperation);
246 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
249 FeaturePtr aFeature = aFOperation->feature();
250 CompositeFeaturePtr aComposite =
251 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
252 isCompositeCommitted = aComposite.get();
253 if (isCompositeCommitted)
260 void XGUI_OperationMgr::onValidateOperation()
264 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
265 (currentOperation());
266 if(aFOperation && aFOperation->feature().get())
267 workshop()->errorMgr()->updateActions(aFOperation->feature());
270 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
272 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
274 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
276 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
279 foreach(ModuleBase_Operation* anOperation, myOperations) {
281 updateApplyOfOperations(anOperation);
286 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
288 //in case of nested (sketch) operation no confirmation needed
289 if (isGrantedOperation(theOperation->id()))
291 if (theOperation && theOperation->isModified()) {
292 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
293 int anAnswer = QMessageBox::question(qApp->activeWindow(),
294 tr("Abort operation"),
296 QMessageBox::Ok | QMessageBox::Cancel,
297 QMessageBox::Cancel);
298 return anAnswer == QMessageBox::Ok;
303 bool XGUI_OperationMgr::commitOperation()
305 if (hasOperation() && currentOperation()->isValid()) {
312 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
314 theOperation->resume();
317 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
319 bool isGranted = false;
321 QListIterator<ModuleBase_Operation*> anIt(myOperations);
323 ModuleBase_Operation* aPreviousOperation = 0;
324 while (anIt.hasPrevious() && !isGranted) {
325 ModuleBase_Operation* anOp = anIt.previous();
327 isGranted = anOp->isGranted(theId);
332 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
334 SessionPtr aMgr = ModelAPI_Session::get();
335 DocumentPtr aDoc = aMgr->activeDocument();
336 bool aIsOp = aMgr->isOperation();
338 aMgr->startOperation();
339 aDoc->setCurrentFeature(theFeature, false);
341 aMgr->finishOperation();
344 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
346 bool aCanStart = true;
347 ModuleBase_Operation* aCurrentOp = currentOperation();
349 bool aGranted = aCurrentOp->isGranted(theId);
350 // the started operation is granted for the current one,
351 // e.g. current - Sketch, started - Line
356 if (!isGrantedOperation(theId)) {
357 // the operation is not granted in the current list of operations
358 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
359 aCanStart = abortAllOperations();
361 else if (canStopOperation(aCurrentOp)) {
362 // the started operation is granted in the parrent operation,
363 // e.g. current - Line in Sketch, started Circle
364 if (workshop()->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
365 aCurrentOp->commit();
367 abortOperation(aCurrentOp);
376 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
378 ModuleBase_Operation* aCurrentOperation = currentOperation();
379 if (theOperation == aCurrentOperation)
380 theOperation->abort();
382 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
383 // all operation from the current to triggered should also be aborted
384 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
385 while(hasOperation()) {
386 ModuleBase_Operation* aCurrentOperation = currentOperation();
387 aCurrentOperation->abort();
388 if(theOperation == aCurrentOperation)
394 void XGUI_OperationMgr::onCommitOperation()
396 ModuleBase_Operation* anOperation = currentOperation();
398 anOperation->commit();
401 void XGUI_OperationMgr::onAbortOperation()
403 ModuleBase_Operation* aCurrentOperation = currentOperation();
404 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
405 abortOperation(aCurrentOperation);
409 void XGUI_OperationMgr::onBeforeOperationStarted()
411 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
412 if (!aCurrentOperation)
415 /// Set current feature and remeber old current feature
416 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
418 SessionPtr aMgr = ModelAPI_Session::get();
419 DocumentPtr aDoc = aMgr->activeDocument();
420 // the parameter of current feature should be false, we should use all feature, not only visible
421 // in order to correctly save the previous feature of the nested operation, where the
422 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
423 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
424 // When sketch entity operation started, the sketch should be cashed here as the current.
425 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
426 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
427 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
428 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
430 #ifdef DEBUG_CURRENT_FEATURE
431 FeaturePtr aFeature = aFOperation->feature();
432 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
433 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
434 .arg(aFOperation->isEditOperation())
435 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
437 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
438 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
441 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
442 // in create operation, the current feature is changed by addFeature()
443 aDoc->setCurrentFeature(aFOperation->feature(), false);
445 #ifdef DEBUG_CURRENT_FEATURE
446 qDebug("\tdocument->setCurrentFeature");
447 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
448 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
450 ModuleBase_IModule* aModule = myWorkshop->module();
452 aModule->beforeOperationStarted(aFOperation);
456 void XGUI_OperationMgr::onOperationStarted()
458 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
459 updateApplyOfOperations(aSenderOperation);
460 emit operationStarted(aSenderOperation);
463 void XGUI_OperationMgr::onBeforeOperationAborted()
465 onBeforeOperationCommitted();
468 void XGUI_OperationMgr::onOperationAborted()
470 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
471 emit operationAborted(aSenderOperation);
474 void XGUI_OperationMgr::onBeforeOperationCommitted()
476 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
477 if (!aCurrentOperation)
480 /// Restore the previous current feature
481 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
483 #ifdef DEBUG_CURRENT_FEATURE
484 QString aKind = aFOperation->feature()->getKind().c_str();
485 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
486 .arg(aFOperation->isEditOperation())
487 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
489 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
490 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
493 if (aFOperation->isEditOperation()) {
494 /// Restore the previous current feature
495 setCurrentFeature(aFOperation->previousCurrentFeature());
497 else { // create operation
498 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
499 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
500 // in order to perform it in the current transaction without opening a new one.
501 if (myOperations.front() != aFOperation)
502 setCurrentFeature(aFOperation->previousCurrentFeature());
504 #ifdef DEBUG_CURRENT_FEATURE
505 qDebug("\tdocument->setCurrentFeature");
506 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
507 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
509 ModuleBase_IModule* aModule = myWorkshop->module();
511 aModule->beforeOperationStopped(aFOperation);
515 void XGUI_OperationMgr::onOperationCommitted()
517 // apply state for all features from the stack of operations should be updated
518 updateApplyOfOperations();
520 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
521 emit operationCommitted(aSenderOperation);
524 void XGUI_OperationMgr::onOperationResumed()
526 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
527 emit operationResumed(aSenderOperation);
530 void XGUI_OperationMgr::onOperationStopped()
532 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
533 ModuleBase_Operation* aCurrentOperation = currentOperation();
534 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
537 myOperations.removeAll(aCurrentOperation);
538 aCurrentOperation->deleteLater();
540 emit operationStopped(aCurrentOperation);
542 // get last operation which can be resumed
543 ModuleBase_Operation* aResultOp = 0;
544 QListIterator<ModuleBase_Operation*> anIt(myOperations);
546 while (anIt.hasPrevious()) {
547 ModuleBase_Operation* anOp = anIt.previous();
554 bool isModified = aCurrentOperation->isModified();
555 aResultOp->setIsModified(aResultOp->isModified() || isModified);
556 resumeOperation(aResultOp);
557 onValidateOperation();
561 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
563 // Let the manager decide what to do with the given key combination.
564 ModuleBase_Operation* anOperation = currentOperation();
565 bool isAccepted = false;
566 switch (theEvent->key()) {
568 case Qt::Key_Enter: {
569 isAccepted = onProcessEnter(theObject);
574 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
576 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
577 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
578 if (!aContext.IsNull()) {
579 Handle(V3d_View) aView = aViewer->activeView();
580 if ((theEvent->key() == Qt::Key_N))
581 aContext->HilightNextDetected(aView);
582 else if ((theEvent->key() == Qt::Key_P))
583 aContext->HilightPreviousDetected(aView);
594 // anOperation->keyReleased(theEvent->key());
599 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
601 bool isAccepted = false;
602 ModuleBase_Operation* aOperation = currentOperation();
603 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
604 // only property panel enter is processed in order to do not process enter in application dialogs
605 bool isPPChild = isChildObject(theObject, aPanel);
609 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
610 bool isAborted = false;
612 QWidget* aFocusWidget = aPanel->focusWidget();
613 QToolButton* aCancelBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_CANCEL);
614 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
615 abortOperation(aOperation);
621 isAccepted = anActiveWgt && anActiveWgt->processEnter();
623 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
625 /// functionality is similar to Apply click
626 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
627 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
628 // key released is emitted to apply the current value to the model if it was modified in PP
629 emit keyEnterReleased();
641 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
643 bool isAccepted = false;
644 ModuleBase_Operation* aOperation = currentOperation();
645 ModuleBase_ModelWidget* anActiveWgt = 0;
646 // firstly the widget should process Delete action
647 ModuleBase_IPropertyPanel* aPanel;
648 bool isPPChildObject = false;
650 aPanel = aOperation->propertyPanel();
652 isPPChildObject = isChildObject(theObject, aPanel);
653 // process delete in active widget only if delete sender is child of property panel
654 // it is necessary for the case when OB is shown, user perform selection and click Delete
655 if (isPPChildObject) {
656 anActiveWgt = aPanel->activeWidget();
658 isAccepted = anActiveWgt->processDelete();
664 // after widget, object browser and viewer should process delete
665 /// other widgets such as line edit controls should not lead to
666 /// processing delete by workshop
667 XGUI_ObjectsBrowser* aBrowser = workshop()->objectBrowser();
668 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
669 // property panel child object is processed to process delete performed on Apply button of PP
670 if (isChildObject(theObject, aBrowser) ||
671 isChildObject(theObject, aViewPort) ||
673 workshop()->deleteObjects();
680 XGUI_Workshop* XGUI_OperationMgr::workshop() const
682 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
683 return aConnector->workshop();
686 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
688 bool isPPChild = false;
689 if (theParent && theObject) {
690 QObject* aParent = (QObject*)theObject;
692 isPPChild = aParent == theParent;
695 aParent = aParent->parent();