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