Salome HOME
Center presentation validation: check interactive object on NULL
[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
23 #include "PartSet_CenterPrs.h"
24 #include "PartSet_Module.h"
25 #include "PartSet_SketcherMgr.h"
26
27 #include <ModelAPI_AttributeRefAttr.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <ModuleBase_Definitions.h>
32 #include <ModuleBase_ViewerPrs.h>
33
34 #include <Config_WidgetAPI.h>
35
36 #include <PartSet_ExternalObjectsMgr.h>
37 #include <SketchPlugin_Feature.h>
38
39 #include <XGUI_Workshop.h>
40
41 #include <XGUI_ModuleConnector.h>
42 #include <XGUI_Displayer.h>
43 #include <XGUI_SelectionMgr.h>
44 #include <XGUI_Selection.h>
45 #include <XGUI_Tools.h>
46
47 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
48                                                          ModuleBase_IWorkshop* theWorkshop,
49                                                          const Config_WidgetAPI* theData)
50 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
51 {
52   myUseSketchPlane = theData->getBooleanAttribute("use_sketch_plane", true);
53   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
54                                          theData->getProperty("can_create_external"), true);
55 }
56
57 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
58 {
59   delete myExternalObjectMgr;
60 }
61
62 //********************************************************************
63 bool PartSet_WidgetShapeSelector::activateSelectionAndFilters(bool toActivate)
64 {
65   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
66                                                                            (toActivate);
67   if (!myUseSketchPlane) {
68     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
69     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
70     bool isUsePlaneFilterOnly = !toActivate;
71     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
72   }
73   return aHasSelectionFilter;
74 }
75
76 //********************************************************************
77 bool PartSet_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
78 {
79   bool aValid = false;
80   if (thePrs.get() && thePrs->interactive().get() &&
81       thePrs->interactive()->IsKind(STANDARD_TYPE(PartSet_CenterPrs)))
82     aValid = true; // we should not check acceptSubShape for such presentation
83   else
84     aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
85
86   if (aValid) {
87     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
88     aValid = myExternalObjectMgr->isValidObject(anObject);
89   }
90   return aValid;
91 }
92
93 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
94                                                    ObjectPtr& theObject,
95                                                    GeomShapePtr& theShape)
96 {
97   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
98
99   myExternalObjectMgr->getGeomSelection(thePrs, theObject, theShape,
100                                         myWorkshop, sketch(), myIsInValidate);
101 }
102
103 //********************************************************************
104 void PartSet_WidgetShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
105                                                         const bool theValid)
106 {
107   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
108   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
109 }
110