Gui: Split SelectionObserverPython call logic into separate handler class.

This commit is contained in:
tritao
2025-01-15 21:07:15 +00:00
committed by Joao Matos
parent 928556c5e5
commit 9c0b95b569
2 changed files with 73 additions and 48 deletions

View File

@@ -32,37 +32,16 @@ using namespace Gui;
std::vector<SelectionObserverPython*> SelectionObserverPython::_instances;
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj, ResolveMode resolve)
: SelectionObserver(true, resolve), inst(obj)
void SelectionObserverPythonHandler::init(const Py::Object& obj)
{
this->inst = obj;
#undef FC_PY_ELEMENT
#define FC_PY_ELEMENT(_name) FC_PY_GetCallable(obj.ptr(),#_name,py_##_name);
FC_PY_SEL_OBSERVER
}
SelectionObserverPython::~SelectionObserverPython() = default;
void SelectionObserverPython::addObserver(const Py::Object& obj, ResolveMode resolve)
{
_instances.push_back(new SelectionObserverPython(obj, resolve));
}
void SelectionObserverPython::removeObserver(const Py::Object& obj)
{
SelectionObserverPython* obs=nullptr;
for (std::vector<SelectionObserverPython*>::iterator it =
_instances.begin(); it != _instances.end(); ++it) {
if ((*it)->inst == obj) {
obs = *it;
_instances.erase(it);
break;
}
}
delete obs;
}
void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg)
void SelectionObserverPythonHandler::handleSelectionChanged(const SelectionChanges& msg)
{
switch (msg.Type)
{
@@ -92,7 +71,7 @@ void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg)
}
}
void SelectionObserverPython::pickedListChanged()
void SelectionObserverPythonHandler::pickedListChanged()
{
if(py_pickedListChanged.isNone())
return;
@@ -106,7 +85,7 @@ void SelectionObserverPython::pickedListChanged()
}
}
void SelectionObserverPython::addSelection(const SelectionChanges& msg)
void SelectionObserverPythonHandler::addSelection(const SelectionChanges& msg)
{
if(py_addSelection.isNone())
return;
@@ -129,7 +108,7 @@ void SelectionObserverPython::addSelection(const SelectionChanges& msg)
}
}
void SelectionObserverPython::removeSelection(const SelectionChanges& msg)
void SelectionObserverPythonHandler::removeSelection(const SelectionChanges& msg)
{
if(py_removeSelection.isNone())
return;
@@ -147,7 +126,7 @@ void SelectionObserverPython::removeSelection(const SelectionChanges& msg)
}
}
void SelectionObserverPython::setSelection(const SelectionChanges& msg)
void SelectionObserverPythonHandler::setSelection(const SelectionChanges& msg)
{
if(py_setSelection.isNone())
return;
@@ -163,7 +142,7 @@ void SelectionObserverPython::setSelection(const SelectionChanges& msg)
}
}
void SelectionObserverPython::clearSelection(const SelectionChanges& msg)
void SelectionObserverPythonHandler::clearSelection(const SelectionChanges& msg)
{
if(py_clearSelection.isNone())
return;
@@ -179,7 +158,7 @@ void SelectionObserverPython::clearSelection(const SelectionChanges& msg)
}
}
void SelectionObserverPython::setPreselection(const SelectionChanges& msg)
void SelectionObserverPythonHandler::setPreselection(const SelectionChanges& msg)
{
if(py_setPreselection.isNone())
return;
@@ -197,7 +176,7 @@ void SelectionObserverPython::setPreselection(const SelectionChanges& msg)
}
}
void SelectionObserverPython::removePreselection(const SelectionChanges& msg)
void SelectionObserverPythonHandler::removePreselection(const SelectionChanges& msg)
{
if(py_removePreselection.isNone())
return;
@@ -214,3 +193,36 @@ void SelectionObserverPython::removePreselection(const SelectionChanges& msg)
e.ReportException();
}
}
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj, ResolveMode resolve)
: SelectionObserver(true, resolve)
{
this->init(obj);
}
SelectionObserverPython::~SelectionObserverPython() = default;
void SelectionObserverPython::addObserver(const Py::Object& obj, ResolveMode resolve)
{
_instances.push_back(new SelectionObserverPython(obj, resolve));
}
void SelectionObserverPython::removeObserver(const Py::Object& obj)
{
SelectionObserverPython* obs=nullptr;
for (auto it =_instances.begin(); it != _instances.end(); ++it) {
if ((*it)->inst == obj) {
obs = *it;
_instances.erase(it);
break;
}
}
delete obs;
}
void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg)
{
handleSelectionChanged(msg);
}

View File

@@ -30,26 +30,16 @@
namespace Gui
{
/**
* The SelectionObserverPython class implements a mechanism to register
* a Python class instance implementing the required interface in order
* to be notified on selection changes.
*
* @author Werner Mayer
*/
class GuiExport SelectionObserverPython : public SelectionObserver
class GuiExport SelectionObserverPythonHandler
{
public:
/// Constructor
explicit SelectionObserverPython(const Py::Object& obj, ResolveMode resolve = ResolveMode::OldStyleElement);
~SelectionObserverPython() override;
explicit SelectionObserverPythonHandler() = default;
void init(const Py::Object& obj);
void handleSelectionChanged(const SelectionChanges& msg);
static void addObserver(const Py::Object& obj, ResolveMode resolve = ResolveMode::OldStyleElement);
static void removeObserver(const Py::Object& obj);
private:
void onSelectionChanged(const SelectionChanges& msg) override;
protected:
void addSelection(const SelectionChanges&);
void removeSelection(const SelectionChanges&);
void setSelection(const SelectionChanges&);
@@ -58,9 +48,10 @@ private:
void removePreselection(const SelectionChanges&);
void pickedListChanged();
private:
Py::Object inst;
private:
#define FC_PY_SEL_OBSERVER \
FC_PY_ELEMENT(onSelectionChanged) \
FC_PY_ELEMENT(addSelection) \
@@ -75,6 +66,28 @@ private:
#define FC_PY_ELEMENT(_name) Py::Object py_##_name;
FC_PY_SEL_OBSERVER
};
/**
* The SelectionObserverPython class implements a mechanism to register
* a Python class instance implementing the required interface in order
* to be notified on selection changes.
*
* @author Werner Mayer
*/
class GuiExport SelectionObserverPython : public SelectionObserverPythonHandler, public SelectionObserver
{
public:
/// Constructor
explicit SelectionObserverPython(const Py::Object& obj, ResolveMode resolve = ResolveMode::OldStyleElement);
~SelectionObserverPython() override;
static void addObserver(const Py::Object& obj, ResolveMode resolve = ResolveMode::OldStyleElement);
static void removeObserver(const Py::Object& obj);
private:
void onSelectionChanged(const SelectionChanges& msg) override;
static std::vector<SelectionObserverPython*> _instances;
};