]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Corrections for extrusion/revolution xml definition and random color
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_OperationMgr.h
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "XGUI_OperationMgr.h"
8
9 #include "ModuleBase_Operation.h"
10
11 #include <QMessageBox>
12 #include <QApplication>
13 #include <QKeyEvent>
14
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16     : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false)
17 {
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 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
42 {
43   foreach(ModuleBase_Operation* aOp, myOperations) {
44     if (aOp->id() == theId)
45       return true;
46   }
47   return false;
48 }
49
50 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
51 {
52   foreach(ModuleBase_Operation* aOp, myOperations) {
53     if (aOp->id() == theId)
54       return aOp;
55   }
56   return 0;
57 }
58
59
60 int XGUI_OperationMgr::operationsCount() const
61 {
62   return myOperations.count();
63 }
64
65 QStringList XGUI_OperationMgr::operationList() const
66 {
67   QStringList result;
68   foreach(ModuleBase_Operation* eachOperation, myOperations) {
69     FeaturePtr aFeature = eachOperation->feature();
70     if(aFeature) {
71       result << QString::fromStdString(aFeature->getKind());
72     }
73   }
74   return result;
75 }
76
77 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
78 {
79   int idx = myOperations.lastIndexOf(theOperation);
80   if(idx == -1 || idx == 0) {
81     return NULL;
82   }
83   return myOperations.at(idx - 1);
84 }
85
86 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
87 {
88   if (theEvent->type() == QEvent::KeyRelease) {
89     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
90     if(aKeyEvent) {
91       return onKeyReleased(aKeyEvent);
92     }
93   }
94   return QObject::eventFilter(theObject, theEvent);
95 }
96
97 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
98 {
99   if (hasOperation())
100     currentOperation()->postpone();
101   myOperations.append(theOperation);
102
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()));
111
112   theOperation->start();
113   onValidateOperation();
114   return true;
115 }
116
117 bool XGUI_OperationMgr::abortAllOperations()
118 {
119   if(!hasOperation()) {
120     return true;
121   } else if (operationsCount() == 1) {
122     onAbortOperation();
123     return true;
124   }
125   QString aMessage = tr("All active operations will be aborted.");
126   int anAnswer = QMessageBox::question(qApp->activeWindow(),
127                                        tr("Abort operation"),
128                                        aMessage,
129                                        QMessageBox::Ok | QMessageBox::Cancel,
130                                        QMessageBox::Cancel);
131   bool result = anAnswer == QMessageBox::Ok;
132   while(result && hasOperation()) {
133     currentOperation()->abort();
134   }
135   return result;
136 }
137
138 bool XGUI_OperationMgr::commitAllOperations()
139 {
140   while (hasOperation()) {
141     if (isApplyEnabled()) {
142       onCommitOperation();
143     } else {
144       currentOperation()->abort();
145     }
146   }
147   return true;
148 }
149
150 void XGUI_OperationMgr::onValidateOperation()
151 {
152   if (!hasOperation())
153     return;
154   ModuleBase_Operation* anOperation = currentOperation();
155   if(anOperation && (!myIsValidationLock)) {
156     setApplyEnabled(anOperation->isValid());
157   }
158 }
159
160 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
161 {
162   myIsApplyEnabled = theEnabled;
163   emit validationStateChanged(theEnabled);
164 }
165
166 bool XGUI_OperationMgr::isApplyEnabled() const
167 {
168   return myIsApplyEnabled;
169 }
170
171 bool XGUI_OperationMgr::canStopOperation()
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 bool XGUI_OperationMgr::commitOperation()
189 {
190   if (hasOperation() && currentOperation()->isValid()) {
191     onCommitOperation();
192     return true;
193   }
194   return false;
195 }
196
197 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
198 {
199   theOperation->resume();
200 }
201
202 bool XGUI_OperationMgr::canStartOperation(QString theId)
203 {
204   bool aCanStart = true;
205   ModuleBase_Operation* aCurrentOp = currentOperation();
206   if (aCurrentOp) {
207     if (!aCurrentOp->isGranted(theId)) {
208       if (canStopOperation()) {
209         if (myIsApplyEnabled)
210           aCurrentOp->commit();
211         else
212           aCurrentOp->abort();
213       } else {
214         aCanStart = false;
215       }
216     }
217   }
218   return aCanStart;
219 }
220
221
222 void XGUI_OperationMgr::onCommitOperation()
223 {
224   ModuleBase_Operation* anOperation = currentOperation();
225   if (anOperation)
226     anOperation->commit();
227 }
228
229 void XGUI_OperationMgr::onAbortOperation()
230 {
231   if (hasOperation() && canStopOperation()) {
232     currentOperation()->abort();
233   }
234 }
235
236 void XGUI_OperationMgr::onOperationStarted()
237 {
238   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
239   
240   bool isNestedOk = (myOperations.count() >= 1) && 
241                      myOperations.at(0)->isValid();
242   emit nestedStateChanged(isNestedOk);
243   emit operationStarted(aSenderOperation);
244 }
245
246 void XGUI_OperationMgr::onOperationAborted()
247 {
248   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
249   emit operationAborted(aSenderOperation);
250 }
251
252 void XGUI_OperationMgr::onOperationCommitted()
253 {
254   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
255   emit nestedStateChanged(myOperations.count() >= 1);
256   emit operationCommitted(aSenderOperation);
257 }
258
259 void XGUI_OperationMgr::onOperationResumed()
260 {
261   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
262   emit operationResumed(aSenderOperation);
263 }
264
265 void XGUI_OperationMgr::onOperationStopped()
266 {
267   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
268   ModuleBase_Operation* anOperation = currentOperation();
269   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
270     return;
271
272   myOperations.removeAll(anOperation);
273   anOperation->deleteLater();
274
275   emit operationStopped(anOperation);
276
277   // get last operation which can be resumed
278   ModuleBase_Operation* aResultOp = 0;
279   QListIterator<ModuleBase_Operation*> anIt(myOperations);
280   anIt.toBack();
281   while (anIt.hasPrevious()) {
282     ModuleBase_Operation* anOp = anIt.previous();
283     if (anOp) {
284       aResultOp = anOp;
285       break;
286     }
287   }
288   if (aResultOp) {
289     resumeOperation(aResultOp);
290     onValidateOperation();
291   }
292 }
293
294 void XGUI_OperationMgr::onOperationTriggered(bool theState)
295 {
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();
301     else {
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)
308           break;
309       }
310     }
311   }
312 }
313
314 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
315 {
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()) {
320     case Qt::Key_Return:
321     case Qt::Key_Enter: {
322       emit keyEnterReleased();
323       commitOperation();
324     }
325     break;
326     default:
327       isAccepted = false;
328       break;
329   }
330   //if(anOperation) {
331   //  anOperation->keyReleased(theEvent->key());
332   //}
333   return isAccepted;
334 }
335