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 setApplyEnabled(myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty());
205 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
207 myIsApplyEnabled = theEnabled;
208 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
209 (currentOperation());
211 workshop()->errorMgr()->updateActions(aFOperation->feature());
213 //emit validationStateChanged(theEnabled);
216 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
218 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
220 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
222 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
223 //emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
224 // theOperation->isValid());
227 foreach(ModuleBase_Operation* anOperation, myOperations) {
229 updateApplyOfOperations(anOperation);
230 //emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
231 // anOperation->isValid());
236 bool XGUI_OperationMgr::isApplyEnabled() const
238 return myIsApplyEnabled;
241 bool XGUI_OperationMgr::isParentOperationValid() const
243 bool isValid = false;
244 // the enable state of the parent operation of the nested one is defined by the rules that
245 // firstly there are nested operations and secondly the parent operation is valid
246 ModuleBase_Operation* aPrevOp = 0;
247 Operations::const_iterator anIt = myOperations.end();
248 if (anIt != myOperations.begin()) { // there are items in the operations list
250 aPrevOp = *anIt; // the last top operation, the operation which is started
251 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
256 return aPrevOp && aPrevOp->isValid();
259 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
261 //in case of nested (sketch) operation no confirmation needed
262 if (isGrantedOperation(theOperation->id()))
264 if (theOperation && theOperation->isModified()) {
265 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
266 int anAnswer = QMessageBox::question(qApp->activeWindow(),
267 tr("Abort operation"),
269 QMessageBox::Ok | QMessageBox::Cancel,
270 QMessageBox::Cancel);
271 return anAnswer == QMessageBox::Ok;
276 bool XGUI_OperationMgr::commitOperation()
278 if (hasOperation() && currentOperation()->isValid()) {
285 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
287 theOperation->resume();
290 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
292 bool isGranted = false;
294 QListIterator<ModuleBase_Operation*> anIt(myOperations);
296 ModuleBase_Operation* aPreviousOperation = 0;
297 while (anIt.hasPrevious()) {
298 ModuleBase_Operation* anOp = anIt.previous();
300 isGranted = anOp->isGranted(theId);
305 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
307 SessionPtr aMgr = ModelAPI_Session::get();
308 DocumentPtr aDoc = aMgr->activeDocument();
309 bool aIsOp = aMgr->isOperation();
311 aMgr->startOperation();
312 aDoc->setCurrentFeature(theFeature, false);
314 aMgr->finishOperation();
317 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
319 bool aCanStart = true;
320 ModuleBase_Operation* aCurrentOp = currentOperation();
322 bool aGranted = aCurrentOp->isGranted(theId);
323 // the started operation is granted for the current one,
324 // e.g. current - Sketch, started - Line
329 if (!isGrantedOperation(theId)) {
330 // the operation is not granted in the current list of operations
331 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
332 aCanStart = abortAllOperations();
334 else if (canStopOperation(aCurrentOp)) {
335 // the started operation is granted in the parrent operation,
336 // e.g. current - Line in Sketch, started Circle
337 if (myIsApplyEnabled && aCurrentOp->isModified())
338 aCurrentOp->commit();
340 abortOperation(aCurrentOp);
349 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
351 ModuleBase_Operation* aCurrentOperation = currentOperation();
352 if (theOperation == aCurrentOperation)
353 theOperation->abort();
355 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
356 // all operation from the current to triggered should also be aborted
357 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
358 while(hasOperation()) {
359 ModuleBase_Operation* aCurrentOperation = currentOperation();
360 aCurrentOperation->abort();
361 if(theOperation == aCurrentOperation)
367 void XGUI_OperationMgr::onCommitOperation()
369 ModuleBase_Operation* anOperation = currentOperation();
371 anOperation->commit();
374 void XGUI_OperationMgr::onAbortOperation()
376 ModuleBase_Operation* aCurrentOperation = currentOperation();
377 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
378 abortOperation(aCurrentOperation);
382 void XGUI_OperationMgr::onBeforeOperationStarted()
384 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
385 if (!aCurrentOperation)
388 /// Set current feature and remeber old current feature
389 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
391 SessionPtr aMgr = ModelAPI_Session::get();
392 DocumentPtr aDoc = aMgr->activeDocument();
393 // the parameter of current feature should be false, we should use all feature, not only visible
394 // in order to correctly save the previous feature of the nested operation, where the
395 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
396 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
397 // When sketch entity operation started, the sketch should be cashed here as the current.
398 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
399 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
400 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
401 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
403 #ifdef DEBUG_CURRENT_FEATURE
404 FeaturePtr aFeature = aFOperation->feature();
405 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
406 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
407 .arg(aFOperation->isEditOperation())
408 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
410 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
411 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
414 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
415 // in create operation, the current feature is changed by addFeature()
416 aDoc->setCurrentFeature(aFOperation->feature(), false);
418 #ifdef DEBUG_CURRENT_FEATURE
419 qDebug("\tdocument->setCurrentFeature");
420 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
421 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
426 void XGUI_OperationMgr::onOperationStarted()
428 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
429 updateApplyOfOperations(aSenderOperation);
430 emit operationStarted(aSenderOperation);
433 void XGUI_OperationMgr::onBeforeOperationAborted()
435 onBeforeOperationCommitted();
438 void XGUI_OperationMgr::onOperationAborted()
440 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
441 emit operationAborted(aSenderOperation);
444 void XGUI_OperationMgr::onBeforeOperationCommitted()
446 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
447 if (!aCurrentOperation)
450 /// Restore the previous current feature
451 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
453 #ifdef DEBUG_CURRENT_FEATURE
454 QString aKind = aFOperation->feature()->getKind().c_str();
455 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
456 .arg(aFOperation->isEditOperation())
457 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
459 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
460 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
463 if (aFOperation->isEditOperation()) {
464 /// Restore the previous current feature
465 setCurrentFeature(aFOperation->previousCurrentFeature());
467 else { // create operation
468 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
469 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
470 // in order to perform it in the current transaction without opening a new one.
471 if (myOperations.front() != aFOperation)
472 setCurrentFeature(aFOperation->previousCurrentFeature());
474 #ifdef DEBUG_CURRENT_FEATURE
475 qDebug("\tdocument->setCurrentFeature");
476 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
477 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
482 void XGUI_OperationMgr::onOperationCommitted()
484 // apply state for all features from the stack of operations should be updated
485 updateApplyOfOperations();
487 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
488 emit operationCommitted(aSenderOperation);
491 void XGUI_OperationMgr::onOperationResumed()
493 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
494 emit operationResumed(aSenderOperation);
497 void XGUI_OperationMgr::onOperationStopped()
499 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
500 ModuleBase_Operation* aCurrentOperation = currentOperation();
501 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
504 myOperations.removeAll(aCurrentOperation);
505 aCurrentOperation->deleteLater();
507 emit operationStopped(aCurrentOperation);
509 // get last operation which can be resumed
510 ModuleBase_Operation* aResultOp = 0;
511 QListIterator<ModuleBase_Operation*> anIt(myOperations);
513 while (anIt.hasPrevious()) {
514 ModuleBase_Operation* anOp = anIt.previous();
521 bool isModified = aCurrentOperation->isModified();
522 aResultOp->setIsModified(aResultOp->isModified() || isModified);
523 resumeOperation(aResultOp);
524 onValidateOperation();
528 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
530 QObject* aSender = sender();
532 // Let the manager decide what to do with the given key combination.
533 ModuleBase_Operation* anOperation = currentOperation();
534 bool isAccepted = true;
535 switch (theEvent->key()) {
537 case Qt::Key_Enter: {
538 ModuleBase_Operation* aOperation = currentOperation();
539 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
540 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
541 if (!aActiveWgt || !aActiveWgt->processEnter()) {
542 if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
543 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
544 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty()) {
545 emit keyEnterReleased();
556 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
558 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
559 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
560 if (!aContext.IsNull()) {
561 Handle(V3d_View) aView = aViewer->activeView();
562 if ((theEvent->key() == Qt::Key_N))
563 aContext->HilightNextDetected(aView);
564 else if ((theEvent->key() == Qt::Key_P))
565 aContext->HilightPreviousDetected(aView);
576 // anOperation->keyReleased(theEvent->key());
581 XGUI_Workshop* XGUI_OperationMgr::workshop() const
583 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
584 return aConnector->workshop();