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