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 #ifndef ModelAPI_Filter_H_
21 #define ModelAPI_Filter_H_
24 #include "ModelAPI_Attribute.h"
25 #include "ModelAPI_Feature.h"
27 #include "ModelAPI_Data.h"
29 #include <GeomAPI_Shape.h>
32 /// separator between the filter name and the filter attribute ID
33 static const std::string kFilterSeparator = "__";
35 /**\class ModelAPI_FiltersFeature
37 * \brief An interface for working with filters in the feature. A filters feature must inherit it
38 * in order to allow management of filters in the feature data structure.
40 class ModelAPI_FiltersFeature: public ModelAPI_Feature
43 /// Adds a filter to the feature. Also initializes arguments of this filter.
44 virtual void addFilter(const std::string theFilterID) = 0;
46 /// Removes an existing filter from the feature.
47 virtual void removeFilter(const std::string theFilterID) = 0;
49 /// Returns the list of existing filters in the feature.
50 virtual std::list<std::string> filters() const = 0;
52 /// Stores the reversed flag for the filter.
53 virtual void setReversed(const std::string theFilterID, const bool theReversed) = 0;
55 /// Returns the reversed flag value for the filter.
56 virtual bool isReversed(const std::string theFilterID) = 0;
58 /// Returns the ordered list of attributes related to the filter.
59 virtual std::list<AttributePtr> filterArgs(const std::string theFilterID) const = 0;
61 /// Sets the attribute (not-persistent field) that contains this filters feature.
62 /// The filter feature may make synchronization by this method call.
63 virtual void setAttribute(const AttributePtr& theAttr) = 0;
65 /// Returns the attribute (not-persistent field) that contains this filters feature.
66 virtual const AttributePtr& baseAttribute() const = 0;
69 typedef std::shared_ptr<ModelAPI_FiltersFeature> FiltersFeaturePtr;
72 /// definition of arguments of filters: id of the argument to attributes
73 class ModelAPI_FiltersArgs {
74 /// a map from the FilterID+AttributeID -> attribute
75 std::map<std::string, AttributePtr> myMap;
76 std::string myCurrentFilter; ///< ID of the filter that will take attributes now
77 FiltersFeaturePtr myFeature; ///< the feature is stored to minimize initAttribute interface
79 ModelAPI_FiltersArgs() {}
81 /// Sets the current filter ID
82 void setFilter(const std::string& theFilterID) {
83 myCurrentFilter = theFilterID;
86 /// Sets the current feature
87 void setFeature(const FiltersFeaturePtr theFeature) {
88 myFeature = theFeature;
91 /// Appends an argument of a filter
92 void add(AttributePtr theAttribute) {
93 myMap[theAttribute->id()] = theAttribute;
96 /// returns the argument of the current filter by the argument id
97 AttributePtr argument(const std::string& theID) const {
98 return myMap.find(myCurrentFilter + kFilterSeparator + theID)->second;
100 /// adds an attribute of the filter
101 std::shared_ptr<ModelAPI_Attribute> initAttribute(
102 const std::string& theID, const std::string theAttrType) {
103 AttributePtr aR = myFeature->data()->addFloatingAttribute(theID, theAttrType, myCurrentFilter);
104 aR->setIsArgument(false); // to avoid parametric update
109 /**\class ModelAPI_ViewFilter
111 * \brief A general interface class filters definition
113 class ModelAPI_Filter
116 /// Returns name of the filter to represent it in GUI
117 virtual const std::string& name() const = 0;
119 /// Returns true if the given shape type is supported
120 virtual bool isSupported(GeomAPI_Shape::ShapeType theType) const = 0;
122 /// This method should contain the filter logic. It returns true if the given shape
123 /// is accepted by the filter.
124 /// \param theShape the given shape
125 virtual bool isOk(const GeomShapePtr& theShape, const ModelAPI_FiltersArgs& theArgs) const = 0;
127 /// Returns XML string which represents GUI of the filter
128 /// By default it returns nothing (no GUI)
129 virtual std::string xmlRepresentation() const { return ""; }
131 /// Initializes arguments of a filter. If a filter has no arguments, this method may be
133 virtual void initAttributes(ModelAPI_FiltersArgs& theArguments) {}
139 typedef std::shared_ptr<ModelAPI_Filter> FilterPtr;
142 /**\class ModelAPI_FiltersFactory
144 * \brief Manages registering of filters
146 class ModelAPI_FiltersFactory
149 /// Register an instance of a filter
150 /// \param theID unique identifier of the filter, not necessary equal to the name of filter
151 /// \param theFilter the filter's instance
152 virtual void registerFilter(const std::string& theID, ModelAPI_Filter* theFilter) = 0;
154 /// Returns true if all filters of the Filters feature are ok for the Shape (taking into account
155 /// the Reversed states).
156 /// \param theFiltersFeature feature that contains all information about the filters
157 /// \param theShape the checked shape
158 virtual bool isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape) = 0;
160 /// Returns the filters that support the given shape type
161 virtual std::list<FilterPtr> filters(GeomAPI_Shape::ShapeType theType) = 0;
163 /// Returns a filter by ID
164 virtual FilterPtr filter(std::string theID) = 0;
166 /// Returns a filter ID by the filter pointer
167 virtual std::string id(FilterPtr theFilter) = 0;
170 /// Get instance from Session
171 ModelAPI_FiltersFactory() {}