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);
450 // this is the only place where flushes must be called after setCurrentFeature for the current
451 // moment: after this the opertion is not finished, so, the ObjectBrowser state may be corrupted
453 static Events_Loop* aLoop = Events_Loop::loop();
454 static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
455 aLoop->flush(aCreateEvent);
456 static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
457 aLoop->flush(aDeleteEvent);
460 #ifdef DEBUG_CURRENT_FEATURE
461 qDebug("\tdocument->setCurrentFeature");
462 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
463 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
465 ModuleBase_IModule* aModule = myWorkshop->module();
467 aModule->beforeOperationStarted(aFOperation);
471 void XGUI_OperationMgr::onOperationStarted()
473 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
474 updateApplyOfOperations(aSenderOperation);
475 emit operationStarted(aSenderOperation);
478 void XGUI_OperationMgr::onBeforeOperationAborted()
480 onBeforeOperationCommitted();
483 void XGUI_OperationMgr::onOperationAborted()
485 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
486 emit operationAborted(aSenderOperation);
489 void XGUI_OperationMgr::onBeforeOperationCommitted()
491 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
492 if (!aCurrentOperation)
495 /// Restore the previous current feature
496 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
498 #ifdef DEBUG_CURRENT_FEATURE
499 QString aKind = aFOperation->feature()->getKind().c_str();
500 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
501 .arg(aFOperation->isEditOperation())
502 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
504 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
505 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
508 if (aFOperation->isEditOperation()) {
509 /// Restore the previous current feature
510 setCurrentFeature(aFOperation->previousCurrentFeature());
512 else { // create operation
513 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
514 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
515 // in order to perform it in the current transaction without opening a new one.
516 if (myOperations.front() != aFOperation)
517 setCurrentFeature(aFOperation->previousCurrentFeature());
519 #ifdef DEBUG_CURRENT_FEATURE
520 qDebug("\tdocument->setCurrentFeature");
521 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
522 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
524 ModuleBase_IModule* aModule = myWorkshop->module();
526 aModule->beforeOperationStopped(aFOperation);
530 void XGUI_OperationMgr::onOperationCommitted()
532 // apply state for all features from the stack of operations should be updated
533 updateApplyOfOperations();
535 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
536 emit operationCommitted(aSenderOperation);
539 void XGUI_OperationMgr::onOperationResumed()
541 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
542 emit operationResumed(aSenderOperation);
545 void XGUI_OperationMgr::onOperationStopped()
547 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
548 ModuleBase_Operation* aCurrentOperation = currentOperation();
549 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
552 myOperations.removeAll(aCurrentOperation);
553 aCurrentOperation->deleteLater();
555 emit operationStopped(aCurrentOperation);
557 // get last operation which can be resumed
558 ModuleBase_Operation* aResultOp = 0;
559 QListIterator<ModuleBase_Operation*> anIt(myOperations);
561 while (anIt.hasPrevious()) {
562 ModuleBase_Operation* anOp = anIt.previous();
569 bool isModified = aCurrentOperation->isModified();
570 aResultOp->setIsModified(aResultOp->isModified() || isModified);
571 resumeOperation(aResultOp);
572 onValidateOperation();
576 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
578 // Let the manager decide what to do with the given key combination.
579 ModuleBase_Operation* anOperation = currentOperation();
580 bool isAccepted = false;
581 switch (theEvent->key()) {
583 case Qt::Key_Enter: {
584 isAccepted = onProcessEnter(theObject);
589 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
591 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
592 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
593 if (!aContext.IsNull()) {
594 Handle(V3d_View) aView = aViewer->activeView();
595 if ((theEvent->key() == Qt::Key_N))
596 aContext->HilightNextDetected(aView);
597 else if ((theEvent->key() == Qt::Key_P))
598 aContext->HilightPreviousDetected(aView);
609 // anOperation->keyReleased(theEvent->key());
614 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
616 bool isAccepted = false;
617 ModuleBase_Operation* aOperation = currentOperation();
618 // to avoid enter processing when operation has not been started yet
621 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
622 // only property panel enter is processed in order to do not process enter in application dialogs
623 bool isPPChild = isChildObject(theObject, aPanel);
627 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
628 bool isAborted = false;
630 QWidget* aFocusWidget = aPanel->focusWidget();
631 QToolButton* aCancelBtn = dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
632 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
633 abortOperation(aOperation);
639 isAccepted = anActiveWgt && anActiveWgt->processEnter();
641 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
643 /// functionality is similar to Apply click
644 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
645 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
646 // key released is emitted to apply the current value to the model if it was modified in PP
647 emit keyEnterReleased();
659 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
661 bool isAccepted = false;
662 ModuleBase_Operation* aOperation = currentOperation();
663 ModuleBase_ModelWidget* anActiveWgt = 0;
664 // firstly the widget should process Delete action
665 ModuleBase_IPropertyPanel* aPanel;
666 bool isPPChildObject = false;
668 aPanel = aOperation->propertyPanel();
670 isPPChildObject = isChildObject(theObject, aPanel);
671 // process delete in active widget only if delete sender is child of property panel
672 // it is necessary for the case when OB is shown, user perform selection and click Delete
673 if (isPPChildObject) {
674 anActiveWgt = aPanel->activeWidget();
676 isAccepted = anActiveWgt->processDelete();
682 // after widget, object browser and viewer should process delete
683 /// other widgets such as line edit controls should not lead to
684 /// processing delete by workshop
685 XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
686 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
687 // property panel child object is processed to process delete performed on Apply button of PP
688 if (theObject == aBrowser->treeView() ||
689 isChildObject(theObject, aViewPort) ||
691 XGUI_Tools::workshop(myWorkshop)->deleteObjects();
698 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
700 bool isPPChild = false;
701 if (theParent && theObject) {
702 QObject* aParent = (QObject*)theObject;
704 isPPChild = aParent == theParent;
707 aParent = aParent->parent();