1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FiltersPlugin_Selection.h"
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeSelectionList.h>
26 // identifier of the reverse flag of a filter
27 static const std::string kReverseAttrID("");
29 void FiltersPlugin_Selection::addFilter(const std::string theFilterID)
31 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
32 FilterPtr aFilter = aFactory->filter(theFilterID);
34 std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
35 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
36 kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), theFilterID));
37 aBool->setValue(false); // not reversed by default
38 // to add attributes related to the filter
39 ModelAPI_FiltersArgs anArgs;
40 anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
41 anArgs.setFilter(theFilterID);
42 aFilter->initAttributes(anArgs);
46 void FiltersPlugin_Selection::removeFilter(const std::string theFilterID)
48 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
49 FilterPtr aFilter = aFactory->filter(theFilterID);
51 data()->removeAttributes(theFilterID);
55 std::list<std::string> FiltersPlugin_Selection::filters() const
57 std::list<std::string> aFilters;
58 data()->allGroups(aFilters);
62 void FiltersPlugin_Selection::setReversed(const std::string theFilterID, const bool theReversed)
64 std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
65 std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
67 aBool->setValue(theReversed);
70 bool FiltersPlugin_Selection::isReversed(const std::string theFilterID)
72 std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
73 std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
74 return aBool->value();
77 std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string theFilterID) const
79 std::list<AttributePtr> aList;
80 data()->attributesOfGroup(theFilterID, aList);
84 void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr)
86 if (myBase != theAttr) { // clear all filters if the attribute is changed or defined a new
87 std::list<std::string> aFilters;
88 data()->allGroups(aFilters);
89 std::list<std::string>::iterator aFIter = aFilters.begin();
90 for(; aFIter != aFilters.end(); aFIter++) {
91 data()->removeAttributes(*aFIter);
95 if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters
96 std::shared_ptr<ModelAPI_AttributeSelectionList> aSelList =
97 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
99 std::string aStrType = aSelList->selectionType();
100 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
101 std::list<std::string> aFilters;
102 data()->allGroups(aFilters);
103 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
104 std::list<std::string>::iterator aFIter = aFilters.begin();
105 for(; aFIter != aFilters.end(); aFIter++) {
106 FilterPtr aFilter = aFactory->filter(*aFIter);
107 if (aFilter.get() && !aFilter->isSupported(aType)) {
108 data()->removeAttributes(*aFIter);
115 const AttributePtr& FiltersPlugin_Selection::baseAttribute() const