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

@@ -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;
};