Gui: Provide ViewProviderFeaturePython selection callbacks

Adds support for Python-based selection callbacks to
`ViewProviderFeaturePython` objects.

It follows the same conventions as `SelectionObserverPython`, follows an
example:

```python
    def setPreselection(self, document, object, element):
        print("setPreselection: %s.%s.%s"%(document, object, element))

    def removePreselection(self, document, object, element):
print("removePreselection: %s.%s.%s"%(document, object,
element))

    def addSelection(self, document, object, element, position):
print("addSelection: %s.%s.%s at %s"%(document, object, element,
str(position)))

    def removeSelection(self,document, object, element):
        print("removeSelection: %s.%s.%s"%(document, object, element))

    def setSelection(self,doc):
        sel = FreeCADGui.Selection.getSelection(doc)
        print("setSelection: %s"%sel)

    def clearSelection(self,doc):
        print("clearSelection\n")
```
This commit is contained in:
tritao
2025-01-15 21:05:07 +00:00
parent 10ad4b7e15
commit ea80310642
6 changed files with 84 additions and 8 deletions

View File

@@ -32,15 +32,28 @@ using namespace Gui;
std::vector<SelectionObserverPython*> SelectionObserverPython::_instances;
void SelectionObserverPythonHandler::init(const Py::Object& obj)
void SelectionObserverPythonHandler::init(PyObject* obj)
{
this->inst = obj;
#undef FC_PY_ELEMENT
#define FC_PY_ELEMENT(_name) FC_PY_GetCallable(obj.ptr(),#_name,py_##_name);
#define FC_PY_ELEMENT(_name) FC_PY_GetCallable(obj,#_name,py_##_name);
FC_PY_SEL_OBSERVER
}
SelectionObserverPythonHandler::~SelectionObserverPythonHandler()
{
#undef FC_PY_ELEMENT
#define FC_PY_ELEMENT(_name) py_##_name = Py::None();
try {
FC_PY_SEL_OBSERVER
}
catch (Py::Exception& e) {
e.clear();
}
}
void SelectionObserverPythonHandler::handleSelectionChanged(const SelectionChanges& msg)
{
switch (msg.Type)
@@ -198,7 +211,7 @@ void SelectionObserverPythonHandler::removePreselection(const SelectionChanges&
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj, ResolveMode resolve)
: SelectionObserver(true, resolve)
{
this->init(obj);
this->init(obj.ptr());
}
SelectionObserverPython::~SelectionObserverPython() = default;