Salome HOME
Issue #672 - Crash when distance constraint
[modules/shaper.git] / src / PartSet / PartSet_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetMultiSelector.cpp
4 // Created:     15 Apr 2015
5 // Author:      Natalia Ermolaeva
6
7 #include "PartSet_WidgetMultiSelector.h"
8
9 #include <ModelAPI_AttributeRefAttr.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12
13 #include <ModuleBase_Definitions.h>
14 #include <ModuleBase_Tools.h>
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_ISelection.h>
17
18 #include <Config_WidgetAPI.h>
19
20 #include <PartSet_Tools.h>
21 #include <PartSet_ExternalObjectsMgr.h>
22 #include <SketchPlugin_Feature.h>
23
24 #include <SketchPlugin_ConstraintRigid.h>
25
26 #include <XGUI_Workshop.h>
27
28 #include <QComboBox>
29
30 PartSet_WidgetMultiSelector::PartSet_WidgetMultiSelector(QWidget* theParent,
31                                                          ModuleBase_IWorkshop* theWorkshop,
32                                                          const Config_WidgetAPI* theData,
33                                                          const std::string& theParentId)
34 : ModuleBase_WidgetMultiSelector(theParent, theWorkshop, theData, theParentId)
35 {
36   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), false);
37 }
38
39 PartSet_WidgetMultiSelector::~PartSet_WidgetMultiSelector()
40 {
41   delete myExternalObjectMgr;
42 }
43
44 bool PartSet_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
45 {
46   bool aSucceed = ModuleBase_WidgetMultiSelector::setSelection(theValues);
47   if (aSucceed) {
48     // TODO(nds): unite with externalObject(), remove parameters
49     //myFeature->execute();
50
51     DataPtr aData = myFeature->data();
52     AttributeSelectionListPtr aSelectionListAttr = 
53       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
54
55     QObjectPtrList aListOfAttributeObjects;
56     for (int i = 0; i < aSelectionListAttr->size(); i++) {
57       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
58       aListOfAttributeObjects.append(anAttr->context());
59     }
60     myExternalObjectMgr->removeUnusedExternalObjects(aListOfAttributeObjects, sketch(), myFeature);
61   }
62   return aSucceed;
63 }
64
65 //********************************************************************
66 bool PartSet_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
67 {
68   bool aValid = ModuleBase_WidgetMultiSelector::isValidSelectionCustom(thePrs);
69   if (aValid) {
70     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
71     aValid = myExternalObjectMgr->isValidObject(anObject);
72   }
73   return aValid;
74 }
75
76 //********************************************************************
77 void PartSet_WidgetMultiSelector::storeAttributeValue()
78 {
79   myIsInVaildate = true;
80   ModuleBase_WidgetMultiSelector::storeAttributeValue();
81 }
82
83 //********************************************************************
84 void PartSet_WidgetMultiSelector::restoreAttributeValue(const bool theValid)
85 {
86   myIsInVaildate = false;
87   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theValid);
88
89   myExternalObjectMgr->removeExternalValidated(sketch(), myFeature);
90 }
91
92 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
93                                                    ObjectPtr& theObject,
94                                                    GeomShapePtr& theShape)
95 {
96   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
97
98   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
99   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
100           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
101   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
102   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
103   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
104     GeomShapePtr aShape = theShape;
105     if (!aShape.get()) {
106       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
107       if (aResult.get())
108         aShape = aResult->shape();
109     }
110     if (aShape.get() != NULL && !aShape->isNull()) {
111       if (myIsInVaildate)
112         theObject = myExternalObjectMgr->externalObjectValidated(theObject, theShape, sketch());
113       else
114         theObject = myExternalObjectMgr->externalObject(theObject, theShape, sketch());
115     }
116   }
117 }