]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
4a5449caf2e66607ad4d0105a4d3599ae383e501
[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 }
17
18 XGUI_OperationMgr::~XGUI_OperationMgr()
19 {
20 }
21
22 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
23 {
24   return myOperations.count() > 0 ? myOperations.last() : 0;
25 }
26
27 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
28 {
29   if(!hasOperation())
30     return false;
31   return currentOperation() == theOperation;
32 }
33
34 bool XGUI_OperationMgr::hasOperation() const
35 {
36   return !myOperations.isEmpty() && (myOperations.last() != NULL);
37 }
38
39 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
40 {
41   foreach(ModuleBase_Operation* aOp, myOperations) {
42     if (aOp->id() == theId)
43       return true;
44   }
45   return false;
46 }
47
48 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
49 {
50   foreach(ModuleBase_Operation* aOp, myOperations) {
51     if (aOp->id() == theId)
52       return aOp;
53   }
54   return 0;
55 }
56
57
58 int XGUI_OperationMgr::operationsCount() const
59 {
60   return myOperations.count();
61 }
62
63 QStringList XGUI_OperationMgr::operationList() const
64 {
65   QStringList result;
66   foreach(ModuleBase_Operation* eachOperation, myOperations) {
67     FeaturePtr aFeature = eachOperation->feature();
68     if(aFeature) {
69       result << QString::fromStdString(aFeature->getKind());
70     }
71   }
72   return result;
73 }
74
75 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
76 {
77   int idx = myOperations.lastIndexOf(theOperation);
78   if(idx == -1 || idx == 0) {
79     return NULL;
80   }
81   return myOperations.at(idx - 1);
82 }
83
84 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
85 {
86   if (theEvent->type() == QEvent::KeyRelease) {
87     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
88     if(aKeyEvent) {
89       return onKeyReleased(aKeyEvent);
90     }
91   }
92   return QObject::eventFilter(theObject, theEvent);
93 }
94
95 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
96 {
97   if (hasOperation())
98     currentOperation()->postpone();
99   myOperations.append(theOperation);
100
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()));
108
109   theOperation->start();
110   onValidateOperation();
111   return true;
112 }
113
114 bool XGUI_OperationMgr::abortAllOperations()
115 {
116   if(!hasOperation()) {
117     return true;
118   } else if (operationsCount() == 1) {
119     onAbortOperation();
120     return true;
121   }
122   QString aMessage = tr("All active operations will be aborted.");
123   int anAnswer = QMessageBox::question(qApp->activeWindow(),
124                                        tr("Abort operation"),
125                                        aMessage,
126                                        QMessageBox::Ok | QMessageBox::Cancel,
127                                        QMessageBox::Cancel);
128   bool result = anAnswer == QMessageBox::Ok;
129   while(result && hasOperation()) {
130     currentOperation()->abort();
131   }
132   return result;
133 }
134
135 void XGUI_OperationMgr::onValidateOperation()
136 {
137   if (!hasOperation())
138     return;
139   ModuleBase_Operation* anOperation = currentOperation();
140   if(anOperation) {
141     bool isValid = anOperation->isValid();
142     emit operationValidated(isValid);
143   }
144 }
145
146 bool XGUI_OperationMgr::commitOperation()
147 {
148   if (hasOperation() && currentOperation()->isValid()) {
149     onCommitOperation();
150     return true;
151   }
152   return false;
153 }
154
155 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
156 {
157   theOperation->resume();
158 }
159
160 bool XGUI_OperationMgr::canStartOperation(QString theId)
161 {
162   bool aCanStart = true;
163   ModuleBase_Operation* aCurrentOp = currentOperation();
164   if (aCurrentOp) {
165     if (!aCurrentOp->isGranted(theId)) {
166       if (canAbortOperation()) {
167         aCurrentOp->abort();
168       } else {
169         aCanStart = false;
170       }
171     }
172   }
173   return aCanStart;
174 }
175
176
177 void XGUI_OperationMgr::onCommitOperation()
178 {
179   ModuleBase_Operation* anOperation = currentOperation();
180   if (anOperation)
181     anOperation->commit();
182 }
183
184 void XGUI_OperationMgr::onAbortOperation()
185 {
186   if (hasOperation() && canAbortOperation()) {
187     currentOperation()->abort();
188   }
189 }
190
191 bool XGUI_OperationMgr::canAbortOperation()
192 {
193   ModuleBase_Operation* anOperation = currentOperation();
194   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
195     return true;
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"),
200                                          aMessage,
201                                          QMessageBox::Ok | QMessageBox::Cancel,
202                                          QMessageBox::Cancel);
203     return anAnswer == QMessageBox::Ok;
204   }
205   return true;
206 }
207
208 void XGUI_OperationMgr::onOperationStarted()
209 {
210   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
211   emit operationStarted(aSenderOperation);
212 }
213
214 void XGUI_OperationMgr::onOperationAborted()
215 {
216   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
217   emit operationAborted(aSenderOperation);
218 }
219
220 void XGUI_OperationMgr::onOperationComitted()
221 {
222   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
223   emit operationComitted(aSenderOperation);
224 }
225
226 void XGUI_OperationMgr::onOperationResumed()
227 {
228   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
229   emit operationResumed(aSenderOperation);
230 }
231
232 void XGUI_OperationMgr::onOperationStopped()
233 {
234   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
235   ModuleBase_Operation* anOperation = currentOperation();
236   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
237     return;
238
239   myOperations.removeAll(anOperation);
240   anOperation->deleteLater();
241
242   emit operationStopped(anOperation);
243
244   // get last operation which can be resumed
245   ModuleBase_Operation* aResultOp = 0;
246   QListIterator<ModuleBase_Operation*> anIt(myOperations);
247   anIt.toBack();
248   while (anIt.hasPrevious()) {
249     ModuleBase_Operation* anOp = anIt.previous();
250     if (anOp) {
251       aResultOp = anOp;
252       break;
253     }
254   }
255   if (aResultOp) {
256     resumeOperation(aResultOp);
257     onValidateOperation();
258   }
259 }
260
261 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
262 {
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()) {
267     case Qt::Key_Return:
268     case Qt::Key_Enter: {
269       emit keyEnterReleased();
270       commitOperation();
271     }
272       break;
273     default:
274       isAccepted = false;
275       break;
276   }
277   //if(anOperation) {
278   //  anOperation->keyReleased(theEvent->key());
279   //}
280   return isAccepted;
281 }
282