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), 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 (workshop()->errorMgr()->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 workshop()->errorMgr()->updateActions(aFOperation->feature());
206 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
208 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
210 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
212 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
215 foreach(ModuleBase_Operation* anOperation, myOperations) {
217 updateApplyOfOperations(anOperation);
222 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
224 //in case of nested (sketch) operation no confirmation needed
225 if (isGrantedOperation(theOperation->id()))
227 if (theOperation && theOperation->isModified()) {
228 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
229 int anAnswer = QMessageBox::question(qApp->activeWindow(),
230 tr("Abort operation"),
232 QMessageBox::Ok | QMessageBox::Cancel,
233 QMessageBox::Cancel);
234 return anAnswer == QMessageBox::Ok;
239 bool XGUI_OperationMgr::commitOperation()
241 if (hasOperation() && currentOperation()->isValid()) {
248 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
250 theOperation->resume();
253 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
255 bool isGranted = false;
257 QListIterator<ModuleBase_Operation*> anIt(myOperations);
259 ModuleBase_Operation* aPreviousOperation = 0;
260 while (anIt.hasPrevious() && !isGranted) {
261 ModuleBase_Operation* anOp = anIt.previous();
263 isGranted = anOp->isGranted(theId);
268 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
270 SessionPtr aMgr = ModelAPI_Session::get();
271 DocumentPtr aDoc = aMgr->activeDocument();
272 bool aIsOp = aMgr->isOperation();
274 aMgr->startOperation();
275 aDoc->setCurrentFeature(theFeature, false);
277 aMgr->finishOperation();
280 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
282 bool aCanStart = true;
283 ModuleBase_Operation* aCurrentOp = currentOperation();
285 bool aGranted = aCurrentOp->isGranted(theId);
286 // the started operation is granted for the current one,
287 // e.g. current - Sketch, started - Line
292 if (!isGrantedOperation(theId)) {
293 // the operation is not granted in the current list of operations
294 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
295 aCanStart = abortAllOperations();
297 else if (canStopOperation(aCurrentOp)) {
298 // the started operation is granted in the parrent operation,
299 // e.g. current - Line in Sketch, started Circle
300 if (workshop()->errorMgr()->isApplyEnabled() && aCurrentOp->isModified())
301 aCurrentOp->commit();
303 abortOperation(aCurrentOp);
312 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
314 ModuleBase_Operation* aCurrentOperation = currentOperation();
315 if (theOperation == aCurrentOperation)
316 theOperation->abort();
318 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
319 // all operation from the current to triggered should also be aborted
320 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
321 while(hasOperation()) {
322 ModuleBase_Operation* aCurrentOperation = currentOperation();
323 aCurrentOperation->abort();
324 if(theOperation == aCurrentOperation)
330 void XGUI_OperationMgr::onCommitOperation()
332 ModuleBase_Operation* anOperation = currentOperation();
334 anOperation->commit();
337 void XGUI_OperationMgr::onAbortOperation()
339 ModuleBase_Operation* aCurrentOperation = currentOperation();
340 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
341 abortOperation(aCurrentOperation);
345 void XGUI_OperationMgr::onBeforeOperationStarted()
347 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
348 if (!aCurrentOperation)
351 /// Set current feature and remeber old current feature
352 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
354 SessionPtr aMgr = ModelAPI_Session::get();
355 DocumentPtr aDoc = aMgr->activeDocument();
356 // the parameter of current feature should be false, we should use all feature, not only visible
357 // in order to correctly save the previous feature of the nested operation, where the
358 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
359 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
360 // When sketch entity operation started, the sketch should be cashed here as the current.
361 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
362 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
363 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
364 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
366 #ifdef DEBUG_CURRENT_FEATURE
367 FeaturePtr aFeature = aFOperation->feature();
368 QString aKind = aFeature ? aFeature->getKind().c_str() : "";
369 qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
370 .arg(aFOperation->isEditOperation())
371 .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
373 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
374 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
377 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
378 // in create operation, the current feature is changed by addFeature()
379 aDoc->setCurrentFeature(aFOperation->feature(), false);
381 #ifdef DEBUG_CURRENT_FEATURE
382 qDebug("\tdocument->setCurrentFeature");
383 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
384 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
386 ModuleBase_IModule* aModule = myWorkshop->module();
388 aModule->beforeOperationStarted(aFOperation);
392 void XGUI_OperationMgr::onOperationStarted()
394 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
395 updateApplyOfOperations(aSenderOperation);
396 emit operationStarted(aSenderOperation);
399 void XGUI_OperationMgr::onBeforeOperationAborted()
401 onBeforeOperationCommitted();
404 void XGUI_OperationMgr::onOperationAborted()
406 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
407 emit operationAborted(aSenderOperation);
410 void XGUI_OperationMgr::onBeforeOperationCommitted()
412 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
413 if (!aCurrentOperation)
416 /// Restore the previous current feature
417 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
419 #ifdef DEBUG_CURRENT_FEATURE
420 QString aKind = aFOperation->feature()->getKind().c_str();
421 qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
422 .arg(aFOperation->isEditOperation())
423 .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
425 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
426 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
429 if (aFOperation->isEditOperation()) {
430 /// Restore the previous current feature
431 setCurrentFeature(aFOperation->previousCurrentFeature());
433 else { // create operation
434 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
435 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
436 // in order to perform it in the current transaction without opening a new one.
437 if (myOperations.front() != aFOperation)
438 setCurrentFeature(aFOperation->previousCurrentFeature());
440 #ifdef DEBUG_CURRENT_FEATURE
441 qDebug("\tdocument->setCurrentFeature");
442 qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
443 ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
445 ModuleBase_IModule* aModule = myWorkshop->module();
447 aModule->beforeOperationStopped(aFOperation);
451 void XGUI_OperationMgr::onOperationCommitted()
453 // apply state for all features from the stack of operations should be updated
454 updateApplyOfOperations();
456 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
457 emit operationCommitted(aSenderOperation);
460 void XGUI_OperationMgr::onOperationResumed()
462 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
463 emit operationResumed(aSenderOperation);
466 void XGUI_OperationMgr::onOperationStopped()
468 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
469 ModuleBase_Operation* aCurrentOperation = currentOperation();
470 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
473 myOperations.removeAll(aCurrentOperation);
474 aCurrentOperation->deleteLater();
476 emit operationStopped(aCurrentOperation);
478 // get last operation which can be resumed
479 ModuleBase_Operation* aResultOp = 0;
480 QListIterator<ModuleBase_Operation*> anIt(myOperations);
482 while (anIt.hasPrevious()) {
483 ModuleBase_Operation* anOp = anIt.previous();
490 bool isModified = aCurrentOperation->isModified();
491 aResultOp->setIsModified(aResultOp->isModified() || isModified);
492 resumeOperation(aResultOp);
493 onValidateOperation();
497 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
499 QObject* aSender = sender();
501 // Let the manager decide what to do with the given key combination.
502 ModuleBase_Operation* anOperation = currentOperation();
503 bool isAccepted = true;
504 switch (theEvent->key()) {
506 case Qt::Key_Enter: {
507 isAccepted = onProcessEnter();
512 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
514 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
515 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
516 if (!aContext.IsNull()) {
517 Handle(V3d_View) aView = aViewer->activeView();
518 if ((theEvent->key() == Qt::Key_N))
519 aContext->HilightNextDetected(aView);
520 else if ((theEvent->key() == Qt::Key_P))
521 aContext->HilightPreviousDetected(aView);
532 // anOperation->keyReleased(theEvent->key());
537 bool XGUI_OperationMgr::onProcessEnter()
539 bool isAccepted = true;
540 ModuleBase_Operation* aOperation = currentOperation();
541 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
542 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
543 if (!aActiveWgt || !aActiveWgt->processEnter()) {
544 if (!myWorkshop->module()->processEnter(aActiveWgt ? aActiveWgt->attributeID() : "")) {
545 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
546 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
547 emit keyEnterReleased();
557 XGUI_Workshop* XGUI_OperationMgr::workshop() const
559 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
560 return aConnector->workshop();