Salome HOME
58260b79c6cd24bce86d4a53a138de985531c0a4
[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   if (!aFeature) return true; // rename operation
118   //Get validators for the Id
119   SessionPtr aMgr = ModelAPI_Session::get();
120   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
121
122   bool isValid = aFactory->validate(aFeature);
123   emit operationValidated(isValid);
124   return isValid;
125 }
126
127 void XGUI_OperationMgr::onValidateOperation()
128 {
129   if (!hasOperation())
130     return;
131   ModuleBase_Operation* anOperation = currentOperation();
132   validateOperation(currentOperation());
133 }
134
135 bool XGUI_OperationMgr::commitOperation()
136 {
137   if (validateOperation(currentOperation())) {
138     onCommitOperation();
139     return true;
140   }
141   return false;
142 }
143
144 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
145 {
146   theOperation->resume();
147 }
148
149 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
150 {
151   bool aCanStart = true;
152   ModuleBase_Operation* aCurrentOp = currentOperation();
153   if (aCurrentOp) {
154     if (!theOperation->isGranted()) {
155       if (!aCurrentOp->isValid(theOperation)) {
156         if (canAbortOperation()) {
157           aCurrentOp->abort();
158         } else {
159           aCanStart = false;
160         }
161       }
162     }
163   }
164   return aCanStart;
165 }
166
167
168 void XGUI_OperationMgr::onCommitOperation()
169 {
170   ModuleBase_Operation* anOperation = currentOperation();
171   if (anOperation)
172     anOperation->commit();
173 }
174
175 void XGUI_OperationMgr::onAbortOperation()
176 {
177   if (hasOperation() && canAbortOperation()) {
178     currentOperation()->abort();
179   }
180 }
181
182 bool XGUI_OperationMgr::canAbortOperation()
183 {
184   ModuleBase_Operation* anOperation = currentOperation();
185   if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
186     return true;
187   if (anOperation && anOperation->isModified()) {
188     QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
189     int anAnswer = QMessageBox::question(qApp->activeWindow(),
190                                          tr("Abort operation"),
191                                          aMessage,
192                                          QMessageBox::Ok | QMessageBox::Cancel,
193                                          QMessageBox::Cancel);
194     return anAnswer == QMessageBox::Ok;
195   }
196   return true;
197 }
198
199 void XGUI_OperationMgr::onOperationStopped()
200 {
201   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
202   ModuleBase_Operation* anOperation = currentOperation();
203   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
204     return;
205
206   myOperations.removeAll(anOperation);
207   anOperation->deleteLater();
208
209   emit operationStopped(anOperation);
210
211   // get last operation which can be resumed
212   ModuleBase_Operation* aResultOp = 0;
213   QListIterator<ModuleBase_Operation*> anIt(myOperations);
214   anIt.toBack();
215   while (anIt.hasPrevious()) {
216     ModuleBase_Operation* anOp = anIt.previous();
217     if (anOp) {
218       aResultOp = anOp;
219       break;
220     }
221   }
222   if (aResultOp) {
223     resumeOperation(aResultOp);
224     onValidateOperation();
225   }
226 }
227
228 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
229 {
230   // Let the manager decide what to do with the given key combination.
231   ModuleBase_Operation* anOperation = currentOperation();
232   bool isRestart = false;
233   switch (theEvent->key()) {
234     case Qt::Key_Escape: {
235       onAbortOperation();
236     }
237       break;
238     case Qt::Key_Return:
239     case Qt::Key_Enter: {
240       if(anOperation) {
241          anOperation->activateNextToCurrentWidget();
242       }
243       commitOperation();
244     }
245       break;
246     default:
247       break;
248   }
249   if(anOperation)
250     anOperation->keyReleased(theEvent->key());
251 }
252
253 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
254 {
255   ModuleBase_Operation* anOperation = currentOperation();
256   if (anOperation)
257     anOperation->onWidgetActivated(theWidget);
258 }