FreeCAD

Форк
0
/
MainWindowPy.cpp 
221 строка · 7.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2021 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

24
#include "PreCompiled.h"
25
#ifndef _PreComp_
26
# include <list>
27
#endif
28

29
#include <Base/TypePy.h>
30

31
#include "DocumentPy.h"
32
#include "MainWindowPy.h"
33
#include "MainWindow.h"
34
#include "MDIView.h"
35
#include "MDIViewPy.h"
36
#include "MDIViewPyWrap.h"
37
#include "PythonWrapper.h"
38

39

40
using namespace Gui;
41

42

43
void MainWindowPy::init_type()
44
{
45
    behaviors().name("MainWindowPy");
46
    behaviors().doc("Python binding class for the MainWindow class");
47
    // you must have overwritten the virtual functions
48
    behaviors().supportRepr();
49
    behaviors().supportGetattr();
50
    behaviors().supportSetattr();
51
    behaviors().set_tp_new(extension_object_new);
52

53
    add_varargs_method("getWindows",&MainWindowPy::getWindows,"getWindows()");
54
    add_varargs_method("getWindowsOfType",&MainWindowPy::getWindowsOfType,"getWindowsOfType(typeid)");
55
    add_varargs_method("setActiveWindow", &MainWindowPy::setActiveWindow, "setActiveWindow(MDIView)");
56
    add_varargs_method("getActiveWindow", &MainWindowPy::getActiveWindow, "getActiveWindow()");
57
    add_varargs_method("addWindow", &MainWindowPy::addWindow, "addWindow(MDIView)");
58
    add_varargs_method("removeWindow", &MainWindowPy::removeWindow, "removeWindow(MDIView)");
59
}
60

61
PyObject *MainWindowPy::extension_object_new(struct _typeobject * /*type*/, PyObject * /*args*/, PyObject * /*kwds*/)
62
{
63
    return new MainWindowPy(nullptr);
64
}
65

66
Py::Object MainWindowPy::type()
67
{
68
    return Py::Object( reinterpret_cast<PyObject *>( behaviors().type_object() ) );
69
}
70

71
Py::ExtensionObject<MainWindowPy> MainWindowPy::create(MainWindow *mw)
72
{
73
    Py::Callable class_type(type());
74
    Py::Tuple arg;
75
    auto inst = Py::ExtensionObject<MainWindowPy>(class_type.apply(arg, Py::Dict()));
76
    inst.extensionObject()->_mw = mw;
77
    return inst;
78
}
79

80
Py::Object MainWindowPy::createWrapper(MainWindow *mw)
81
{
82
    PythonWrapper wrap;
83
    if (!wrap.loadCoreModule() ||
84
        !wrap.loadGuiModule() ||
85
        !wrap.loadWidgetsModule()) {
86
        throw Py::RuntimeError("Failed to load Python wrapper for Qt");
87
    }
88

89
    // copy attributes
90
    std::list<std::string> attr = {"getWindows", "getWindowsOfType", "setActiveWindow", "getActiveWindow", "addWindow", "removeWindow"};
91

92
    Py::Object py = wrap.fromQWidget(mw, "QMainWindow");
93
    Py::ExtensionObject<MainWindowPy> inst(create(mw));
94
    for (const auto& it : attr) {
95
        py.setAttr(it, inst.getAttr(it));
96
    }
97
    return py;
98
}
99

100
MainWindowPy::MainWindowPy(MainWindow *mw)
101
  : _mw(mw)
102
{
103
}
104

105
MainWindowPy::~MainWindowPy()
106
{
107
    // in case the class is instantiated on the stack
108
    ob_refcnt = 0;
109
}
110

111
Py::Object MainWindowPy::repr()
112
{
113
    std::string s;
114
    std::ostringstream s_out;
115
    if (!_mw)
116
        throw Py::RuntimeError("Cannot print representation of deleted object");
117
    s_out << "MainWindow";
118
    return Py::String(s_out.str());
119
}
120

121
Py::Object MainWindowPy::getWindows(const Py::Tuple& args)
122
{
123
    if (!PyArg_ParseTuple(args.ptr(), ""))
124
        throw Py::Exception();
125

126
    Py::List mdis;
127
    if (_mw) {
128
        QList<QWidget*> windows = _mw->windows();
129
        for (auto it : windows) {
130
            auto view = qobject_cast<MDIView*>(it);
131
            if (view) {
132
                mdis.append(Py::asObject(view->getPyObject()));
133
            }
134
        }
135
    }
136

137
    return mdis;
138
}
139

140
Py::Object MainWindowPy::getWindowsOfType(const Py::Tuple& args)
141
{
142
    PyObject* t;
143
    if (!PyArg_ParseTuple(args.ptr(), "O!", &Base::TypePy::Type, &t))
144
        throw Py::Exception();
145

146
    Base::Type typeId = *static_cast<Base::TypePy*>(t)->getBaseTypePtr();
147

148
    Py::List mdis;
149
    if (_mw) {
150
        QList<QWidget*> windows = _mw->windows();
151
        for (auto it : windows) {
152
            auto view = qobject_cast<MDIView*>(it);
153
            if (view && view->isDerivedFrom(typeId)) {
154
                mdis.append(Py::asObject(view->getPyObject()));
155
            }
156
        }
157
    }
158

159
    return mdis;
160
}
161

162
Py::Object MainWindowPy::setActiveWindow(const Py::Tuple& args)
163
{
164
    Py::ExtensionObject<MDIViewPy> mdi(args[0].callMemberFunction("cast_to_base"));
165
    if (_mw) {
166
        _mw->setActiveWindow(mdi.extensionObject()->getMDIViewPtr());
167
    }
168

169
    return Py::None();
170
}
171

172
Py::Object MainWindowPy::getActiveWindow(const Py::Tuple& args)
173
{
174
    if (!PyArg_ParseTuple(args.ptr(), ""))
175
        throw Py::Exception();
176

177
    if (_mw) {
178
        MDIView* mdi = _mw->activeWindow();
179
        if (mdi) {
180
            return Py::asObject(mdi->getPyObject());
181
        }
182
    }
183
    return Py::None();
184
}
185

186
Py::Object MainWindowPy::addWindow(const Py::Tuple& args)
187
{
188
    PyObject* obj;
189
    if (!PyArg_ParseTuple(args.ptr(), "O", &obj))
190
        throw Py::Exception();
191

192
    if (_mw) {
193
        Py::Object py(obj);
194
        Gui::Document* document{nullptr};
195
        // Check if the py object has a reference to a Gui document
196
        if (py.hasAttr("document")) {
197
            Py::Object attr(py.getAttr("document"));
198
            if (PyObject_TypeCheck(attr.ptr(), &DocumentPy::Type)) {
199
                document = static_cast<DocumentPy*>(attr.ptr())->getDocumentPtr();
200
            }
201
        }
202

203
        MDIViewPyWrap* mdi = new MDIViewPyWrap(py, document);
204
        _mw->addWindow(mdi);
205
        return Py::asObject(mdi->getPyObject());
206
    }
207
    return Py::None();
208
}
209

210
Py::Object MainWindowPy::removeWindow(const Py::Tuple& args)
211
{
212
    PyObject* obj;
213
    if (!PyArg_ParseTuple(args.ptr(), "O!", MDIViewPy::type_object(), &obj))
214
        throw Py::Exception();
215

216
    if (_mw) {
217
        MDIViewPy* mdi = static_cast<MDIViewPy*>(obj);
218
        _mw->removeWindow(mdi->getMDIViewPtr());
219
    }
220
    return Py::None();
221
}
222

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

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

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

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