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()));
106 connect(theOperation, SIGNAL(activatedByPreselection()),
107 SIGNAL(operationActivatedByPreselection()));
109 theOperation->start();
110 onValidateOperation();
114 bool XGUI_OperationMgr::abortAllOperations()
116 if(!hasOperation()) {
118 } else if (operationsCount() == 1) {
122 QString aMessage = tr("All active operations will be aborted.");
123 int anAnswer = QMessageBox::question(qApp->activeWindow(),
124 tr("Abort operation"),
126 QMessageBox::Ok | QMessageBox::Cancel,
127 QMessageBox::Cancel);
128 bool result = anAnswer == QMessageBox::Ok;
129 while(result && hasOperation()) {
130 currentOperation()->abort();
135 void XGUI_OperationMgr::onValidateOperation()
139 ModuleBase_Operation* anOperation = currentOperation();
141 bool isValid = anOperation->isValid();
142 emit operationValidated(isValid);
146 bool XGUI_OperationMgr::commitOperation()
148 if (hasOperation() && currentOperation()->isValid()) {
155 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
157 theOperation->resume();
160 bool XGUI_OperationMgr::canStartOperation(QString theId)
162 bool aCanStart = true;
163 ModuleBase_Operation* aCurrentOp = currentOperation();
165 if (!aCurrentOp->isGranted(theId)) {
166 if (canAbortOperation()) {
177 void XGUI_OperationMgr::onCommitOperation()
179 ModuleBase_Operation* anOperation = currentOperation();
181 anOperation->commit();
184 void XGUI_OperationMgr::onAbortOperation()
186 if (hasOperation() && canAbortOperation()) {
187 currentOperation()->abort();
191 bool XGUI_OperationMgr::canAbortOperation()
193 ModuleBase_Operation* anOperation = currentOperation();
194 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
196 if (anOperation && anOperation->isModified()) {
197 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
198 int anAnswer = QMessageBox::question(qApp->activeWindow(),
199 tr("Abort operation"),
201 QMessageBox::Ok | QMessageBox::Cancel,
202 QMessageBox::Cancel);
203 return anAnswer == QMessageBox::Ok;
208 void XGUI_OperationMgr::onOperationStarted()
210 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
211 emit operationStarted(aSenderOperation);
214 void XGUI_OperationMgr::onOperationAborted()
216 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
217 emit operationAborted(aSenderOperation);
220 void XGUI_OperationMgr::onOperationComitted()
222 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
223 emit operationComitted(aSenderOperation);
226 void XGUI_OperationMgr::onOperationResumed()
228 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
229 emit operationResumed(aSenderOperation);
232 void XGUI_OperationMgr::onOperationStopped()
234 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
235 ModuleBase_Operation* anOperation = currentOperation();
236 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
239 myOperations.removeAll(anOperation);
240 anOperation->deleteLater();
242 emit operationStopped(anOperation);
244 // get last operation which can be resumed
245 ModuleBase_Operation* aResultOp = 0;
246 QListIterator<ModuleBase_Operation*> anIt(myOperations);
248 while (anIt.hasPrevious()) {
249 ModuleBase_Operation* anOp = anIt.previous();
256 resumeOperation(aResultOp);
257 onValidateOperation();
261 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
263 // Let the manager decide what to do with the given key combination.
264 ModuleBase_Operation* anOperation = currentOperation();
265 bool isAccepted = true;
266 switch (theEvent->key()) {
268 case Qt::Key_Enter: {
269 emit keyEnterReleased();
278 // anOperation->keyReleased(theEvent->key());