| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 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 <QApplication> |
| 25 |
#include <QVBoxLayout> |
| 26 |
|
| 27 |
#include <FCConfig.h> |
| 28 |
|
| 29 |
#ifdef FC_OS_WIN32 |
| 30 |
# undef max |
| 31 |
# undef min |
| 32 |
# ifdef _MSC_VER |
| 33 |
# pragma warning(disable : 4099) |
| 34 |
# pragma warning(disable : 4522) |
| 35 |
# endif |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <App/Application.h> |
| 39 |
#include <Base/Console.h> |
| 40 |
#include <Base/Exception.h> |
| 41 |
#include <Base/Interpreter.h> |
| 42 |
|
| 43 |
#include "WidgetFactory.h" |
| 44 |
#include "PrefWidgets.h" |
| 45 |
#include "PythonWrapper.h" |
| 46 |
#include "UiLoader.h" |
| 47 |
|
| 48 |
|
| 49 |
using namespace Gui; |
| 50 |
|
| 51 |
Gui::WidgetFactoryInst* Gui::WidgetFactoryInst::_pcSingleton = nullptr; |
| 52 |
|
| 53 |
WidgetFactoryInst& WidgetFactoryInst::instance() |
| 54 |
{ |
| 55 |
if (!_pcSingleton) { |
| 56 |
_pcSingleton = new WidgetFactoryInst; |
| 57 |
} |
| 58 |
return *_pcSingleton; |
| 59 |
} |
| 60 |
|
| 61 |
void WidgetFactoryInst::destruct() |
| 62 |
{ |
| 63 |
if (_pcSingleton) { |
| 64 |
delete _pcSingleton; |
| 65 |
} |
| 66 |
_pcSingleton = nullptr; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Creates a widget with the name \a sName which is a child of \a parent. |
| 71 |
* To create an instance of this widget once it must has been registered. |
| 72 |
* If there is no appropriate widget registered nullptr is returned. |
| 73 |
*/ |
| 74 |
QWidget* WidgetFactoryInst::createWidget(const char* sName, QWidget* parent) const |
| 75 |
{ |
| 76 |
auto w = static_cast<QWidget*>(Produce(sName)); |
| 77 |
|
| 78 |
// this widget class is not registered |
| 79 |
if (!w) { |
| 80 |
#ifdef FC_DEBUG |
| 81 |
Base::Console().warning("\"%s\" is not registered\n", sName); |
| 82 |
#else |
| 83 |
Base::Console().log("\"%s\" is not registered\n", sName); |
| 84 |
#endif |
| 85 |
return nullptr; |
| 86 |
} |
| 87 |
|
| 88 |
try { |
| 89 |
#ifdef FC_DEBUG |
| 90 |
const char* cName = qobject_cast<QWidget*>(w)->metaObject()->className(); |
| 91 |
Base::Console().log("Widget of type '%s' created.\n", cName); |
| 92 |
#endif |
| 93 |
} |
| 94 |
catch (...) { |
| 95 |
#ifdef FC_DEBUG |
| 96 |
Base::Console().error("%s does not inherit from \"QWidget\"\n", sName); |
| 97 |
#else |
| 98 |
Base::Console().log("%s does not inherit from \"QWidget\"\n", sName); |
| 99 |
#endif |
| 100 |
delete w; |
| 101 |
return nullptr; |
| 102 |
} |
| 103 |
|
| 104 |
// set the parent to the widget |
| 105 |
if (parent) { |
| 106 |
w->setParent(parent); |
| 107 |
} |
| 108 |
|
| 109 |
return w; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Creates a widget with the name \a sName which is a child of \a parent. |
| 114 |
* To create an instance of this widget once it must has been registered. |
| 115 |
* If there is no appropriate widget registered nullptr is returned. |
| 116 |
*/ |
| 117 |
Gui::Dialog::PreferencePage* WidgetFactoryInst::createPreferencePage( |
| 118 |
const char* sName, |
| 119 |
QWidget* parent |
| 120 |
) const |
| 121 |
{ |
| 122 |
auto w = (Gui::Dialog::PreferencePage*)Produce(sName); |
| 123 |
|
| 124 |
// this widget class is not registered |
| 125 |
if (!w) { |
| 126 |
#ifdef FC_DEBUG |
| 127 |
Base::Console().warning("Cannot create an instance of \"%s\"\n", sName); |
| 128 |
#else |
| 129 |
Base::Console().log("Cannot create an instance of \"%s\"\n", sName); |
| 130 |
#endif |
| 131 |
return nullptr; |
| 132 |
} |
| 133 |
|
| 134 |
if (qobject_cast<Gui::Dialog::PreferencePage*>(w)) { |
| 135 |
#ifdef FC_DEBUG |
| 136 |
Base::Console().log("Preference page of type '%s' created.\n", w->metaObject()->className()); |
| 137 |
#endif |
| 138 |
} |
| 139 |
else { |
| 140 |
#ifdef FC_DEBUG |
| 141 |
Base::Console().error("%s does not inherit from 'Gui::Dialog::PreferencePage'\n", sName); |
| 142 |
#endif |
| 143 |
delete w; |
| 144 |
return nullptr; |
| 145 |
} |
| 146 |
|
| 147 |
// set the parent to the widget |
| 148 |
if (parent) { |
| 149 |
w->setParent(parent); |
| 150 |
} |
| 151 |
|
| 152 |
return w; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Creates a preference widget with the name \a sName and the preference name \a sPref |
| 157 |
* which is a child of \a parent. |
| 158 |
* To create an instance of this widget once it must has been registered. |
| 159 |
* If there is no appropriate widget registered nullptr is returned. |
| 160 |
* After creation of this widget its recent preferences are restored automatically. |
| 161 |
*/ |
| 162 |
QWidget* WidgetFactoryInst::createPrefWidget(const char* sName, QWidget* parent, const char* sPref) |
| 163 |
{ |
| 164 |
QWidget* w = createWidget(sName); |
| 165 |
// this widget class is not registered |
| 166 |
if (!w) { |
| 167 |
return nullptr; // no valid QWidget object |
| 168 |
} |
| 169 |
|
| 170 |
// set the parent to the widget |
| 171 |
w->setParent(parent); |
| 172 |
|
| 173 |
try { |
| 174 |
auto pw = dynamic_cast<PrefWidget*>(w); |
| 175 |
if (pw) { |
| 176 |
pw->setEntryName(sPref); |
| 177 |
pw->restorePreferences(); |
| 178 |
} |
| 179 |
} |
| 180 |
catch (...) { |
| 181 |
#ifdef FC_DEBUG |
| 182 |
Base::Console().error("%s does not inherit from \"PrefWidget\"\n", w->metaObject()->className()); |
| 183 |
#endif |
| 184 |
delete w; |
| 185 |
return nullptr; |
| 186 |
} |
| 187 |
|
| 188 |
return w; |
| 189 |
} |
| 190 |
|
| 191 |
// ---------------------------------------------------- |
| 192 |
|
| 193 |
WidgetFactorySupplier* WidgetFactorySupplier::_pcSingleton = nullptr; |
| 194 |
|
| 195 |
WidgetFactorySupplier& WidgetFactorySupplier::instance() |
| 196 |
{ |
| 197 |
// not initialized? |
| 198 |
if (!_pcSingleton) { |
| 199 |
_pcSingleton = new WidgetFactorySupplier; |
| 200 |
} |
| 201 |
return *_pcSingleton; |
| 202 |
} |
| 203 |
|
| 204 |
void WidgetFactorySupplier::destruct() |
| 205 |
{ |
| 206 |
// delete the widget factory and all its producers first |
| 207 |
WidgetFactoryInst::destruct(); |
| 208 |
delete _pcSingleton; |
| 209 |
_pcSingleton = nullptr; |
| 210 |
} |
| 211 |
|
| 212 |
// ---------------------------------------------------- |
| 213 |
|
| 214 |
PrefPageUiProducer::PrefPageUiProducer(const char* filename, const char* group) |
| 215 |
: fn(QString::fromUtf8(filename)) |
| 216 |
{ |
| 217 |
WidgetFactoryInst::instance().AddProducer(filename, this); |
| 218 |
Gui::Dialog::DlgPreferencesImp::addPage(filename, group); |
| 219 |
} |
| 220 |
|
| 221 |
PrefPageUiProducer::~PrefPageUiProducer() = default; |
| 222 |
|
| 223 |
void* PrefPageUiProducer::Produce() const |
| 224 |
{ |
| 225 |
QWidget* page = new Gui::Dialog::PreferenceUiForm(fn); |
| 226 |
return static_cast<void*>(page); |
| 227 |
} |
| 228 |
|
| 229 |
// ---------------------------------------------------- |
| 230 |
|
| 231 |
PrefPagePyProducer::PrefPagePyProducer(const Py::Object& p, const char* group) |
| 232 |
: type(p) |
| 233 |
{ |
| 234 |
std::string str; |
| 235 |
Base::PyGILStateLocker lock; |
| 236 |
if (type.hasAttr("__name__")) { |
| 237 |
str = static_cast<std::string>(Py::String(type.getAttr("__name__"))); |
| 238 |
} |
| 239 |
|
| 240 |
WidgetFactoryInst::instance().AddProducer(str.c_str(), this); |
| 241 |
Gui::Dialog::DlgPreferencesImp::addPage(str, group); |
| 242 |
} |
| 243 |
|
| 244 |
PrefPagePyProducer::~PrefPagePyProducer() |
| 245 |
{ |
| 246 |
Base::PyGILStateLocker lock; |
| 247 |
type = Py::None(); |
| 248 |
} |
| 249 |
|
| 250 |
void* PrefPagePyProducer::Produce() const |
| 251 |
{ |
| 252 |
Base::PyGILStateLocker lock; |
| 253 |
try { |
| 254 |
Py::Callable method(type); |
| 255 |
Py::Tuple args; |
| 256 |
Py::Object page = method.apply(args); |
| 257 |
QWidget* widget = new Gui::Dialog::PreferencePagePython(page); |
| 258 |
if (!widget->layout()) { |
| 259 |
delete widget; |
| 260 |
widget = nullptr; |
| 261 |
} |
| 262 |
return widget; |
| 263 |
} |
| 264 |
catch (Py::Exception&) { |
| 265 |
PyErr_Print(); |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// ---------------------------------------------------- |
| 271 |
|
| 272 |
using namespace Gui::Dialog; |
| 273 |
|
| 274 |
PreferencePagePython::PreferencePagePython(const Py::Object& p, QWidget* parent) |
| 275 |
: PreferencePage(parent) |
| 276 |
, page(p) |
| 277 |
{ |
| 278 |
Base::PyGILStateLocker lock; |
| 279 |
Gui::PythonWrapper wrap; |
| 280 |
if (wrap.loadCoreModule()) { |
| 281 |
|
| 282 |
// old style class must have a form attribute while |
| 283 |
// new style classes can be the widget itself |
| 284 |
Py::Object widget; |
| 285 |
if (page.hasAttr(std::string("form"))) { |
| 286 |
widget = page.getAttr(std::string("form")); |
| 287 |
} |
| 288 |
else { |
| 289 |
widget = page; |
| 290 |
} |
| 291 |
|
| 292 |
QObject* object = wrap.toQObject(widget); |
| 293 |
if (object) { |
| 294 |
QWidget* form = qobject_cast<QWidget*>(object); |
| 295 |
if (form) { |
| 296 |
this->setWindowTitle(form->windowTitle()); |
| 297 |
auto layout = new QVBoxLayout; |
| 298 |
layout->addWidget(form); |
| 299 |
setLayout(layout); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
PreferencePagePython::~PreferencePagePython() |
| 306 |
{ |
| 307 |
Base::PyGILStateLocker lock; |
| 308 |
page = Py::None(); |
| 309 |
} |
| 310 |
|
| 311 |
void PreferencePagePython::changeEvent(QEvent* e) |
| 312 |
{ |
| 313 |
QWidget::changeEvent(e); |
| 314 |
} |
| 315 |
|
| 316 |
void PreferencePagePython::loadSettings() |
| 317 |
{ |
| 318 |
Base::PyGILStateLocker lock; |
| 319 |
try { |
| 320 |
if (page.hasAttr(std::string("loadSettings"))) { |
| 321 |
Py::Callable method(page.getAttr(std::string("loadSettings"))); |
| 322 |
Py::Tuple args; |
| 323 |
method.apply(args); |
| 324 |
} |
| 325 |
} |
| 326 |
catch (Py::Exception&) { |
| 327 |
Base::PyException e; // extract the Python error text |
| 328 |
e.reportException(); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
void PreferencePagePython::saveSettings() |
| 333 |
{ |
| 334 |
Base::PyGILStateLocker lock; |
| 335 |
try { |
| 336 |
if (page.hasAttr(std::string("saveSettings"))) { |
| 337 |
Py::Callable method(page.getAttr(std::string("saveSettings"))); |
| 338 |
Py::Tuple args; |
| 339 |
method.apply(args); |
| 340 |
} |
| 341 |
} |
| 342 |
catch (Py::Exception&) { |
| 343 |
Base::PyException e; // extract the Python error text |
| 344 |
e.reportException(); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
// ---------------------------------------------------- |
| 349 |
|
| 350 |
/* TRANSLATOR Gui::ContainerDialog */ |
| 351 |
|
| 352 |
/** |
| 353 |
* Constructs a ContainerDialog which embeds the child \a templChild. |
| 354 |
* The dialog will be modal. |
| 355 |
*/ |
| 356 |
ContainerDialog::ContainerDialog(QWidget* templChild) |
| 357 |
: QDialog(QApplication::activeWindow()) |
| 358 |
{ |
| 359 |
setModal(true); |
| 360 |
setWindowTitle(templChild->objectName()); |
| 361 |
setObjectName(templChild->objectName()); |
| 362 |
|
| 363 |
setSizeGripEnabled(true); |
| 364 |
MyDialogLayout = new QGridLayout(this); |
| 365 |
|
| 366 |
buttonOk = new QPushButton(this); |
| 367 |
buttonOk->setObjectName(QLatin1String("buttonOK")); |
| 368 |
buttonOk->setText(tr("&OK")); |
| 369 |
buttonOk->setAutoDefault(true); |
| 370 |
buttonOk->setDefault(true); |
| 371 |
|
| 372 |
MyDialogLayout->addWidget(buttonOk, 1, 0); |
| 373 |
auto spacer = new QSpacerItem(210, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); |
| 374 |
MyDialogLayout->addItem(spacer, 1, 1); |
| 375 |
|
| 376 |
buttonCancel = new QPushButton(this); |
| 377 |
buttonCancel->setObjectName(QLatin1String("buttonCancel")); |
| 378 |
buttonCancel->setText(tr("&Cancel")); |
| 379 |
buttonCancel->setAutoDefault(true); |
| 380 |
|
| 381 |
MyDialogLayout->addWidget(buttonCancel, 1, 2); |
| 382 |
|
| 383 |
templChild->setParent(this); |
| 384 |
|
| 385 |
MyDialogLayout->addWidget(templChild, 0, 0, 0, 2); |
| 386 |
resize(QSize(307, 197).expandedTo(minimumSizeHint())); |
| 387 |
|
| 388 |
// signals and slots connections |
| 389 |
connect(buttonOk, &QPushButton::clicked, this, &QDialog::accept); |
| 390 |
connect(buttonCancel, &QPushButton::clicked, this, &QDialog::reject); |
| 391 |
} |
| 392 |
|
| 393 |
/** Destroys the object and frees any allocated resources */ |
| 394 |
ContainerDialog::~ContainerDialog() = default; |
| 395 |
|
| 396 |
// ---------------------------------------------------- |
| 397 |
|
| 398 |
void PyResource::init_type() |
| 399 |
{ |
| 400 |
behaviors().name("PyResource"); |
| 401 |
behaviors().doc("PyResource"); |
| 402 |
// you must have overwritten the virtual functions |
| 403 |
behaviors().supportRepr(); |
| 404 |
behaviors().supportGetattr(); |
| 405 |
behaviors().supportSetattr(); |
| 406 |
add_varargs_method("value", &PyResource::value); |
| 407 |
add_varargs_method("setValue", &PyResource::setValue); |
| 408 |
add_varargs_method("show", &PyResource::show); |
| 409 |
add_varargs_method("connect", &PyResource::connect); |
| 410 |
} |
| 411 |
|
| 412 |
PyResource::PyResource() |
| 413 |
: myDlg(nullptr) |
| 414 |
{} |
| 415 |
|
| 416 |
PyResource::~PyResource() |
| 417 |
{ |
| 418 |
delete myDlg; |
| 419 |
for (auto it : mySignals) { |
| 420 |
SignalConnect* sc = it; |
| 421 |
delete sc; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Loads an .ui file with the name \a name. If the .ui file cannot be found or the QWidgetFactory |
| 427 |
* cannot create an instance an exception is thrown. If the created resource does not inherit from |
| 428 |
* QDialog an instance of ContainerDialog is created to embed it. |
| 429 |
*/ |
| 430 |
void PyResource::load(const char* name) |
| 431 |
{ |
| 432 |
QString fn = QString::fromUtf8(name); |
| 433 |
QFileInfo fi(fn); |
| 434 |
|
| 435 |
// checks whether it's a relative path |
| 436 |
if (fi.isRelative()) { |
| 437 |
QString cwd = QDir::currentPath(); |
| 438 |
QString home = QDir(QString::fromStdString(App::Application::getHomePath())).path(); |
| 439 |
|
| 440 |
// search in cwd and home path for the file |
| 441 |
// |
| 442 |
// file does not reside in cwd, check home path now |
| 443 |
if (!fi.exists()) { |
| 444 |
if (cwd == home) { |
| 445 |
QString what = QObject::tr("Cannot find file %1").arg(fi.absoluteFilePath()); |
| 446 |
throw Base::FileSystemError(what.toUtf8().constData()); |
| 447 |
} |
| 448 |
else { |
| 449 |
fi.setFile(QDir(home), fn); |
| 450 |
|
| 451 |
if (!fi.exists()) { |
| 452 |
QString what = QObject::tr("Cannot find file %1 neither in %2 nor in %3") |
| 453 |
.arg(fn, cwd, home); |
| 454 |
throw Base::FileSystemError(what.toUtf8().constData()); |
| 455 |
} |
| 456 |
else { |
| 457 |
fn = fi.absoluteFilePath(); // file resides in FreeCAD's home directory |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
else { |
| 463 |
if (!fi.exists()) { |
| 464 |
QString what = QObject::tr("Cannot find file %1").arg(fn); |
| 465 |
throw Base::FileSystemError(what.toUtf8().constData()); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
QWidget* w = nullptr; |
| 470 |
try { |
| 471 |
auto loader = UiLoader::newInstance(); |
| 472 |
QFile file(fn); |
| 473 |
if (file.open(QFile::ReadOnly)) { |
| 474 |
w = loader->load(&file, QApplication::activeWindow()); |
| 475 |
} |
| 476 |
file.close(); |
| 477 |
} |
| 478 |
catch (...) { |
| 479 |
throw Base::RuntimeError("Cannot create resource"); |
| 480 |
} |
| 481 |
|
| 482 |
if (!w) { |
| 483 |
throw Base::ValueError("Invalid widget."); |
| 484 |
} |
| 485 |
|
| 486 |
if (w->inherits("QDialog")) { |
| 487 |
myDlg = static_cast<QDialog*>(w); |
| 488 |
} |
| 489 |
else { |
| 490 |
myDlg = new ContainerDialog(w); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Makes a connection between the sender widget \a sender and its signal \a signal |
| 496 |
* of the created resource and Python callback function \a cb. |
| 497 |
* If the sender widget does not exist or no resource has been loaded this method returns false, |
| 498 |
* otherwise it returns true. |
| 499 |
*/ |
| 500 |
bool PyResource::connect(const char* sender, const char* signal, PyObject* cb) |
| 501 |
{ |
| 502 |
if (!myDlg) { |
| 503 |
return false; |
| 504 |
} |
| 505 |
|
| 506 |
QObject* objS = nullptr; |
| 507 |
QList<QWidget*> list = myDlg->findChildren<QWidget*>(); |
| 508 |
QList<QWidget*>::const_iterator it = list.cbegin(); |
| 509 |
QObject* obj; |
| 510 |
QString sigStr = QStringLiteral("2%1").arg(QString::fromLatin1(signal)); |
| 511 |
|
| 512 |
while (it != list.cend()) { |
| 513 |
obj = *it; |
| 514 |
++it; |
| 515 |
if (obj->objectName() == QLatin1String(sender)) { |
| 516 |
objS = obj; |
| 517 |
break; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if (objS) { |
| 522 |
auto sc = new SignalConnect(this, cb); |
| 523 |
mySignals.push_back(sc); |
| 524 |
return QObject::connect(objS, sigStr.toLatin1(), sc, SLOT(onExecute())); |
| 525 |
} |
| 526 |
else { |
| 527 |
qWarning("'%s' does not exist.\n", sender); |
| 528 |
} |
| 529 |
|
| 530 |
return false; |
| 531 |
} |
| 532 |
|
| 533 |
Py::Object PyResource::repr() |
| 534 |
{ |
| 535 |
return Py::String("Resource object"); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Searches for a widget and its value in the argument object \a args |
| 540 |
* to returns its value as Python object. |
| 541 |
* In the case it fails nullptr is returned. |
| 542 |
*/ |
| 543 |
Py::Object PyResource::value(const Py::Tuple& args) |
| 544 |
{ |
| 545 |
char* psName; |
| 546 |
char* psProperty; |
| 547 |
if (!PyArg_ParseTuple(args.ptr(), "ss", &psName, &psProperty)) { |
| 548 |
throw Py::Exception(); |
| 549 |
} |
| 550 |
|
| 551 |
QVariant v; |
| 552 |
if (myDlg) { |
| 553 |
QList<QWidget*> list = myDlg->findChildren<QWidget*>(); |
| 554 |
QList<QWidget*>::const_iterator it = list.cbegin(); |
| 555 |
QObject* obj; |
| 556 |
|
| 557 |
bool fnd = false; |
| 558 |
while (it != list.cend()) { |
| 559 |
obj = *it; |
| 560 |
++it; |
| 561 |
if (obj->objectName() == QLatin1String(psName)) { |
| 562 |
fnd = true; |
| 563 |
v = obj->property(psProperty); |
| 564 |
break; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
if (!fnd) { |
| 569 |
qWarning("'%s' not found.\n", psName); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
Py::Object item = Py::None(); |
| 574 |
switch (v.userType()) { |
| 575 |
case QMetaType::QStringList: { |
| 576 |
QStringList str = v.toStringList(); |
| 577 |
int nSize = str.count(); |
| 578 |
Py::List slist(nSize); |
| 579 |
for (int i = 0; i < nSize; ++i) { |
| 580 |
slist.setItem(i, Py::String(str[i].toLatin1())); |
| 581 |
} |
| 582 |
item = slist; |
| 583 |
} break; |
| 584 |
case QMetaType::QByteArray: |
| 585 |
break; |
| 586 |
case QMetaType::QString: |
| 587 |
item = Py::String(v.toString().toLatin1()); |
| 588 |
break; |
| 589 |
case QMetaType::Double: |
| 590 |
item = Py::Float(v.toDouble()); |
| 591 |
break; |
| 592 |
case QMetaType::Bool: |
| 593 |
item = Py::Boolean(v.toBool()); |
| 594 |
break; |
| 595 |
case QMetaType::UInt: |
| 596 |
item = Py::Long(static_cast<unsigned long>(v.toUInt())); |
| 597 |
break; |
| 598 |
case QMetaType::Int: |
| 599 |
item = Py::Long(v.toInt()); |
| 600 |
break; |
| 601 |
default: |
| 602 |
item = Py::String(""); |
| 603 |
break; |
| 604 |
} |
| 605 |
|
| 606 |
return item; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Searches for a widget, its value name and the new value in the argument object \a args |
| 611 |
* to set even this new value. |
| 612 |
* In the case it fails nullptr is returned. |
| 613 |
*/ |
| 614 |
Py::Object PyResource::setValue(const Py::Tuple& args) |
| 615 |
{ |
| 616 |
char* psName; |
| 617 |
char* psProperty; |
| 618 |
PyObject* psValue; |
| 619 |
if (!PyArg_ParseTuple(args.ptr(), "ssO", &psName, &psProperty, &psValue)) { |
| 620 |
throw Py::Exception(); |
| 621 |
} |
| 622 |
|
| 623 |
QVariant v; |
| 624 |
if (PyUnicode_Check(psValue)) { |
| 625 |
v = QString::fromUtf8(PyUnicode_AsUTF8(psValue)); |
| 626 |
} |
| 627 |
else if (PyLong_Check(psValue)) { |
| 628 |
unsigned int val = PyLong_AsLong(psValue); |
| 629 |
v = val; |
| 630 |
} |
| 631 |
else if (PyFloat_Check(psValue)) { |
| 632 |
v = PyFloat_AsDouble(psValue); |
| 633 |
} |
| 634 |
else if (PyList_Check(psValue)) { |
| 635 |
QStringList str; |
| 636 |
int nSize = PyList_Size(psValue); |
| 637 |
for (int i = 0; i < nSize; ++i) { |
| 638 |
PyObject* item = PyList_GetItem(psValue, i); |
| 639 |
if (!PyUnicode_Check(item)) { |
| 640 |
continue; |
| 641 |
} |
| 642 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 643 |
str.append(QString::fromUtf8(pItem)); |
| 644 |
} |
| 645 |
|
| 646 |
v = str; |
| 647 |
} |
| 648 |
else { |
| 649 |
throw Py::TypeError("Unsupported type"); |
| 650 |
} |
| 651 |
|
| 652 |
if (myDlg) { |
| 653 |
QList<QWidget*> list = myDlg->findChildren<QWidget*>(); |
| 654 |
QList<QWidget*>::const_iterator it = list.cbegin(); |
| 655 |
QObject* obj; |
| 656 |
|
| 657 |
bool fnd = false; |
| 658 |
while (it != list.cend()) { |
| 659 |
obj = *it; |
| 660 |
++it; |
| 661 |
if (obj->objectName() == QLatin1String(psName)) { |
| 662 |
fnd = true; |
| 663 |
obj->setProperty(psProperty, v); |
| 664 |
break; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if (!fnd) { |
| 669 |
qWarning("'%s' not found.\n", psName); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
return Py::None(); |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* If any resource has been loaded this methods shows it as a modal dialog. |
| 678 |
*/ |
| 679 |
Py::Object PyResource::show(const Py::Tuple&) |
| 680 |
{ |
| 681 |
if (myDlg) { |
| 682 |
// small trick to get focus |
| 683 |
myDlg->showMinimized(); |
| 684 |
|
| 685 |
#ifdef Q_WS_X11 |
| 686 |
// On X11 this may not work. For further information see QWidget::showMaximized |
| 687 |
// |
| 688 |
// workaround for X11 |
| 689 |
myDlg->hide(); |
| 690 |
myDlg->show(); |
| 691 |
#endif |
| 692 |
|
| 693 |
myDlg->showNormal(); |
| 694 |
myDlg->exec(); |
| 695 |
} |
| 696 |
|
| 697 |
return Py::None(); |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* Searches for the sender, the signal and the callback function to connect with |
| 702 |
* in the argument object \a args. In the case it fails nullptr is returned. |
| 703 |
*/ |
| 704 |
Py::Object PyResource::connect(const Py::Tuple& args) |
| 705 |
{ |
| 706 |
char* psSender; |
| 707 |
char* psSignal; |
| 708 |
|
| 709 |
PyObject* temp; |
| 710 |
|
| 711 |
if (PyArg_ParseTuple(args.ptr(), "ssO", &psSender, &psSignal, &temp)) { |
| 712 |
if (!PyCallable_Check(temp)) { |
| 713 |
PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 714 |
throw Py::Exception(); |
| 715 |
} |
| 716 |
|
| 717 |
Py_XINCREF(temp); /* Add a reference to new callback */ |
| 718 |
std::string sSender = psSender; |
| 719 |
std::string sSignal = psSignal; |
| 720 |
|
| 721 |
if (!connect(psSender, psSignal, temp)) { |
| 722 |
// no signal object found => dispose the callback object |
| 723 |
Py_XDECREF(temp); /* Dispose of callback */ |
| 724 |
} |
| 725 |
|
| 726 |
return Py::None(); |
| 727 |
} |
| 728 |
|
| 729 |
// error set by PyArg_ParseTuple |
| 730 |
throw Py::Exception(); |
| 731 |
} |
| 732 |
|
| 733 |
// ---------------------------------------------------- |
| 734 |
|
| 735 |
SignalConnect::SignalConnect(PyObject* res, PyObject* cb) |
| 736 |
: myResource(res) |
| 737 |
, myCallback(cb) |
| 738 |
{} |
| 739 |
|
| 740 |
SignalConnect::~SignalConnect() |
| 741 |
{ |
| 742 |
Base::PyGILStateLocker lock; |
| 743 |
Py_XDECREF(myCallback); /* Dispose of callback */ |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* Calls the callback function of the connected Python object. |
| 748 |
*/ |
| 749 |
void SignalConnect::onExecute() |
| 750 |
{ |
| 751 |
PyObject* arglist; |
| 752 |
PyObject* result; |
| 753 |
|
| 754 |
/* Time to call the callback */ |
| 755 |
arglist = Py_BuildValue("(O)", myResource); |
| 756 |
result = PyObject_CallObject(myCallback, arglist); |
| 757 |
Py_XDECREF(result); |
| 758 |
Py_DECREF(arglist); |
| 759 |
} |
| 760 |
|
| 761 |
#include "moc_WidgetFactory.cpp" |