1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_OperationMgr.h
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_OperationMgr.h"
9 #include "ModuleBase_Operation.h"
11 #include <QMessageBox>
12 #include <QApplication>
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false)
20 XGUI_OperationMgr::~XGUI_OperationMgr()
24 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
26 return myOperations.count() > 0 ? myOperations.last() : 0;
29 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
33 return currentOperation() == theOperation;
36 bool XGUI_OperationMgr::hasOperation() const
38 return !myOperations.isEmpty() && (myOperations.last() != NULL);
41 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
43 foreach(ModuleBase_Operation* aOp, myOperations) {
44 if (aOp->id() == theId)
50 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
52 foreach(ModuleBase_Operation* aOp, myOperations) {
53 if (aOp->id() == theId)
60 int XGUI_OperationMgr::operationsCount() const
62 return myOperations.count();
65 QStringList XGUI_OperationMgr::operationList() const
68 foreach(ModuleBase_Operation* eachOperation, myOperations) {
69 FeaturePtr aFeature = eachOperation->feature();
71 result << QString::fromStdString(aFeature->getKind());
77 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
79 int idx = myOperations.lastIndexOf(theOperation);
80 if(idx == -1 || idx == 0) {
83 return myOperations.at(idx - 1);
86 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
88 if (theEvent->type() == QEvent::KeyRelease) {
89 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
91 return onKeyReleased(aKeyEvent);
94 return QObject::eventFilter(theObject, theEvent);
97 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
100 currentOperation()->postpone();
101 myOperations.append(theOperation);
103 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
104 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
105 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
106 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
107 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
108 connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
109 connect(theOperation, SIGNAL(activatedByPreselection()),
110 SIGNAL(operationActivatedByPreselection()));
112 theOperation->start();
113 onValidateOperation();
117 bool XGUI_OperationMgr::abortAllOperations()
119 if(!hasOperation()) {
121 } else if (operationsCount() == 1) {
125 QString aMessage = tr("All active operations will be aborted.");
126 int anAnswer = QMessageBox::question(qApp->activeWindow(),
127 tr("Abort operation"),
129 QMessageBox::Ok | QMessageBox::Cancel,
130 QMessageBox::Cancel);
131 bool result = anAnswer == QMessageBox::Ok;
132 while(result && hasOperation()) {
133 currentOperation()->abort();
138 bool XGUI_OperationMgr::commitAllOperations()
140 while (hasOperation()) {
141 if (isApplyEnabled()) {
144 currentOperation()->abort();
150 void XGUI_OperationMgr::onValidateOperation()
154 ModuleBase_Operation* anOperation = currentOperation();
155 if(anOperation && (!myIsValidationLock)) {
156 setApplyEnabled(anOperation->isValid());
160 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
162 myIsApplyEnabled = theEnabled;
163 emit validationStateChanged(theEnabled);
166 bool XGUI_OperationMgr::isApplyEnabled() const
168 return myIsApplyEnabled;
171 bool XGUI_OperationMgr::canStopOperation()
173 ModuleBase_Operation* anOperation = currentOperation();
174 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
176 if (anOperation && anOperation->isModified()) {
177 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
178 int anAnswer = QMessageBox::question(qApp->activeWindow(),
179 tr("Abort operation"),
181 QMessageBox::Ok | QMessageBox::Cancel,
182 QMessageBox::Cancel);
183 return anAnswer == QMessageBox::Ok;
188 bool XGUI_OperationMgr::commitOperation()
190 if (hasOperation() && currentOperation()->isValid()) {
197 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
199 theOperation->resume();
202 bool XGUI_OperationMgr::canStartOperation(QString theId)
204 bool aCanStart = true;
205 ModuleBase_Operation* aCurrentOp = currentOperation();
207 if (!aCurrentOp->isGranted(theId)) {
208 if (canStopOperation()) {
209 if (myIsApplyEnabled)
210 aCurrentOp->commit();
222 void XGUI_OperationMgr::onCommitOperation()
224 ModuleBase_Operation* anOperation = currentOperation();
226 anOperation->commit();
229 void XGUI_OperationMgr::onAbortOperation()
231 if (hasOperation() && canStopOperation()) {
232 currentOperation()->abort();
236 void XGUI_OperationMgr::onOperationStarted()
238 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
240 bool isNestedOk = (myOperations.count() >= 1) &&
241 myOperations.at(0)->isValid();
242 emit nestedStateChanged(isNestedOk);
243 emit operationStarted(aSenderOperation);
246 void XGUI_OperationMgr::onOperationAborted()
248 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
249 emit operationAborted(aSenderOperation);
252 void XGUI_OperationMgr::onOperationCommitted()
254 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
255 emit nestedStateChanged(myOperations.count() >= 1);
256 emit operationCommitted(aSenderOperation);
259 void XGUI_OperationMgr::onOperationResumed()
261 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
262 emit operationResumed(aSenderOperation);
265 void XGUI_OperationMgr::onOperationStopped()
267 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
268 ModuleBase_Operation* anOperation = currentOperation();
269 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
272 myOperations.removeAll(anOperation);
273 anOperation->deleteLater();
275 emit operationStopped(anOperation);
277 // get last operation which can be resumed
278 ModuleBase_Operation* aResultOp = 0;
279 QListIterator<ModuleBase_Operation*> anIt(myOperations);
281 while (anIt.hasPrevious()) {
282 ModuleBase_Operation* anOp = anIt.previous();
289 resumeOperation(aResultOp);
290 onValidateOperation();
294 void XGUI_OperationMgr::onOperationTriggered(bool theState)
296 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
297 if (aSenderOperation && !theState) {
298 ModuleBase_Operation* aCurrentOperation = currentOperation();
299 if (aSenderOperation == aCurrentOperation)
300 aCurrentOperation->abort();
302 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
303 // all operation from the current to triggered should also be aborted
304 while(hasOperation()) {
305 ModuleBase_Operation* aCurrentOperation = currentOperation();
306 aCurrentOperation->abort();
307 if(aSenderOperation == aCurrentOperation)
314 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
316 // Let the manager decide what to do with the given key combination.
317 ModuleBase_Operation* anOperation = currentOperation();
318 bool isAccepted = true;
319 switch (theEvent->key()) {
321 case Qt::Key_Enter: {
322 emit keyEnterReleased();
331 // anOperation->keyReleased(theEvent->key());