Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / PartSet / PartSet_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "PartSet_WidgetMultiSelector.h"
22
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <ModuleBase_Definitions.h>
30 #include <ModuleBase_Tools.h>
31 #include <ModuleBase_IWorkshop.h>
32 #include <ModuleBase_ISelection.h>
33 #include <ModuleBase_ViewerPrs.h>
34
35 #include <Config_WidgetAPI.h>
36
37 #include <PartSet_CenterPrs.h>
38 #include <PartSet_Tools.h>
39 #include <PartSet_ExternalObjectsMgr.h>
40 #include <SketchPlugin_Feature.h>
41
42 #include <SketchPlugin_ConstraintRigid.h>
43
44 #include <XGUI_Workshop.h>
45
46 #include <QComboBox>
47
48 PartSet_WidgetMultiSelector::PartSet_WidgetMultiSelector(QWidget* theParent,
49                                                          ModuleBase_IWorkshop* theWorkshop,
50                                                          const Config_WidgetAPI* theData)
51 : ModuleBase_WidgetMultiSelector(theParent, theWorkshop, theData)
52 {
53   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
54                                         theData->getProperty("can_create_external"), false);
55 }
56
57 PartSet_WidgetMultiSelector::~PartSet_WidgetMultiSelector()
58 {
59   delete myExternalObjectMgr;
60 }
61
62 //********************************************************************
63 bool PartSet_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
64 {
65   bool aValid = false;
66   if (thePrs.get() && thePrs->interactive().get() &&
67       thePrs->interactive()->IsKind(STANDARD_TYPE(PartSet_CenterPrs)))
68     aValid = true; // we should not check acceptSubShape for such presentation
69   else
70     aValid = ModuleBase_WidgetMultiSelector::isValidSelectionCustom(thePrs);
71
72   if (aValid) {
73     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
74     aValid = myExternalObjectMgr->isValidObject(anObject);
75   }
76   return aValid;
77 }
78
79 //********************************************************************
80 void PartSet_WidgetMultiSelector::restoreAttributeValue(const AttributePtr& theAttribute,
81                                                         const bool theValid)
82 {
83   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theAttribute, theValid);
84
85   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
86 }
87
88 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
89                                                    ObjectPtr& theObject,
90                                                    GeomShapePtr& theShape)
91 {
92   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
93
94   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
95   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
96           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
97   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
98   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
99   if (aSPFeature.get() == NULL) {
100     ObjectPtr anExternalObject = ObjectPtr();
101     GeomShapePtr anExternalShape = GeomShapePtr();
102     if (myExternalObjectMgr->useExternal()) {
103       if (myExternalObjectMgr->canCreateExternal()) {
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           anExternalObject =
112             myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
113       }
114       else {
115         anExternalObject = theObject;
116         anExternalShape = theShape;
117       }
118     }
119     /// the object is null if the selected feature is "external"(not sketch entity feature of the
120     /// current sketch) and it is not created by object manager
121     theObject = anExternalObject;
122     theShape = anExternalShape;
123   }
124 }