]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Filter.h
Salome HOME
Several improvement of the filters functionality, correct work with shapes types...
[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   /// 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;
64
65   /// Returns the attribute (not-persistent field) that contains this filters feature.
66   virtual const AttributePtr& baseAttribute() const = 0;
67 };
68
69 typedef std::shared_ptr<ModelAPI_FiltersFeature> FiltersFeaturePtr;
70
71
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
78 public:
79   ModelAPI_FiltersArgs() {}
80
81   /// Sets the current filter ID
82   void setFilter(const std::string& theFilterID) {
83     myCurrentFilter = theFilterID;
84   }
85
86   /// Sets the current feature
87   void setFeature(const FiltersFeaturePtr theFeature) {
88     myFeature = theFeature;
89   }
90
91   /// Appends an argument of a filter
92   void add(AttributePtr theAttribute) {
93     myMap[theAttribute->id()] = theAttribute;
94   }
95
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;
99   }
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
105     return aR;
106   }
107 };
108
109 /**\class ModelAPI_ViewFilter
110 * \ingroup DataModel
111 * \brief A general interface class filters definition
112 */
113 class ModelAPI_Filter
114 {
115 public:
116   /// Returns name of the filter to represent it in GUI
117   virtual const std::string& name() const = 0;
118
119   /// Returns true if the given shape type is supported
120   virtual bool isSupported(GeomAPI_Shape::ShapeType theType) const = 0;
121
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;
126
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 ""; }
130
131   /// Initializes arguments of a filter. If a filter has no arguments, this method may be
132   /// not redefined.
133   virtual void initAttributes(ModelAPI_FiltersArgs& theArguments) {}
134
135 private:
136   bool myIsReverse;
137 };
138
139 typedef std::shared_ptr<ModelAPI_Filter> FilterPtr;
140
141
142 /**\class ModelAPI_FiltersFactory
143 * \ingroup DataModel
144 * \brief Manages registering of filters
145 */
146 class ModelAPI_FiltersFactory
147 {
148 public:
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;
153
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;
159
160   /// Returns the filters that support the given shape type
161   virtual std::list<FilterPtr> filters(GeomAPI_Shape::ShapeType theType) = 0;
162
163   /// Returns a filter by ID
164   virtual FilterPtr filter(std::string theID) = 0;
165
166   /// Returns a filter ID by the filter pointer
167   virtual std::string id(FilterPtr theFilter) = 0;
168
169 protected:
170   /// Get instance from Session
171   ModelAPI_FiltersFactory() {}
172 };
173
174 #endif