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"
12 #include <ModuleBase_IPropertyPanel.h>
13 #include <ModuleBase_ModelWidget.h>
14 #include "ModuleBase_Operation.h"
15 #include "ModuleBase_IWorkshop.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IViewer.h>
18 #include "ModuleBase_OperationDescription.h"
19 #include "ModuleBase_OperationFeature.h"
20 #include "ModuleBase_Tools.h"
22 #include "ModelAPI_CompositeFeature.h"
23 #include "ModelAPI_Session.h"
25 #include <XGUI_PropertyPanel.h>
26 #include <QToolButton>
28 #include <QMessageBox>
29 #include <QApplication>
32 //#define DEBUG_CURRENT_FEATURE
34 /// Processes "Delete" key event of application. This key is used by several application actions.
35 /// There is a logical order of the actions processing. So the key can not be set for actions
36 /// as a shortcut. The class listens the key event and call operation manager processor.
37 class XGUI_ShortCutListener : public QObject
41 /// \param theParent the parent to be deleted when the parent is deleted
42 /// \param theOperationMgr the class to perform deletion
43 XGUI_ShortCutListener(QObject* theParent, XGUI_OperationMgr* theOperationMgr)
44 : QObject(theParent), myOperationMgr(theOperationMgr)
46 qApp->installEventFilter(this);
48 ~XGUI_ShortCutListener() {}
50 /// Redefinition of virtual function to process Delete key release
51 virtual bool eventFilter(QObject *theObject, QEvent *theEvent)
53 bool isAccepted = false;
54 if (theEvent->type() == QEvent::KeyRelease) {
55 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
57 switch (aKeyEvent->key()) {
58 case Qt::Key_Delete: {
59 isAccepted = myOperationMgr->onProcessDelete();
65 isAccepted = QObject::eventFilter(theObject, theEvent);
70 XGUI_OperationMgr* myOperationMgr; /// processor for key event
73 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
74 ModuleBase_IWorkshop* theWorkshop)
75 : QObject(theParent), myWorkshop(theWorkshop)
77 /// we need to install filter to the application in order to react to 'Delete' key button
78 /// this key can not be a short cut for a corresponded action because we need to set
79 /// the actions priority
80 XGUI_ShortCutListener* aShortCutListener = new XGUI_ShortCutListener(theParent, this);
83 XGUI_OperationMgr::~XGUI_OperationMgr()
87 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
89 return myOperations.count() > 0 ? myOperations.last() : 0;
92 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
96 return currentOperation() == theOperation;
99 bool XGUI_OperationMgr::hasOperation() const
101 return !myOperations.isEmpty() && (myOperations.last() != NULL);
104 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
106 foreach(ModuleBase_Operation* aOp, myOperations) {
107 if (aOp->id() == theId)
113 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
115 foreach(ModuleBase_Operation* aOp, myOperations) {
116 if (aOp->id() == theId)
123 int XGUI_OperationMgr::operationsCount() const
125 return myOperations.count();
128 QStringList XGUI_OperationMgr::operationList() const
131 foreach(ModuleBase_Operation* eachOperation, myOperations) {
132 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
134 FeaturePtr aFeature = aFOperation->feature();
136 result << QString::fromStdString(aFeature->getKind());
143 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
145 int idx = myOperations.lastIndexOf(theOperation);
146 if(idx == -1 || idx == 0) {
149 return myOperations.at(idx - 1);
152 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
154 bool isAccepted = false;
155 if (theEvent->type() == QEvent::KeyRelease) {
156 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
158 isAccepted = onKeyReleased(aKeyEvent);
162 isAccepted = QObject::eventFilter(theObject, theEvent);
167 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
170 currentOperation()->postpone();
171 myOperations.append(theOperation);
173 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
174 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
175 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
177 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
178 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
179 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
181 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
182 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
183 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
186 connect(aFOperation, SIGNAL(activatedByPreselection()),
187 SIGNAL(operationActivatedByPreselection()));
189 bool isStarted = theOperation->start();
191 onValidateOperation();
195 bool XGUI_OperationMgr::abortAllOperations()
201 if (operationsCount() == 1) {
202 ModuleBase_Operation* aCurrentOperation = currentOperation();
203 if (canStopOperation(aCurrentOperation)) {
204 abortOperation(aCurrentOperation);
210 aResult = QMessageBox::question(qApp->activeWindow(),
211 tr("Abort operation"),
212 tr("All active operations will be aborted."),
213 QMessageBox::Ok | QMessageBox::Cancel,
214 QMessageBox::Cancel) == QMessageBox::Ok;
215 while(aResult && hasOperation()) {
216 abortOperation(currentOperation());
222 bool XGUI_OperationMgr::commitAllOperations()
224 bool isCompositeCommitted = false;
225 while (hasOperation()) {
226 ModuleBase_Operation* anOperation = currentOperation();
227 if (workshop()->errorMgr()->isApplyEnabled()) {
230 abortOperation(anOperation);
232 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
235 FeaturePtr aFeature = aFOperation->feature();
236 CompositeFeaturePtr aComposite =
237 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
238 isCompositeCommitted = aComposite.get();
239 if (isCompositeCommitted)
246 void XGUI_OperationMgr::onValidateOperation()
250 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
251 (currentOperation());
252 if(aFOperation && aFOperation->feature().get())
253 workshop()->errorMgr()->updateActions(aFOperation->feature());
256 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
258 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
260 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
262 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
265 foreach(ModuleBase_Operation* anOperation, myOperations) {
267 updateApplyOfOperations(anOperation);
272 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
274 //in case of nested (sketch) operation no confirmation needed
275 if (isGrantedOperation(theOperation->id()))
277 if (theOperation && theOperation->isModified()) {
278 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
279 int anAnswer = QMessageBox::question(qApp->activeWindow(),
280 tr("Abort operation"),
282 QMessageBox::Ok | QMessageBox::Cancel,
283 QMessageBox::Cancel);
284 return anAnswer == QMessageBox::Ok;
289 bool XGUI_OperationMgr::commitOperation()
291 if (hasOperation() && currentOperation()->isValid()) {
298 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
300 theOperation->resume();
303 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
305 bool isGranted = false;
307 QListIterator<ModuleBase_Operation*> anIt(myOperations);
309 ModuleBase_Operation* aPreviousOperation = 0;
310 while (anIt.hasPrevious() && !isGranted) {
311 ModuleBase_Operation* anOp = anIt.previous();
313 isGranted = anOp->isGranted(theId);
318 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
320 SessionPtr aMgr = ModelAPI_Session::get();
321 DocumentPtr aDoc = aMgr->activeDocument();
322 bool aIsOp = aMgr->isOperation();
324 aMgr->startOperation();
325 aDoc->setCurrentFeature(theFeature, false);
327 aMgr->finishOperation();
330 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
332 bool aCanStart = true;
333 ModuleBase_Operation* aCurrentOp = currentOperation();
335 bool aGranted = aCurrentOp->isGranted(theId);
336 // the started operation is granted for the current one,
337 // e.g. current - Sketch, started - Line
342 if (!isGrantedOperation(theId)) {
343 // the operation is not granted in the current list of operations
344 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
345 aCanStart = abortAllOperations();
347 else if (canStopOperation(aCurrentOp)) {
348 // the started operation is granted in the parrent operation,
349 // e.g. current - Line in Sketch, started Circle
350 if (workshop()->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
351 aCurrentOp->commit();
353 abortOperation(aCurrentOp);
362 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
364 ModuleBase_Operation* aCurrentOperation = currentOperation();
365 if (theOperation == aCurrentOperation)
366 theOperation->abort();
368 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
369 // all operation from the current to triggered should also be aborted
370 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
371 while(hasOperation()) {
372 ModuleBase_Operation* aCurrentOperation = currentOperation();
373 aCurrentOperation->abort();
374 if(theOperation == aCurrentOperation)
380 void XGUI_OperationMgr::onCommitOperation()
382 ModuleBase_Operation* anOperation = currentOperation();
384 anOperation->commit();
387 void XGUI_OperationMgr::onAbortOperation()
389 ModuleBase_Operation* aCurrentOperation = currentOperation();
390 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
391 abortOperation(aCurrentOperation);
395 void XGUI_OperationMgr::onBeforeOperationStarted()
397 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
398 if (!aCurrentOperation)
401 /// Set current feature and remeber old current feature
402 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
404 SessionPtr aMgr = ModelAPI_Session::get();
405 DocumentPtr aDoc = aMgr->activeDocument();
406 // the parameter of current feature should be false, we should use all feature, not only visible
407 // in order to correctly save the previous feature of the nested operation, where the
408 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
409 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
410 // When sketch entity operation started, the sketch should be cashed here as the current.
411 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
412 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
413 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
414 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
416 #ifdef DEBUG_CURRENT_FEATURE
417 FeaturePtr aFeature = aFOperation->feature();
418 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
419 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
420 .arg(aFOperation->isEditOperation())
421 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
423 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
424 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
427 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
428 // in create operation, the current feature is changed by addFeature()
429 aDoc->setCurrentFeature(aFOperation->feature(), false);
431 #ifdef DEBUG_CURRENT_FEATURE
432 qDebug("\tdocument->setCurrentFeature");
433 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
434 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
436 ModuleBase_IModule* aModule = myWorkshop->module();
438 aModule->beforeOperationStarted(aFOperation);
442 void XGUI_OperationMgr::onOperationStarted()
444 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
445 updateApplyOfOperations(aSenderOperation);
446 emit operationStarted(aSenderOperation);
449 void XGUI_OperationMgr::onBeforeOperationAborted()
451 onBeforeOperationCommitted();
454 void XGUI_OperationMgr::onOperationAborted()
456 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
457 emit operationAborted(aSenderOperation);
460 void XGUI_OperationMgr::onBeforeOperationCommitted()
462 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
463 if (!aCurrentOperation)
466 /// Restore the previous current feature
467 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
469 #ifdef DEBUG_CURRENT_FEATURE
470 QString aKind = aFOperation->feature()->getKind().c_str();
471 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
472 .arg(aFOperation->isEditOperation())
473 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
475 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
476 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
479 if (aFOperation->isEditOperation()) {
480 /// Restore the previous current feature
481 setCurrentFeature(aFOperation->previousCurrentFeature());
483 else { // create operation
484 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
485 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
486 // in order to perform it in the current transaction without opening a new one.
487 if (myOperations.front() != aFOperation)
488 setCurrentFeature(aFOperation->previousCurrentFeature());
490 #ifdef DEBUG_CURRENT_FEATURE
491 qDebug("\tdocument->setCurrentFeature");
492 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
493 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
495 ModuleBase_IModule* aModule = myWorkshop->module();
497 aModule->beforeOperationStopped(aFOperation);
501 void XGUI_OperationMgr::onOperationCommitted()
503 // apply state for all features from the stack of operations should be updated
504 updateApplyOfOperations();
506 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
507 emit operationCommitted(aSenderOperation);
510 void XGUI_OperationMgr::onOperationResumed()
512 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
513 emit operationResumed(aSenderOperation);
516 void XGUI_OperationMgr::onOperationStopped()
518 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
519 ModuleBase_Operation* aCurrentOperation = currentOperation();
520 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
523 myOperations.removeAll(aCurrentOperation);
524 aCurrentOperation->deleteLater();
526 emit operationStopped(aCurrentOperation);
528 // get last operation which can be resumed
529 ModuleBase_Operation* aResultOp = 0;
530 QListIterator<ModuleBase_Operation*> anIt(myOperations);
532 while (anIt.hasPrevious()) {
533 ModuleBase_Operation* anOp = anIt.previous();
540 bool isModified = aCurrentOperation->isModified();
541 aResultOp->setIsModified(aResultOp->isModified() || isModified);
542 resumeOperation(aResultOp);
543 onValidateOperation();
547 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
549 // Let the manager decide what to do with the given key combination.
550 ModuleBase_Operation* anOperation = currentOperation();
551 bool isAccepted = false;
552 switch (theEvent->key()) {
554 case Qt::Key_Enter: {
555 isAccepted = onProcessEnter();
560 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
562 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
563 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
564 if (!aContext.IsNull()) {
565 Handle(V3d_View) aView = aViewer->activeView();
566 if ((theEvent->key() == Qt::Key_N))
567 aContext->HilightNextDetected(aView);
568 else if ((theEvent->key() == Qt::Key_P))
569 aContext->HilightPreviousDetected(aView);
580 // anOperation->keyReleased(theEvent->key());
585 bool XGUI_OperationMgr::onProcessEnter()
587 bool isAccepted = false;
588 ModuleBase_Operation* aOperation = currentOperation();
589 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
590 ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
591 bool isAborted = false;
593 QWidget* aFocusWidget = aPanel->focusWidget();
594 QToolButton* aCancelBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_CANCEL);
595 if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
596 abortOperation(aOperation);
602 isAccepted = anActiveWgt && anActiveWgt->processEnter();
604 isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
606 /// functionality is similar to Apply click
607 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
608 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
609 // key released is emitted to apply the current value to the model if it was modified in PP
610 emit keyEnterReleased();
622 bool XGUI_OperationMgr::onProcessDelete()
624 bool isAccepted = false;
625 ModuleBase_Operation* aOperation = currentOperation();
626 ModuleBase_ModelWidget* anActiveWgt = 0;
628 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
630 anActiveWgt = aPanel->activeWidget();
633 isAccepted = anActiveWgt->processDelete();
635 workshop()->deleteObjects();
642 XGUI_Workshop* XGUI_OperationMgr::workshop() const
644 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
645 return aConnector->workshop();