FreeCAD

Форк
0
/
MDIViewPyWrap.cpp 
345 строк · 9.3 Кб
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
#ifndef _PreComp_
25
# include <unordered_map>
26
# include <stdexcept>
27
#endif
28

29
#include <Base/Interpreter.h>
30
#include <App/Document.h>
31

32
#include "MDIViewPyWrap.h"
33
#include "PythonWrapper.h"
34

35

36
using namespace Gui;
37
namespace sp = std::placeholders;
38

39
namespace Gui {
40

41
class MDIViewPyWrapImp
42
{
43
public:
44
    MDIViewPyWrapImp(Py::Object pyobject)
45
        : pyobject{pyobject}
46
    {
47
        Base::PyGILStateLocker lock;
48
        std::vector<std::string> methods = {"widget", "onMsg", "onHasMsg", "canClose", "printDocument", "print", "printPdf", "printPreview", "redoActions", "undoActions"};
49

50
        for (const auto& it : methods) {
51
            if (pyobject.hasAttr(it)) {
52
                func[it] = pyobject.getAttr(it);
53
            }
54
        }
55
    }
56

57
    ~MDIViewPyWrapImp()
58
    {
59
        Base::PyGILStateLocker lock;
60
        pyobject = Py::None();
61
        func.clear();
62
    }
63

64
    QWidget* widget()
65
    {
66
        Base::PyGILStateLocker lock;
67
        PythonWrapper wrap;
68
        wrap.loadWidgetsModule();
69
        if (func.count("widget") == 0) {
70
            throw Py::AttributeError("Object has no attribute 'widget'");
71
        }
72
        Py::Callable target(func.at("widget"));
73
        Py::Object pywidget(target.apply(Py::Tuple()));
74
        return qobject_cast<QWidget*>(wrap.toQObject(pywidget));
75
    }
76

77
    bool onMsg(const char* pMsg)
78
    {
79
        Base::PyGILStateLocker lock;
80
        Py::Callable target(func.at("onMsg"));
81
        Py::Boolean result(target.apply(Py::TupleN(Py::String(pMsg))));
82
        return static_cast<bool>(result);
83
    }
84

85
    bool onHasMsg(const char* pMsg)
86
    {
87
        Base::PyGILStateLocker lock;
88
        Py::Callable target(func.at("onHasMsg"));
89
        Py::Boolean result(target.apply(Py::TupleN(Py::String(pMsg))));
90
        return static_cast<bool>(result);
91
    }
92

93
    bool canClose()
94
    {
95
        Base::PyGILStateLocker lock;
96
        Py::Callable target(func.at("canClose"));
97
        Py::Boolean result(target.apply(Py::Tuple()));
98
        return static_cast<bool>(result);
99
    }
100

101
    void printDocument(QPrinter* printer)
102
    {
103
        Base::PyGILStateLocker lock;
104
        PythonWrapper wrap;
105
        wrap.loadPrintSupportModule();
106
        Py::Object pyprint = wrap.fromQPrinter(printer);
107
        Py::Callable target(func.at("printDocument"));
108
        target.apply(Py::TupleN(pyprint));
109
    }
110

111
    void print()
112
    {
113
        Base::PyGILStateLocker lock;
114
        Py::Callable target(func.at("print"));
115
        target.apply(Py::Tuple());
116
    }
117

118
    void printPdf()
119
    {
120
        Base::PyGILStateLocker lock;
121
        Py::Callable target(func.at("printPdf"));
122
        target.apply(Py::Tuple());
123
    }
124

125
    void printPreview()
126
    {
127
        Base::PyGILStateLocker lock;
128
        Py::Callable target(func.at("printPreview"));
129
        target.apply(Py::Tuple());
130
    }
131

132
    QStringList undoActions()
133
    {
134
        Base::PyGILStateLocker lock;
135
        Py::Callable target(func.at("undoActions"));
136
        Py::List list(target.apply(Py::Tuple()));
137
        QStringList actions;
138
        for (const auto& it : list) {
139
            Py::String str(it);
140
            actions << QString::fromStdString(str);
141
        }
142
        return actions;
143
    }
144

145
    QStringList redoActions()
146
    {
147
        Base::PyGILStateLocker lock;
148
        Py::Callable target(func.at("redoActions"));
149
        Py::List list(target.apply(Py::Tuple()));
150
        QStringList actions;
151
        for (const auto& it : list) {
152
            Py::String str(it);
153
            actions << QString::fromStdString(str);
154
        }
155
        return actions;
156
    }
157

158
private:
159
    std::unordered_map<std::string, Py::Object> func;
160
    Py::Object pyobject;
161
};
162

163
}
164

165

166
TYPESYSTEM_SOURCE_ABSTRACT(Gui::MDIViewPyWrap,Gui::MDIView)
167

168
MDIViewPyWrap::MDIViewPyWrap(const Py::Object& py, Gui::Document* pcDocument,QWidget* parent, Qt::WindowFlags wflags)
169
  : MDIView(pcDocument, parent, wflags)
