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