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 <QMessageBox>
26 #include <QApplication>
29 //#define DEBUG_CURRENT_FEATURE
31 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
32 ModuleBase_IWorkshop* theWorkshop)
33 : QObject(theParent), myIsApplyEnabled(false), myWorkshop(theWorkshop)
37 XGUI_OperationMgr::~XGUI_OperationMgr()
41 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
43 return myOperations.count() > 0 ? myOperations.last() : 0;
46 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
50 return currentOperation() == theOperation;
53 bool XGUI_OperationMgr::hasOperation() const
55 return !myOperations.isEmpty() && (myOperations.last() != NULL);
58 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
60 foreach(ModuleBase_Operation* aOp, myOperations) {
61 if (aOp->id() == theId)
67 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
69 foreach(ModuleBase_Operation* aOp, myOperations) {
70 if (aOp->id() == theId)
77 int XGUI_OperationMgr::operationsCount() const
79 return myOperations.count();
82 QStringList XGUI_OperationMgr::operationList() const
85 foreach(ModuleBase_Operation* eachOperation, myOperations) {
86 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
88 FeaturePtr aFeature = aFOperation->feature();
90 result << QString::fromStdString(aFeature->getKind());
97 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
99 int idx = myOperations.lastIndexOf(theOperation);
100 if(idx == -1 || idx == 0) {
103 return myOperations.at(idx - 1);
106 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
108 if (theEvent->type() == QEvent::KeyRelease) {
109 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
111 return onKeyReleased(aKeyEvent);
114 return QObject::eventFilter(theObject, theEvent);
117 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
120 currentOperation()->postpone();
121 myOperations.append(theOperation);
123 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
124 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
125 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
127 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
128 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
129 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
131 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
132 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
133 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
136 connect(aFOperation, SIGNAL(activatedByPreselection()),
137 SIGNAL(operationActivatedByPreselection()));
139 bool isStarted = theOperation->start();
141 onValidateOperation();
145 bool XGUI_OperationMgr::abortAllOperations()
151 if (operationsCount() == 1) {
152 ModuleBase_Operation* aCurrentOperation = currentOperation();
153 if (canStopOperation(aCurrentOperation)) {
154 abortOperation(aCurrentOperation);
160 aResult = QMessageBox::question(qApp->activeWindow(),
161 tr("Abort operation"),
162 tr("All active operations will be aborted."),
163 QMessageBox::Ok | QMessageBox::Cancel,
164 QMessageBox::Cancel) == QMessageBox::Ok;
165 while(aResult && hasOperation()) {
166 abortOperation(currentOperation());
172 bool XGUI_OperationMgr::commitAllOperations()
174 bool isCompositeCommitted = false;
175 while (hasOperation()) {
176 ModuleBase_Operation* anOperation = currentOperation();
177 if (isApplyEnabled()) {
180 abortOperation(anOperation);
182 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
185 FeaturePtr aFeature = aFOperation->feature();
186 CompositeFeaturePtr aComposite =
187 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
188 isCompositeCommitted = aComposite.get();
189 if (isCompositeCommitted)
196 void XGUI_OperationMgr::onValidateOperation()
200 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
201 (currentOperation());
202 if(aFOperation && aFOperation->feature().get()) {
203 QString anError = myWorkshop->module()->getFeatureError(aFOperation->feature());
204 if (anError.isEmpty()) {
205 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
207 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
209 anError = myWorkshop->module()->getWidgetError(anActiveWidget);
212 setApplyEnabled(anError.isEmpty());
216 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
218 myIsApplyEnabled = theEnabled;
219 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
220 (currentOperation());
222 workshop()->errorMgr()->updateActions(aFOperation->feature());
224 //emit validationStateChanged(theEnabled);
227 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
229 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
231 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
233 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
234 //emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
235 // theOperation->isValid());
238 foreach(ModuleBase_Operation* anOperation, myOperations) {
240 updateApplyOfOperations(anOperation);
241 //emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
242 // anOperation->isValid());
247 bool XGUI_OperationMgr::isApplyEnabled() const
249 return myIsApplyEnabled;
252 bool XGUI_OperationMgr::isParentOperationValid() const
254 bool isValid = false;
255 // the enable state of the parent operation of the nested one is defined by the rules that
256 // firstly there are nested operations and secondly the parent operation is valid
257 ModuleBase_Operation* aPrevOp = 0;
258 Operations::const_iterator anIt = myOperations.end();
259 if (anIt != myOperations.begin()) { // there are items in the operations list
261 aPrevOp = *anIt; // the last top operation, the operation which is started
262 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
267 return aPrevOp && aPrevOp->isValid();
270 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
272 //in case of nested (sketch) operation no confirmation needed
273 if (isGrantedOperation(theOperation->id()))
275 if (theOperation && theOperation->isModified()) {
276 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
277 int anAnswer = QMessageBox::question(qApp->activeWindow(),
278 tr("Abort operation"),
280 QMessageBox::Ok | QMessageBox::Cancel,
281 QMessageBox::Cancel);
282 return anAnswer == QMessageBox::Ok;
287 bool XGUI_OperationMgr::commitOperation()
289 if (hasOperation() && currentOperation()->isValid()) {
296 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
298 theOperation->resume();
301 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
303 bool isGranted = false;
305 QListIterator<ModuleBase_Operation*> anIt(myOperations);
307 ModuleBase_Operation* aPreviousOperation = 0;
308 while (anIt.hasPrevious() && !isGranted) {
309 ModuleBase_Operation* anOp = anIt.previous();
311 isGranted = anOp->isGranted(theId);
316 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
318 SessionPtr aMgr = ModelAPI_Session::get();
319 DocumentPtr aDoc = aMgr->activeDocument();
320 bool aIsOp = aMgr->isOperation();
322 aMgr->startOperation();
323 aDoc->setCurrentFeature(theFeature, false);
325 aMgr->finishOperation();
328 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
330 bool aCanStart = true;
331 ModuleBase_Operation* aCurrentOp = currentOperation();
333 bool aGranted = aCurrentOp->isGranted(theId);
334 // the started operation is granted for the current one,
335 // e.g. current - Sketch, started - Line
340 if (!isGrantedOperation(theId)) {
341 // the operation is not granted in the current list of operations
342 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
343 aCanStart = abortAllOperations();
345 else if (canStopOperation(aCurrentOp)) {
346 // the started operation is granted in the parrent operation,
347 // e.g. current - Line in Sketch, started Circle
348 if (myIsApplyEnabled && aCurrentOp->isModified())
349 aCurrentOp->commit();
351 abortOperation(aCurrentOp);
360 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
362 ModuleBase_Operation* aCurrentOperation = currentOperation();
363 if (theOperation == aCurrentOperation)
364 theOperation->abort();
366 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
367 // all operation from the current to triggered should also be aborted
368 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
369 while(hasOperation()) {
370 ModuleBase_Operation* aCurrentOperation = currentOperation();
371 aCurrentOperation->abort();
372 if(theOperation == aCurrentOperation)
378 void XGUI_OperationMgr::onCommitOperation()
380 ModuleBase_Operation* anOperation = currentOperation();
382 anOperation->commit();
385 void XGUI_OperationMgr::onAbortOperation()
387 ModuleBase_Operation* aCurrentOperation = currentOperation();
388 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
389 abortOperation(aCurrentOperation);
393 void XGUI_OperationMgr::onBeforeOperationStarted()
395 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
396 if (!aCurrentOperation)
399 /// Set current feature and remeber old current feature
400 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
402 SessionPtr aMgr = ModelAPI_Session::get();
403 DocumentPtr aDoc = aMgr->activeDocument();
404 // the parameter of current feature should be false, we should use all feature, not only visible
405 // in order to correctly save the previous feature of the nested operation, where the
406 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
407 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
408 // When sketch entity operation started, the sketch should be cashed here as the current.
409 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
410 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
411 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
412 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
414 #ifdef DEBUG_CURRENT_FEATURE
415 FeaturePtr aFeature = aFOperation->feature();
416 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
417 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
418 .arg(aFOperation->isEditOperation())
419 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
421 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
422 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
425 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
426 // in create operation, the current feature is changed by addFeature()
427 aDoc->setCurrentFeature(aFOperation->feature(), false);
429 #ifdef DEBUG_CURRENT_FEATURE
430 qDebug("\tdocument->setCurrentFeature");
431 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
432 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
437 void XGUI_OperationMgr::onOperationStarted()
439 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
440 updateApplyOfOperations(aSenderOperation);
441 emit operationStarted(aSenderOperation);
444 void XGUI_OperationMgr::onBeforeOperationAborted()
446 onBeforeOperationCommitted();
449 void XGUI_OperationMgr::onOperationAborted()
451 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
452 emit operationAborted(aSenderOperation);
455 void XGUI_OperationMgr::onBeforeOperationCommitted()
457 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
458 if (!aCurrentOperation)
461 /// Restore the previous current feature
462 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
464 #ifdef DEBUG_CURRENT_FEATURE
465 QString aKind = aFOperation->feature()->getKind().c_str();
466 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
467 .arg(aFOperation->isEditOperation())
468 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
470 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
471 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
474 if (aFOperation->isEditOperation()) {
475 /// Restore the previous current feature
476 setCurrentFeature(aFOperation->previousCurrentFeature());
478 else { // create operation
479 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
480 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
481 // in order to perform it in the current transaction without opening a new one.
482 if (myOperations.front() != aFOperation)
483 setCurrentFeature(aFOperation->previousCurrentFeature());
485 #ifdef DEBUG_CURRENT_FEATURE
486 qDebug("\tdocument->setCurrentFeature");
487 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
488 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
493 void XGUI_OperationMgr::onOperationCommitted()
495 // apply state for all features from the stack of operations should be updated
496 updateApplyOfOperations();
498 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
499 emit operationCommitted(aSenderOperation);
502 void XGUI_OperationMgr::onOperationResumed()
504 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
505 emit operationResumed(aSenderOperation);
508 void XGUI_OperationMgr::onOperationStopped()
510 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
511 ModuleBase_Operation* aCurrentOperation = currentOperation();
512 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
515 myOperations.removeAll(aCurrentOperation);
516 aCurrentOperation->deleteLater();
518 emit operationStopped(aCurrentOperation);
520 // get last operation which can be resumed
521 ModuleBase_Operation* aResultOp = 0;
522 QListIterator<ModuleBase_Operation*> anIt(myOperations);
524 while (anIt.hasPrevious()) {
525 ModuleBase_Operation* anOp = anIt.previous();
532 bool isModified = aCurrentOperation->isModified();
533 aResultOp->setIsModified(aResultOp->isModified() || isModified);
534 resumeOperation(aResultOp);
535 onValidateOperation();
539 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
541 QObject* aSender = sender();
543 // Let the manager decide what to do with the given key combination.
544 ModuleBase_Operation* anOperation = currentOperation();
545 bool isAccepted = true;
546 switch (theEvent->key()) {
548 case Qt::Key_Enter: {
549 ModuleBase_Operation* aOperation = currentOperation();
550 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
551 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
552 if (!aActiveWgt || !aActiveWgt->processEnter()) {
553 if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
554 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
555 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
556 emit keyEnterReleased();
567 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
569 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
570 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
571 if (!aContext.IsNull()) {
572 Handle(V3d_View) aView = aViewer->activeView();
573 if ((theEvent->key() == Qt::Key_N))
574 aContext->HilightNextDetected(aView);
575 else if ((theEvent->key() == Qt::Key_P))
576 aContext->HilightPreviousDetected(aView);
587 // anOperation->keyReleased(theEvent->key());
592 XGUI_Workshop* XGUI_OperationMgr::workshop() const
594 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
595 return aConnector->workshop();