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;
62 typedef std::shared_ptr<ModelAPI_FiltersFeature> FiltersFeaturePtr;
65 /// definition of arguments of filters: id of the argument to attributes
66 class ModelAPI_FiltersArgs {
67 /// a map from the FilterID+AttributeID -> attribute
68 std::map<std::string, AttributePtr> myMap;
69 std::string myCurrentFilter; ///< ID of the filter that will take attributes now
70 FiltersFeaturePtr myFeature; ///< the feature is stored to minimize initAttribute interface
72 ModelAPI_FiltersArgs() {}
74 /// Sets the current filter ID
75 void setFilter(const std::string& theFilterID) {
76 myCurrentFilter = theFilterID;
79 /// Sets the current feature
80 void setFeature(const FiltersFeaturePtr theFeature) {
81 myFeature = theFeature;
84 /// Appends an argument of a filter
85 void add(AttributePtr theAttribute) {
86 myMap[theAttribute->id()] = theAttribute;
89 /// returns the argument of the current filter by the argument id
90 AttributePtr argument(const std::string& theID) {
91 return myMap.find(myCurrentFilter + kFilterSeparator + theID)->second;
93 /// adds an attribute of the filter
94 std::shared_ptr<ModelAPI_Attribute> initAttribute(
95 const std::string& theID, const std::string theAttrType) {
96 return myFeature->data()->addFloatingAttribute(theID, theAttrType, myCurrentFilter);
100 /**\class ModelAPI_ViewFilter
102 * \brief A general interface class filters definition
104 class ModelAPI_Filter
107 /// Returns name of the filter to represent it in GUI
108 virtual const std::string& name() const = 0;
110 /// Returns true if the given shape type is supported
111 virtual bool isSupported(GeomAPI_Shape::ShapeType theType) const = 0;
113 /// This method should contain the filter logic. It returns true if the given shape
114 /// is accepted by the filter.
115 /// \param theShape the given shape
116 virtual bool isOk(const GeomShapePtr& theShape, const ModelAPI_FiltersArgs& theArgs) const = 0;
118 /// Returns XML string which represents GUI of the filter
119 /// By default it returns nothing (no GUI)
120 virtual std::string xmlRepresentation() const { return ""; }
122 /// Initializes arguments of a filter. If a filter has no arguments, this method may be
124 virtual void initAttributes(ModelAPI_FiltersArgs& theArguments) {}
130 typedef std::shared_ptr<ModelAPI_Filter> FilterPtr;
133 /**\class ModelAPI_FiltersFactory
135 * \brief Manages registering of filters
137 class ModelAPI_FiltersFactory
140 /// Register an instance of a filter
141 /// \param theID unique identifier of the filter, not necessary equal to the name of filter
142 /// \param theFilter the filter's instance
143 virtual void registerFilter(const std::string& theID, ModelAPI_Filter* theFilter) = 0;
145 /// Returns true if all filters of the Filters feature are ok for the Shape (taking into account
146 /// the Reversed states).
147 /// \param theFiltersFeature feature that contains all information about the filters
148 /// \param theShape the checked shape
149 virtual bool isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape) = 0;
151 /// Returns the filters that support the given shape type
152 virtual std::list<FilterPtr> filters(GeomAPI_Shape::ShapeType theType) = 0;
154 /// Returns a filter by ID
155 virtual FilterPtr filter(std::string theID) = 0;
157 /// Returns a filter ID by the filter pointer
158 virtual std::string id(FilterPtr theFilter) = 0;
161 /// Get instance from Session
162 ModelAPI_FiltersFactory() {}