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