FreeCAD

Форк
0
/
SelectionObserverPython.cpp 
216 строк · 7.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#include "SelectionObserverPython.h"
26
#include <Base/Interpreter.h>
27

28

29
FC_LOG_LEVEL_INIT("Selection",false,true,true)
30

31
using namespace Gui;
32

33
std::vector<SelectionObserverPython*> SelectionObserverPython::_instances;
34

35
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj, ResolveMode resolve)
36
    : SelectionObserver(true, resolve), inst(obj)
37
{
38
#undef FC_PY_ELEMENT
39
#define FC_PY_ELEMENT(_name) FC_PY_GetCallable(obj.ptr(),#_name,py_##_name);
40
    FC_PY_SEL_OBSERVER
41
}
42

43
SelectionObserverPython::~SelectionObserverPython() = default;
44

45
void SelectionObserverPython::addObserver(const Py::Object& obj, ResolveMode resolve)
46
{
47
    _instances.push_back(new SelectionObserverPython(obj, resolve));
48
}
49

50
void SelectionObserverPython::removeObserver(const Py::Object& obj)
51
{
52
    SelectionObserverPython* obs=nullptr;
53
    for (std::vector<SelectionObserverPython*>::iterator it =
54
        _instances.begin(); it != _instances.end(); ++it) {
55
        if ((*it)->inst == obj) {
56
            obs = *it;
57
            _instances.erase(it);
58
            break;
59
        }
60
    }
61

62
    delete obs;
63
}
64

65
void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg)
66
{
67
    switch (msg.Type)
68
    {
69
    case SelectionChanges::AddSelection:
70
        addSelection(msg);
71
        break;
72
    case SelectionChanges::RmvSelection:
73
        removeSelection(msg);
74
        break;
75
    case SelectionChanges::SetSelection:
76
        setSelection(msg);
77
        break;
78
    case SelectionChanges::ClrSelection:
79
        clearSelection(msg);
80
        break;
81
    case SelectionChanges::SetPreselect:
82
        setPreselection(msg);
83
        break;
84
    case SelectionChanges::RmvPreselect:
85
        removePreselection(msg);
86
        break;
87
    case SelectionChanges::PickedListChanged:
88
        pickedListChanged();
89
        break;
90
    default:
91
        break;
92
    }
93
}
94

95
void SelectionObserverPython::pickedListChanged()
96
{
97
    if(py_pickedListChanged.isNone())
98
        return;
99
    Base::PyGILStateLocker lock;
100
    try {
101
        Py::Callable(py_pickedListChanged).apply(Py::Tuple());
102
    }
103
    catch (Py::Exception&) {
104
        Base::PyException e; // extract the Python error text
105
        e.ReportException();
106
    }
107
}
108

109
void SelectionObserverPython::addSelection(const SelectionChanges& msg)
110
{
111
    if(py_addSelection.isNone())
112
        return;
113
    Base::PyGILStateLocker lock;
114
    try {
115
        Py::Tuple args(4);
116
        args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
117
        args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
118
        args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
119
        Py::Tuple tuple(3);
120
        tuple[0] = Py::Float(msg.x);
121
        tuple[1] = Py::Float(msg.y);
122
        tuple[2] = Py::Float(msg.z);
123
        args.setItem(3, tuple);
124
        Base::pyCall(py_addSelection.ptr(),args.ptr());
125
    }
126
    catch (Py::Exception&) {
127
        Base::PyException e; // extract the Python error text
128
        e.ReportException();
129
    }
130
}
131

132
void SelectionObserverPython::removeSelection(const SelectionChanges& msg)
133
{
134
    if(py_removeSelection.isNone())
135
        return;
136
    Base::PyGILStateLocker lock;
137
    try {
138
        Py::Tuple args(3);
139
        args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
140
        args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
141
        args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
142
        Base::pyCall(py_removeSelection.ptr(),args.ptr());
143
    }
144
    catch (Py::Exception&) {
145
        Base::PyException e; // extract the Python error text
146
        e.ReportException();
147
    }
148
}
149

150
void SelectionObserverPython::setSelection(const SelectionChanges& msg)
151
{
152
    if(py_setSelection.isNone())
153
        return;
154
    Base::PyGILStateLocker lock;
155
    try {
156
        Py::Tuple args(1);
157
        args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
158
        Base::pyCall(py_setSelection.ptr(),args.ptr());
159
    }
160
    catch (Py::Exception&) {
161
        Base::PyException e; // extract the Python error text
162
        e.ReportException();
163
    }
164
}
165

166
void SelectionObserverPython::clearSelection(const SelectionChanges& msg)
167
{
168
    if(py_clearSelection.isNone())
169
        return;
170
    Base::PyGILStateLocker lock;
171
    try {
172
        Py::Tuple args(1);
173
        args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
174
        Base::pyCall(py_clearSelection.ptr(),args.ptr());
175
    }
176
    catch (Py::Exception&) {
177
        Base::PyException e; // extract the Python error text
178
        e.ReportException();
179
    }
180
}
181

182
void SelectionObserverPython::setPreselection(const SelectionChanges& msg)
183
{
184
    if(py_setPreselection.isNone())
185
        return;
186
    Base::PyGILStateLocker lock;
187
    try {
188
        Py::Tuple args(3);
189
        args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
190
        args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
191
        args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
192
        Base::pyCall(py_setPreselection.ptr(),args.ptr());
193
    }
194
    catch (Py::Exception&) {
195
        Base::PyException e; // extract the Python error text
196
        e.ReportException();
197
    }
198
}
199

200
void SelectionObserverPython::removePreselection(const SelectionChanges& msg)
201
{
202
    if(py_removePreselection.isNone())
203
        return;
204
    Base::PyGILStateLocker lock;
205
    try {
206
        Py::Tuple args(3);
207
        args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
208
        args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
209
        args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
210
        Base::pyCall(py_removePreselection.ptr(),args.ptr());
211
    }
212
    catch (Py::Exception&) {
213
        Base::PyException e; // extract the Python error text
214
        e.ReportException();
215
    }
216
}
217

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.