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"
14 #include <ModuleBase_IPropertyPanel.h>
15 #include <ModuleBase_ModelWidget.h>
16 #include "ModuleBase_Operation.h"
17 #include "ModuleBase_IWorkshop.h"
18 #include "ModuleBase_IModule.h"
19 #include <ModuleBase_IViewer.h>
20 #include "ModuleBase_OperationDescription.h"
21 #include "ModuleBase_OperationFeature.h"
22 #include "ModuleBase_Tools.h"
24 #include "ModelAPI_CompositeFeature.h"
25 #include "ModelAPI_Session.h"
27 #include <XGUI_PropertyPanel.h>
28 #include <QToolButton>
30 #include <QMessageBox>
31 #include <QApplication>
34 //#define DEBUG_CURRENT_FEATURE
36 /// Processes "Delete" key event of application. This key is used by several application actions.
37 /// There is a logical order of the actions processing. So the key can not be set for actions
38 /// as a shortcut. The class listens the key event and call operation manager processor.
39 class XGUI_ShortCutListener : public QObject
43 /// \param theParent the parent to be deleted when the parent is deleted
44 /// \param theOperationMgr the class to perform deletion
45 XGUI_ShortCutListener(QObject* theParent, XGUI_OperationMgr* theOperationMgr)
46 : QObject(theParent), myOperationMgr(theOperationMgr)
48 qApp->installEventFilter(this);
50 ~XGUI_ShortCutListener() {}
52 /// Switch on short cut listener
53 void setActive(const bool theIsActive) { myIsActive = theIsActive; }
55 /// Redefinition of virtual function to process Delete key release
56 virtual bool eventFilter(QObject *theObject, QEvent *theEvent)
58 bool isAccepted = false;
59 if (myIsActive && theEvent->type() == QEvent::KeyRelease) {
60 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
62 switch (aKeyEvent->key()) {
63 case Qt::Key_Delete: {
64 isAccepted = myOperationMgr->onProcessDelete(theObject);
70 isAccepted = QObject::eventFilter(theObject, theEvent);
75 XGUI_OperationMgr* myOperationMgr; /// processor for key event
76 bool myIsActive; /// boolean state whether the event filter perform own signal processing
79 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
80 ModuleBase_IWorkshop* theWorkshop)
81 : QObject(theParent), myWorkshop(theWorkshop)
83 /// we need to install filter to the application in order to react to 'Delete' key button
84 /// this key can not be a short cut for a corresponded action because we need to set
85 /// the actions priority
86 myShortCutListener = new XGUI_ShortCutListener(theParent, this);
89 XGUI_OperationMgr::~XGUI_OperationMgr()
93 void XGUI_OperationMgr::activate()
95 myShortCutListener->setActive(true);
98 void XGUI_OperationMgr::deactivate()
100 myShortCutListener->setActive(false);
103 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
105 return myOperations.count() > 0 ? myOperations.last() : 0;
108 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
112 return currentOperation() == theOperation;
115 bool XGUI_OperationMgr::hasOperation() const
117 return !myOperations.isEmpty() && (myOperations.last() != NULL);
120 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
122 foreach(ModuleBase_Operation* aOp, myOperations) {
123 if (aOp->id() == theId)
129 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
131 foreach(ModuleBase_Operation* aOp, myOperations) {
132 if (aOp->id() == theId)
139 int XGUI_OperationMgr::operationsCount() const
141 return myOperations.count();
144 QStringList XGUI_OperationMgr::operationList() const
147 foreach(ModuleBase_Operation* eachOperation, myOperations) {
148 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
150 FeaturePtr aFeature = aFOperation->feature();
152 result << QString::fromStdString(aFeature->getKind());
159 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
161 int idx = myOperations.lastIndexOf(theOperation);
162 if(idx == -1 || idx == 0) {
165 return myOperations.at(idx - 1);
168 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
170 bool isAccepted = false;
171 if (theEvent->type() == QEvent::KeyRelease) {
172 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
174 isAccepted = onKeyReleased(theObject, aKeyEvent);
177 isAccepted = QObject::eventFilter(theObject, theEvent);
182 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
185 currentOperation()->postpone();
186 myOperations.append(theOperation);
188 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
189 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
190 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
192 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
193 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
194 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
196 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
197 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
199 bool isStarted = theOperation->start();
201 onValidateOperation();
205 bool XGUI_OperationMgr::abortAllOperations()
211 if (operationsCount() == 1) {
212 ModuleBase_Operation* aCurrentOperation = currentOperation();
213 if (canStopOperation(aCurrentOperation)) {
214 abortOperation(aCurrentOperation);
220 aResult = QMessageBox::question(qApp->activeWindow(),
221 tr("Abort operation"),
222 tr("All active operations will be aborted."),
223 QMessageBox::Ok | QMessageBox::Cancel,
224 QMessageBox::Cancel) == QMessageBox::Ok;
225 while(aResult && hasOperation()) {
226 abortOperation(currentOperation());
232 bool XGUI_OperationMgr::commitAllOperations()
234 bool isCompositeCommitted = false, anOperationProcessed = false;
235 while (hasOperation()) {
236 ModuleBase_Operation* anOperation = currentOperation();
237 if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled()) {
238 anOperationProcessed = onCommitOperation();
240 abortOperation(anOperation);
241 anOperationProcessed = true;
243 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
246 FeaturePtr aFeature = aFOperation->feature();
247 CompositeFeaturePtr aComposite =
248 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
249 isCompositeCommitted = aComposite.get();
250 if (isCompositeCommitted)
253 // not processed[committed] operation might be used in composite feature,
254 // so the while will be stopped by the previous check.
255 // this code is not necessary, but logically should be done when the processing will not
256 // be done for not composite feature by some reasons
257 if (!anOperationProcessed)
263 void XGUI_OperationMgr::onValidateOperation()
267 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
268 (currentOperation());
269 if(aFOperation && aFOperation->feature().get())
270 XGUI_Tools::workshop(myWorkshop)->errorMgr()->updateActions(aFOperation->feature());
273 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
275 XGUI_ErrorMgr* anErrorMgr = XGUI_Tools::workshop(myWorkshop)->errorMgr();
277 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
279 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
282 foreach(ModuleBase_Operation* anOperation, myOperations) {
284 updateApplyOfOperations(anOperation);
289 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
291 //in case of nested (sketch) operation no confirmation needed
292 if (isGrantedOperation(theOperation->id()))
294 if (theOperation && theOperation->isModified()) {
295 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
296 int anAnswer = QMessageBox::question(qApp->activeWindow(),
297 tr("Abort operation"),
299 QMessageBox::Ok | QMessageBox::Cancel,
300 QMessageBox::Cancel);
301 return anAnswer == QMessageBox::Ok;
306 bool XGUI_OperationMgr::commitOperation()
308 //if (hasOperation() && currentOperation()->isValid()) {
309 // onCommitOperation();
313 return onCommitOperation();
316 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
318 theOperation->resume();
321 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
323 bool isGranted = false;
325 QListIterator<ModuleBase_Operation*> anIt(myOperations);
327 ModuleBase_Operation* aPreviousOperation = 0;
328 while (anIt.hasPrevious() && !isGranted) {
329 ModuleBase_Operation* anOp = anIt.previous();
331 isGranted = anOp->isGranted(theId);
336 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
338 SessionPtr aMgr = ModelAPI_Session::get();
339 DocumentPtr aDoc = aMgr->activeDocument();
340 bool aIsOp = aMgr->isOperation();
342 aMgr->startOperation(QString("Set current feature: %1").arg(theFeature->getKind().c_str()).toStdString());
343 aDoc->setCurrentFeature(theFeature, false);
345 aMgr->finishOperation();
348 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
350 bool aCanStart = true;
351 ModuleBase_Operation* aCurrentOp = currentOperation();
353 bool aGranted = aCurrentOp->isGranted(theId);
354 // the started operation is granted for the current one,
355 // e.g. current - Sketch, started - Line
360 if (!isGrantedOperation(theId)) {
361 // the operation is not granted in the current list of operations
362 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
363 aCanStart = abortAllOperations();
365 else if (canStopOperation(aCurrentOp)) {
366 // the started operation is granted in the parrent operation,
367 // e.g. current - Line in Sketch, started Circle
368 if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
369 aCurrentOp->commit();
371 abortOperation(aCurrentOp);
380 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
382 ModuleBase_Operation* aCurrentOperation = currentOperation();
383 if (theOperation == aCurrentOperation)
384 theOperation->abort();
386 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
387 // all operation from the current to triggered should also be aborted
388 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
389 while(hasOperation()) {
390 ModuleBase_Operation* aCurrentOperation = currentOperation();
391 aCurrentOperation->abort();
392 if(theOperation == aCurrentOperation)
398 bool XGUI_OperationMgr::onCommitOperation()
400 bool isCommitted = false;
401 ModuleBase_Operation* anOperation = currentOperation();
402 if (anOperation && myWorkshop->module()->canCommitOperation())
403 isCommitted = anOperation->commit();
407 void XGUI_OperationMgr::onAbortOperation()
409 ModuleBase_Operation* aCurrentOperation = currentOperation();
410 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
411 abortOperation(aCurrentOperation);
415 void XGUI_OperationMgr::onBeforeOperationStarted()
417 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
418 if (!aCurrentOperation)
421 /// Set current feature and remeber old current feature
422 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
424 SessionPtr aMgr = ModelAPI_Session::get();
425 DocumentPtr aDoc = aMgr->activeDocument();
426 // the parameter of current feature should be false, we should use all feature, not only visible
427 // in order to correctly save the previous feature of the nested operation, where the
428 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
429 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
430 // When sketch entity operation started, the sketch should be cashed here as the current.
431 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
432 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
433 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
434 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
436 #ifdef DEBUG_CURRENT_FEATURE
437 FeaturePtr aFeature = aFOperation->feature();
438 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
439 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
440 .arg(aFOperation->isEditOperation())
441 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
443 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
444 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
447 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
448 // in create operation, the current feature is changed by addFeature()
449 aDoc->setCurrentFeature(aFOperation->feature(), false);
451 #ifdef DEBUG_CURRENT_FEATURE
452 qDebug("\tdocument->setCurrentFeature");
453 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
454 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
456 ModuleBase_IModule* aModule = myWorkshop->module();
458 aModule->beforeOperationStarted(aFOperation);
462 void XGUI_OperationMgr::onOperationStarted()
464 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
465 updateApplyOfOperations(aSenderOperation);
466 emit operationStarted(aSenderOperation);
469 void XGUI_OperationMgr::onBeforeOperationAborted()
471 onBeforeOperationCommitted();
474 void XGUI_OperationMgr::onOperationAborted()
476 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
477 emit operationAborted(aSenderOperation);
480 void XGUI_OperationMgr::onBeforeOperationCommitted()
482 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
483 if (!aCurrentOperation)
486 /// Restore the previous current feature
487 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
489 #ifdef DEBUG_CURRENT_FEATURE
490 QString aKind = aFOperation->feature()->getKind().c_str();
491 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
492 .arg(aFOperation->isEditOperation())
493 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
495 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
496 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
499 if (aFOperation->isEditOperation()) {
500 /// Restore the previous current feature
501 setCurrentFeature(aFOperation->previousCurrentFeature());
503 else { // create operation
504 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
505 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
506 // in order to perform it in the current transaction without opening a new one.
507 if (myOperations.front() != aFOperation)
508 setCurrentFeature(aFOperation->previousCurrentFeature());
510 #ifdef DEBUG_CURRENT_FEATURE
511 qDebug("\tdocument->setCurrentFeature");
512 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
513 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
515 ModuleBase_IModule* aModule = myWorkshop->module();
517 aModule->beforeOperationStopped(aFOperation);
521 void XGUI_OperationMgr::onOperationCommitted()
523 // apply state for all features from the stack of operations should be updated
524 updateApplyOfOperations();
526 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
527 emit operationCommitted(aSenderOperation);
530 void XGUI_OperationMgr::onOperationResumed()
532 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
533 emit operationResumed(aSenderOperation);
536 void XGUI_OperationMgr::onOperationStopped()
538 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
539 ModuleBase_Operation* aCurrentOperation = currentOperation();
540 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
543 myOperations.removeAll(aCurrentOperation);
544 aCurrentOperation->deleteLater();
546 emit operationStopped(aCurrentOperation);
548 // get last operation which can be resumed
549 ModuleBase_Operation* aResultOp = 0;
550 QListIterator<ModuleBase_Operation*> anIt(myOperations);
552 while (anIt.hasPrevious()) {
553 ModuleBase_Operation* anOp = anIt.previous();
560 bool isModified = aCurrentOperation->isModified();
561 aResultOp->setIsModified(aResultOp->isModified() || isModified);
562 resumeOperation(aResultOp);
563 onValidateOperation();
567 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
569 // Let the manager decide what to do with the given key combination.
570 ModuleBase_Operation* anOperation = currentOperation();
571 bool isAccepted = false;
572 switch (theEvent->key()) {
574 case Qt::Key_Enter: {
575 isAccepted = onProcessEnter(theObject);
580 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
582 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
583 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
584 if (!aContext.IsNull()) {
585 Handle(V3d_View) aView = aViewer->activeView();
586 if ((theEvent->key() == Qt::Key_N))
587 aContext->HilightNextDetected(aView);
588 else if ((theEvent->key() == Qt::Key_P))
589 aContext->HilightPreviousDetected(aView);
600 // anOperation->keyReleased(theEvent->key());
605 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
607 bool isAccepted = false;
608 ModuleBase_Operation* aOperation = currentOperation();
609 // to avoid enter processing when operation has not been started yet
612 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
613 // only property panel enter is processed in order to do not process enter in application dialogs
614 bool isPPChild = isChildObject(theObject, aPanel);
618 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
619 bool isAborted = false;
621 QWidget* aFocusWidget = aPanel->focusWidget();
622 QToolButton* aCancelBtn = dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
623 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
624 abortOperation(aOperation);
630 isAccepted = anActiveWgt && anActiveWgt->processEnter();
632 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
634 /// functionality is similar to Apply click
635 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
636 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
637 // key released is emitted to apply the current value to the model if it was modified in PP
638 emit keyEnterReleased();
650 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
652 bool isAccepted = false;
653 ModuleBase_Operation* aOperation = currentOperation();
654 ModuleBase_ModelWidget* anActiveWgt = 0;
655 // firstly the widget should process Delete action
656 ModuleBase_IPropertyPanel* aPanel;
657 bool isPPChildObject = false;
659 aPanel = aOperation->propertyPanel();
661 isPPChildObject = isChildObject(theObject, aPanel);
662 // process delete in active widget only if delete sender is child of property panel
663 // it is necessary for the case when OB is shown, user perform selection and click Delete
664 if (isPPChildObject) {
665 anActiveWgt = aPanel->activeWidget();
667 isAccepted = anActiveWgt->processDelete();
673 // after widget, object browser and viewer should process delete
674 /// other widgets such as line edit controls should not lead to
675 /// processing delete by workshop
676 XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
677 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
678 // property panel child object is processed to process delete performed on Apply button of PP
679 if (theObject == aBrowser->treeView() ||
680 isChildObject(theObject, aViewPort) ||
682 XGUI_Tools::workshop(myWorkshop)->deleteObjects();
689 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
691 bool isPPChild = false;
692 if (theParent && theObject) {
693 QObject* aParent = (QObject*)theObject;
695 isPPChild = aParent == theParent;
698 aParent = aParent->parent();