1 // File: XGUI_OperationMgr.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_OperationMgr.h"
7 #include "ModuleBase_Operation.h"
10 #include <QApplication>
13 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
18 XGUI_OperationMgr::~XGUI_OperationMgr()
22 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
24 return myOperations.count() > 0 ? myOperations.last() : 0;
27 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
31 return currentOperation() == theOperation;
34 bool XGUI_OperationMgr::hasOperation() const
36 return !myOperations.isEmpty() && (myOperations.last() != NULL);
39 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
41 foreach(ModuleBase_Operation* aOp, myOperations) {
42 if (aOp->id() == theId)
48 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
50 foreach(ModuleBase_Operation* aOp, myOperations) {
51 if (aOp->id() == theId)
58 int XGUI_OperationMgr::operationsCount() const
60 return myOperations.count();
63 QStringList XGUI_OperationMgr::operationList() const
66 foreach(ModuleBase_Operation* eachOperation, myOperations) {
67 FeaturePtr aFeature = eachOperation->feature();
69 result << QString::fromStdString(aFeature->getKind());
75 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
77 int idx = myOperations.lastIndexOf(theOperation);
78 if(idx == -1 || idx == 0) {
81 return myOperations.at(idx - 1);
84 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
86 if (theEvent->type() == QEvent::KeyRelease) {
87 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
89 return onKeyReleased(aKeyEvent);
92 return QObject::eventFilter(theObject, theEvent);
95 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
98 currentOperation()->postpone();
99 myOperations.append(theOperation);
101 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
102 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
103 connect(theOperation, SIGNAL(committed()), SLOT(onOperationComitted()));
104 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
105 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
107 theOperation->start();
108 onValidateOperation();
112 bool XGUI_OperationMgr::abortAllOperations()
114 if(!hasOperation()) {
116 } else if (operationsCount() == 1) {
120 QString aMessage = tr("All active operations will be aborted.");
121 int anAnswer = QMessageBox::question(qApp->activeWindow(),
122 tr("Abort operation"),
124 QMessageBox::Ok | QMessageBox::Cancel,
125 QMessageBox::Cancel);
126 bool result = anAnswer == QMessageBox::Ok;
127 while(result && hasOperation()) {
128 currentOperation()->abort();
133 void XGUI_OperationMgr::onValidateOperation()
137 ModuleBase_Operation* anOperation = currentOperation();
139 bool isValid = anOperation->isValid();
140 emit operationValidated(isValid);
144 bool XGUI_OperationMgr::commitOperation()
146 if (hasOperation() && currentOperation()->isValid()) {
153 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
155 theOperation->resume();
158 bool XGUI_OperationMgr::canStartOperation(QString theId)
160 bool aCanStart = true;
161 ModuleBase_Operation* aCurrentOp = currentOperation();
163 if (!aCurrentOp->isGranted(theId)) {
164 if (canAbortOperation()) {
175 void XGUI_OperationMgr::onCommitOperation()
177 ModuleBase_Operation* anOperation = currentOperation();
179 anOperation->commit();
182 void XGUI_OperationMgr::onAbortOperation()
184 if (hasOperation() && canAbortOperation()) {
185 currentOperation()->abort();
189 bool XGUI_OperationMgr::canAbortOperation()
191 ModuleBase_Operation* anOperation = currentOperation();
192 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
194 if (anOperation && anOperation->isModified()) {
195 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
196 int anAnswer = QMessageBox::question(qApp->activeWindow(),
197 tr("Abort operation"),
199 QMessageBox::Ok | QMessageBox::Cancel,
200 QMessageBox::Cancel);
201 return anAnswer == QMessageBox::Ok;
206 void XGUI_OperationMgr::onOperationStarted()
208 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
209 emit operationStarted(aSenderOperation);
212 void XGUI_OperationMgr::onOperationAborted()
214 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
215 emit operationAborted(aSenderOperation);
218 void XGUI_OperationMgr::onOperationComitted()
220 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
221 emit operationComitted(aSenderOperation);
224 void XGUI_OperationMgr::onOperationResumed()
226 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
227 emit operationResumed(aSenderOperation);
230 void XGUI_OperationMgr::onOperationStopped()
232 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
233 ModuleBase_Operation* anOperation = currentOperation();
234 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
237 myOperations.removeAll(anOperation);
238 anOperation->deleteLater();
240 emit operationStopped(anOperation);
242 // get last operation which can be resumed
243 ModuleBase_Operation* aResultOp = 0;
244 QListIterator<ModuleBase_Operation*> anIt(myOperations);
246 while (anIt.hasPrevious()) {
247 ModuleBase_Operation* anOp = anIt.previous();
254 resumeOperation(aResultOp);
255 onValidateOperation();
259 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
261 // Let the manager decide what to do with the given key combination.
262 ModuleBase_Operation* anOperation = currentOperation();
263 bool isAccepted = true;
264 switch (theEvent->key()) {
266 case Qt::Key_Enter: {
275 // anOperation->keyReleased(theEvent->key());