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"
9 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_IWorkshop.h"
11 #include "ModuleBase_IModule.h"
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_OperationFeature.h"
15 #include "ModelAPI_CompositeFeature.h"
16 #include "ModelAPI_Session.h"
18 #include <QMessageBox>
19 #include <QApplication>
22 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
23 ModuleBase_IWorkshop* theWorkshop)
24 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
25 myWorkshop(theWorkshop)
29 XGUI_OperationMgr::~XGUI_OperationMgr()
33 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
35 return myOperations.count() > 0 ? myOperations.last() : 0;
38 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
42 return currentOperation() == theOperation;
45 bool XGUI_OperationMgr::hasOperation() const
47 return !myOperations.isEmpty() && (myOperations.last() != NULL);
50 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
52 foreach(ModuleBase_Operation* aOp, myOperations) {
53 if (aOp->id() == theId)
59 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
61 foreach(ModuleBase_Operation* aOp, myOperations) {
62 if (aOp->id() == theId)
69 int XGUI_OperationMgr::operationsCount() const
71 return myOperations.count();
74 QStringList XGUI_OperationMgr::operationList() const
77 foreach(ModuleBase_Operation* eachOperation, myOperations) {
78 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
80 FeaturePtr aFeature = aFOperation->feature();
82 result << QString::fromStdString(aFeature->getKind());
89 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
91 int idx = myOperations.lastIndexOf(theOperation);
92 if(idx == -1 || idx == 0) {
95 return myOperations.at(idx - 1);
98 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
100 if (theEvent->type() == QEvent::KeyRelease) {
101 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
103 return onKeyReleased(aKeyEvent);
106 return QObject::eventFilter(theObject, theEvent);
109 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
112 currentOperation()->postpone();
113 myOperations.append(theOperation);
115 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
116 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
117 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
118 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
119 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
120 connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
121 connect(theOperation, SIGNAL(activatedByPreselection()),
122 SIGNAL(operationActivatedByPreselection()));
124 theOperation->start();
125 onValidateOperation();
129 bool XGUI_OperationMgr::abortAllOperations()
135 if (operationsCount() == 1) {
136 if (canStopOperation()) {
137 abortOperation(currentOperation());
143 aResult = QMessageBox::question(qApp->activeWindow(),
144 tr("Abort operation"),
145 tr("All active operations will be aborted."),
146 QMessageBox::Ok | QMessageBox::Cancel,
147 QMessageBox::Cancel) == QMessageBox::Ok;
148 while(aResult && hasOperation()) {
149 abortOperation(currentOperation());
155 bool XGUI_OperationMgr::commitAllOperations()
157 bool isCompositeCommitted = false;
158 while (hasOperation()) {
159 ModuleBase_Operation* anOperation = currentOperation();
160 if (isApplyEnabled()) {
163 abortOperation(anOperation);
165 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
168 FeaturePtr aFeature = aFOperation->feature();
169 CompositeFeaturePtr aComposite =
170 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
171 isCompositeCommitted = aComposite.get();
172 if (isCompositeCommitted)
179 void XGUI_OperationMgr::onValidateOperation()
183 ModuleBase_Operation* anOperation = currentOperation();
185 bool aCanCommit = myWorkshop->module()->canCommitOperation();
186 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
190 void XGUI_OperationMgr::setLockValidating(bool toLock)
192 myIsValidationLock = toLock;
193 onValidateOperation();
196 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
198 myIsApplyEnabled = theEnabled;
199 emit validationStateChanged(theEnabled);
202 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
205 emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
206 theOperation->isValid());
208 foreach(ModuleBase_Operation* anOperation, myOperations) {
209 emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
210 anOperation->isValid());
215 bool XGUI_OperationMgr::isApplyEnabled() const
217 return myIsApplyEnabled;
220 bool XGUI_OperationMgr::isParentOperationValid() const
222 bool isValid = false;
223 // the enable state of the parent operation of the nested one is defined by the rules that
224 // firstly there are nested operations and secondly the parent operation is valid
225 ModuleBase_Operation* aPrevOp = 0;
226 Operations::const_iterator anIt = myOperations.end();
227 if (anIt != myOperations.begin()) { // there are items in the operations list
229 aPrevOp = *anIt; // the last top operation, the operation which is started
230 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
235 return aPrevOp && aPrevOp->isValid();
238 bool XGUI_OperationMgr::canStopOperation()
240 ModuleBase_Operation* anOperation = currentOperation();
241 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
243 if (anOperation && anOperation->isModified()) {
244 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
245 int anAnswer = QMessageBox::question(qApp->activeWindow(),
246 tr("Abort operation"),
248 QMessageBox::Ok | QMessageBox::Cancel,
249 QMessageBox::Cancel);
250 return anAnswer == QMessageBox::Ok;
255 bool XGUI_OperationMgr::commitOperation()
257 if (hasOperation() && currentOperation()->isValid()) {
264 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
266 theOperation->resume();
269 bool XGUI_OperationMgr::canStartOperation(QString theId)
271 bool aCanStart = true;
272 ModuleBase_Operation* aCurrentOp = currentOperation();
274 if (!aCurrentOp->isGranted(theId)) {
275 if (canStopOperation()) {
276 if (myIsApplyEnabled)
277 aCurrentOp->commit();
279 abortOperation(aCurrentOp);
288 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
290 ModuleBase_Operation* aCurrentOperation = currentOperation();
291 if (theOperation == aCurrentOperation)
292 theOperation->abort();
294 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
295 // all operation from the current to triggered should also be aborted
296 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
297 while(hasOperation()) {
298 ModuleBase_Operation* aCurrentOperation = currentOperation();
299 aCurrentOperation->abort();
300 if(theOperation == aCurrentOperation)
306 void XGUI_OperationMgr::onCommitOperation()
308 ModuleBase_Operation* anOperation = currentOperation();
310 anOperation->commit();
313 void XGUI_OperationMgr::onAbortOperation()
315 if (hasOperation() && canStopOperation()) {
316 abortOperation(currentOperation());
320 void XGUI_OperationMgr::onOperationStarted()
322 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
323 updateApplyOfOperations(aSenderOperation);
324 emit operationStarted(aSenderOperation);
327 void XGUI_OperationMgr::onOperationAborted()
329 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
330 emit operationAborted(aSenderOperation);
333 void XGUI_OperationMgr::onOperationCommitted()
335 updateApplyOfOperations();
337 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
338 emit operationCommitted(aSenderOperation);
341 void XGUI_OperationMgr::onOperationResumed()
343 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
344 emit operationResumed(aSenderOperation);
347 void XGUI_OperationMgr::onOperationStopped()
349 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
350 ModuleBase_Operation* aCurrentOperation = currentOperation();
351 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
354 myOperations.removeAll(aCurrentOperation);
355 aCurrentOperation->deleteLater();
357 emit operationStopped(aCurrentOperation);
359 // get last operation which can be resumed
360 ModuleBase_Operation* aResultOp = 0;
361 QListIterator<ModuleBase_Operation*> anIt(myOperations);
363 while (anIt.hasPrevious()) {
364 ModuleBase_Operation* anOp = anIt.previous();
371 bool isModified = aCurrentOperation->isModified();
372 aResultOp->setIsModified(aResultOp->isModified() || isModified);
373 resumeOperation(aResultOp);
374 onValidateOperation();
378 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
380 // Let the manager decide what to do with the given key combination.
381 ModuleBase_Operation* anOperation = currentOperation();
382 bool isAccepted = true;
383 switch (theEvent->key()) {
385 case Qt::Key_Enter: {
386 emit keyEnterReleased();
395 // anOperation->keyReleased(theEvent->key());