Salome HOME
Create filter "Belongs To"
[modules/shaper.git] / src / ModelAPI / ModelAPI_Filter.h
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 #ifndef ModelAPI_Filter_H_
21 #define ModelAPI_Filter_H_
22
23 #include "ModelAPI.h"
24 #include "ModelAPI_Attribute.h"
25 #include "ModelAPI_Feature.h"
26
27 #include "ModelAPI_Data.h"
28
29 #include <GeomAPI_Shape.h>
30 #include <map>
31
32 /// separator between the filter name and the filter attribute ID
33 static const std::string kFilterSeparator = "__";
34
35 /**\class ModelAPI_FiltersFeature
36 * \ingroup DataModel
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.
39 */
40 class ModelAPI_FiltersFeature: public ModelAPI_Feature
41 {
42 public:
43   /// Adds a filter to the feature. Also initializes arguments of this filter.
44   virtual void addFilter(const std::string theFilterID) = 0;
45
46   /// Removes an existing filter from the feature.
47   virtual void removeFilter(const std::string theFilterID) = 0;
48
49   /// Returns the list of existing filters in the feature.
50   virtual std::list<std::string> filters() const = 0;
51
52   /// Stores the reversed flag for the filter.
53   virtual void setReversed(const std::string theFilterID, const bool theReversed) = 0;
54
55   /// Returns the reversed flag value for the filter.
56   virtual bool isReversed(const std::string theFilterID) = 0;
57
58   /// Returns the ordered list of attributes related to the filter.
59   virtual std::list<AttributePtr> filterArgs(const std::string theFilterID) const = 0;
60 };
61
62 typedef std::shared_ptr<ModelAPI_FiltersFeature> FiltersFeaturePtr;
63
64
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
71 public:
72   ModelAPI_FiltersArgs() {}
73
74   /// Sets the current filter ID
75   void setFilter(const std::string& theFilterID) {
76     myCurrentFilter = theFilterID;
77   }
78
79   /// Sets the current feature
80   void setFeature(const FiltersFeaturePtr theFeature) {
81     myFeature = theFeature;
82   }
83
84   /// Appends an argument of a filter
85   void add(AttributePtr theAttribute) {
86     myMap[theAttribute->id()] = theAttribute;
87   }
88
89   /// returns the argument of the current filter by the argument id
90   AttributePtr argument(const std::string& theID) const {
91     return myMap.find(myCurrentFilter + kFilterSeparator + theID)->second;
92   }
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);
97   }
98 };
99
100 /**\class ModelAPI_ViewFilter
101 * \ingroup DataModel
102 * \brief A general interface class filters definition
103 */
104 class ModelAPI_Filter
105 {
106 public:
107   /// Returns name of the filter to represent it in GUI
108   virtual const std::string& name() const = 0;
109
110   /// Returns true if the given shape type is supported
111   virtual bool isSupported(GeomAPI_Shape::ShapeType theType) const = 0;
112
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;
117
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 ""; }
121
122   /// Initializes arguments of a filter. If a filter has no arguments, this method may be
123   /// not redefined.
124   virtual void initAttributes(ModelAPI_FiltersArgs& theArguments) {}
125
126 private:
127   bool myIsReverse;
128 };
129
130 typedef std::shared_ptr<ModelAPI_Filter> FilterPtr;
131
132
133 /**\class ModelAPI_FiltersFactory
134 * \ingroup DataModel
135 * \brief Manages registering of filters
136 */
137 class ModelAPI_FiltersFactory
138 {
139 public:
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;
144
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;
150
151   /// Returns the filters that support the given shape type
152   virtual std::list<FilterPtr> filters(GeomAPI_Shape::ShapeType theType) = 0;
153
154   /// Returns a filter by ID
155   virtual FilterPtr filter(std::string theID) = 0;
156
157   /// Returns a filter ID by the filter pointer
158   virtual std::string id(FilterPtr theFilter) = 0;
159
160 protected:
161   /// Get instance from Session
162   ModelAPI_FiltersFactory() {}
163 };
164
165 #endif