170
  , ptr(std::make_unique<MDIViewPyWrapImp>(py))
171
{
172
    try {
173
        QWidget* widget = ptr->widget();
174
        if (widget) {
175
            setCentralWidget(widget);
176
            QString title = widget->windowTitle();
177
            if (!title.isEmpty()) {
178
                setWindowTitle(title);
179
            }
180
        }
181
    }
182
    catch (Py::Exception&) {
183
        Base::PyGILStateLocker lock;
184
        Base::PyException exc;
185
        exc.ReportException();
186
    }
187
}
188

189
MDIViewPyWrap::~MDIViewPyWrap()
190
{
191
    ptr.reset(nullptr);
192
}
193

194
PyObject* MDIViewPyWrap::getPyObject()
195
{
196
    return MDIView::getPyObject();
197
}
198

199
bool MDIViewPyWrap::onMsg(const char* pMsg,const char** ppReturn)
200
{
201
    try {
202
        if (ptr->onMsg(pMsg)) {
203
            return true;
204
        }
205
        return MDIView::onMsg(pMsg, ppReturn);
206
    }
207
    catch (const std::exception&) {
208
        return MDIView::onMsg(pMsg, ppReturn);
209
    }
210
    catch (Py::Exception&) {
211
        Base::PyGILStateLocker lock;
212
        Base::PyException exc;
213
        exc.ReportException();
214
        return false;
215
    }
216
}
217

218
bool MDIViewPyWrap::onHasMsg(const char* pMsg) const
219
{
220
    try {
221
        if (ptr->onHasMsg(pMsg)) {
222
            return true;
223
        }
224
        return MDIView::onHasMsg(pMsg);
225
    }
226
    catch (const std::exception&) {
227
        return MDIView::onHasMsg(pMsg);
228
    }
229
    catch (Py::Exception&) {
230
        Base::PyGILStateLocker lock;
231
        Base::PyException exc;
232
        exc.ReportException();
233
        return false;
234
    }
235
}
236

237
bool MDIViewPyWrap::canClose()
238
{
239
    try {
240
        return ptr->canClose();
241
    }
242
    catch (const std::exception&) {
243
        return MDIView::canClose();
244
    }
245
    catch (Py::Exception&) {
246
        Base::PyGILStateLocker lock;
247
        Base::PyException exc;
248
        exc.ReportException();
249
        return false;
250
    }
251
}
252

253
void MDIViewPyWrap::print(QPrinter* printer)
254
{
255
    try {
256
        return ptr->printDocument(printer);
257
    }
258
    catch (const std::exception&) {
259
        return MDIView::print(printer);
260
    }
261
    catch (Py::Exception&) {
262
        Base::PyGILStateLocker lock;
263
        Base::PyException exc;
264
        exc.ReportException();
265
    }
266
}
267

268
void MDIViewPyWrap::print()
269
{
270
    try {
271
        return ptr->print();
272
    }
273
    catch (const std::exception&) {
274
        return MDIView::print();
275
    }
276
    catch (Py::Exception&) {
277
        Base::PyGILStateLocker lock;
278
        Base::PyException exc;
279
        exc.ReportException();
280
    }
281
}
282

283
void MDIViewPyWrap::printPdf()
284
{
285
    try {
286
        return ptr->printPdf();
287
    }
288
    catch (const std::exception&) {
289
        return MDIView::printPdf();
290
    }
291
    catch (Py::Exception&) {
292
        Base::PyGILStateLocker lock;
293
        Base::PyException exc;
294
        exc.ReportException();
295
    }
296
}
297

298
void MDIViewPyWrap::printPreview()
299
{
300
    try {
301
        return ptr->printPreview();
302
    }
303
    catch (const std::exception&) {
304
        return MDIView::printPreview();
305
    }
306
    catch (Py::Exception&) {
307
        Base::PyGILStateLocker lock;
308
        Base::PyException exc;
309
        exc.ReportException();
310
    }
311
}
312

313
QStringList MDIViewPyWrap::undoActions() const
314
{
315
    try {
316
        return ptr->undoActions();
317
    }
318
    catch (const std::exception&) {
319
        return MDIView::undoActions();
320
    }
321
    catch (Py::Exception&) {
322
        Base::PyGILStateLocker lock;
323
        Base::PyException exc;
324
        exc.ReportException();
325
        return MDIView::undoActions();
326
    }
327
}
328

329
QStringList MDIViewPyWrap::redoActions() const
330
{
331
    try {
332
        return ptr->redoActions();
333
    }
334
    catch (const std::exception&) {
335
        return MDIView::redoActions();
336
    }
337
    catch (Py::Exception&) {
338
        Base::PyGILStateLocker lock;
339
        Base::PyException exc;
340
        exc.ReportException();
341
        return MDIView::redoActions();
342
    }
343
}
344

345
#include "moc_MDIViewPyWrap.cpp"
346

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

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

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

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