Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // File:        XGUI_OperationMgr.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "XGUI_OperationMgr.h"
6
7 #include "ModuleBase_Operation.h"
8
9 #include <QMessageBox>
10 #include <QApplication>
11 #include <QKeyEvent>
12
13 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
14     : QObject(theParent)
15 {
16   // listen to Escape signal to stop the current operation
17   qApp->installEventFilter(this);
18 }
19
20 XGUI_OperationMgr::~XGUI_OperationMgr()
21 {
22 }
23
24 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
25 {
26   return myOperations.count() > 0 ? myOperations.last() : 0;
27 }
28
29 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
30 {
31   if(!hasOperation())
32     return false;
33   return currentOperation() == theOperation;
34 }
35
36 bool XGUI_OperationMgr::hasOperation() const
37 {
38   return !myOperations.isEmpty() && (myOperations.last() != NULL);
39 }
40
41 int XGUI_OperationMgr::operationsCount() const
42 {
43   return myOperations.count();
44 }
45
46 QStringList XGUI_OperationMgr::operationList() const
47 {
48   QStringList result;
49   foreach(ModuleBase_Operation* eachOperation, myOperations) {
50     FeaturePtr aFeature = eachOperation->feature();
51     if(aFeature) {
52       result << QString::fromStdString(aFeature->getKind());
53     }
54   }
55   return result;
56 }
57
58 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
59 {
60   int idx = myOperations.lastIndexOf(theOperation);
61   if(idx == -1 || idx == 0) {
62     return NULL;
63   }
64   return myOperations.at(idx - 1);
65 }
66
67 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
68 {
69   if (theEvent->type() == QEvent::KeyRelease) {
70     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
71     if(aKeyEvent && onKeyReleased(aKeyEvent)) {
72       return true;
73     }
74   }
75   return QObject::eventFilter(theObject, theEvent);
76 }
77
78 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
79 {
80   if (!canStartOperation(theOperation))
81     return false;
82
83   myOperations.append(theOperation);
84
85   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
86   connect(theOperation, SIGNAL(started()), this, SLOT(onOperationStarted()));
87   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
88
89   theOperation->start();
90   onValidateOperation();
91   return true;
92 }
93
94 bool XGUI_OperationMgr::abortAllOperations()
95 {
96   if(!hasOperation()) {
97     return true;
98   } else if (operationsCount() == 1) {
99     onAbortOperation();
100     return true;
101   }
102   QString aMessage = tr("All active operations will be aborted.");
103   int anAnswer = QMessageBox::question(qApp->activeWindow(),
104                                        tr("Abort operation"),
105                                        aMessage,
106                                        QMessageBox::Ok | QMessageBox::Cancel,
107                                        QMessageBox::Cancel);
108   bool result = anAnswer == QMessageBox::Ok;
109   while(result && hasOperation()) {
110     currentOperation()->abort();
111   }
112   return result;
113 }
114
115 void XGUI_OperationMgr::onValidateOperation()
116 {
117   if (!hasOperation())
118     return;
119   ModuleBase_Operation* anOperation = currentOperation();
120   if(anOperation) {
121     bool isValid = anOperation->isValid();
122     emit operationValidated(isValid);
123   }
124 }
125
126 bool XGUI_OperationMgr::commitOperation()
127 {
128   if (hasOperation() && currentOperation()->isValid()) {
129     onCommitOperation();
130     return true;
131   }
132   return false;
133 }
134
135 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
136 {
137   theOperation->resume();
138 }
139
140 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
141 {
142   bool aCanStart = true;
143   ModuleBase_Operation* aCurrentOp = currentOperation();
144   if (aCurrentOp) {
145     if (!aCurrentOp->isGranted(theOperation)) {
146       if (canAbortOperation()) {
147         aCurrentOp->abort();
148       } else {
149         aCanStart = false;
150       }
151     }
152   }
153   return aCanStart;
154 }
155
156
157 void XGUI_OperationMgr::onCommitOperation()
158 {
159   ModuleBase_Operation* anOperation = currentOperation();
160   if (anOperation)
161     anOperation->commit();
162 }
163
164 void XGUI_OperationMgr::onAbortOperation()
165 {
166   if (hasOperation() && canAbortOperation()) {
167     currentOperation()->abort();
168   }
169 }
170
171 bool XGUI_OperationMgr::canAbortOperation()
172 {
173   ModuleBase_Operation* anOperation = currentOperation();
174   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
175     return true;
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"),
180                                          aMessage,
181                                          QMessageBox::Ok | QMessageBox::Cancel,
182                                          QMessageBox::Cancel);
183     return anAnswer == QMessageBox::Ok;
184   }
185   return true;
186 }
187
188 void XGUI_OperationMgr::onOperationStarted()
189 {
190   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
191   emit operationStarted(aSenderOperation);
192 }
193
194 void XGUI_OperationMgr::onOperationStopped()
195 {
196   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
197   ModuleBase_Operation* anOperation = currentOperation();
198   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
199     return;
200
201   myOperations.removeAll(anOperation);
202   anOperation->deleteLater();
203
204   emit operationStopped(anOperation);
205
206   // get last operation which can be resumed
207   ModuleBase_Operation* aResultOp = 0;
208   QListIterator<ModuleBase_Operation*> anIt(myOperations);
209   anIt.toBack();
210   while (anIt.hasPrevious()) {
211     ModuleBase_Operation* anOp = anIt.previous();
212     if (anOp) {
213       aResultOp = anOp;
214       break;
215     }
216   }
217   if (aResultOp) {
218     resumeOperation(aResultOp);
219     onValidateOperation();
220   }
221 }
222
223 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
224 {
225   // Let the manager decide what to do with the given key combination.
226   ModuleBase_Operation* anOperation = currentOperation();
227   bool isAccepted = true;
228   switch (theEvent->key()) {
229     case Qt::Key_Escape: {
230       onAbortOperation();
231     }
232       break;
233     case Qt::Key_Return:
234     case Qt::Key_Enter: {
235       commitOperation();
236     }
237       break;
238     default:
239       isAccepted = false;
240       break;
241   }
242   if(anOperation) {
243     anOperation->keyReleased(theEvent->key());
244   }
245   return isAccepted;
246 }
247