]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Issue #147 Abort'n'start of sketch operations on click
[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 #include <ModelAPI_Validator.h>
9 #include <ModelAPI_FeatureValidator.h>
10
11 #include <QMessageBox>
12 #include <QApplication>
13 #include <QKeyEvent>
14
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16     : QObject(theParent)
17 {
18   // listen to Escape signal to stop the current operation
19   qApp->installEventFilter(this);
20 }
21
22 XGUI_OperationMgr::~XGUI_OperationMgr()
23 {
24 }
25
26 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
27 {
28   return myOperations.count() > 0 ? myOperations.last() : 0;
29 }
30
31 bool XGUI_OperationMgr::hasOperation() const
32 {
33   return (myOperations.count() > 0) && (myOperations.last() != NULL);
34 }
35
36 int XGUI_OperationMgr::operationsCount() const
37 {
38   return myOperations.count();
39 }
40
41 QStringList XGUI_OperationMgr::operationList() const
42 {
43   QStringList result;
44   foreach(ModuleBase_Operation* eachOperation, myOperations) {
45     FeaturePtr aFeature = eachOperation->feature();
46     if(aFeature) {
47       result << QString::fromStdString(aFeature->getKind());
48     }
49   }
50   return result;
51 }
52
53 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
54 {
55   int idx = myOperations.lastIndexOf(theOperation);
56   if(idx == -1 || idx == 0) {
57     return NULL;
58   }
59   return myOperations.at(idx - 1);
60 }
61
62 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
63 {
64   if (theEvent->type() == QEvent::KeyRelease) {
65     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
66     if(aKeyEvent) {
67       onKeyReleased(aKeyEvent);
68       return true;
69     }
70   }
71   return QObject::eventFilter(theObject, theEvent);
72 }
73
74 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
75 {
76   if (!canStartOperation(theOperation))
77     return false;
78
79   myOperations.append(theOperation);
80
81   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
82   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
83   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
84   connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
85           SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
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 bool XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
114 {
115   //Get operation feature to validate
116   FeaturePtr aFeature = theOperation->feature();
117   //Get validators for the Id
118   SessionPtr aMgr = ModelAPI_Session::get();
119   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
120
121   bool isValid = aFactory->validate(aFeature);
122   emit operationValidated(isValid);
123   return isValid;
124 }
125
126 void XGUI_OperationMgr::onValidateOperation()
127 {
128   if (!hasOperation())
129     return;
130   ModuleBase_Operation* anOperation = currentOperation();
131   validateOperation(currentOperation());
132 }
133
134 bool XGUI_OperationMgr::commitOperation()
135 {
136   if (validateOperation(currentOperation())) {
137     onCommitOperation();
138     return true;
139   }
140   return false;
141 }
142
143 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
144 {
145   theOperation->resume();
146 }
147
148 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
149 {
150   bool aCanStart = true;
151   ModuleBase_Operation* aCurrentOp = currentOperation();
152   if (aCurrentOp) {
153     if (!theOperation->isGranted()) {
154       if (!aCurrentOp->isValid(theOperation)) {
155         if (canAbortOperation()) {
156           aCurrentOp->abort();
157         } else {
158           aCanStart = false;
159         }
160       }
161     }
162   }
163   return aCanStart;
164 }
165
166
167 void XGUI_OperationMgr::onCommitOperation()
168 {
169   ModuleBase_Operation* anOperation = currentOperation();
170   if (anOperation)
171     anOperation->commit();
172 }
173
174 void XGUI_OperationMgr::onAbortOperation()
175 {
176   if (hasOperation() && canAbortOperation()) {
177     currentOperation()->abort();
178   }
179 }
180
181 bool XGUI_OperationMgr::canAbortOperation()
182 {
183   ModuleBase_Operation* anOperation = currentOperation();
184   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
185     return true;
186   if (anOperation && anOperation->isModified()) {
187     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
188     int anAnswer = QMessageBox::question(qApp->activeWindow(),
189                                          tr("Abort operation"),
190                                          aMessage,
191                                          QMessageBox::Ok | QMessageBox::Cancel,
192                                          QMessageBox::Cancel);
193     return anAnswer == QMessageBox::Ok;
194   }
195   return true;
196 }
197
198 void XGUI_OperationMgr::onOperationStopped()
199 {
200   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
201   ModuleBase_Operation* anOperation = currentOperation();
202   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
203     return;
204
205   myOperations.removeAll(anOperation);
206   anOperation->deleteLater();
207
208   emit operationStopped(anOperation);
209
210   // get last operation which can be resumed
211   ModuleBase_Operation* aResultOp = 0;
212   QListIterator<ModuleBase_Operation*> anIt(myOperations);
213   anIt.toBack();
214   while (anIt.hasPrevious()) {
215     ModuleBase_Operation* anOp = anIt.previous();
216     if (anOp) {
217       aResultOp = anOp;
218       break;
219     }
220   }
221   if (aResultOp) {
222     resumeOperation(aResultOp);
223     onValidateOperation();
224   }
225 }
226
227 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
228 {
229   // Let the manager decide what to do with the given key combination.
230   ModuleBase_Operation* anOperation = currentOperation();
231   bool isRestart = false;
232   switch (theEvent->key()) {
233     case Qt::Key_Escape: {
234       onAbortOperation();
235     }
236       break;
237     case Qt::Key_Return:
238     case Qt::Key_Enter: {
239       if(anOperation) {
240          anOperation->activateNextToCurrentWidget();
241       }
242       commitOperation();
243     }
244       break;
245     default:
246       break;
247   }
248   if(anOperation)
249     anOperation->keyReleased(theEvent->key());
250 }
251
252 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
253 {
254   ModuleBase_Operation* anOperation = currentOperation();
255   if (anOperation)
256     anOperation->onWidgetActivated(theWidget);
257 }