]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[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 int XGUI_OperationMgr::operationsCount() const
40 {
41   return myOperations.count();
42 }
43
44 QStringList XGUI_OperationMgr::operationList() const
45 {
46   QStringList result;
47   foreach(ModuleBase_Operation* eachOperation, myOperations) {
48     FeaturePtr aFeature = eachOperation->feature();
49     if(aFeature) {
50       result << QString::fromStdString(aFeature->getKind());
51     }
52   }
53   return result;
54 }
55
56 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
57 {
58   int idx = myOperations.lastIndexOf(theOperation);
59   if(idx == -1 || idx == 0) {
60     return NULL;
61   }
62   return myOperations.at(idx - 1);
63 }
64
65 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
66 {
67   if (theEvent->type() == QEvent::KeyRelease) {
68     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
69     if(aKeyEvent) {
70       return onKeyReleased(aKeyEvent);
71     }
72   }
73   return QObject::eventFilter(theObject, theEvent);
74 }
75
76 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
77 {
78   if (!canStartOperation(theOperation))
79     return false;
80
81   myOperations.append(theOperation);
82
83   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
84   connect(theOperation, SIGNAL(started()), this, SLOT(onOperationStarted()));
85   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
86
87   theOperation->start();
88   onValidateOperation();
89   return true;
90 }
91
92 bool XGUI_OperationMgr::abortAllOperations()
93 {
94   if(!hasOperation()) {
95     return true;
96   } else if (operationsCount() == 1) {
97     onAbortOperation();
98     return true;
99   }
100   QString aMessage = tr("All active operations will be aborted.");
101   int anAnswer = QMessageBox::question(qApp->activeWindow(),
102                                        tr("Abort operation"),
103                                        aMessage,
104                                        QMessageBox::Ok | QMessageBox::Cancel,
105                                        QMessageBox::Cancel);
106   bool result = anAnswer == QMessageBox::Ok;
107   while(result && hasOperation()) {
108     currentOperation()->abort();
109   }
110   return result;
111 }
112
113 void XGUI_OperationMgr::onValidateOperation()
114 {
115   if (!hasOperation())
116     return;
117   ModuleBase_Operation* anOperation = currentOperation();
118   if(anOperation) {
119     bool isValid = anOperation->isValid();
120     emit operationValidated(isValid);
121   }
122 }
123
124 bool XGUI_OperationMgr::commitOperation()
125 {
126   if (hasOperation() && currentOperation()->isValid()) {
127     onCommitOperation();
128     return true;
129   }
130   return false;
131 }
132
133 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
134 {
135   theOperation->resume();
136 }
137
138 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
139 {
140   bool aCanStart = true;
141   ModuleBase_Operation* aCurrentOp = currentOperation();
142   if (aCurrentOp) {
143     if (!aCurrentOp->isGranted(theOperation)) {
144       if (canAbortOperation()) {
145         aCurrentOp->abort();
146       } else {
147         aCanStart = false;
148       }
149     }
150   }
151   return aCanStart;
152 }
153
154
155 void XGUI_OperationMgr::onCommitOperation()
156 {
157   ModuleBase_Operation* anOperation = currentOperation();
158   if (anOperation)
159     anOperation->commit();
160 }
161
162 void XGUI_OperationMgr::onAbortOperation()
163 {
164   if (hasOperation() && canAbortOperation()) {
165     currentOperation()->abort();
166   }
167 }
168
169 bool XGUI_OperationMgr::canAbortOperation()
170 {
171   ModuleBase_Operation* anOperation = currentOperation();
172   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
173     return true;
174   if (anOperation && anOperation->isModified()) {
175     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
176     int anAnswer = QMessageBox::question(qApp->activeWindow(),
177                                          tr("Abort operation"),
178                                          aMessage,
179                                          QMessageBox::Ok | QMessageBox::Cancel,
180                                          QMessageBox::Cancel);
181     return anAnswer == QMessageBox::Ok;
182   }
183   return true;
184 }
185
186 void XGUI_OperationMgr::onOperationStarted()
187 {
188   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
189   emit operationStarted(aSenderOperation);
190 }
191
192 void XGUI_OperationMgr::onOperationStopped()
193 {
194   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
195   ModuleBase_Operation* anOperation = currentOperation();
196   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
197     return;
198
199   myOperations.removeAll(anOperation);
200   anOperation->deleteLater();
201
202   emit operationStopped(anOperation);
203
204   // get last operation which can be resumed
205   ModuleBase_Operation* aResultOp = 0;
206   QListIterator<ModuleBase_Operation*> anIt(myOperations);
207   anIt.toBack();
208   while (anIt.hasPrevious()) {
209     ModuleBase_Operation* anOp = anIt.previous();
210     if (anOp) {
211       aResultOp = anOp;
212       break;
213     }
214   }
215   if (aResultOp) {
216     resumeOperation(aResultOp);
217     onValidateOperation();
218   }
219 }
220
221 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
222 {
223   // Let the manager decide what to do with the given key combination.
224   ModuleBase_Operation* anOperation = currentOperation();
225   bool isAccepted = true;
226   switch (theEvent->key()) {
227     case Qt::Key_Return:
228     case Qt::Key_Enter: {
229       commitOperation();
230     }
231       break;
232     default:
233       isAccepted = false;
234       break;
235   }
236   if(anOperation) {
237     anOperation->keyReleased(theEvent->key());
238   }
239   return isAccepted;
240 }
241