Salome HOME
2a98fc4ffa6d5cc49997e6e0627876bb37aeab54
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_Selection.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "FiltersPlugin_Selection.h"
21
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25
26 // identifier of the reverse flag of a filter
27 static const std::string kReverseAttrID("");
28
29 void FiltersPlugin_Selection::addFilter(const std::string theFilterID)
30 {
31   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
32   FilterPtr aFilter = aFactory->filter(theFilterID);
33   if (aFilter.get()) {
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);
43   }
44 }
45
46 void FiltersPlugin_Selection::removeFilter(const std::string theFilterID)
47 {
48   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
49   FilterPtr aFilter = aFactory->filter(theFilterID);
50   if (aFilter.get()) {
51     data()->removeAttributes(theFilterID);
52   }
53 }
54
55 std::list<std::string> FiltersPlugin_Selection::filters() const
56 {
57   std::list<std::string> aFilters;
58   data()->allGroups(aFilters);
59   return aFilters;
60 }
61
62 void FiltersPlugin_Selection::setReversed(const std::string theFilterID, const bool theReversed)
63 {
64   std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
65   std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
66   if (aBool.get())
67     aBool->setValue(theReversed);
68 }
69
70 bool FiltersPlugin_Selection::isReversed(const std::string theFilterID)
71 {
72   std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
73   std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
74   if (aBool.get())
75     return aBool->value();
76   return false;
77 }
78
79 std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string theFilterID) const
80 {
81   std::list<AttributePtr> aList;
82   data()->attributesOfGroup(theFilterID, aList);
83   return aList;
84 }
85
86 void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr)
87 {
88   myBase = theAttr;
89   if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters
90     std::shared_ptr<ModelAPI_AttributeSelectionList> aSelList =
91       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
92     if (aSelList.get()) {
93       std::string aStrType = aSelList->selectionType();
94       GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
95       std::list<std::string> aFilters;
96       data()->allGroups(aFilters);
97       ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
98       std::list<std::string>::iterator aFIter = aFilters.begin();
99       for(; aFIter != aFilters.end(); aFIter++) {
100         FilterPtr aFilter = aFactory->filter(*aFIter);
101         if (aFilter.get() && !aFilter->isSupported(aType)) {
102           data()->removeAttributes(*aFIter);
103         }
104       }
105     }
106   }
107 }
108
109 const AttributePtr& FiltersPlugin_Selection::baseAttribute() const
110 {
111   return myBase;
112 }
113
114 void FiltersPlugin_Selection::initAttributes()
115 {
116   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
117   std::list<std::string> aFilters;
118   data()->allGroups(aFilters);
119   for(std::list<std::string>::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); aFIt++) {
120     FilterPtr aFilter = aFactory->filter(*aFIt);
121     if (aFilter.get()) {
122       std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
123         std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
124           kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), *aFIt));
125       aBool->setValue(false); // not reversed by default
126       ModelAPI_FiltersArgs anArgs;
127       anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
128       anArgs.setFilter(*aFIt);
129       aFilter->initAttributes(anArgs);
130     }
131   }
132 }