From 9c0b95b569aba9a8d307cde6e6e05585343d6452 Mon Sep 17 00:00:00 2001 From: tritao Date: Wed, 15 Jan 2025 21:07:15 +0000 Subject: [PATCH] Gui: Split `SelectionObserverPython` call logic into separate handler class. --- src/Gui/Selection/SelectionObserverPython.cpp | 76 +++++++++++-------- src/Gui/Selection/SelectionObserverPython.h | 45 +++++++---- 2 files changed, 73 insertions(+), 48 deletions(-) diff --git a/src/Gui/Selection/SelectionObserverPython.cpp b/src/Gui/Selection/SelectionObserverPython.cpp index e1f670df28..936e074b1f 100644 --- a/src/Gui/Selection/SelectionObserverPython.cpp +++ b/src/Gui/Selection/SelectionObserverPython.cpp @@ -32,37 +32,16 @@ using namespace Gui; std::vector 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::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); +} diff --git a/src/Gui/Selection/SelectionObserverPython.h b/src/Gui/Selection/SelectionObserverPython.h index 3f5643487e..c7d258d20e 100644 --- a/src/Gui/Selection/SelectionObserverPython.h +++ b/src/Gui/Selection/SelectionObserverPython.h @@ -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 _instances; };