]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Synchronization of Apply/ApplyAll buttons.
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_OperationMgr.cpp
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "XGUI_OperationMgr.h"
8
9 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_IWorkshop.h"
11 #include "ModuleBase_IModule.h"
12
13 #include "ModelAPI_CompositeFeature.h"
14 #include "ModelAPI_Session.h"
15
16 #include <QMessageBox>
17 #include <QApplication>
18 #include <QKeyEvent>
19
20 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
21                                      ModuleBase_IWorkshop* theWorkshop)
22 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
23   myWorkshop(theWorkshop)
24 {
25 }
26
27 XGUI_OperationMgr::~XGUI_OperationMgr()
28 {
29 }
30
31 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
32 {
33   return myOperations.count() > 0 ? myOperations.last() : 0;
34 }
35
36 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
37 {
38   if(!hasOperation())
39     return false;
40   return currentOperation() == theOperation;
41 }
42
43 bool XGUI_OperationMgr::hasOperation() const
44 {
45   return !myOperations.isEmpty() && (myOperations.last() != NULL);
46 }
47
48 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
49 {
50   foreach(ModuleBase_Operation* aOp, myOperations) {
51     if (aOp->id() == theId)
52       return true;
53   }
54   return false;
55 }
56
57 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
58 {
59   foreach(ModuleBase_Operation* aOp, myOperations) {
60     if (aOp->id() == theId)
61       return aOp;
62   }
63   return 0;
64 }
65
66
67 int XGUI_OperationMgr::operationsCount() const
68 {
69   return myOperations.count();
70 }
71
72 QStringList XGUI_OperationMgr::operationList() const
73 {
74   QStringList result;
75   foreach(ModuleBase_Operation* eachOperation, myOperations) {
76     FeaturePtr aFeature = eachOperation->feature();
77     if(aFeature) {
78       result << QString::fromStdString(aFeature->getKind());
79     }
80   }
81   return result;
82 }
83
84 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
85 {
86   int idx = myOperations.lastIndexOf(theOperation);
87   if(idx == -1 || idx == 0) {
88     return NULL;
89   }
90   return myOperations.at(idx - 1);
91 }
92
93 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
94 {
95   if (theEvent->type() == QEvent::KeyRelease) {
96     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
97     if(aKeyEvent) {
98       return onKeyReleased(aKeyEvent);
99     }
100   }
101   return QObject::eventFilter(theObject, theEvent);
102 }
103
104 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
105 {
106   if (hasOperation())
107     currentOperation()->postpone();
108   myOperations.append(theOperation);
109
110   connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
111   connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
112   connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
113   connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
114   connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
115   connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
116   connect(theOperation, SIGNAL(activatedByPreselection()),
117           SIGNAL(operationActivatedByPreselection()));
118
119   theOperation->start();
120   onValidateOperation();
121   return true;
122 }
123
124 bool XGUI_OperationMgr::abortAllOperations()
125 {
126   bool aResult = true;
127   if(!hasOperation())
128     return aResult;
129
130   if (operationsCount() == 1) {
131     if (canStopOperation()) {
132       abortOperation(currentOperation());
133     }
134     else
135       aResult = false;
136   }
137   else {
138     aResult = QMessageBox::question(qApp->activeWindow(),
139                                     tr("Abort operation"),
140                                     tr("All active operations will be aborted."),
141                                     QMessageBox::Ok | QMessageBox::Cancel,
142                                     QMessageBox::Cancel) == QMessageBox::Ok;
143     while(aResult && hasOperation()) {
144       abortOperation(currentOperation());
145     }
146   }
147   return aResult;
148 }
149
150 bool XGUI_OperationMgr::commitAllOperations()
151 {
152   bool isCompositeCommitted = false;
153   while (hasOperation()) {
154     ModuleBase_Operation* anOperation = currentOperation();
155     if (isApplyEnabled()) {
156       onCommitOperation();
157     } else {
158       abortOperation(anOperation);
159     }
160     FeaturePtr aFeature = anOperation->feature();
161     CompositeFeaturePtr aComposite = 
162         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
163     isCompositeCommitted = aComposite.get();
164     if (isCompositeCommitted)
165       break;
166   }
167   return true;
168 }
169
170 void XGUI_OperationMgr::onValidateOperation()
171 {
172   if (!hasOperation())
173     return;
174   ModuleBase_Operation* anOperation = currentOperation();
175   if(anOperation) {
176     bool aCanCommit = myWorkshop->module()->canCommitOperation();
177     setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
178   }
179 }
180
181 void XGUI_OperationMgr::setLockValidating(bool toLock)
182 {
183   myIsValidationLock = toLock;
184   onValidateOperation();
185 }
186
187 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
188 {
189   myIsApplyEnabled = theEnabled;
190   emit validationStateChanged(theEnabled);
191
192   bool aParentValid = isParentOperationValid();
193   emit nestedStateChanged(aParentValid);
194 }
195
196 bool XGUI_OperationMgr::isApplyEnabled() const
197 {
198   return myIsApplyEnabled;
199 }
200
201 bool XGUI_OperationMgr::isParentOperationValid() const
202 {
203   bool isValid = false;
204   // the enable state of the parent operation of the nested one is defined by the rules that
205   // firstly there are nested operations and secondly the parent operation is valid
206   ModuleBase_Operation* aPrevOp = 0;
207   Operations::const_iterator anIt = myOperations.end();
208   if (anIt != myOperations.begin()) { // there are items in the operations list
209     --anIt;
210     aPrevOp = *anIt; // the last top operation, the operation which is started
211     if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
212       --anIt;
213       aPrevOp = *anIt;
214     }
215   }
216   return aPrevOp && aPrevOp->isValid();
217 }
218
219 bool XGUI_OperationMgr::canStopOperation()
220 {
221   ModuleBase_Operation* anOperation = currentOperation();
222   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
223     return true;
224   if (anOperation && anOperation->isModified()) {
225     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
226     int anAnswer = QMessageBox::question(qApp->activeWindow(),
227                                          tr("Abort operation"),
228                                          aMessage,
229                                          QMessageBox::Ok | QMessageBox::Cancel,
230                                          QMessageBox::Cancel);
231     return anAnswer == QMessageBox::Ok;
232   }
233   return true;
234 }
235
236 bool XGUI_OperationMgr::commitOperation()
237 {
238   if (hasOperation() && currentOperation()->isValid()) {
239     onCommitOperation();
240     return true;
241   }
242   return false;
243 }
244
245 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
246 {
247   theOperation->resume();
248 }
249
250 bool XGUI_OperationMgr::canStartOperation(QString theId)
251 {
252   bool aCanStart = true;
253   ModuleBase_Operation* aCurrentOp = currentOperation();
254   if (aCurrentOp) {
255     if (!aCurrentOp->isGranted(theId)) {
256       if (canStopOperation()) {
257         if (myIsApplyEnabled)
258           aCurrentOp->commit();
259         else
260           abortOperation(aCurrentOp);
261       } else {
262         aCanStart = false;
263       }
264     }
265   }
266   return aCanStart;
267 }
268
269 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
270 {
271   ModuleBase_Operation* aCurrentOperation = currentOperation();
272   if (theOperation == aCurrentOperation)
273     theOperation->abort();
274   else {
275     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
276     // all operation from the current to triggered should also be aborted
277     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
278     while(hasOperation()) {
279       ModuleBase_Operation* aCurrentOperation = currentOperation();
280       aCurrentOperation->abort();
281       if(theOperation == aCurrentOperation)
282         break;
283     }
284   }
285 }
286
287 void XGUI_OperationMgr::onCommitOperation()
288 {
289   ModuleBase_Operation* anOperation = currentOperation();
290   if (anOperation)
291     anOperation->commit();
292 }
293
294 void XGUI_OperationMgr::onAbortOperation()
295 {
296   if (hasOperation() && canStopOperation()) {
297     abortOperation(currentOperation());
298   }
299 }
300
301 void XGUI_OperationMgr::onOperationStarted()
302 {
303   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
304   emit operationStarted(aSenderOperation);
305 }
306
307 void XGUI_OperationMgr::onOperationAborted()
308 {
309   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
310   emit operationAborted(aSenderOperation);
311 }
312
313 void XGUI_OperationMgr::onOperationCommitted()
314 {
315   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
316   emit operationCommitted(aSenderOperation);
317 }
318
319 void XGUI_OperationMgr::onOperationResumed()
320 {
321   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
322   emit operationResumed(aSenderOperation);
323 }
324
325 void XGUI_OperationMgr::onOperationStopped()
326 {
327   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
328   ModuleBase_Operation* aCurrentOperation = currentOperation();
329   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
330     return;
331
332   myOperations.removeAll(aCurrentOperation);
333   aCurrentOperation->deleteLater();
334
335   emit operationStopped(aCurrentOperation);
336
337   // get last operation which can be resumed
338   ModuleBase_Operation* aResultOp = 0;
339   QListIterator<ModuleBase_Operation*> anIt(myOperations);
340   anIt.toBack();
341   while (anIt.hasPrevious()) {
342     ModuleBase_Operation* anOp = anIt.previous();
343     if (anOp) {
344       aResultOp = anOp;
345       break;
346     }
347   }
348   if (aResultOp) {
349     bool isModified = aCurrentOperation->isModified();
350     aResultOp->setIsModified(aResultOp->isModified() || isModified);
351     resumeOperation(aResultOp);
352     onValidateOperation();
353   }
354 }
355
356 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
357 {
358   // Let the manager decide what to do with the given key combination.
359   ModuleBase_Operation* anOperation = currentOperation();
360   bool isAccepted = true;
361   switch (theEvent->key()) {
362     case Qt::Key_Return:
363     case Qt::Key_Enter: {
364       emit keyEnterReleased();
365       commitOperation();
366     }
367     break;
368     default:
369       isAccepted = false;
370       break;
371   }
372   //if(anOperation) {
373   //  anOperation->keyReleased(theEvent->key());
374   //}
375   return isAccepted;
376 }
377