Salome HOME
Abort operation correction, Cases are: 1. Deselect feature button in ToolBar, 2....
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_Operation.cpp
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #include "ModuleBase_Operation.h"
11
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_ModelWidget.h"
14 #include "ModuleBase_ViewerPrs.h"
15 #include "ModuleBase_IPropertyPanel.h"
16 #include "ModuleBase_ISelection.h"
17 #include "ModuleBase_IViewer.h"
18
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Pnt2d.h>
31
32 #include <Events_Loop.h>
33
34 #include <QTimer>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
41     : QObject(theParent),
42       myIsEditing(false),
43       myIsModified(false),
44       myPropertyPanel(NULL)
45 {
46   myDescription = new ModuleBase_OperationDescription(theId);
47 }
48
49 ModuleBase_Operation::~ModuleBase_Operation()
50 {
51   delete myDescription;
52   clearPreselection();
53 }
54
55 QString ModuleBase_Operation::id() const
56 {
57   return getDescription()->operationId();
58 }
59
60 FeaturePtr ModuleBase_Operation::feature() const
61 {
62   return myFeature;
63 }
64
65 bool ModuleBase_Operation::isValid() const
66 {
67   if (!myFeature || !myFeature->data()->isValid())
68     return true; // rename operation
69   if (myFeature->isAction())
70     return true;
71   //Get validators for the Id
72   SessionPtr aMgr = ModelAPI_Session::get();
73   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74   bool aValid = aFactory->validate(myFeature);
75
76   // the feature exec state should be checked in order to do not apply features, which result can not
77   // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
78   bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
79                || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
80
81   return aValid && isDone;
82 }
83
84
85 bool ModuleBase_Operation::canBeCommitted() const
86 {
87   return isValid();
88 }
89
90 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
91 {
92   if (myParentFeature.get()) {
93     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
94   } else {
95     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
96     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
97   }
98   if (myFeature) {  // TODO: generate an error if feature was not created
99     myIsModified = true;
100     // Model update should call "execute" of a feature.
101     //myFeature->execute();
102     // Init default values
103     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
104      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
105      for (; anIt != aLast; anIt++) {
106      (*anIt)->storeValue(aFeature);
107      }*/
108   }
109
110   if (theFlushMessage)
111     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
112   return myFeature;
113 }
114
115 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
116 {
117   myFeature = theFeature;
118   myIsEditing = true;
119 }
120
121 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
122 {
123   FeaturePtr aFeature = feature();
124   if (aFeature) {
125     if (aFeature == theObj)
126       return true;
127     std::list<ResultPtr> aResults = aFeature->results();
128     std::list<ResultPtr>::const_iterator aIt;
129     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
130       if (theObj == (*aIt))
131         return true;
132     }
133   }
134   return false;
135 }
136
137 void ModuleBase_Operation::start()
138 {
139   myIsModified = false;
140   QString anId = getDescription()->operationId();
141   if (myIsEditing) {
142     anId = anId.append(EditSuffix());
143   }
144   ModelAPI_Session::get()->startOperation(anId.toStdString());
145
146   if (!myIsEditing) {
147     FeaturePtr aFeature = createFeature();
148     // if the feature is not created, there is no sense to start the operation
149     if (aFeature.get() == NULL) {
150       // it is necessary to abor the operation in the session and emit the aborted signal
151       // in order to update commands status in the workshop, to be exact the feature action
152       // to be unchecked
153       abort();
154       return;
155     }
156   }
157   /// Set current feature and remeber old current feature
158   if (myIsEditing) {
159     SessionPtr aMgr = ModelAPI_Session::get();
160     DocumentPtr aDoc = aMgr->activeDocument();
161     myCurrentFeature = aDoc->currentFeature(true);
162     aDoc->setCurrentFeature(feature(), false);
163   }
164
165   startOperation();
166   emit started();
167
168 }
169
170 void ModuleBase_Operation::postpone()
171 {
172   postponeOperation();
173   emit postponed();
174 }
175
176 void ModuleBase_Operation::resume()
177 {
178   resumeOperation();
179   emit resumed();
180 }
181
182 void ModuleBase_Operation::abort()
183 {
184   // the viewer update should be blocked in order to avoid the features blinking before they are
185   // hidden
186   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
187       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
188   Events_Loop::loop()->send(aMsg);
189
190   // the widgets of property panel should not process any events come from data mode
191   // after abort clicked. Some signal such as redisplay/create influence on content
192   // of the object browser and viewer context. Therefore it influence to the current
193   // selection and if the active widget listens it, the attribute value is errnoneous
194   // changed.
195   if (myPropertyPanel)
196     myPropertyPanel->cleanContent();
197
198   SessionPtr aMgr = ModelAPI_Session::get();
199   if (myIsEditing) {
200     DocumentPtr aDoc = aMgr->activeDocument();
201     bool aIsOp = aMgr->isOperation();
202     if (!aIsOp)
203       aMgr->startOperation();
204     aDoc->setCurrentFeature(myCurrentFeature, true);
205     if (!aIsOp)
206       aMgr->finishOperation();
207     myCurrentFeature = FeaturePtr();
208   }
209   abortOperation();
210
211   stopOperation();
212   // is is necessary to deactivate current widgets before the model operation is aborted
213   // because abort removes the feature and activated filters should not check it
214   propertyPanel()->cleanContent();
215
216   aMgr->abortOperation();
217   emit stopped();
218   // the viewer update should be unblocked in order to avoid the features blinking before they are
219   // hidden
220   aMsg = std::shared_ptr<Events_Message>(
221                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
222
223   Events_Loop::loop()->send(aMsg);
224
225   emit aborted();
226 }
227
228 bool ModuleBase_Operation::commit()
229 {
230   if (canBeCommitted()) {
231     // the widgets of property panel should not process any events come from data mode
232     // after commit clicked. Some signal such as redisplay/create influence on content
233     // of the object browser and viewer context. Therefore it influence to the current
234     // selection and if the active widget listens it, the attribute value is errnoneous
235     // changed.
236     if (myPropertyPanel)
237       myPropertyPanel->cleanContent();
238
239     SessionPtr aMgr = ModelAPI_Session::get();
240     /// Set current feature and remeber old current feature
241     if (myIsEditing) {
242       DocumentPtr aDoc = aMgr->activeDocument();
243       bool aIsOp = aMgr->isOperation();
244       if (!aIsOp)
245         aMgr->startOperation();
246       aDoc->setCurrentFeature(myCurrentFeature, true);
247       if (!aIsOp)
248         aMgr->finishOperation();
249       myCurrentFeature = FeaturePtr();
250     }
251     commitOperation();
252     aMgr->finishOperation();
253
254     stopOperation();
255     emit stopped();
256     emit committed();
257
258     afterCommitOperation();
259     return true;
260   }
261   return false;
262 }
263
264 void ModuleBase_Operation::onValuesChanged()
265 {
266   myIsModified = true;
267 }
268
269 void ModuleBase_Operation::commitOperation()
270 {
271   if(!myPropertyPanel) {
272     return;
273   }
274 }
275
276 void ModuleBase_Operation::activateByPreselection()
277 {
278   if (myPreSelection.empty())
279     return;
280
281   ModuleBase_ModelWidget* aFilledWgt = 0;
282   if (myPropertyPanel) {
283     const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
284     if (!aWidgets.empty()) {
285       ModuleBase_ModelWidget* aWgt = 0;
286       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
287       bool isSet = false;
288       // 1. apply the selection to controls
289       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
290         aWgt = (*aWIt);
291         if (!aWgt->canSetValue())
292           continue;
293         myPropertyPanel->setPreselectionWidget(aWgt);
294         if (!aWgt->setSelection(myPreSelection, true)) {
295           isSet = false;
296           break;
297         } else {
298           isSet = true;
299           aFilledWgt = aWgt;
300         }
301       }
302       myPropertyPanel->setPreselectionWidget(NULL);
303       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
304       // it is better to perform it not in setSelection of each widget, but do it here,
305       // after the preselection is processed
306       ModuleBase_ModelWidget::updateObject(myFeature);
307
308       // 3. a signal should be emitted before the next widget activation
309       // because, the activation of the next widget will give a focus to the widget. As a result
310       // the value of the widget is initialized. And commit may happens until the value is entered.
311       if (aFilledWgt)
312         emit activatedByPreselection();
313     }
314     // 4. activate the next obligatory widget
315     myPropertyPanel->activateNextWidget(aFilledWgt);
316   }
317
318   clearPreselection();
319 }
320
321 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
322 {
323   myParentFeature = theParent;
324 }
325
326 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
327 {
328   return myParentFeature;
329 }
330
331 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
332                                          ModuleBase_IViewer* theViewer)
333 {
334   clearPreselection();
335
336   QList<ModuleBase_ViewerPrs> aPreSelected;
337   // Check that the selected result are not results of operation feature
338   FeaturePtr aFeature = feature();
339   if (aFeature) {
340     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
341
342     std::list<ResultPtr> aResults = aFeature->results();
343     QObjectPtrList aResList;
344     std::list<ResultPtr>::const_iterator aIt;
345     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
346       aResList.append(*aIt);
347
348     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
349       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
350         aPreSelected.append(aPrs);
351     }
352   } else
353     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
354
355   // convert the selection values to the values, which are set to the operation widgets
356
357   //Handle(V3d_View) aView = theViewer->activeView();
358   //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
359   //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
360   //  aValue->setObject(aPrs.object());
361
362   //  double aX, anY;
363   //  if (getViewerPoint(aPrs, theViewer, aX, anY))
364   //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
365   //  myPreSelection.append(aValue);
366   //}
367   myPreSelection = aPreSelected;
368 }
369
370
371 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
372                                                ModuleBase_IViewer* theViewer,
373                                                double& theX, double& theY)
374 {
375   return false;
376 }
377
378 void ModuleBase_Operation::clearPreselection()
379 {
380   myPreSelection.clear();
381 }
382
383 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
384
385   myPropertyPanel = theProp; 
386   myPropertyPanel->setEditingMode(isEditOperation());
387
388   if (myPropertyPanel) {
389     const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
390     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
391     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
392       ModuleBase_ModelWidget* aWgt = (*aWIt);
393       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
394     }
395   }
396
397   // Do not activate widgets by default if the current operation is editing operation
398   // Because we don't know which widget is going to be edited. 
399   if (!isEditOperation()) {
400     // 4. activate the first obligatory widget
401     myPropertyPanel->activateNextWidget(NULL);
402   }
403 }
404
405 bool ModuleBase_Operation::isGranted(QString theId) const
406 {
407   return myNestedFeatures.contains(theId);
408 }