Salome HOME
9f76f81200fd7bbd5ae520cb26b3649cd7b816e9
[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_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
14
15 #include <ModuleBase_Definitions.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_IWorkshop.h>
18 #include <ModuleBase_ISelection.h>
19
20 #include <Config_WidgetAPI.h>
21
22 #include <PartSet_Tools.h>
23 #include <PartSet_ExternalObjectsMgr.h>
24 #include <SketchPlugin_Feature.h>
25
26 #include <SketchPlugin_ConstraintRigid.h>
27
28 #include <XGUI_Workshop.h>
29
30 #include <QComboBox>
31
32 PartSet_WidgetMultiSelector::PartSet_WidgetMultiSelector(QWidget* theParent,
33                                                          ModuleBase_IWorkshop* theWorkshop,
34                                                          const Config_WidgetAPI* theData,
35                                                          const std::string& theParentId)
36 : ModuleBase_WidgetMultiSelector(theParent, theWorkshop, theData, theParentId)
37 {
38   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), false);
39 }
40
41 PartSet_WidgetMultiSelector::~PartSet_WidgetMultiSelector()
42 {
43   delete myExternalObjectMgr;
44 }
45
46 bool PartSet_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
47                                                const bool theToValidate)
48 {
49   bool aSucceed = ModuleBase_WidgetMultiSelector::setSelection(theValues, theToValidate);
50   if (aSucceed) {
51     // TODO(nds): unite with externalObject(), remove parameters
52     //myFeature->execute();
53
54     QObjectPtrList aListOfAttributeObjects;
55
56     AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
57     if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
58       AttributeSelectionListPtr aSelectionListAttr = 
59                            std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
60       for (int i = 0; i < aSelectionListAttr->size(); i++) {
61         AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
62         aListOfAttributeObjects.append(anAttr->context());
63       }
64     }
65     else if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) {
66       AttributeRefListPtr aRefListAttr = 
67                                  std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
68       for (int i = 0; i < aRefListAttr->size(); i++) {
69         aListOfAttributeObjects.append(aRefListAttr->object(i));
70       }
71     }
72
73     myExternalObjectMgr->removeUnusedExternalObjects(aListOfAttributeObjects, sketch(), myFeature);
74   }
75   return aSucceed;
76 }
77
78 //********************************************************************
79 bool PartSet_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
80 {
81   bool aValid = ModuleBase_WidgetMultiSelector::isValidSelectionCustom(thePrs);
82   if (aValid) {
83     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
84     aValid = myExternalObjectMgr->isValidObject(anObject);
85   }
86   return aValid;
87 }
88
89 //********************************************************************
90 void PartSet_WidgetMultiSelector::restoreAttributeValue(const bool theValid)
91 {
92   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theValid);
93
94   myExternalObjectMgr->removeExternal/*Validated*/(sketch(), myFeature, myWorkshop, true);
95 }
96
97 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
98                                                    ObjectPtr& theObject,
99                                                    GeomShapePtr& theShape)
100 {
101   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
102
103   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
104   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
105           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
106   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
107   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
108   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
109     GeomShapePtr aShape = theShape;
110     if (!aShape.get()) {
111       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
112       if (aResult.get())
113         aShape = aResult->shape();
114     }
115     if (aShape.get() != NULL && !aShape->isNull()) {
116       //if (myIsInValidate)
117       //  theObject = myExternalObjectMgr->externalObjectValidated(theObject, aShape, sketch());
118       //else
119       theObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
120     }
121   }
122 }