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");
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 * , PyObject * , PyObject * )
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()
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};
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)
213
if (!PyArg_ParseTuple(args.ptr(), "O!", MDIViewPy::type_object(), &obj))
214
throw Py::Exception();
217
MDIViewPy* mdi = static_cast<MDIViewPy*>(obj);
218
_mw->removeWindow(mdi->getMDIViewPtr());