]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_OperationFeature.cpp
Salome HOME
Merge branch 'BR_ADVANCED_CURRENT_FEATURE_MANAGEMENT' of salome:modules/shaper into...
[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_ModelWidget* aFilledWgt = 0;
279   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
280   if (aPropertyPanel) {
281     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
282     if (!aWidgets.empty()) {
283       ModuleBase_ModelWidget* aWgt = 0;
284       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
285       bool isSet = false;
286       // 1. apply the selection to controls
287       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
288         aWgt = (*aWIt);
289         if (!aWgt->canSetValue())
290           continue;
291         aPropertyPanel->setPreselectionWidget(aWgt);
292         if (!aWgt->setSelection(myPreSelection, true)) {
293           isSet = false;
294           break;
295         } else {
296           isSet = true;
297           aFilledWgt = aWgt;
298         }
299       }
300       aPropertyPanel->setPreselectionWidget(NULL);
301       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
302       // it is better to perform it not in setSelection of each widget, but do it here,
303       // after the preselection is processed
304       ModuleBase_ModelWidget::updateObject(myFeature);
305
306       // 3. a signal should be emitted before the next widget activation
307       // because, the activation of the next widget will give a focus to the widget. As a result
308       // the value of the widget is initialized. And commit may happens until the value is entered.
309       if (aFilledWgt)
310         emit activatedByPreselection();
311     }
312     // 4. activate the next obligatory widget
313     aPropertyPanel->activateNextWidget(aFilledWgt);
314   }
315
316   clearPreselection();
317 }
318
319 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
320 {
321   myParentFeature = theParent;
322 }
323
324 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
325 {
326   return myParentFeature;
327 }
328
329 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
330 {
331   myPreviousCurrentFeature = theFeature;
332 }
333
334 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
335 {
336   return myPreviousCurrentFeature;
337 }
338
339 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
340                                          ModuleBase_IViewer* theViewer)
341 {
342   clearPreselection();
343
344   QList<ModuleBase_ViewerPrs> aPreSelected;
345   // Check that the selected result are not results of operation feature
346   FeaturePtr aFeature = feature();
347   if (aFeature) {
348     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
349
350     std::list<ResultPtr> aResults = aFeature->results();
351     QObjectPtrList aResList;
352     std::list<ResultPtr>::const_iterator aIt;
353     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
354       aResList.append(*aIt);
355
356     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
357       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
358         aPreSelected.append(aPrs);
359     }
360   } else
361     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
362
363   myPreSelection = aPreSelected;
364 }
365
366 void ModuleBase_OperationFeature::clearPreselection()
367 {
368   myPreSelection.clear();
369 }
370
371 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
372 {
373   ModuleBase_Operation::setPropertyPanel(theProp);
374
375   theProp->setEditingMode(isEditOperation());
376
377   if (theProp) {
378     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
379     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
380     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
381       ModuleBase_ModelWidget* aWgt = (*aWIt);
382       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
383     }
384   }
385
386   // Do not activate widgets by default if the current operation is editing operation
387   // Because we don't know which widget is going to be edited. 
388   if (!isEditOperation()) {
389     // 4. activate the first obligatory widget
390     theProp->activateNextWidget(NULL);
391   }
392   else {
393     // set focus on Ok button in order to operation manager could process Enter press
394     if (theProp)
395       theProp->setFocusOnOkButton();
396   }
397 }