Salome HOME
Update Filters data model using the previous filters implementation: filter arguments...
[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 <GeomAPI_Shape.h>
28 #include <map>
29
30 /// separator between the filter name and the filter attribute ID
31 static const std::string kFilterSeparator = "__";
32
33 /// definition of arguments of filters: id of the argument to attributes
34 class ModelAPI_FiltersArgs {
35   /// a map from the FilterID+AttributeID -> attribute
36   std::map<std::string, AttributePtr> myMap;
37   std::string myCurrentFilter; ///< ID of the filter that will take attributes now
38 public:
39   ModelAPI_FiltersArgs() {}
40
41   /// Sets the current filter ID
42   void setFilter(const std::string& theFilterID) {
43     myCurrentFilter = theFilterID;
44   }
45
46   /// Appends an argument of a filter
47   void add(AttributePtr theAttribute) {
48     myMap[theAttribute->id()] = theAttribute;
49   }
50
51   /// returns the argument of the current filter by the argument id
52   AttributePtr argument(const std::string& theID) {
53     return myMap.find(myCurrentFilter + kFilterSeparator + theID)->second;
54   }
55
56 };
57
58 /**\class ModelAPI_ViewFilter
59 * \ingroup DataModel
60 * \brief A general interface class filters definition
61 */
62 class ModelAPI_Filter
63 {
64 public:
65   /// Returns name of the filter to represent it in GUI
66   virtual const std::string& name() const = 0;
67
68   /// Returns true if the given shape type is supported
69   virtual bool isSupported(GeomAPI_Shape::ShapeType theType) const = 0;
70
71   /// This method should contain the filter logic. It returns true if the given shape
72   /// is accepted by the filter.
73   /// \param theShape the given shape
74   virtual bool isOk(const GeomShapePtr& theShape, const ModelAPI_FiltersArgs& theArgs) const = 0;
75
76 private:
77   bool myIsReverse;
78 };
79
80 typedef std::shared_ptr<ModelAPI_Filter> FilterPtr;
81
82
83 /**\class ModelAPI_FiltersFactory
84 * \ingroup DataModel
85 * \brief Manages registering of filters
86 */
87 class ModelAPI_FiltersFactory
88 {
89 public:
90   /// Register an instance of a filter
91   /// \param theID unique identifier of the filter, not necessary equal to the name of filter
92   /// \param theFilter the filter's instance
93   virtual void registerFilter(const std::string& theID, ModelAPI_Filter* theFilter) = 0;
94
95   /// Returns true if all filters of the Filters feature are ok for the Shape (taking into account
96   /// the Reversed states).
97   /// \param theFiltersFeature feature that contains all information about the filters
98   /// \param theShape the checked shape
99   virtual bool isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape) = 0;
100
101   /// Returns the filters that support the given shape type
102   virtual std::list<FilterPtr> filters(GeomAPI_Shape::ShapeType theType) = 0;
103
104   /// Returns a filter by ID
105   virtual FilterPtr filter(std::string theID) = 0;
106
107   /// Returns a filter ID by the filter pointer
108   virtual std::string id(FilterPtr theFilter) = 0;
109
110 protected:
111   /// Get instance from Session
112   ModelAPI_FiltersFactory() {}
113 };
114
115 /**\class ModelAPI_FiltersFeature
116 * \ingroup DataModel
117 * \brief An interface for working with filters in the feature. A filters feature must inherit it
118 *       in order to allow management of filters in the feature data structure.
119 */
120 class ModelAPI_FiltersFeature
121 {
122 public:
123   /// Adds a filter to the feature. Also initializes arguments of this filter.
124   virtual void addFilter(const std::string theFilterID) = 0;
125
126   /// Removes an existing filter from the feature.
127   virtual void removeFilter(const std::string theFilterID) = 0;
128
129   /// Returns the list of existing filters in the feature.
130   virtual std::list<std::string> filters() const = 0;
131
132   /// Stores the reversed flag for the filter.
133   virtual void setReversed(const std::string theFilterID, const bool theReversed) = 0;
134
135   /// Returns the reversed flag value for the filter.
136   virtual bool isReversed(const std::string theFilterID) = 0;
137
138   /// Returns the ordered list of attributes related to the filter.
139   virtual std::list<AttributePtr> filterArgs(const std::string theFilterID) const = 0;
140 };
141
142 typedef std::shared_ptr<ModelAPI_FiltersFeature> FiltersFeaturePtr;
143
144 #endif