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