Debian Codegraph @

src/Gui/Selection/SelectionObserverPython.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24 #ifndef GUI_SELECTIONOBSERVERPYTHON_H
25 #define GUI_SELECTIONOBSERVERPYTHON_H
26
27 #include <CXX/Objects.hxx>
28 #include "Selection.h"
29
30
31 namespace Gui
32 {
33
34 class GuiExport SelectionObserverPythonHandler
35 {
36
37 public:
38 /// Constructor
39 explicit SelectionObserverPythonHandler() = default;
40 virtual ~SelectionObserverPythonHandler();
41
42 void init(PyObject* obj);
43 void handleSelectionChanged(const SelectionChanges& msg);
44
45 protected:
46 void addSelection(const SelectionChanges&);
47 void removeSelection(const SelectionChanges&);
48 void setSelection(const SelectionChanges&);
49 void clearSelection(const SelectionChanges&);
50 void setPreselection(const SelectionChanges&);
51 void removePreselection(const SelectionChanges&);
52 void pickedListChanged();
53
54 private:
55 #define FC_PY_SEL_OBSERVER \
56 FC_PY_ELEMENT(onSelectionChanged) \
57 FC_PY_ELEMENT(addSelection) \
58 FC_PY_ELEMENT(removeSelection) \
59 FC_PY_ELEMENT(setSelection) \
60 FC_PY_ELEMENT(clearSelection) \
61 FC_PY_ELEMENT(setPreselection) \
62 FC_PY_ELEMENT(removePreselection) \
63 FC_PY_ELEMENT(pickedListChanged)
64
65 #undef FC_PY_ELEMENT
66 #define FC_PY_ELEMENT(_name) Py::Object py_##_name;
67
68 FC_PY_SEL_OBSERVER
69 };
70
71 /**
72 * The SelectionObserverPython class implements a mechanism to register
73 * a Python class instance implementing the required interface in order
74 * to be notified on selection changes.
75 *
76 * @author Werner Mayer
77 */
78 class GuiExport SelectionObserverPython: public SelectionObserverPythonHandler,
79 public SelectionObserver
80 {
81
82 public:
83 /// Constructor
84 explicit SelectionObserverPython(
85 const Py::Object& obj,
86 ResolveMode resolve = ResolveMode::OldStyleElement
87 );
88 ~SelectionObserverPython() override;
89
90 static void addObserver(const Py::Object& obj, ResolveMode resolve = ResolveMode::OldStyleElement);
91 static void removeObserver(const Py::Object& obj);
92
93 private:
94 void onSelectionChanged(const SelectionChanges& msg) override;
95
96 Py::Object inst;
97 static std::vector<SelectionObserverPython*> _instances;
98 };
99
100 } // namespace Gui
101
102 #endif // GUI_SELECTIONOBSERVERPYTHON_H