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;
235 while (hasOperation()) {
236 ModuleBase_Operation* anOperation = currentOperation();
237 if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled()) {
240 abortOperation(anOperation);
242 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
245 FeaturePtr aFeature = aFOperation->feature();
246 CompositeFeaturePtr aComposite =
247 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
248 isCompositeCommitted = aComposite.get();
249 if (isCompositeCommitted)
256 void XGUI_OperationMgr::onValidateOperation()
260 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
261 (currentOperation());
262 if(aFOperation && aFOperation->feature().get())
263 XGUI_Tools::workshop(myWorkshop)->errorMgr()->updateActions(aFOperation->feature());
266 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
268 XGUI_ErrorMgr* anErrorMgr = XGUI_Tools::workshop(myWorkshop)->errorMgr();
270 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
272 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
275 foreach(ModuleBase_Operation* anOperation, myOperations) {
277 updateApplyOfOperations(anOperation);
282 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
284 //in case of nested (sketch) operation no confirmation needed
285 if (isGrantedOperation(theOperation->id()))
287 if (theOperation && theOperation->isModified()) {
288 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
289 int anAnswer = QMessageBox::question(qApp->activeWindow(),
290 tr("Abort operation"),
292 QMessageBox::Ok | QMessageBox::Cancel,
293 QMessageBox::Cancel);
294 return anAnswer == QMessageBox::Ok;
299 bool XGUI_OperationMgr::commitOperation()
301 if (hasOperation() && currentOperation()->isValid()) {
308 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
310 theOperation->resume();
313 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
315 bool isGranted = false;
317 QListIterator<ModuleBase_Operation*> anIt(myOperations);
319 ModuleBase_Operation* aPreviousOperation = 0;
320 while (anIt.hasPrevious() && !isGranted) {
321 ModuleBase_Operation* anOp = anIt.previous();
323 isGranted = anOp->isGranted(theId);
328 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
330 SessionPtr aMgr = ModelAPI_Session::get();
331 DocumentPtr aDoc = aMgr->activeDocument();
332 bool aIsOp = aMgr->isOperation();
334 aMgr->startOperation();
335 aDoc->setCurrentFeature(theFeature, false);
337 aMgr->finishOperation();
340 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
342 bool aCanStart = true;
343 ModuleBase_Operation* aCurrentOp = currentOperation();
345 bool aGranted = aCurrentOp->isGranted(theId);
346 // the started operation is granted for the current one,
347 // e.g. current - Sketch, started - Line
352 if (!isGrantedOperation(theId)) {
353 // the operation is not granted in the current list of operations
354 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
355 aCanStart = abortAllOperations();
357 else if (canStopOperation(aCurrentOp)) {
358 // the started operation is granted in the parrent operation,
359 // e.g. current - Line in Sketch, started Circle
360 if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
361 aCurrentOp->commit();
363 abortOperation(aCurrentOp);
372 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
374 ModuleBase_Operation* aCurrentOperation = currentOperation();
375 if (theOperation == aCurrentOperation)
376 theOperation->abort();
378 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
379 // all operation from the current to triggered should also be aborted
380 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
381 while(hasOperation()) {
382 ModuleBase_Operation* aCurrentOperation = currentOperation();
383 aCurrentOperation->abort();
384 if(theOperation == aCurrentOperation)
390 void XGUI_OperationMgr::onCommitOperation()
392 ModuleBase_Operation* anOperation = currentOperation();
394 anOperation->commit();
397 void XGUI_OperationMgr::onAbortOperation()
399 ModuleBase_Operation* aCurrentOperation = currentOperation();
400 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
401 abortOperation(aCurrentOperation);
405 void XGUI_OperationMgr::onBeforeOperationStarted()
407 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
408 if (!aCurrentOperation)
411 /// Set current feature and remeber old current feature
412 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
414 SessionPtr aMgr = ModelAPI_Session::get();
415 DocumentPtr aDoc = aMgr->activeDocument();
416 // the parameter of current feature should be false, we should use all feature, not only visible
417 // in order to correctly save the previous feature of the nested operation, where the
418 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
419 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
420 // When sketch entity operation started, the sketch should be cashed here as the current.
421 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
422 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
423 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
424 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
426 #ifdef DEBUG_CURRENT_FEATURE
427 FeaturePtr aFeature = aFOperation->feature();
428 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
429 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
430 .arg(aFOperation->isEditOperation())
431 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
433 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
434 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
437 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
438 // in create operation, the current feature is changed by addFeature()
439 aDoc->setCurrentFeature(aFOperation->feature(), false);
441 #ifdef DEBUG_CURRENT_FEATURE
442 qDebug("\tdocument->setCurrentFeature");
443 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
444 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
446 ModuleBase_IModule* aModule = myWorkshop->module();
448 aModule->beforeOperationStarted(aFOperation);
452 void XGUI_OperationMgr::onOperationStarted()
454 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
455 updateApplyOfOperations(aSenderOperation);
456 emit operationStarted(aSenderOperation);
459 void XGUI_OperationMgr::onBeforeOperationAborted()
461 onBeforeOperationCommitted();
464 void XGUI_OperationMgr::onOperationAborted()
466 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
467 emit operationAborted(aSenderOperation);
470 void XGUI_OperationMgr::onBeforeOperationCommitted()
472 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
473 if (!aCurrentOperation)
476 /// Restore the previous current feature
477 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
479 #ifdef DEBUG_CURRENT_FEATURE
480 QString aKind = aFOperation->feature()->getKind().c_str();
481 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
482 .arg(aFOperation->isEditOperation())
483 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
485 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
486 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
489 if (aFOperation->isEditOperation()) {
490 /// Restore the previous current feature
491 setCurrentFeature(aFOperation->previousCurrentFeature());
493 else { // create operation
494 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
495 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
496 // in order to perform it in the current transaction without opening a new one.
497 if (myOperations.front() != aFOperation)
498 setCurrentFeature(aFOperation->previousCurrentFeature());
500 #ifdef DEBUG_CURRENT_FEATURE
501 qDebug("\tdocument->setCurrentFeature");
502 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
503 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
505 ModuleBase_IModule* aModule = myWorkshop->module();
507 aModule->beforeOperationStopped(aFOperation);
511 void XGUI_OperationMgr::onOperationCommitted()
513 // apply state for all features from the stack of operations should be updated
514 updateApplyOfOperations();
516 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
517 emit operationCommitted(aSenderOperation);
520 void XGUI_OperationMgr::onOperationResumed()
522 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
523 emit operationResumed(aSenderOperation);
526 void XGUI_OperationMgr::onOperationStopped()
528 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
529 ModuleBase_Operation* aCurrentOperation = currentOperation();
530 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
533 myOperations.removeAll(aCurrentOperation);
534 aCurrentOperation->deleteLater();
536 emit operationStopped(aCurrentOperation);
538 // get last operation which can be resumed
539 ModuleBase_Operation* aResultOp = 0;
540 QListIterator<ModuleBase_Operation*> anIt(myOperations);
542 while (anIt.hasPrevious()) {
543 ModuleBase_Operation* anOp = anIt.previous();
550 bool isModified = aCurrentOperation->isModified();
551 aResultOp->setIsModified(aResultOp->isModified() || isModified);
552 resumeOperation(aResultOp);
553 onValidateOperation();
557 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
559 // Let the manager decide what to do with the given key combination.
560 ModuleBase_Operation* anOperation = currentOperation();
561 bool isAccepted = false;
562 switch (theEvent->key()) {
564 case Qt::Key_Enter: {
565 isAccepted = onProcessEnter(theObject);
570 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
572 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
573 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
574 if (!aContext.IsNull()) {
575 Handle(V3d_View) aView = aViewer->activeView();
576 if ((theEvent->key() == Qt::Key_N))
577 aContext->HilightNextDetected(aView);
578 else if ((theEvent->key() == Qt::Key_P))
579 aContext->HilightPreviousDetected(aView);
590 // anOperation->keyReleased(theEvent->key());
595 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
597 bool isAccepted = false;
598 ModuleBase_Operation* aOperation = currentOperation();
601 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
602 // only property panel enter is processed in order to do not process enter in application dialogs
603 bool isPPChild = isChildObject(theObject, aPanel);
607 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
608 bool isAborted = false;
610 QWidget* aFocusWidget = aPanel->focusWidget();
611 QToolButton* aCancelBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_CANCEL);
612 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
613 abortOperation(aOperation);
619 isAccepted = anActiveWgt && anActiveWgt->processEnter();
621 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
623 /// functionality is similar to Apply click
624 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
625 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
626 // key released is emitted to apply the current value to the model if it was modified in PP
627 emit keyEnterReleased();
639 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
641 bool isAccepted = false;
642 ModuleBase_Operation* aOperation = currentOperation();
643 ModuleBase_ModelWidget* anActiveWgt = 0;
644 // firstly the widget should process Delete action
645 ModuleBase_IPropertyPanel* aPanel;
646 bool isPPChildObject = false;
648 aPanel = aOperation->propertyPanel();
650 isPPChildObject = isChildObject(theObject, aPanel);
651 // process delete in active widget only if delete sender is child of property panel
652 // it is necessary for the case when OB is shown, user perform selection and click Delete
653 if (isPPChildObject) {
654 anActiveWgt = aPanel->activeWidget();
656 isAccepted = anActiveWgt->processDelete();
662 // after widget, object browser and viewer should process delete
663 /// other widgets such as line edit controls should not lead to
664 /// processing delete by workshop
665 XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
666 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
667 // property panel child object is processed to process delete performed on Apply button of PP
668 if (theObject == aBrowser->treeView() ||
669 isChildObject(theObject, aViewPort) ||
671 XGUI_Tools::workshop(myWorkshop)->deleteObjects();
678 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
680 bool isPPChild = false;
681 if (theParent && theObject) {
682 QObject* aParent = (QObject*)theObject;
684 isPPChild = aParent == theParent;
687 aParent = aParent->parent();