Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PartSet / PartSet_WidgetShapeSelector.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_WidgetShapeSelector.h"
22 #include "PartSet_Module.h"
23 #include "PartSet_SketcherMgr.h"
24
25 #include <ModelAPI_AttributeRefAttr.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <ModuleBase_Definitions.h>
30 #include <ModuleBase_ViewerPrs.h>
31
32 #include <Config_WidgetAPI.h>
33
34 #include <PartSet_ExternalObjectsMgr.h>
35 #include <SketchPlugin_Feature.h>
36
37 #include <XGUI_Workshop.h>
38
39 #include <XGUI_ModuleConnector.h>
40 #include <XGUI_Displayer.h>
41 #include <XGUI_SelectionMgr.h>
42 #include <XGUI_Selection.h>
43 #include <XGUI_Tools.h>
44
45 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
46                                                          ModuleBase_IWorkshop* theWorkshop,
47                                                          const Config_WidgetAPI* theData)
48 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
49 {
50   myUseSketchPlane = theData->getBooleanAttribute("use_sketch_plane", true);
51   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
52                                          theData->getProperty("can_create_external"), true);
53 }
54
55 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
56 {
57   delete myExternalObjectMgr;
58 }
59
60 //********************************************************************
61 bool PartSet_WidgetShapeSelector::activateSelectionAndFilters(bool toActivate)
62 {
63   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
64                                                                            (toActivate);
65   if (!myUseSketchPlane) {
66     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
67     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
68     bool isUsePlaneFilterOnly = !toActivate;
69     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
70   }
71   return aHasSelectionFilter;
72 }
73
74 //********************************************************************
75 bool PartSet_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
76 {
77   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
78   if (aValid) {
79     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
80     aValid = myExternalObjectMgr->isValidObject(anObject);
81   }
82   return aValid;
83 }
84
85 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
86                                                    ObjectPtr& theObject,
87                                                    GeomShapePtr& theShape)
88 {
89   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
90
91   myExternalObjectMgr->getGeomSelection(thePrs, theObject, theShape,
92                                         myWorkshop, sketch(), myIsInValidate);
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,
98   // try to create an exernal object
99   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
100   if (aSPFeature.get() == NULL) {
101     ObjectPtr anExternalObject = ObjectPtr();
102     GeomShapePtr anExternalShape = GeomShapePtr();
103     if (myExternalObjectMgr->useExternal()) {
104       if (myExternalObjectMgr->canCreateExternal()) {
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           anExternalObject =
113             myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
114       }
115       else { /// use objects of found selection
116         anExternalObject = theObject;
117         anExternalShape = theShape;
118       }
119     }
120     /// the object is null if the selected feature is "external"(not sketch entity feature of the
121     /// current sketch) and it is not created by object manager
122     theObject = anExternalObject;
123     theShape = anExternalShape;
124   }*/
125 }
126
127 //********************************************************************
128 void PartSet_WidgetShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
129                                                         const bool theValid)
130 {
131   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
132   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
133 }
134