Salome HOME
Issue #725 - Translation with parameters - wrong coordinates
[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                                                const bool theToValidate)
46 {
47   bool aSucceed = ModuleBase_WidgetMultiSelector::setSelection(theValues, theToValidate);
48   if (aSucceed) {
49     // TODO(nds): unite with externalObject(), remove parameters
50     //myFeature->execute();
51
52     DataPtr aData = myFeature->data();
53     AttributeSelectionListPtr aSelectionListAttr = 
54       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
55
56     QObjectPtrList aListOfAttributeObjects;
57     for (int i = 0; i < aSelectionListAttr->size(); i++) {
58       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
59       aListOfAttributeObjects.append(anAttr->context());
60     }
61     myExternalObjectMgr->removeUnusedExternalObjects(aListOfAttributeObjects, sketch(), myFeature);
62   }
63   return aSucceed;
64 }
65
66 //********************************************************************
67 bool PartSet_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
68 {
69   bool aValid = ModuleBase_WidgetMultiSelector::isValidSelectionCustom(thePrs);
70   if (aValid) {
71     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
72     aValid = myExternalObjectMgr->isValidObject(anObject);
73   }
74   return aValid;
75 }
76
77 //********************************************************************
78 void PartSet_WidgetMultiSelector::storeAttributeValue()
79 {
80   myIsInVaildate = true;
81   ModuleBase_WidgetMultiSelector::storeAttributeValue();
82 }
83
84 //********************************************************************
85 void PartSet_WidgetMultiSelector::restoreAttributeValue(const bool theValid)
86 {
87   myIsInVaildate = false;
88   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theValid);
89
90   myExternalObjectMgr->removeExternalValidated(sketch(), myFeature, myWorkshop);
91 }
92
93 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
94                                                    ObjectPtr& theObject,
95                                                    GeomShapePtr& theShape)
96 {
97   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
98
99   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
100   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
101           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
102   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
103   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
104   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
105     GeomShapePtr aShape = theShape;
106     if (!aShape.get()) {
107       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
108       if (aResult.get())
109         aShape = aResult->shape();
110     }
111     if (aShape.get() != NULL && !aShape->isNull()) {
112       if (myIsInVaildate)
113         theObject = myExternalObjectMgr->externalObjectValidated(theObject, aShape, sketch());
114       else
115         theObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch());
116     }
117   }
118 }