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