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