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