]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
859c0da92888aca8f62c4198b75b1cad1b00aefb
[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)
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(activatedByPreselection()),
109           SIGNAL(operationActivatedByPreselection()));
110
111   theOperation->start();
112   onValidateOperation();
113   return true;
114 }
115
116 bool XGUI_OperationMgr::abortAllOperations()
117 {
118   if(!hasOperation()) {
119     return true;
120   } else if (operationsCount() == 1) {
121     onAbortOperation();
122     return true;
123   }
124   QString aMessage = tr("All active operations will be aborted.");
125   int anAnswer = QMessageBox::question(qApp->activeWindow(),
126                                        tr("Abort operation"),
127                                        aMessage,
128                                        QMessageBox::Ok | QMessageBox::Cancel,
129                                        QMessageBox::Cancel);
130   bool result = anAnswer == QMessageBox::Ok;
131   while(result && hasOperation()) {
132     currentOperation()->abort();
133   }
134   return result;
135 }
136
137 void XGUI_OperationMgr::onValidateOperation()
138 {
139   if (!hasOperation())
140     return;
141   ModuleBase_Operation* anOperation = currentOperation();
142   if(anOperation && (!myIsValidationLock)) {
143     setApplyEnabled(anOperation->isValid());
144   }
145 }
146
147 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
148 {
149   myIsApplyEnabled = theEnabled;
150   emit applyEnableChanged(theEnabled);
151 }
152
153 bool XGUI_OperationMgr::isApplyEnabled() const
154 {
155   return myIsApplyEnabled;
156 }
157
158 bool XGUI_OperationMgr::canStopOperation()
159 {
160   ModuleBase_Operation* anOperation = currentOperation();
161   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
162     return true;
163   if (anOperation && anOperation->isModified()) {
164     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
165     int anAnswer = QMessageBox::question(qApp->activeWindow(),
166                                          tr("Abort operation"),
167                                          aMessage,
168                                          QMessageBox::Ok | QMessageBox::Cancel,
169                                          QMessageBox::Cancel);
170     return anAnswer == QMessageBox::Ok;
171   }
172   return true;
173 }
174
175 bool XGUI_OperationMgr::commitOperation()
176 {
177   if (hasOperation() && currentOperation()->isValid()) {
178     onCommitOperation();
179     return true;
180   }
181   return false;
182 }
183
184 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
185 {
186   theOperation->resume();
187 }
188
189 bool XGUI_OperationMgr::canStartOperation(QString theId)
190 {
191   bool aCanStart = true;
192   ModuleBase_Operation* aCurrentOp = currentOperation();
193   if (aCurrentOp) {
194     if (!aCurrentOp->isGranted(theId)) {
195       if (canStopOperation()) {
196         if (myIsApplyEnabled)
197           aCurrentOp->commit();
198         else
199           aCurrentOp->abort();
200       } else {
201         aCanStart = false;
202       }
203     }
204   }
205   return aCanStart;
206 }
207
208
209 void XGUI_OperationMgr::onCommitOperation()
210 {
211   ModuleBase_Operation* anOperation = currentOperation();
212   if (anOperation)
213     anOperation->commit();
214 }
215
216 void XGUI_OperationMgr::onAbortOperation()
217 {
218   if (hasOperation() && canStopOperation()) {
219     currentOperation()->abort();
220   }
221 }
222
223 void XGUI_OperationMgr::onOperationStarted()
224 {
225   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
226   emit operationStarted(aSenderOperation);
227 }
228
229 void XGUI_OperationMgr::onOperationAborted()
230 {
231   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
232   emit operationAborted(aSenderOperation);
233 }
234
235 void XGUI_OperationMgr::onOperationCommitted()
236 {
237   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
238   emit operationCommitted(aSenderOperation);
239 }
240
241 void XGUI_OperationMgr::onOperationResumed()
242 {
243   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
244   emit operationResumed(aSenderOperation);
245 }
246
247 void XGUI_OperationMgr::onOperationStopped()
248 {
249   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
250   ModuleBase_Operation* anOperation = currentOperation();
251   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
252     return;
253
254   myOperations.removeAll(anOperation);
255   anOperation->deleteLater();
256
257   emit operationStopped(anOperation);
258
259   // get last operation which can be resumed
260   ModuleBase_Operation* aResultOp = 0;
261   QListIterator<ModuleBase_Operation*> anIt(myOperations);
262   anIt.toBack();
263   while (anIt.hasPrevious()) {
264     ModuleBase_Operation* anOp = anIt.previous();
265     if (anOp) {
266       aResultOp = anOp;
267       break;
268     }
269   }
270   if (aResultOp) {
271     resumeOperation(aResultOp);
272     onValidateOperation();
273   }
274 }
275
276 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
277 {
278   // Let the manager decide what to do with the given key combination.
279   ModuleBase_Operation* anOperation = currentOperation();
280   bool isAccepted = true;
281   switch (theEvent->key()) {
282     case Qt::Key_Return:
283     case Qt::Key_Enter: {
284       emit keyEnterReleased();
285       commitOperation();
286     }
287       break;
288     default:
289       isAccepted = false;
290       break;
291   }
292   //if(anOperation) {
293   //  anOperation->keyReleased(theEvent->key());
294   //}
295   return isAccepted;
296 }
297