Debian Codegraph @

src/Gui/Selection/SelectionFilter.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2009 Jürgen Riegel <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24
25 #ifndef GUI_SelectionFilter_H
26 #define GUI_SelectionFilter_H
27
28 #include <limits>
29 #include <memory>
30 #include <string>
31 #include <CXX/Extensions.hxx>
32 #include "Selection.h"
33
34 namespace App
35 {
36 class DocumentObject;
37 }
38
39 namespace Gui
40 {
41 struct Node_Block;
42 class SelectionFilterPy;
43
44 /** Selection filter definition
45 * This class builds up a type/count tree out of a string
46 * to test very fast a selection or object/subelement type
47 * against it.
48 *
49 * Example strings are:
50 * "SELECT Part::Feature SUBELEMENT Edge",
51 * "SELECT Robot::RobotObject",
52 * "SELECT Robot::RobotObject COUNT 1..5"
53 */
54 class GuiExport SelectionFilter
55 {
56
57 public:
58 /** Constructs a SelectionFilter object. */
59 explicit SelectionFilter(const char* filter, App::DocumentObject* container = nullptr);
60 explicit SelectionFilter(const std::string& filter, App::DocumentObject* container = nullptr);
61 virtual ~SelectionFilter();
62
63 /// Set a new filter string
64 void setFilter(const char* filter);
65 const std::string& getFilter() const
66 {
67 return Filter;
68 }
69 /** Test to current selection
70 * This method tests the current selection set
71 * against the filter and returns true if the
72 * described object(s) are selected.
73 */
74 bool match();
75 /** Test objects
76 * This method tests if a given object is described in the
77 * filter. If SubName is not NULL the Subelement gets also
78 * tested.
79 */
80 bool test(App::DocumentObject* pObj, const char* sSubName);
81
82 void addError(const char* e);
83
84 friend class SelectionSingleton;
85
86 std::vector<std::vector<SelectionObject>> Result;
87
88 /// true if a valid filter is set
89 bool isValid() const
90 {
91 return Ast ? true : false;
92 }
93
94 protected:
95 std::string Filter;
96 std::string Errors;
97 bool parse();
98 App::DocumentObject* container;
99
100 std::shared_ptr<Node_Block> Ast;
101 };
102
103 /** Filter object for the SelectionSengleton
104 * This object is a link between the selection
105 * filter class and the selection singleton. Created with a
106 * filter string and registered in the selection it will only
107 * allow the described object types to be selected.
108 * @see SelectionFilter
109 * @see SelectionSingleton
110 */
111 class GuiExport SelectionFilterGate: public SelectionGate
112 {
113 public:
114 /// construct with the filter string
115 explicit SelectionFilterGate(const char* filter);
116 explicit SelectionFilterGate(SelectionFilter* filter);
117 ~SelectionFilterGate() override;
118 bool allow(App::Document*, App::DocumentObject*, const char*) override;
119
120 protected:
121 static SelectionFilter* nullPointer()
122 {
123 return nullptr;
124 }
125
126 SelectionFilterGate();
127
128 protected:
129 SelectionFilter* Filter;
130 };
131
132 /**
133 * A wrapper around a Python class that implements the SelectionGate interface
134 * @author Werner Mayer
135 */
136 class SelectionGatePython: public SelectionGate
137 {
138 public:
139 /// Constructor
140 explicit SelectionGatePython(const Py::Object& obj);
141 ~SelectionGatePython() override;
142
143 bool allow(App::Document*, App::DocumentObject*, const char*) override;
144
145 private:
146 Py::Object gate;
147 };
148
149 /**
150 * A Python wrapper around SelectionFilterPy to implement the SelectionGate interface
151 * \code
152 * class SelectionGate(object):
153 * def allow(self, doc, obj, sub):
154 * if not obj.isDerivedFrom("Part::Feature"):
155 * return False
156 * if not str(sub).startswith("Edge"):
157 * return False
158 * return True
159 *
160 * gate=SelectionGate()
161 * Gui.Selection.addSelectionGate(gate)
162 * \endcode
163 * @author Werner Mayer
164 */
165 class SelectionFilterGatePython: public SelectionGate
166 {
167 public:
168 /// Constructor
169 explicit SelectionFilterGatePython(SelectionFilterPy* obj);
170 ~SelectionFilterGatePython() override;
171
172 bool allow(App::Document*, App::DocumentObject*, const char*) override;
173
174 private:
175 SelectionFilterPy* filter;
176 };
177
178 // === Abstract syntax tree (AST) ===========================================
179
180 struct Node_Slice
181 {
182 explicit Node_Slice(int min = 1, int max = std::numeric_limits<int>::max())
183 : Min(min)
184 , Max(max)
185 {}
186 int Min, Max;
187 };
188
189
190 struct Node_Object
191 {
192 Node_Object(std::string* type, std::string* subname, Node_Slice* slc)
193 : Slice(slc)
194 {
195 ObjectType = Base::Type::fromName(type->c_str());
196 if (subname) {
197 SubName = *subname;
198 }
199 }
200 ~Node_Object()
201 {
202 delete Slice;
203 }
204 Base::Type ObjectType;
205 Node_Slice* Slice;
206 std::string SubName;
207 };
208 using Node_ObjectPtr = std::shared_ptr<Node_Object>;
209
210 struct Node_Block
211 {
212 explicit Node_Block(Node_Object* obj)
213 {
214 Objects.emplace_back(obj);
215 }
216 std::vector<Node_ObjectPtr> Objects;
217 };
218
219
220 } // namespace Gui
221
222
223 #endif // GUI_SelectionFilter_H