Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr.
[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 #include "ModuleBase_Tools.h"
19
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31
32 #include <GeomAPI_Pnt2d.h>
33
34 #include <Events_Loop.h>
35
36 #include <QTimer>
37
38 // the define to check the activated object as a sub-feature by argument of
39 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
40 #define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
41 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
42 #include <ModelAPI_AttributeRefList.h>
43 #endif
44
45 #ifdef _DEBUG
46 #include <QDebug>
47 #endif
48
49 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
50 : ModuleBase_Operation(theId, theParent),
51   myIsEditing(false)
52 {
53 }
54
55 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
56 {
57   clearPreselection();
58 }
59
60 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
61                                                    /*const bool theRestartTransaction*/)
62 {
63   bool isCurrentEditState = isEditOperation();
64   if (isCurrentEditState == isEditState)
65     return;
66
67   /*
68   // this case is obsolete as it was not approved for reentrant sketch operation
69   // it was implemented when isEditState did not exist and only edit operation can be set
70   if (theRestartTransaction) {
71     // finsh previous create operation
72     emit beforeCommitted();
73     SessionPtr aMgr = ModelAPI_Session::get();
74     ModelAPI_Session::get()->finishOperation();
75
76     // start new edit operation
77     myIsEditing = true;
78     QString anId = getDescription()->operationId();
79     if (myIsEditing) {
80       anId = anId.append(EditSuffix());
81     }
82     ModelAPI_Session::get()->startOperation(anId.toStdString());
83     emit beforeStarted();
84   } else*/
85   myIsEditing = isEditState;
86
87   propertyPanel()->setEditingMode(isEditOperation());
88 }
89
90 FeaturePtr ModuleBase_OperationFeature::feature() const
91 {
92   return myFeature;
93 }
94
95 bool ModuleBase_OperationFeature::isValid() const
96 {
97   if (!myFeature || !myFeature->data()->isValid())
98     return true; // rename operation
99   if (myFeature->isAction())
100     return true;
101
102   std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
103   return anError.empty();
104 }
105
106 void ModuleBase_OperationFeature::startOperation()
107 {
108   FeaturePtr aFeature = feature();
109   if (!aFeature.get() || !isEditOperation())
110     return;
111
112   if (aFeature.get() && isEditOperation())
113     aFeature->setStable(false);
114
115   myVisualizedObjects.clear();
116   // store hidden result features
117   std::list<ResultPtr> aResults = aFeature->results();
118   std::list<ResultPtr>::const_iterator aIt;
119   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
120     ObjectPtr anObject = *aIt;
121     if (anObject.get() && !anObject->isDisplayed()) {
122       myVisualizedObjects.insert(*aIt);
123       anObject->setDisplayed(true);
124     }
125   }
126   if (!aFeature->isDisplayed()) {
127     myVisualizedObjects.insert(aFeature);
128     aFeature->setDisplayed(true);
129   }
130   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
131 }
132
133 void ModuleBase_OperationFeature::stopOperation()
134 {
135   FeaturePtr aFeature = feature();
136   if (!aFeature.get() || !isEditOperation())
137     return;
138
139   // store hidden result features
140   std::list<ResultPtr> aResults = aFeature->results();
141   std::list<ResultPtr>::const_iterator aIt;
142   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
143     ObjectPtr anObject = *aIt;
144     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
145       anObject->setDisplayed(false);
146     }
147   }
148   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
149     aFeature->setDisplayed(false);
150   }
151   if (myVisualizedObjects.size() > 0)
152     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
153 }
154
155 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
156 {
157   if (myParentFeature.get()) {
158     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
159   } else {
160     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
161     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
162   }
163   if (myFeature) {  // TODO: generate an error if feature was not created
164     setIsModified(true);
165     // Model update should call "execute" of a feature.
166     //myFeature->execute();
167     // Init default values
168     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
169      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
170      for (; anIt != aLast; anIt++) {
171      (*anIt)->storeValue(aFeature);
172      }*/
173   }
174
175   if (theFlushMessage)
176     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
177   return myFeature;
178 }
179
180 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
181 {
182   myFeature = theFeature;
183   myIsEditing = true;
184 }
185
186 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
187 {
188   FeaturePtr aFeature = feature();
189   if (aFeature) {
190     if (aFeature == theObj)
191       return true;
192     std::list<ResultPtr> aResults = aFeature->results();
193     std::list<ResultPtr>::const_iterator aIt;
194     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
195       if (theObj == (*aIt))
196         return true;
197     }
198 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
199     if (aFeature->isMacro()) {
200       // macro feature may refers to sub-features, which also should be deactivated when the operation
201       // is active, e.g. rectangle'lines.
202       FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
203       std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
204                                               ModelAPI_AttributeRefList::typeId());
205       std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
206       bool aFoundObject = false;
207       for (; anIt != aLast && !aFoundObject; anIt++) {
208         std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
209                                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
210         for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
211           ObjectPtr anObject = aCurSelList->object(i);
212           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
213           if (aFeature.get()) {
214             aFoundObject = anObjectFeature == aFeature;
215           }
216         }
217       }
218       return aFoundObject;
219     }
220 #endif
221   }
222   return false;
223 }
224
225 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
226 {
227   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
228 }
229
230 bool ModuleBase_OperationFeature::start()
231 {
232   setIsModified(false);
233   QString anId = getDescription()->operationId();
234   if (myIsEditing) {
235     anId = anId.append(EditSuffix());
236   }
237   ModelAPI_Session::get()->startOperation(anId.toStdString());
238
239   emit beforeStarted();
240   startOperation();
241
242   if (!myIsEditing) {
243     FeaturePtr aFeature = createFeature();
244     // if the feature is not created, there is no sense to start the operation
245     if (aFeature.get() == NULL) {
246       // it is necessary to abor the operation in the session and emit the aborted signal
247       // in order to update commands status in the workshop, to be exact the feature action
248       // to be unchecked
249       abort();
250       return false;
251     }
252   }
253   //Already called startOperation();
254   emit started();
255   return true;
256 }
257
258 void ModuleBase_OperationFeature::abort()
259 {
260   emit beforeAborted();
261
262   // the viewer update should be blocked in order to avoid the features blinking before they are
263   // hidden
264   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
265       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
266   Events_Loop::loop()->send(aMsg);
267
268   // the widgets of property panel should not process any events come from data mode
269   // after abort clicked. Some signal such as redisplay/create influence on content
270   // of the object browser and viewer context. Therefore it influence to the current
271   // selection and if the active widget listens it, the attribute value is errnoneous
272   // changed.
273   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
274   if (aPropertyPanel)
275     aPropertyPanel->cleanContent();
276
277   if (myFeature.get())
278     myFeature->setStable(true);
279
280   abortOperation();
281   stopOperation();
282
283   SessionPtr aMgr = ModelAPI_Session::get();
284   aMgr->abortOperation();
285   emit stopped();
286   // the viewer update should be unblocked in order to avoid the features blinking before they are
287   // hidden
288   aMsg = std::shared_ptr<Events_Message>(
289                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
290
291   Events_Loop::loop()->send(aMsg);
292
293   emit aborted();
294 }
295
296 bool ModuleBase_OperationFeature::commit()
297 {
298   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
299   if (aPanel) {
300     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
301     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
302       anActiveWidget->storeValue();
303     }
304   }
305   if (canBeCommitted()) {
306     emit beforeCommitted();
307     // the widgets of property panel should not process any events come from data mode
308     // after commit clicked. Some signal such as redisplay/create influence on content
309     // of the object browser and viewer context. Therefore it influence to the current
310     // selection and if the active widget listens it, the attribute value is errnoneous
311     // changed.
312     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
313     if (aPropertyPanel)
314       aPropertyPanel->cleanContent();
315
316     myFeature->setStable(true);
317
318     SessionPtr aMgr = ModelAPI_Session::get();
319     /// Set current feature and remeber old current feature
320
321     commitOperation();
322     aMgr->finishOperation();
323
324     stopOperation();
325     emit stopped();
326     emit committed();
327
328     afterCommitOperation();
329     return true;
330   }
331   return false;
332 }
333
334 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
335                                               const std::string& theGreedAttributeId)
336 {
337   ModuleBase_ModelWidget* aWidget = 0;
338   if (myPreSelection.empty())
339     return aWidget;
340   // equal vertices should not be used here
341   ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
342
343   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
344   ModuleBase_ModelWidget* aFilledWgt = 0;
345   if (aPropertyPanel) {
346     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
347     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
348     ModuleBase_ModelWidget* aWgt = 0;
349     if (!aWidgets.empty()) {
350       if (!theGreedAttributeId.empty()) {
351         // set preselection to greed widget
352         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
353           aWgt = (*aWIt);
354           if (aWgt->attributeID() == theGreedAttributeId) {
355             aPropertyPanel->setPreselectionWidget(aWgt);
356             aWgt->setSelection(myPreSelection, true);
357             aPropertyPanel->setPreselectionWidget(NULL);
358             aFilledWgt = aWgt;
359             break;
360           }
361         }
362       }
363       else {
364         bool isSet = false;
365         // 1. apply the selection to controls
366         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
367           aWgt = (*aWIt);
368           if (!aWgt->canAcceptFocus())
369             continue;
370           aPropertyPanel->setPreselectionWidget(aWgt);
371           if (!aWgt->setSelection(myPreSelection, true)) {
372             isSet = false;
373             break;
374           } else {
375             isSet = true;
376             aFilledWgt = aWgt;
377           }
378         }
379       }
380       aPropertyPanel->setPreselectionWidget(NULL);
381       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
382       // it is better to perform it not in setSelection of each widget, but do it here,
383       // after the preselection is processed
384       ModuleBase_ModelWidget::updateObject(myFeature);
385     }
386   }
387   clearPreselection();
388
389   return aFilledWgt;
390 }
391
392 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
393 {
394   myParentFeature = theParent;
395 }
396
397 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
398 {
399   return myParentFeature;
400 }
401
402 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
403 {
404   myPreviousCurrentFeature = theFeature;
405 }
406
407 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
408 {
409   return myPreviousCurrentFeature;
410 }
411
412 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
413                                          ModuleBase_IViewer* theViewer)
414 {
415   QList<ModuleBase_ViewerPrsPtr> aPreSelected;
416   // Check that the selected result are not results of operation feature
417   FeaturePtr aFeature = feature();
418   if (aFeature) {
419     QList<ModuleBase_ViewerPrsPtr> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
420
421     std::list<ResultPtr> aResults = aFeature->results();
422     QObjectPtrList aResList;
423     std::list<ResultPtr>::const_iterator aIt;
424     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
425       aResList.append(*aIt);
426
427     foreach (ModuleBase_ViewerPrsPtr aPrs, aSelected) {
428       if ((!aResList.contains(aPrs->object())) && (aPrs->object() != aFeature))
429         aPreSelected.append(aPrs);
430     }
431   } else
432     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
433
434   setPreselection(aPreSelected);
435 }
436
437 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
438 {
439   clearPreselection();
440   myPreSelection = theValues;
441 }
442
443 void ModuleBase_OperationFeature::clearPreselection()
444 {
445   myPreSelection.clear();
446 }
447
448 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
449 {
450   ModuleBase_Operation::setPropertyPanel(theProp);
451
452   theProp->setEditingMode(isEditOperation());
453
454   if (theProp) {
455     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
456     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
457     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
458       ModuleBase_ModelWidget* aWgt = (*aWIt);
459       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
460     }
461   }
462
463   // Do not activate widgets by default if the current operation is editing operation
464   // Because we don't know which widget is going to be edited. 
465   if (!isEditOperation()) {
466     // 4. activate the first obligatory widget
467     theProp->activateNextWidget(NULL);
468   }
469   else {
470     // set focus on Ok button in order to operation manager could process Enter press
471     if (theProp)
472       theProp->setFocusOnOkButton();
473   }
474 }