1
/***************************************************************************
2
* Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
24
#include "PreCompiled.h"
29
#include <Base/TypePy.h>
31
#include "DocumentPy.h"
32
#include "MainWindowPy.h"
33
#include "MainWindow.h"
36
#include "MDIViewPyWrap.h"
37
#include "PythonWrapper.h"
43
void MainWindowPy::init_type()
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);
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)");
61
PyObject *MainWindowPy::extension_object_new(struct _typeobject * /*type*/, PyObject * /*args*/, PyObject * /*kwds*/)
63
return new MainWindowPy(nullptr);
66
Py::Object MainWindowPy::type()
68
return Py::Object( reinterpret_cast<PyObject *>( behaviors().type_object() ) );
71
Py::ExtensionObject<MainWindowPy> MainWindowPy::create(MainWindow *mw)
73
Py::Callable class_type(type());
75
auto inst = Py::ExtensionObject<MainWindowPy>(class_type.apply(arg, Py::Dict()));
76
inst.extensionObject()->_mw = mw;
80
Py::Object MainWindowPy::createWrapper(MainWindow *mw)
83
if (!wrap.loadCoreModule() ||
84
!wrap.loadGuiModule() ||
85
!wrap.loadWidgetsModule()) {
86
throw Py::RuntimeError("Failed to load Python wrapper for Qt");
90
std::list<std::string> attr = {"getWindows", "getWindowsOfType", "setActiveWindow", "getActiveWindow", "addWindow", "removeWindow"};
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));
100
MainWindowPy::MainWindowPy(MainWindow *mw)
105
MainWindowPy::~MainWindowPy()
107
// in case the class is instantiated on the stack
111
Py::Object MainWindowPy::repr()
114
std::ostringstream s_out;
116
throw Py::RuntimeError("Cannot print representation of deleted object");
117
s_out << "MainWindow";
118
return Py::String(s_out.str());
121
Py::Object MainWindowPy::getWindows(const Py::Tuple& args)
123
if (!PyArg_ParseTuple(args.ptr(), ""))
124
throw Py::Exception();
128
QList<QWidget*> windows = _mw->windows();
129
for (auto it : windows) {
130
auto view = qobject_cast<MDIView*>(it);
132
mdis.append(Py::asObject(view->getPyObject()));
140
Py::Object MainWindowPy::getWindowsOfType(const Py::Tuple& args)
143
if (!PyArg_ParseTuple(args.ptr(), "O!", &Base::TypePy::Type, &t))
144
throw Py::Exception();
146
Base::Type typeId = *static_cast<Base::TypePy*>(t)->getBaseTypePtr();
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()));
162
Py::Object MainWindowPy::setActiveWindow(const Py::Tuple& args)
164
Py::ExtensionObject<MDIViewPy> mdi(args[0].callMemberFunction("cast_to_base"));
166
_mw->setActiveWindow(mdi.extensionObject()->getMDIViewPtr());
172
Py::Object MainWindowPy::getActiveWindow(const Py::Tuple& args)
174
if (!PyArg_ParseTuple(args.ptr(), ""))
175
throw Py::Exception();
178
MDIView* mdi = _mw->activeWindow();
180
return Py::asObject(mdi->getPyObject());
186
Py::Object MainWindowPy::addWindow(const Py::Tuple& args)
189
if (!PyArg_ParseTuple(args.ptr(), "O", &obj))
190
throw Py::Exception();
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();
203
MDIViewPyWrap* mdi = new MDIViewPyWrap(py, document);
205
return Py::asObject(mdi->getPyObject());
210
Py::Object MainWindowPy::removeWindow(const Py::Tuple& args)
212
Py::ExtensionObject<MDIViewPy> mdi(args[0].callMemberFunction("cast_to_base"));
214
_mw->removeWindow(mdi.extensionObject()->getMDIViewPtr());