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