Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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_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 = ModuleBase_WidgetMultiSelector::isValidSelectionCustom(thePrs);
65   if (aValid) {
66     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
67     aValid = myExternalObjectMgr->isValidObject(anObject);
68   }
69   return aValid;
70 }
71
72 //********************************************************************
73 void PartSet_WidgetMultiSelector::restoreAttributeValue(const AttributePtr& theAttribute,
74                                                         const bool theValid)
75 {
76   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theAttribute, theValid);
77
78   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
79 }
80
81 void PartSet_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
82                                                    ObjectPtr& theObject,
83                                                    GeomShapePtr& theShape)
84 {
85   ModuleBase_WidgetMultiSelector::getGeomSelection(thePrs, theObject, theShape);
86
87   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
88   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
89           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
90   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
91   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
92   if (aSPFeature.get() == NULL) {
93     ObjectPtr anExternalObject = ObjectPtr();
94     GeomShapePtr anExternalShape = GeomShapePtr();
95     if (myExternalObjectMgr->useExternal()) {
96       if (myExternalObjectMgr->canCreateExternal()) {
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           anExternalObject =
105             myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
106       }
107       else {
108         anExternalObject = theObject;
109         anExternalShape = theShape;
110       }
111     }
112     /// the object is null if the selected feature is "external"(not sketch entity feature of the
113     /// current sketch) and it is not created by object manager
114     theObject = anExternalObject;
115     theShape = anExternalShape;
116   }
117 }