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 theOperation->start();
140 onValidateOperation();
144 bool XGUI_OperationMgr::abortAllOperations()
150 if (operationsCount() == 1) {
151 ModuleBase_Operation* aCurrentOperation = currentOperation();
152 if (canStopOperation(aCurrentOperation)) {
153 abortOperation(aCurrentOperation);
159 aResult = QMessageBox::question(qApp->activeWindow(),
160 tr("Abort operation"),
161 tr("All active operations will be aborted."),
162 QMessageBox::Ok | QMessageBox::Cancel,
163 QMessageBox::Cancel) == QMessageBox::Ok;
164 while(aResult && hasOperation()) {
165 abortOperation(currentOperation());
171 bool XGUI_OperationMgr::commitAllOperations()
173 bool isCompositeCommitted = false;
174 while (hasOperation()) {
175 ModuleBase_Operation* anOperation = currentOperation();
176 if (isApplyEnabled()) {
179 abortOperation(anOperation);
181 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
184 FeaturePtr aFeature = aFOperation->feature();
185 CompositeFeaturePtr aComposite =
186 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
187 isCompositeCommitted = aComposite.get();
188 if (isCompositeCommitted)
195 void XGUI_OperationMgr::onValidateOperation()
199 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
200 (currentOperation());
201 if(aFOperation && aFOperation->feature().get()) {
202 QString anError = myWorkshop->module()->getFeatureError(aFOperation->feature());
203 if (anError.isEmpty()) {
204 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
206 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
208 anError = myWorkshop->module()->getWidgetError(anActiveWidget);
211 setApplyEnabled(anError.isEmpty());
215 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
217 myIsApplyEnabled = theEnabled;
218 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
219 (currentOperation());
221 workshop()->errorMgr()->updateActions(aFOperation->feature());
223 //emit validationStateChanged(theEnabled);
226 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
228 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
230 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
232 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
233 //emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
234 // theOperation->isValid());
237 foreach(ModuleBase_Operation* anOperation, myOperations) {
239 updateApplyOfOperations(anOperation);
240 //emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
241 // anOperation->isValid());
246 bool XGUI_OperationMgr::isApplyEnabled() const
248 return myIsApplyEnabled;
251 bool XGUI_OperationMgr::isParentOperationValid() const
253 bool isValid = false;
254 // the enable state of the parent operation of the nested one is defined by the rules that
255 // firstly there are nested operations and secondly the parent operation is valid
256 ModuleBase_Operation* aPrevOp = 0;
257 Operations::const_iterator anIt = myOperations.end();
258 if (anIt != myOperations.begin()) { // there are items in the operations list
260 aPrevOp = *anIt; // the last top operation, the operation which is started
261 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
266 return aPrevOp && aPrevOp->isValid();
269 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
271 //in case of nested (sketch) operation no confirmation needed
272 if (isGrantedOperation(theOperation->id()))
274 if (theOperation && theOperation->isModified()) {
275 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
276 int anAnswer = QMessageBox::question(qApp->activeWindow(),
277 tr("Abort operation"),
279 QMessageBox::Ok | QMessageBox::Cancel,
280 QMessageBox::Cancel);
281 return anAnswer == QMessageBox::Ok;
286 bool XGUI_OperationMgr::commitOperation()
288 if (hasOperation() && currentOperation()->isValid()) {
295 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
297 theOperation->resume();
300 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
302 bool isGranted = false;
304 QListIterator<ModuleBase_Operation*> anIt(myOperations);
306 ModuleBase_Operation* aPreviousOperation = 0;
307 while (anIt.hasPrevious() && !isGranted) {
308 ModuleBase_Operation* anOp = anIt.previous();
310 isGranted = anOp->isGranted(theId);
315 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
317 SessionPtr aMgr = ModelAPI_Session::get();
318 DocumentPtr aDoc = aMgr->activeDocument();
319 bool aIsOp = aMgr->isOperation();
321 aMgr->startOperation();
322 aDoc->setCurrentFeature(theFeature, false);
324 aMgr->finishOperation();
327 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
329 bool aCanStart = true;
330 ModuleBase_Operation* aCurrentOp = currentOperation();
332 bool aGranted = aCurrentOp->isGranted(theId);
333 // the started operation is granted for the current one,
334 // e.g. current - Sketch, started - Line
339 if (!isGrantedOperation(theId)) {
340 // the operation is not granted in the current list of operations
341 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
342 aCanStart = abortAllOperations();
344 else if (canStopOperation(aCurrentOp)) {
345 // the started operation is granted in the parrent operation,
346 // e.g. current - Line in Sketch, started Circle
347 if (myIsApplyEnabled && aCurrentOp->isModified())
348 aCurrentOp->commit();
350 abortOperation(aCurrentOp);
359 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
361 ModuleBase_Operation* aCurrentOperation = currentOperation();
362 if (theOperation == aCurrentOperation)
363 theOperation->abort();
365 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
366 // all operation from the current to triggered should also be aborted
367 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
368 while(hasOperation()) {
369 ModuleBase_Operation* aCurrentOperation = currentOperation();
370 aCurrentOperation->abort();
371 if(theOperation == aCurrentOperation)
377 void XGUI_OperationMgr::onCommitOperation()
379 ModuleBase_Operation* anOperation = currentOperation();
381 anOperation->commit();
384 void XGUI_OperationMgr::onAbortOperation()
386 ModuleBase_Operation* aCurrentOperation = currentOperation();
387 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
388 abortOperation(aCurrentOperation);
392 void XGUI_OperationMgr::onBeforeOperationStarted()
394 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
395 if (!aCurrentOperation)
398 /// Set current feature and remeber old current feature
399 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
401 SessionPtr aMgr = ModelAPI_Session::get();
402 DocumentPtr aDoc = aMgr->activeDocument();
403 // the parameter of current feature should be false, we should use all feature, not only visible
404 // in order to correctly save the previous feature of the nested operation, where the
405 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
406 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
407 // When sketch entity operation started, the sketch should be cashed here as the current.
408 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
409 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
410 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
411 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
413 #ifdef DEBUG_CURRENT_FEATURE
414 FeaturePtr aFeature = aFOperation->feature();
415 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
416 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
417 .arg(aFOperation->isEditOperation())
418 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
420 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
421 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
424 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
425 // in create operation, the current feature is changed by addFeature()
426 aDoc->setCurrentFeature(aFOperation->feature(), false);
428 #ifdef DEBUG_CURRENT_FEATURE
429 qDebug("\tdocument->setCurrentFeature");
430 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
431 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
436 void XGUI_OperationMgr::onOperationStarted()
438 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
439 updateApplyOfOperations(aSenderOperation);
440 emit operationStarted(aSenderOperation);
443 void XGUI_OperationMgr::onBeforeOperationAborted()
445 onBeforeOperationCommitted();
448 void XGUI_OperationMgr::onOperationAborted()
450 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
451 emit operationAborted(aSenderOperation);
454 void XGUI_OperationMgr::onBeforeOperationCommitted()
456 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
457 if (!aCurrentOperation)
460 /// Restore the previous current feature
461 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
463 #ifdef DEBUG_CURRENT_FEATURE
464 QString aKind = aFOperation->feature()->getKind().c_str();
465 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
466 .arg(aFOperation->isEditOperation())
467 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
469 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
470 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
473 if (aFOperation->isEditOperation()) {
474 /// Restore the previous current feature
475 setCurrentFeature(aFOperation->previousCurrentFeature());
477 else { // create operation
478 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
479 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
480 // in order to perform it in the current transaction without opening a new one.
481 if (myOperations.front() != aFOperation)
482 setCurrentFeature(aFOperation->previousCurrentFeature());
484 #ifdef DEBUG_CURRENT_FEATURE
485 qDebug("\tdocument->setCurrentFeature");
486 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
487 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
492 void XGUI_OperationMgr::onOperationCommitted()
494 // apply state for all features from the stack of operations should be updated
495 updateApplyOfOperations();
497 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
498 emit operationCommitted(aSenderOperation);
501 void XGUI_OperationMgr::onOperationResumed()
503 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
504 emit operationResumed(aSenderOperation);
507 void XGUI_OperationMgr::onOperationStopped()
509 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
510 ModuleBase_Operation* aCurrentOperation = currentOperation();
511 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
514 myOperations.removeAll(aCurrentOperation);
515 aCurrentOperation->deleteLater();
517 emit operationStopped(aCurrentOperation);
519 // get last operation which can be resumed
520 ModuleBase_Operation* aResultOp = 0;
521 QListIterator<ModuleBase_Operation*> anIt(myOperations);
523 while (anIt.hasPrevious()) {
524 ModuleBase_Operation* anOp = anIt.previous();
531 bool isModified = aCurrentOperation->isModified();
532 aResultOp->setIsModified(aResultOp->isModified() || isModified);
533 resumeOperation(aResultOp);
534 onValidateOperation();
538 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
540 QObject* aSender = sender();
542 // Let the manager decide what to do with the given key combination.
543 ModuleBase_Operation* anOperation = currentOperation();
544 bool isAccepted = true;
545 switch (theEvent->key()) {
547 case Qt::Key_Enter: {
548 ModuleBase_Operation* aOperation = currentOperation();
549 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
550 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
551 if (!aActiveWgt || !aActiveWgt->processEnter()) {
552 if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
553 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
554 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
555 emit keyEnterReleased();
566 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
568 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
569 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
570 if (!aContext.IsNull()) {
571 Handle(V3d_View) aView = aViewer->activeView();
572 if ((theEvent->key() == Qt::Key_N))
573 aContext->HilightNextDetected(aView);
574 else if ((theEvent->key() == Qt::Key_P))
575 aContext->HilightPreviousDetected(aView);
586 // anOperation->keyReleased(theEvent->key());
591 XGUI_Workshop* XGUI_OperationMgr::workshop() const
593 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
594 return aConnector->workshop();