Salome HOME
Issue #478 Problem of performance with selection: remove last instead of clearing...
[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::restoreAttributeValue(const bool theValid)
79 {
80   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theValid);
81
82   myExternalObjectMgr->removeExternalValidated(sketch(), myFeature, myWorkshop);
83 }
84
85 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
86                                                    ObjectPtr& theObject,
87                                                    GeomShapePtr& theShape)
88 {
89   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
90
91   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
92   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
93           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
94   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
95   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
96   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
97     GeomShapePtr aShape = theShape;
98     if (!aShape.get()) {
99       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
100       if (aResult.get())
101         aShape = aResult->shape();
102     }
103     if (aShape.get() != NULL && !aShape->isNull()) {
104       if (myIsInValidate)
105         theObject = myExternalObjectMgr->externalObjectValidated(theObject, aShape, sketch());
106       else
107         theObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch());
108     }
109   }
110 }