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