| 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 <list> |
| 25 |
|
| 26 |
|
| 27 |
#include <Base/TypePy.h> |
| 28 |
|
| 29 |
#include "DocumentPy.h" |
| 30 |
#include "MainWindowPy.h" |
| 31 |
#include "MainWindow.h" |
| 32 |
#include "MDIView.h" |
| 33 |
#include "MDIViewPy.h" |
| 34 |
#include "MDIViewPyWrap.h" |
| 35 |
#include "PythonWrapper.h" |
| 36 |
|
| 37 |
|
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
|
| 41 |
void MainWindowPy::init_type() |
| 42 |
{ |
| 43 |
behaviors().name("MainWindowPy"); |
| 44 |
behaviors().doc("Python binding class for the MainWindow class"); |
| 45 |
// you must have overwritten the virtual functions |
| 46 |
behaviors().supportRepr(); |
| 47 |
behaviors().supportGetattr(); |
| 48 |
behaviors().supportSetattr(); |
| 49 |
behaviors().set_tp_new(extension_object_new); |
| 50 |
|
| 51 |
add_varargs_method("getWindows", &MainWindowPy::getWindows, "getWindows()"); |
| 52 |
add_varargs_method("getWindowsOfType", &MainWindowPy::getWindowsOfType, "getWindowsOfType(typeid)"); |
| 53 |
add_varargs_method("setActiveWindow", &MainWindowPy::setActiveWindow, "setActiveWindow(MDIView)"); |
| 54 |
add_varargs_method("getActiveWindow", &MainWindowPy::getActiveWindow, "getActiveWindow()"); |
| 55 |
add_varargs_method("addWindow", &MainWindowPy::addWindow, "addWindow(MDIView)"); |
| 56 |
add_varargs_method("removeWindow", &MainWindowPy::removeWindow, "removeWindow(MDIView)"); |
| 57 |
add_varargs_method("showHint", &MainWindowPy::showHint, "showHint(hint)"); |
| 58 |
add_varargs_method("hideHint", &MainWindowPy::hideHint, "hideHint()"); |
| 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() || !wrap.loadGuiModule() || !wrap.loadWidgetsModule()) { |
| 84 |
throw Py::RuntimeError("Failed to load Python wrapper for Qt"); |
| 85 |
} |
| 86 |
|
| 87 |
// copy attributes |
| 88 |
static constexpr std::initializer_list<const char*> attr = { |
| 89 |
"getWindows", |
| 90 |
"getWindowsOfType", |
| 91 |
"setActiveWindow", |
| 92 |
"getActiveWindow", |
| 93 |
"addWindow", |
| 94 |
"removeWindow", |
| 95 |
"showHint", |
| 96 |
"hideHint", |
| 97 |
}; |
| 98 |
|
| 99 |
Py::Object py = wrap.fromQWidget(mw, "QMainWindow"); |
| 100 |
Py::ExtensionObject<MainWindowPy> inst(create(mw)); |
| 101 |
for (const auto& it : attr) { |
| 102 |
py.setAttr(it, inst.getAttr(it)); |
| 103 |
} |
| 104 |
return py; |
| 105 |
} |
| 106 |
|
| 107 |
MainWindowPy::MainWindowPy(MainWindow* mw) |
| 108 |
: _mw(mw) |
| 109 |
{} |
| 110 |
|
| 111 |
MainWindowPy::~MainWindowPy() |
| 112 |
{ |
| 113 |
// in case the class is instantiated on the stack |
| 114 |
ob_refcnt = 0; |
| 115 |
} |
| 116 |
|
| 117 |
Py::Object MainWindowPy::repr() |
| 118 |
{ |
| 119 |
if (!_mw) { |
| 120 |
throw Py::RuntimeError("Cannot print representation of deleted object"); |
| 121 |
} |
| 122 |
|
| 123 |
return Py::String("MainWindow"); |
| 124 |
} |
| 125 |
|
| 126 |
Py::Object MainWindowPy::getWindows(const Py::Tuple& args) |
| 127 |
{ |
| 128 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 129 |
throw Py::Exception(); |
| 130 |
} |
| 131 |
|
| 132 |
Py::List mdis; |
| 133 |
if (_mw) { |
| 134 |
QList<QWidget*> windows = _mw->windows(); |
| 135 |
for (auto it : windows) { |
| 136 |
auto view = dynamic_cast<MDIView*>(it); |
| 137 |
if (view) { |
| 138 |
mdis.append(Py::asObject(view->getPyObject())); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return mdis; |
| 144 |
} |
| 145 |
|
| 146 |
Py::Object MainWindowPy::getWindowsOfType(const Py::Tuple& args) |
| 147 |
{ |
| 148 |
PyObject* t; |
| 149 |
if (!PyArg_ParseTuple(args.ptr(), "O!", &Base::TypePy::Type, &t)) { |
| 150 |
throw Py::Exception(); |
| 151 |
} |
| 152 |
|
| 153 |
Base::Type typeId = *static_cast<Base::TypePy*>(t)->getBaseTypePtr(); |
| 154 |
|
| 155 |
Py::List mdis; |
| 156 |
if (_mw) { |
| 157 |
QList<QWidget*> windows = _mw->windows(); |
| 158 |
for (auto it : windows) { |
| 159 |
auto view = dynamic_cast<MDIView*>(it); |
| 160 |
if (view && view->isDerivedFrom(typeId)) { |
| 161 |
mdis.append(Py::asObject(view->getPyObject())); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return mdis; |
| 167 |
} |
| 168 |
|
| 169 |
Py::Object MainWindowPy::setActiveWindow(const Py::Tuple& args) |
| 170 |
{ |
| 171 |
Py::ExtensionObject<MDIViewPy> mdi(args[0].callMemberFunction("cast_to_base")); |
| 172 |
if (_mw) { |
| 173 |
_mw->setActiveWindow(mdi.extensionObject()->getMDIViewPtr()); |
| 174 |
} |
| 175 |
|
| 176 |
return Py::None(); |
| 177 |
} |
| 178 |
|
| 179 |
Py::Object MainWindowPy::getActiveWindow(const Py::Tuple& args) |
| 180 |
{ |
| 181 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 182 |
throw Py::Exception(); |
| 183 |
} |
| 184 |
|
| 185 |
if (_mw) { |
| 186 |
MDIView* mdi = _mw->activeWindow(); |
| 187 |
if (mdi) { |
| 188 |
return Py::asObject(mdi->getPyObject()); |
| 189 |
} |
| 190 |
} |
| 191 |
return Py::None(); |
| 192 |
} |
| 193 |
|
| 194 |
Py::Object MainWindowPy::addWindow(const Py::Tuple& args) |
| 195 |
{ |
| 196 |
PyObject* obj; |
| 197 |
if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) { |
| 198 |
throw Py::Exception(); |
| 199 |
} |
| 200 |
|
| 201 |
if (_mw) { |
| 202 |
Py::Object py(obj); |
| 203 |
Gui::Document* document {nullptr}; |
| 204 |
// Check if the py object has a reference to a Gui document |
| 205 |
if (py.hasAttr("document")) { |
| 206 |
Py::Object attr(py.getAttr("document")); |
| 207 |
if (PyObject_TypeCheck(attr.ptr(), &DocumentPy::Type)) { |
| 208 |
document = static_cast<DocumentPy*>(attr.ptr())->getDocumentPtr(); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
MDIViewPyWrap* mdi = new MDIViewPyWrap(py, document); |
| 213 |
_mw->addWindow(mdi); |
| 214 |
return Py::asObject(mdi->getPyObject()); |
| 215 |
} |
| 216 |
return Py::None(); |
| 217 |
} |
| 218 |
|
| 219 |
Py::Object MainWindowPy::removeWindow(const Py::Tuple& args) |
| 220 |
{ |
| 221 |
Py::ExtensionObject<MDIViewPy> mdi(args[0].callMemberFunction("cast_to_base")); |
| 222 |
if (_mw) { |
| 223 |
_mw->removeWindow(mdi.extensionObject()->getMDIViewPtr()); |
| 224 |
} |
| 225 |
return Py::None(); |
| 226 |
} |
| 227 |
|
| 228 |
Py::Object MainWindowPy::showHint(const Py::Tuple& args) |
| 229 |
{ |
| 230 |
static auto userInputFromPyObject = [](const Py::Object& object) -> InputHint::UserInput { |
| 231 |
Py::Long value(object.getAttr("value")); |
| 232 |
|
| 233 |
return static_cast<InputHint::UserInput>(value.as_long()); |
| 234 |
}; |
| 235 |
|
| 236 |
static auto inputSequenceFromPyObject = [](const Py::Object& sequence) -> InputHint::InputSequence { |
| 237 |
if (sequence.isTuple()) { |
| 238 |
Py::Tuple pyInputs(sequence); |
| 239 |
|
| 240 |
std::list<InputHint::UserInput> inputs; |
| 241 |
for (auto pyInput : pyInputs) { |
| 242 |
inputs.push_back(userInputFromPyObject(pyInput)); |
| 243 |
} |
| 244 |
|
| 245 |
return InputHint::InputSequence(inputs); |
| 246 |
} |
| 247 |
|
| 248 |
return userInputFromPyObject(sequence); |
| 249 |
}; |
| 250 |
|
| 251 |
static auto hintFromPyObject = [](const Py::Object& object) -> InputHint { |
| 252 |
Py::List pySequences = object.getAttr("sequences"); |
| 253 |
|
| 254 |
std::list<InputHint::InputSequence> sequences; |
| 255 |
for (auto pySequence : pySequences) { |
| 256 |
sequences.push_back(inputSequenceFromPyObject(pySequence)); |
| 257 |
} |
| 258 |
|
| 259 |
return InputHint { |
| 260 |
.message = QString::fromStdString(object.getAttr("message").as_string()), |
| 261 |
.sequences = sequences |
| 262 |
}; |
| 263 |
}; |
| 264 |
|
| 265 |
std::list<InputHint> hints; |
| 266 |
for (auto arg : args) { |
| 267 |
hints.push_back(hintFromPyObject(arg)); |
| 268 |
} |
| 269 |
|
| 270 |
_mw->showHints(hints); |
| 271 |
|
| 272 |
return Py::None(); |
| 273 |
} |
| 274 |
|
| 275 |
Py::Object MainWindowPy::hideHint(const Py::Tuple&) |
| 276 |
{ |
| 277 |
_mw->hideHints(); |
| 278 |
|
| 279 |
return Py::None(); |
| 280 |
} |