| 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 <QAction> |
| 25 |
#include <QActionGroup> |
| 26 |
#include <QCoreApplication> |
| 27 |
#include <QDir> |
| 28 |
#include <QFile> |
| 29 |
#include <QLayout> |
| 30 |
#include <QTextStream> |
| 31 |
|
| 32 |
|
| 33 |
#include <functional> |
| 34 |
#include <Base/Interpreter.h> |
| 35 |
|
| 36 |
#include "UiLoader.h" |
| 37 |
#include "PythonWrapper.h" |
| 38 |
#include "WidgetFactory.h" |
| 39 |
|
| 40 |
|
| 41 |
using namespace Gui; |
| 42 |
|
| 43 |
namespace |
| 44 |
{ |
| 45 |
|
| 46 |
QWidget* createFromWidgetFactory(const QString& className, QWidget* parent, const QString& name) |
| 47 |
{ |
| 48 |
QWidget* widget = nullptr; |
| 49 |
if (WidgetFactory().CanProduce((const char*)className.toLatin1())) { |
| 50 |
widget = WidgetFactory().createWidget((const char*)className.toLatin1(), parent); |
| 51 |
} |
| 52 |
if (widget) { |
| 53 |
widget->setObjectName(name); |
| 54 |
} |
| 55 |
return widget; |
| 56 |
} |
| 57 |
|
| 58 |
Py::Object wrapFromWidgetFactory( |
| 59 |
const Py::Tuple& args, |
| 60 |
const std::function<QWidget*(const QString&, QWidget*, const QString&)>& callableFunc |
| 61 |
) |
| 62 |
{ |
| 63 |
Gui::PythonWrapper wrap; |
| 64 |
|
| 65 |
// 1st argument |
| 66 |
Py::String str(args[0]); |
| 67 |
std::string className; |
| 68 |
className = str.as_std_string("utf-8"); |
| 69 |
// 2nd argument |
| 70 |
QWidget* parent = nullptr; |
| 71 |
if (wrap.loadCoreModule() && args.size() > 1) { |
| 72 |
QObject* object = wrap.toQObject(args[1]); |
| 73 |
if (object) { |
| 74 |
parent = qobject_cast<QWidget*>(object); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// 3rd argument |
| 79 |
std::string objectName; |
| 80 |
if (args.size() > 2) { |
| 81 |
Py::String str(args[2]); |
| 82 |
objectName = str.as_std_string("utf-8"); |
| 83 |
} |
| 84 |
|
| 85 |
QWidget* widget = callableFunc( |
| 86 |
QString::fromLatin1(className.c_str()), |
| 87 |
parent, |
| 88 |
QString::fromLatin1(objectName.c_str()) |
| 89 |
); |
| 90 |
if (!widget) { |
| 91 |
return Py::None(); |
| 92 |
// std::string err = "No such widget class '"; |
| 93 |
// err += className; |
| 94 |
// err += "'"; |
| 95 |
// throw Py::RuntimeError(err); |
| 96 |
} |
| 97 |
wrap.loadGuiModule(); |
| 98 |
wrap.loadWidgetsModule(); |
| 99 |
|
| 100 |
const char* typeName = wrap.getWrapperName(widget); |
| 101 |
return wrap.fromQWidget(widget, typeName); |
| 102 |
} |
| 103 |
|
| 104 |
} // namespace |
| 105 |
|
| 106 |
PySideUicModule::PySideUicModule() |
| 107 |
: Py::ExtensionModule<PySideUicModule>("PySideUic") |
| 108 |
{ |
| 109 |
add_varargs_method( |
| 110 |
"loadUiType", |
| 111 |
&PySideUicModule::loadUiType, |
| 112 |
"PySide lacks the \"loadUiType\" command, so we have to convert the ui file to py code " |
| 113 |
"in-memory first\n" |
| 114 |
"and then execute it in a special frame to retrieve the form_class." |
| 115 |
); |
| 116 |
add_varargs_method("loadUi", &PySideUicModule::loadUi, "Addition of \"loadUi\" to PySide."); |
| 117 |
add_varargs_method( |
| 118 |
"createCustomWidget", |
| 119 |
&PySideUicModule::createCustomWidget, |
| 120 |
"Create custom widgets." |
| 121 |
); |
| 122 |
initialize("PySideUic helper module"); // register with Python |
| 123 |
} |
| 124 |
|
| 125 |
Py::Object PySideUicModule::loadUiType(const Py::Tuple& args) |
| 126 |
{ |
| 127 |
Base::PyGILStateLocker lock; |
| 128 |
PyObject* main = PyImport_AddModule("__main__"); |
| 129 |
PyObject* dict = PyModule_GetDict(main); |
| 130 |
Py::Dict d(PyDict_Copy(dict), true); |
| 131 |
Py::String uiFile(args.getItem(0)); |
| 132 |
std::string file = uiFile.as_string(); |
| 133 |
std::replace(file.begin(), file.end(), '\\', '/'); |
| 134 |
|
| 135 |
QString cmd; |
| 136 |
QTextStream str(&cmd); |
| 137 |
// https://github.com/albop/dolo/blob/master/bin/load_ui.py |
| 138 |
str << "import pyside2uic\n" |
| 139 |
<< "from PySide import QtCore, QtGui, QtWidgets\n" |
| 140 |
<< "import xml.etree.ElementTree as xml\n" |
| 141 |
<< "try:\n" |
| 142 |
<< " from cStringIO import StringIO\n" |
| 143 |
<< "except Exception:\n" |
| 144 |
<< " from io import StringIO\n" |
| 145 |
<< "\n" |
| 146 |
<< "uiFile = \"" << file.c_str() << "\"\n" |
| 147 |
<< "parsed = xml.parse(uiFile)\n" |
| 148 |
<< "widget_class = parsed.find('widget').get('class')\n" |
| 149 |
<< "form_class = parsed.find('class').text\n" |
| 150 |
<< "with open(uiFile, 'r') as f:\n" |
| 151 |
<< " o = StringIO()\n" |
| 152 |
<< " frame = {}\n" |
| 153 |
<< " pyside2uic.compileUi(f, o, indent=0)\n" |
| 154 |
<< " pyc = compile(o.getvalue(), '<string>', 'exec')\n" |
| 155 |
<< " exec(pyc, frame)\n" |
| 156 |
<< " #Fetch the base_class and form class based on their type in the xml from designer\n" |
| 157 |
<< " form_class = frame['Ui_%s'%form_class]\n" |
| 158 |
<< " base_class = eval('QtWidgets.%s'%widget_class)\n"; |
| 159 |
|
| 160 |
PyObject* result = PyRun_String((const char*)cmd.toLatin1(), Py_file_input, d.ptr(), d.ptr()); |
| 161 |
if (result) { |
| 162 |
Py_DECREF(result); |
| 163 |
if (d.hasKey("form_class") && d.hasKey("base_class")) { |
| 164 |
Py::Tuple t(2); |
| 165 |
t.setItem(0, d.getItem("form_class")); |
| 166 |
t.setItem(1, d.getItem("base_class")); |
| 167 |
return t; |
| 168 |
} |
| 169 |
} |
| 170 |
else { |
| 171 |
throw Py::Exception(); |
| 172 |
} |
| 173 |
|
| 174 |
return Py::None(); |
| 175 |
} |
| 176 |
|
| 177 |
Py::Object PySideUicModule::loadUi(const Py::Tuple& args) |
| 178 |
{ |
| 179 |
Base::PyGILStateLocker lock; |
| 180 |
PyObject* main = PyImport_AddModule("__main__"); |
| 181 |
PyObject* dict = PyModule_GetDict(main); |
| 182 |
Py::Dict d(PyDict_Copy(dict), true); |
| 183 |
d.setItem("uiFile_", args[0]); |
| 184 |
if (args.size() > 1) { |
| 185 |
d.setItem("base_", args[1]); |
| 186 |
} |
| 187 |
else { |
| 188 |
d.setItem("base_", Py::None()); |
| 189 |
} |
| 190 |
|
| 191 |
QString cmd; |
| 192 |
QTextStream str(&cmd); |
| 193 |
|
| 194 |
str << "from PySide import QtCore, QtGui, QtWidgets\n" |
| 195 |
<< "import FreeCADGui" |
| 196 |
<< "\n" |
| 197 |
<< "loader = FreeCADGui.UiLoader()\n" |
| 198 |
<< "widget = loader.load(globals()[\"uiFile_\"])\n" |
| 199 |
<< "\n"; |
| 200 |
|
| 201 |
|
| 202 |
PyObject* result = PyRun_String((const char*)cmd.toLatin1(), Py_file_input, d.ptr(), d.ptr()); |
| 203 |
if (result) { |
| 204 |
Py_DECREF(result); |
| 205 |
if (d.hasKey("widget")) { |
| 206 |
return d.getItem("widget"); |
| 207 |
} |
| 208 |
} |
| 209 |
else { |
| 210 |
throw Py::Exception(); |
| 211 |
} |
| 212 |
|
| 213 |
return Py::None(); |
| 214 |
} |
| 215 |
|
| 216 |
Py::Object PySideUicModule::createCustomWidget(const Py::Tuple& args) |
| 217 |
{ |
| 218 |
return wrapFromWidgetFactory(args, &createFromWidgetFactory); |
| 219 |
} |
| 220 |
|
| 221 |
// ---------------------------------------------------- |
| 222 |
|
| 223 |
#if !defined(HAVE_QT_UI_TOOLS) |
| 224 |
QUiLoader::QUiLoader(QObject* parent) |
| 225 |
{ |
| 226 |
Base::PyGILStateLocker lock; |
| 227 |
PythonWrapper wrap; |
| 228 |
wrap.loadUiToolsModule(); |
| 229 |
// PyObject* module = PyImport_ImportModule("PySide2.QtUiTools"); |
| 230 |
PyObject* module = PyImport_ImportModule("freecad.UiTools"); |
| 231 |
if (module) { |
| 232 |
Py::Tuple args(1); |
| 233 |
args[0] = wrap.fromQObject(parent); |
| 234 |
Py::Module mod(module, true); |
| 235 |
uiloader = mod.callMemberFunction("QUiLoader", args); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
QUiLoader::~QUiLoader() |
| 240 |
{ |
| 241 |
Base::PyGILStateLocker lock; |
| 242 |
uiloader = Py::None(); |
| 243 |
} |
| 244 |
|
| 245 |
QStringList QUiLoader::pluginPaths() const |
| 246 |
{ |
| 247 |
Base::PyGILStateLocker lock; |
| 248 |
try { |
| 249 |
Py::List list(uiloader.callMemberFunction("pluginPaths")); |
| 250 |
QStringList paths; |
| 251 |
for (const auto& it : list) { |
| 252 |
paths << QString::fromStdString(Py::String(it).as_std_string()); |
| 253 |
} |
| 254 |
return paths; |
| 255 |
} |
| 256 |
catch (Py::Exception& e) { |
| 257 |
e.clear(); |
| 258 |
return QStringList(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void QUiLoader::clearPluginPaths() |
| 263 |
{ |
| 264 |
Base::PyGILStateLocker lock; |
| 265 |
try { |
| 266 |
uiloader.callMemberFunction("clearPluginPaths"); |
| 267 |
} |
| 268 |
catch (Py::Exception& e) { |
| 269 |
e.clear(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
void QUiLoader::addPluginPath(const QString& path) |
| 274 |
{ |
| 275 |
Base::PyGILStateLocker lock; |
| 276 |
try { |
| 277 |
Py::Tuple args(1); |
| 278 |
args[0] = Py::String(path.toStdString()); |
| 279 |
uiloader.callMemberFunction("addPluginPath", args); |
| 280 |
} |
| 281 |
catch (Py::Exception& e) { |
| 282 |
e.clear(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
QWidget* QUiLoader::load(QIODevice* device, QWidget* parentWidget) |
| 287 |
{ |
| 288 |
Base::PyGILStateLocker lock; |
| 289 |
try { |
| 290 |
PythonWrapper wrap; |
| 291 |
Py::Tuple args(2); |
| 292 |
args[0] = wrap.fromQObject(device); |
| 293 |
args[1] = wrap.fromQObject(parentWidget); |
| 294 |
Py::Object form(uiloader.callMemberFunction("load", args)); |
| 295 |
return qobject_cast<QWidget*>(wrap.toQObject(form)); |
| 296 |
} |
| 297 |
catch (Py::Exception& e) { |
| 298 |
e.clear(); |
| 299 |
return nullptr; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
QStringList QUiLoader::availableWidgets() const |
| 304 |
{ |
| 305 |
Base::PyGILStateLocker lock; |
| 306 |
try { |
| 307 |
Py::List list(uiloader.callMemberFunction("availableWidgets")); |
| 308 |
QStringList widgets; |
| 309 |
for (const auto& it : list) { |
| 310 |
widgets << QString::fromStdString(Py::String(it).as_std_string()); |
| 311 |
} |
| 312 |
return widgets; |
| 313 |
} |
| 314 |
catch (Py::Exception& e) { |
| 315 |
e.clear(); |
| 316 |
return QStringList(); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
QStringList QUiLoader::availableLayouts() const |
| 321 |
{ |
| 322 |
Base::PyGILStateLocker lock; |
| 323 |
try { |
| 324 |
Py::List list(uiloader.callMemberFunction("availableLayouts")); |
| 325 |
QStringList layouts; |
| 326 |
for (const auto& it : list) { |
| 327 |
layouts << QString::fromStdString(Py::String(it).as_std_string()); |
| 328 |
} |
| 329 |
return layouts; |
| 330 |
} |
| 331 |
catch (Py::Exception& e) { |
| 332 |
e.clear(); |
| 333 |
return QStringList(); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
QWidget* QUiLoader::createWidget(const QString& className, QWidget* parent, const QString& name) |
| 338 |
{ |
| 339 |
Base::PyGILStateLocker lock; |
| 340 |
try { |
| 341 |
PythonWrapper wrap; |
| 342 |
Py::Tuple args(3); |
| 343 |
args[0] = Py::String(className.toStdString()); |
| 344 |
args[1] = wrap.fromQObject(parent); |
| 345 |
args[2] = Py::String(name.toStdString()); |
| 346 |
Py::Object form(uiloader.callMemberFunction("createWidget", args)); |
| 347 |
return qobject_cast<QWidget*>(wrap.toQObject(form)); |
| 348 |
} |
| 349 |
catch (Py::Exception& e) { |
| 350 |
e.clear(); |
| 351 |
return nullptr; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
QLayout* QUiLoader::createLayout(const QString& className, QObject* parent, const QString& name) |
| 356 |
{ |
| 357 |
Base::PyGILStateLocker lock; |
| 358 |
try { |
| 359 |
PythonWrapper wrap; |
| 360 |
Py::Tuple args(3); |
| 361 |
args[0] = Py::String(className.toStdString()); |
| 362 |
args[1] = wrap.fromQObject(parent); |
| 363 |
args[2] = Py::String(name.toStdString()); |
| 364 |
Py::Object form(uiloader.callMemberFunction("createLayout", args)); |
| 365 |
return qobject_cast<QLayout*>(wrap.toQObject(form)); |
| 366 |
} |
| 367 |
catch (Py::Exception& e) { |
| 368 |
e.clear(); |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
QActionGroup* QUiLoader::createActionGroup(QObject* parent, const QString& name) |
| 374 |
{ |
| 375 |
Base::PyGILStateLocker lock; |
| 376 |
try { |
| 377 |
PythonWrapper wrap; |
| 378 |
Py::Tuple args(2); |
| 379 |
args[0] = wrap.fromQObject(parent); |
| 380 |
args[1] = Py::String(name.toStdString()); |
| 381 |
Py::Object action(uiloader.callMemberFunction("createActionGroup", args)); |
| 382 |
return qobject_cast<QActionGroup*>(wrap.toQObject(action)); |
| 383 |
} |
| 384 |
catch (Py::Exception& e) { |
| 385 |
e.clear(); |
| 386 |
return nullptr; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
QAction* QUiLoader::createAction(QObject* parent, const QString& name) |
| 391 |
{ |
| 392 |
Base::PyGILStateLocker lock; |
| 393 |
try { |
| 394 |
PythonWrapper wrap; |
| 395 |
Py::Tuple args(2); |
| 396 |
args[0] = wrap.fromQObject(parent); |
| 397 |
args[1] = Py::String(name.toStdString()); |
| 398 |
Py::Object action(uiloader.callMemberFunction("createAction", args)); |
| 399 |
return qobject_cast<QAction*>(wrap.toQObject(action)); |
| 400 |
} |
| 401 |
catch (Py::Exception& e) { |
| 402 |
e.clear(); |
| 403 |
return nullptr; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void QUiLoader::setWorkingDirectory(const QDir& dir) |
| 408 |
{ |
| 409 |
Base::PyGILStateLocker lock; |
| 410 |
try { |
| 411 |
PythonWrapper wrap; |
| 412 |
Py::Tuple args(1); |
| 413 |
args[0] = wrap.fromQDir(dir); |
| 414 |
uiloader.callMemberFunction("setWorkingDirectory", args); |
| 415 |
} |
| 416 |
catch (Py::Exception& e) { |
| 417 |
e.clear(); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
QDir QUiLoader::workingDirectory() const |
| 422 |
{ |
| 423 |
Base::PyGILStateLocker lock; |
| 424 |
try { |
| 425 |
PythonWrapper wrap; |
| 426 |
Py::Object dir((uiloader.callMemberFunction("workingDirectory"))); |
| 427 |
QDir* d = wrap.toQDir(dir.ptr()); |
| 428 |
if (d) { |
| 429 |
return *d; |
| 430 |
} |
| 431 |
return QDir::current(); |
| 432 |
} |
| 433 |
catch (Py::Exception& e) { |
| 434 |
e.clear(); |
| 435 |
return QDir::current(); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
void QUiLoader::setLanguageChangeEnabled(bool enabled) |
| 440 |
{ |
| 441 |
Base::PyGILStateLocker lock; |
| 442 |
try { |
| 443 |
Py::Tuple args(1); |
| 444 |
args[0] = Py::Boolean(enabled); |
| 445 |
uiloader.callMemberFunction("setLanguageChangeEnabled", args); |
| 446 |
} |
| 447 |
catch (Py::Exception& e) { |
| 448 |
e.clear(); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
bool QUiLoader::isLanguageChangeEnabled() const |
| 453 |
{ |
| 454 |
Base::PyGILStateLocker lock; |
| 455 |
try { |
| 456 |
Py::Boolean ok((uiloader.callMemberFunction("isLanguageChangeEnabled"))); |
| 457 |
return static_cast<bool>(ok); |
| 458 |
} |
| 459 |
catch (Py::Exception& e) { |
| 460 |
e.clear(); |
| 461 |
return false; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
void QUiLoader::setTranslationEnabled(bool enabled) |
| 466 |
{ |
| 467 |
Base::PyGILStateLocker lock; |
| 468 |
try { |
| 469 |
Py::Tuple args(1); |
| 470 |
args[0] = Py::Boolean(enabled); |
| 471 |
uiloader.callMemberFunction("setTranslationEnabled", args); |
| 472 |
} |
| 473 |
catch (Py::Exception& e) { |
| 474 |
e.clear(); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
bool QUiLoader::isTranslationEnabled() const |
| 479 |
{ |
| 480 |
Base::PyGILStateLocker lock; |
| 481 |
try { |
| 482 |
Py::Boolean ok((uiloader.callMemberFunction("isTranslationEnabled"))); |
| 483 |
return static_cast<bool>(ok); |
| 484 |
} |
| 485 |
catch (Py::Exception& e) { |
| 486 |
e.clear(); |
| 487 |
return false; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
QString QUiLoader::errorString() const |
| 492 |
{ |
| 493 |
Base::PyGILStateLocker lock; |
| 494 |
try { |
| 495 |
Py::String error((uiloader.callMemberFunction("errorString"))); |
| 496 |
return QString::fromStdString(error.as_std_string()); |
| 497 |
} |
| 498 |
catch (Py::Exception& e) { |
| 499 |
e.clear(); |
| 500 |
return QString(); |
| 501 |
} |
| 502 |
} |
| 503 |
#endif |
| 504 |
|
| 505 |
// ---------------------------------------------------- |
| 506 |
|
| 507 |
UiLoader::UiLoader(QObject* parent) |
| 508 |
: QUiLoader(parent) |
| 509 |
{ |
| 510 |
this->cw = availableWidgets(); |
| 511 |
setLanguageChangeEnabled(true); |
| 512 |
} |
| 513 |
|
| 514 |
std::unique_ptr<UiLoader> UiLoader::newInstance(QObject* parent) |
| 515 |
{ |
| 516 |
QCoreApplication* app = QCoreApplication::instance(); |
| 517 |
QStringList libPaths = app->libraryPaths(); |
| 518 |
|
| 519 |
app->setLibraryPaths(QStringList {}); //< backup library paths, so QUiLoader won't load plugins |
| 520 |
// by default |
| 521 |
std::unique_ptr<UiLoader> rv {new UiLoader {parent}}; |
| 522 |
app->setLibraryPaths(libPaths); |
| 523 |
|
| 524 |
return rv; |
| 525 |
} |
| 526 |
|
| 527 |
UiLoader::~UiLoader() = default; |
| 528 |
|
| 529 |
QWidget* UiLoader::createWidget(const QString& className, QWidget* parent, const QString& name) |
| 530 |
{ |
| 531 |
if (this->cw.contains(className)) { |
| 532 |
return QUiLoader::createWidget(className, parent, name); |
| 533 |
} |
| 534 |
|
| 535 |
return createFromWidgetFactory(className, parent, name); |
| 536 |
} |
| 537 |
|
| 538 |
// ---------------------------------------------------- |
| 539 |
|
| 540 |
PyObject* UiLoaderPy::PyMake(struct _typeobject* /*type*/, PyObject* args, PyObject* /*kwds*/) |
| 541 |
{ |
| 542 |
if (!PyArg_ParseTuple(args, "")) { |
| 543 |
return nullptr; |
| 544 |
} |
| 545 |
return new UiLoaderPy(); |
| 546 |
} |
| 547 |
|
| 548 |
void UiLoaderPy::init_type() |
| 549 |
{ |
| 550 |
behaviors().name("UiLoader"); |
| 551 |
behaviors().doc("UiLoader to create widgets"); |
| 552 |
behaviors().set_tp_new(PyMake); |
| 553 |
// you must have overwritten the virtual functions |
| 554 |
behaviors().supportRepr(); |
| 555 |
behaviors().supportGetattr(); |
| 556 |
behaviors().supportSetattr(); |
| 557 |
add_varargs_method( |
| 558 |
"load", |
| 559 |
&UiLoaderPy::load, |
| 560 |
"load(string, QWidget parent=None) -> QWidget\n" |
| 561 |
"load(QIODevice, QWidget parent=None) -> QWidget" |
| 562 |
); |
| 563 |
add_varargs_method("createWidget", &UiLoaderPy::createWidget, "createWidget()"); |
| 564 |
|
| 565 |
add_varargs_method("availableWidgets", &UiLoaderPy::availableWidgets, "availableWidgets()"); |
| 566 |
add_varargs_method("clearPluginPaths", &UiLoaderPy::clearPluginPaths, "clearPluginPaths()"); |
| 567 |
add_varargs_method("pluginPaths", &UiLoaderPy::pluginPaths, "pluginPaths()"); |
| 568 |
add_varargs_method("addPluginPath", &UiLoaderPy::addPluginPath, "addPluginPath()"); |
| 569 |
add_varargs_method("errorString", &UiLoaderPy::errorString, "errorString()"); |
| 570 |
add_varargs_method( |
| 571 |
"isLanguageChangeEnabled", |
| 572 |
&UiLoaderPy::isLanguageChangeEnabled, |
| 573 |
"isLanguageChangeEnabled()" |
| 574 |
); |
| 575 |
add_varargs_method( |
| 576 |
"setLanguageChangeEnabled", |
| 577 |
&UiLoaderPy::setLanguageChangeEnabled, |
| 578 |
"setLanguageChangeEnabled()" |
| 579 |
); |
| 580 |
add_varargs_method("setWorkingDirectory", &UiLoaderPy::setWorkingDirectory, "setWorkingDirectory()"); |
| 581 |
add_varargs_method("workingDirectory", &UiLoaderPy::workingDirectory, "workingDirectory()"); |
| 582 |
} |
| 583 |
|
| 584 |
UiLoaderPy::UiLoaderPy() |
| 585 |
: loader {UiLoader::newInstance()} |
| 586 |
{} |
| 587 |
|
| 588 |
UiLoaderPy::~UiLoaderPy() = default; |
| 589 |
|
| 590 |
Py::Object UiLoaderPy::repr() |
| 591 |
{ |
| 592 |
return Py::String("Ui loader"); |
| 593 |
} |
| 594 |
|
| 595 |
Py::Object UiLoaderPy::load(const Py::Tuple& args) |
| 596 |
{ |
| 597 |
Gui::PythonWrapper wrap; |
| 598 |
if (wrap.loadCoreModule()) { |
| 599 |
std::string fn; |
| 600 |
QFile file; |
| 601 |
QIODevice* device = nullptr; |
| 602 |
QWidget* parent = nullptr; |
| 603 |
if (wrap.toCString(args[0], fn)) { |
| 604 |
file.setFileName(QString::fromUtf8(fn.c_str())); |
| 605 |
if (!file.open(QFile::ReadOnly)) { |
| 606 |
throw Py::RuntimeError("Cannot open file"); |
| 607 |
} |
| 608 |
device = &file; |
| 609 |
} |
| 610 |
else if (args[0].isString()) { |
| 611 |
fn = static_cast<std::string>(Py::String(args[0])); |
| 612 |
file.setFileName(QString::fromUtf8(fn.c_str())); |
| 613 |
if (!file.open(QFile::ReadOnly)) { |
| 614 |
throw Py::RuntimeError("Cannot open file"); |
| 615 |
} |
| 616 |
device = &file; |
| 617 |
} |
| 618 |
else { |
| 619 |
QObject* obj = wrap.toQObject(args[0]); |
| 620 |
device = qobject_cast<QIODevice*>(obj); |
| 621 |
} |
| 622 |
|
| 623 |
if (args.size() > 1) { |
| 624 |
QObject* obj = wrap.toQObject(args[1]); |
| 625 |
parent = qobject_cast<QWidget*>(obj); |
| 626 |
} |
| 627 |
|
| 628 |
if (device) { |
| 629 |
QWidget* widget = loader->load(device, parent); |
| 630 |
if (widget) { |
| 631 |
wrap.loadGuiModule(); |
| 632 |
wrap.loadWidgetsModule(); |
| 633 |
|
| 634 |
const char* typeName = wrap.getWrapperName(widget); |
| 635 |
Py::Object pyWdg = wrap.fromQWidget(widget, typeName); |
| 636 |
wrap.createChildrenNameAttributes(*pyWdg, widget); |
| 637 |
wrap.setParent(*pyWdg, parent); |
| 638 |
return pyWdg; |
| 639 |
} |
| 640 |
} |
| 641 |
else { |
| 642 |
throw Py::TypeError("string or QIODevice expected"); |
| 643 |
} |
| 644 |
} |
| 645 |
return Py::None(); |
| 646 |
} |
| 647 |
|
| 648 |
Py::Object UiLoaderPy::createWidget(const Py::Tuple& args) |
| 649 |
{ |
| 650 |
// NOLINTBEGIN |
| 651 |
return wrapFromWidgetFactory( |
| 652 |
args, |
| 653 |
std::bind( |
| 654 |
&UiLoader::createWidget, |
| 655 |
loader.get(), |
| 656 |
std::placeholders::_1, |
| 657 |
std::placeholders::_2, |
| 658 |
std::placeholders::_3 |
| 659 |
) |
| 660 |
); |
| 661 |
// NOLINTEND |
| 662 |
} |
| 663 |
|
| 664 |
Py::Object UiLoaderPy::addPluginPath(const Py::Tuple& args) |
| 665 |
{ |
| 666 |
Gui::PythonWrapper wrap; |
| 667 |
if (wrap.loadCoreModule()) { |
| 668 |
std::string fn; |
| 669 |
if (wrap.toCString(args[0], fn)) { |
| 670 |
loader->addPluginPath(QString::fromStdString(fn)); |
| 671 |
} |
| 672 |
} |
| 673 |
return Py::None(); |
| 674 |
} |
| 675 |
|
| 676 |
Py::Object UiLoaderPy::clearPluginPaths(const Py::Tuple& /*args*/) |
| 677 |
{ |
| 678 |
loader->clearPluginPaths(); |
| 679 |
return Py::None(); |
| 680 |
} |
| 681 |
|
| 682 |
Py::Object UiLoaderPy::pluginPaths(const Py::Tuple& /*args*/) |
| 683 |
{ |
| 684 |
auto list = loader->pluginPaths(); |
| 685 |
Py::List py; |
| 686 |
for (const auto& it : list) { |
| 687 |
py.append(Py::String(it.toStdString())); |
| 688 |
} |
| 689 |
return py; |
| 690 |
} |
| 691 |
|
| 692 |
Py::Object UiLoaderPy::availableWidgets(const Py::Tuple& /*args*/) |
| 693 |
{ |
| 694 |
auto list = loader->availableWidgets(); |
| 695 |
Py::List py; |
| 696 |
for (const auto& it : list) { |
| 697 |
py.append(Py::String(it.toStdString())); |
| 698 |
} |
| 699 |
|
| 700 |
auto producer = WidgetFactory().CanProduce(); |
| 701 |
for (const auto& it : producer) { |
| 702 |
py.append(Py::String(it)); |
| 703 |
} |
| 704 |
|
| 705 |
return py; |
| 706 |
} |
| 707 |
|
| 708 |
Py::Object UiLoaderPy::errorString(const Py::Tuple& /*args*/) |
| 709 |
{ |
| 710 |
return Py::String(loader->errorString().toStdString()); |
| 711 |
} |
| 712 |
|
| 713 |
Py::Object UiLoaderPy::isLanguageChangeEnabled(const Py::Tuple& /*args*/) |
| 714 |
{ |
| 715 |
return Py::Boolean(loader->isLanguageChangeEnabled()); |
| 716 |
} |
| 717 |
|
| 718 |
Py::Object UiLoaderPy::setLanguageChangeEnabled(const Py::Tuple& args) |
| 719 |
{ |
| 720 |
loader->setLanguageChangeEnabled(Py::Boolean(args[0])); |
| 721 |
return Py::None(); |
| 722 |
} |
| 723 |
|
| 724 |
Py::Object UiLoaderPy::setWorkingDirectory(const Py::Tuple& args) |
| 725 |
{ |
| 726 |
Gui::PythonWrapper wrap; |
| 727 |
if (wrap.loadCoreModule()) { |
| 728 |
std::string fn; |
| 729 |
if (wrap.toCString(args[0], fn)) { |
| 730 |
loader->setWorkingDirectory(QString::fromStdString(fn)); |
| 731 |
} |
| 732 |
} |
| 733 |
return Py::None(); |
| 734 |
} |
| 735 |
|
| 736 |
Py::Object UiLoaderPy::workingDirectory(const Py::Tuple& /*args*/) |
| 737 |
{ |
| 738 |
QDir dir = loader->workingDirectory(); |
| 739 |
QString path = dir.absolutePath(); |
| 740 |
return Py::String(path.toStdString()); |
| 741 |
} |
| 742 |
|
| 743 |
#if !defined(HAVE_QT_UI_TOOLS) |
| 744 |
# include "moc_UiLoader.cpp" |
| 745 |
#endif |