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()));
198 bool isStarted = theOperation->start();
200 onValidateOperation();
204 bool XGUI_OperationMgr::abortAllOperations()
210 if (operationsCount() == 1) {
211 ModuleBase_Operation* aCurrentOperation = currentOperation();
212 if (canStopOperation(aCurrentOperation)) {
213 abortOperation(aCurrentOperation);
219 aResult = QMessageBox::question(qApp->activeWindow(),
220 tr("Abort operation"),
221 tr("All active operations will be aborted."),
222 QMessageBox::Ok | QMessageBox::Cancel,
223 QMessageBox::Cancel) == QMessageBox::Ok;
224 while(aResult && hasOperation()) {
225 abortOperation(currentOperation());
231 bool XGUI_OperationMgr::commitAllOperations()
233 bool isCompositeCommitted = false;
234 while (hasOperation()) {
235 ModuleBase_Operation* anOperation = currentOperation();
236 if (workshop()->errorMgr()->isApplyEnabled()) {
239 abortOperation(anOperation);
241 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
244 FeaturePtr aFeature = aFOperation->feature();
245 CompositeFeaturePtr aComposite =
246 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
247 isCompositeCommitted = aComposite.get();
248 if (isCompositeCommitted)
255 void XGUI_OperationMgr::onValidateOperation()
259 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
260 (currentOperation());
261 if(aFOperation && aFOperation->feature().get())
262 workshop()->errorMgr()->updateActions(aFOperation->feature());
265 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
267 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
269 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
271 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
274 foreach(ModuleBase_Operation* anOperation, myOperations) {
276 updateApplyOfOperations(anOperation);
281 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
283 //in case of nested (sketch) operation no confirmation needed
284 if (isGrantedOperation(theOperation->id()))
286 if (theOperation && theOperation->isModified()) {
287 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
288 int anAnswer = QMessageBox::question(qApp->activeWindow(),
289 tr("Abort operation"),
291 QMessageBox::Ok | QMessageBox::Cancel,
292 QMessageBox::Cancel);
293 return anAnswer == QMessageBox::Ok;
298 bool XGUI_OperationMgr::commitOperation()
300 if (hasOperation() && currentOperation()->isValid()) {
307 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
309 theOperation->resume();
312 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
314 bool isGranted = false;
316 QListIterator<ModuleBase_Operation*> anIt(myOperations);
318 ModuleBase_Operation* aPreviousOperation = 0;
319 while (anIt.hasPrevious() && !isGranted) {
320 ModuleBase_Operation* anOp = anIt.previous();
322 isGranted = anOp->isGranted(theId);
327 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
329 SessionPtr aMgr = ModelAPI_Session::get();
330 DocumentPtr aDoc = aMgr->activeDocument();
331 bool aIsOp = aMgr->isOperation();
333 aMgr->startOperation();
334 aDoc->setCurrentFeature(theFeature, false);
336 aMgr->finishOperation();
339 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
341 bool aCanStart = true;
342 ModuleBase_Operation* aCurrentOp = currentOperation();
344 bool aGranted = aCurrentOp->isGranted(theId);
345 // the started operation is granted for the current one,
346 // e.g. current - Sketch, started - Line
351 if (!isGrantedOperation(theId)) {
352 // the operation is not granted in the current list of operations
353 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
354 aCanStart = abortAllOperations();
356 else if (canStopOperation(aCurrentOp)) {
357 // the started operation is granted in the parrent operation,
358 // e.g. current - Line in Sketch, started Circle
359 if (workshop()->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
360 aCurrentOp->commit();
362 abortOperation(aCurrentOp);
371 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
373 ModuleBase_Operation* aCurrentOperation = currentOperation();
374 if (theOperation == aCurrentOperation)
375 theOperation->abort();
377 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
378 // all operation from the current to triggered should also be aborted
379 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
380 while(hasOperation()) {
381 ModuleBase_Operation* aCurrentOperation = currentOperation();
382 aCurrentOperation->abort();
383 if(theOperation == aCurrentOperation)
389 void XGUI_OperationMgr::onCommitOperation()
391 ModuleBase_Operation* anOperation = currentOperation();
393 anOperation->commit();
396 void XGUI_OperationMgr::onAbortOperation()
398 ModuleBase_Operation* aCurrentOperation = currentOperation();
399 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
400 abortOperation(aCurrentOperation);
404 void XGUI_OperationMgr::onBeforeOperationStarted()
406 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
407 if (!aCurrentOperation)
410 /// Set current feature and remeber old current feature
411 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
413 SessionPtr aMgr = ModelAPI_Session::get();
414 DocumentPtr aDoc = aMgr->activeDocument();
415 // the parameter of current feature should be false, we should use all feature, not only visible
416 // in order to correctly save the previous feature of the nested operation, where the
417 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
418 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
419 // When sketch entity operation started, the sketch should be cashed here as the current.
420 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
421 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
422 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
423 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
425 #ifdef DEBUG_CURRENT_FEATURE
426 FeaturePtr aFeature = aFOperation->feature();
427 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
428 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
429 .arg(aFOperation->isEditOperation())
430 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
432 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
433 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
436 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
437 // in create operation, the current feature is changed by addFeature()
438 aDoc->setCurrentFeature(aFOperation->feature(), false);
440 #ifdef DEBUG_CURRENT_FEATURE
441 qDebug("\tdocument->setCurrentFeature");
442 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
443 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
445 ModuleBase_IModule* aModule = myWorkshop->module();
447 aModule->beforeOperationStarted(aFOperation);
451 void XGUI_OperationMgr::onOperationStarted()
453 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
454 updateApplyOfOperations(aSenderOperation);
455 emit operationStarted(aSenderOperation);
458 void XGUI_OperationMgr::onBeforeOperationAborted()
460 onBeforeOperationCommitted();
463 void XGUI_OperationMgr::onOperationAborted()
465 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
466 emit operationAborted(aSenderOperation);
469 void XGUI_OperationMgr::onBeforeOperationCommitted()
471 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
472 if (!aCurrentOperation)
475 /// Restore the previous current feature
476 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
478 #ifdef DEBUG_CURRENT_FEATURE
479 QString aKind = aFOperation->feature()->getKind().c_str();
480 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
481 .arg(aFOperation->isEditOperation())
482 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
484 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
485 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
488 if (aFOperation->isEditOperation()) {
489 /// Restore the previous current feature
490 setCurrentFeature(aFOperation->previousCurrentFeature());
492 else { // create operation
493 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
494 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
495 // in order to perform it in the current transaction without opening a new one.
496 if (myOperations.front() != aFOperation)
497 setCurrentFeature(aFOperation->previousCurrentFeature());
499 #ifdef DEBUG_CURRENT_FEATURE
500 qDebug("\tdocument->setCurrentFeature");
501 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
502 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
504 ModuleBase_IModule* aModule = myWorkshop->module();
506 aModule->beforeOperationStopped(aFOperation);
510 void XGUI_OperationMgr::onOperationCommitted()
512 // apply state for all features from the stack of operations should be updated
513 updateApplyOfOperations();
515 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
516 emit operationCommitted(aSenderOperation);
519 void XGUI_OperationMgr::onOperationResumed()
521 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
522 emit operationResumed(aSenderOperation);
525 void XGUI_OperationMgr::onOperationStopped()
527 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
528 ModuleBase_Operation* aCurrentOperation = currentOperation();
529 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
532 myOperations.removeAll(aCurrentOperation);
533 aCurrentOperation->deleteLater();
535 emit operationStopped(aCurrentOperation);
537 // get last operation which can be resumed
538 ModuleBase_Operation* aResultOp = 0;
539 QListIterator<ModuleBase_Operation*> anIt(myOperations);
541 while (anIt.hasPrevious()) {
542 ModuleBase_Operation* anOp = anIt.previous();
549 bool isModified = aCurrentOperation->isModified();
550 aResultOp->setIsModified(aResultOp->isModified() || isModified);
551 resumeOperation(aResultOp);
552 onValidateOperation();
556 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
558 // Let the manager decide what to do with the given key combination.
559 ModuleBase_Operation* anOperation = currentOperation();
560 bool isAccepted = false;
561 switch (theEvent->key()) {
563 case Qt::Key_Enter: {
564 isAccepted = onProcessEnter(theObject);
569 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
571 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
572 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
573 if (!aContext.IsNull()) {
574 Handle(V3d_View) aView = aViewer->activeView();
575 if ((theEvent->key() == Qt::Key_N))
576 aContext->HilightNextDetected(aView);
577 else if ((theEvent->key() == Qt::Key_P))
578 aContext->HilightPreviousDetected(aView);
589 // anOperation->keyReleased(theEvent->key());
594 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
596 bool isAccepted = false;
597 ModuleBase_Operation* aOperation = currentOperation();
598 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
599 // only property panel enter is processed in order to do not process enter in application dialogs
600 bool isPPChild = isChildObject(theObject, aPanel);
604 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
605 bool isAborted = false;
607 QWidget* aFocusWidget = aPanel->focusWidget();
608 QToolButton* aCancelBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_CANCEL);
609 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
610 abortOperation(aOperation);
616 isAccepted = anActiveWgt && anActiveWgt->processEnter();
618 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
620 /// functionality is similar to Apply click
621 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
622 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
623 // key released is emitted to apply the current value to the model if it was modified in PP
624 emit keyEnterReleased();
636 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
638 bool isAccepted = false;
639 ModuleBase_Operation* aOperation = currentOperation();
640 ModuleBase_ModelWidget* anActiveWgt = 0;
641 // firstly the widget should process Delete action
642 ModuleBase_IPropertyPanel* aPanel;
643 bool isPPChildObject = false;
645 aPanel = aOperation->propertyPanel();
647 isPPChildObject = isChildObject(theObject, aPanel);
648 // process delete in active widget only if delete sender is child of property panel
649 // it is necessary for the case when OB is shown, user perform selection and click Delete
650 if (isPPChildObject) {
651 anActiveWgt = aPanel->activeWidget();
653 isAccepted = anActiveWgt->processDelete();
659 // after widget, object browser and viewer should process delete
660 /// other widgets such as line edit controls should not lead to
661 /// processing delete by workshop
662 XGUI_ObjectsBrowser* aBrowser = workshop()->objectBrowser();
663 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
664 // property panel child object is processed to process delete performed on Apply button of PP
665 if (theObject == aBrowser->treeView() ||
666 isChildObject(theObject, aViewPort) ||
668 workshop()->deleteObjects();
675 XGUI_Workshop* XGUI_OperationMgr::workshop() const
677 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
678 return aConnector->workshop();
681 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
683 bool isPPChild = false;
684 if (theParent && theObject) {
685 QObject* aParent = (QObject*)theObject;
687 isPPChild = aParent == theParent;
690 aParent = aParent->parent();