]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Sources formated according to the codeing standards
[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 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
42 {
43   if (!canStartOperation(theOperation))
44     return false;
45
46   myOperations.append(theOperation);
47
48   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
49   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
50   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
51   connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
52           SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
53
54   theOperation->start();
55   validateCurrentOperation();
56   return true;
57 }
58
59 bool XGUI_OperationMgr::abortOperation()
60 {
61   ModuleBase_Operation* aCurrentOp = currentOperation();
62   if (!aCurrentOp || !canStopOperation())
63     return false;
64
65   aCurrentOp->abort();
66   return true;
67 }
68
69 QStringList XGUI_OperationMgr::operationList()
70 {
71   QStringList result;
72   foreach(ModuleBase_Operation* eachOperation, myOperations)
73   {
74     result << eachOperation->id();
75   }
76   return result;
77 }
78
79 void XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
80 {
81   //Get operation Id and feature to validate
82   QString anOperationId = theOperation->id();
83   FeaturePtr aFeature = theOperation->feature();
84   //Get validators for the Id
85   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
86   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
87   std::list<ModelAPI_Validator*> aValidators;
88   aFactory->validators(anOperationId.toStdString(), aValidators);
89   //
90   std::list<ModelAPI_Validator*>::iterator it = aValidators.begin();
91   bool isValid = true;
92   for (; it != aValidators.end(); it++) {
93     const ModelAPI_FeatureValidator* aFeatureValidator =
94         dynamic_cast<const ModelAPI_FeatureValidator*>(*it);
95     if (!aFeatureValidator)
96       continue;
97     if (!aFeatureValidator->isValid(aFeature)) {
98       isValid = false;
99       break;
100     }
101   }
102   emit operationValidated(isValid);
103 }
104
105 void XGUI_OperationMgr::validateCurrentOperation()
106 {
107   if (!hasOperation())
108     return;
109   ModuleBase_Operation* anOperation = currentOperation();
110   validateOperation(currentOperation());
111 }
112
113 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
114 {
115   if (theEvent->type() == QEvent::KeyRelease) {
116     QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
117     if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
118       // TODO: this is Escape button processing when the property panel has empty content,
119       // but the operation should be stopped by the Enter has been clicked
120       onKeyReleased("", aKeyEvent);
121       return true;
122     }
123   }
124   return QObject::eventFilter(theObject, theEvent);
125 }
126
127 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
128 {
129   theOperation->resume();
130 }
131
132 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
133 {
134   bool aCanStart = true;
135   ModuleBase_Operation* aCurrentOp = currentOperation();
136   if (aCurrentOp && !theOperation->isGranted(aCurrentOp)) {
137     if (canStopOperation()) {
138       aCurrentOp->abort();
139     } else {
140       aCanStart = false;
141     }
142   }
143   return aCanStart;
144 }
145
146 bool XGUI_OperationMgr::canStopOperation()
147 {
148   ModuleBase_Operation* anOperation = currentOperation();
149   if (anOperation) {
150     if (anOperation->isModified()) {
151       int anAnswer = QMessageBox::question(
152           qApp->activeWindow(), tr("Operation launch"),
153           tr("Previous operation is not finished and will be aborted"), QMessageBox::Ok,
154           QMessageBox::Cancel);
155       return anAnswer == QMessageBox::Ok;
156     }
157   }
158   return true;
159 }
160
161 void XGUI_OperationMgr::onCommitOperation()
162 {
163   ModuleBase_Operation* anOperation = currentOperation();
164   if (anOperation)
165     anOperation->commit();
166 }
167
168 void XGUI_OperationMgr::onAbortOperation()
169 {
170   ModuleBase_Operation* anOperation = currentOperation();
171   if (anOperation && canAbortOperation())
172     anOperation->abort();
173 }
174
175 bool XGUI_OperationMgr::canAbortOperation()
176 {
177   ModuleBase_Operation* anOperation = currentOperation();
178   if (anOperation && anOperation->isModified()) {
179     int anAnswer = QMessageBox::question(
180         qApp->activeWindow(), tr("Cancel operation"),
181         tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
182         QMessageBox::No);
183     return anAnswer == QMessageBox::Yes;
184   }
185   return true;
186 }
187
188 void XGUI_OperationMgr::onOperationStopped()
189 {
190   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
191   ModuleBase_Operation* anOperation = currentOperation();
192   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
193     return;
194
195   myOperations.removeAll(anOperation);
196   anOperation->deleteLater();
197
198   emit operationStopped(anOperation);
199
200   // get last operation which can be resumed
201   ModuleBase_Operation* aResultOp = 0;
202   QListIterator<ModuleBase_Operation*> anIt(myOperations);
203   anIt.toBack();
204   while (anIt.hasPrevious()) {
205     ModuleBase_Operation* anOp = anIt.previous();
206     if (anOp) {
207       aResultOp = anOp;
208       break;
209     }
210   }
211   if (aResultOp) {
212     resumeOperation(aResultOp);
213     validateCurrentOperation();
214   }
215 }
216
217 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
218 {
219   ModuleBase_Operation* anOperation = currentOperation();
220   if (anOperation)
221     anOperation->keyReleased(theName, theEvent);
222 }
223
224 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
225 {
226   ModuleBase_Operation* anOperation = currentOperation();
227   if (anOperation)
228     anOperation->onWidgetActivated(theWidget);
229 }