Salome HOME
Update copyrights
[modules/shaper.git] / src / PartSet / PartSet_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_WidgetMultiSelector.h"
21
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Validator.h>
27
28 #include <ModuleBase_Definitions.h>
29 #include <ModuleBase_Tools.h>
30 #include <ModuleBase_IWorkshop.h>
31 #include <ModuleBase_ISelection.h>
32 #include <ModuleBase_ViewerPrs.h>
33
34 #include <Config_WidgetAPI.h>
35
36 #include <PartSet_CenterPrs.h>
37 #include <PartSet_Tools.h>
38 #include <PartSet_ExternalObjectsMgr.h>
39 #include <SketchPlugin_Feature.h>
40
41 #include <SketchPlugin_ConstraintRigid.h>
42
43 #include <XGUI_Workshop.h>
44
45 #include <QComboBox>
46
47 PartSet_WidgetMultiSelector::PartSet_WidgetMultiSelector(QWidget* theParent,
48                                                          ModuleBase_IWorkshop* theWorkshop,
49                                                          const Config_WidgetAPI* theData)
50 : ModuleBase_WidgetMultiSelector(theParent, theWorkshop, theData)
51 {
52   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
53                                         theData->getProperty("can_create_external"), false);
54 }
55
56 PartSet_WidgetMultiSelector::~PartSet_WidgetMultiSelector()
57 {
58   delete myExternalObjectMgr;
59 }
60
61 //********************************************************************
62 bool PartSet_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
63 {
64   bool aValid = false;
65   if (thePrs.get() && thePrs->interactive().get() &&
66       thePrs->interactive()->IsKind(STANDARD_TYPE(PartSet_CenterPrs)))
67     aValid = true; // we should not check acceptSubShape for such presentation
68   else
69     aValid = ModuleBase_WidgetMultiSelector::isValidSelectionCustom(thePrs);
70
71   if (aValid) {
72     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
73     aValid = myExternalObjectMgr->isValidObject(anObject);
74   }
75   return aValid;
76 }
77
78 //********************************************************************
79 void PartSet_WidgetMultiSelector::restoreAttributeValue(const AttributePtr& theAttribute,
80                                                         const bool theValid)
81 {
82   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theAttribute, theValid);
83
84   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
85 }
86
87 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
88                                                    ObjectPtr& theObject,
89                                                    GeomShapePtr& theShape)
90 {
91   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
92
93   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
94   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
95           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
96   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
97   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
98   if (aSPFeature.get() == NULL) {
99     ObjectPtr anExternalObject = ObjectPtr();
100     GeomShapePtr anExternalShape = GeomShapePtr();
101     if (myExternalObjectMgr->useExternal()) {
102       if (myExternalObjectMgr->canCreateExternal()) {
103         GeomShapePtr aShape = theShape;
104         if (!aShape.get()) {
105           ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
106           if (aResult.get())
107             aShape = aResult->shape();
108         }
109         if (aShape.get() != NULL && !aShape->isNull())
110           anExternalObject =
111             myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
112       }
113       else {
114         anExternalObject = theObject;
115         anExternalShape = theShape;
116       }
117     }
118     /// the object is null if the selected feature is "external"(not sketch entity feature of the
119     /// current sketch) and it is not created by object manager
120     theObject = anExternalObject;
121     theShape = anExternalShape;
122   }
123 }