| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <sstream> |
| 26 |
#include <QEvent> |
| 27 |
#include <QFile> |
| 28 |
#include <QPointer> |
| 29 |
|
| 30 |
|
| 31 |
#include <Base/Interpreter.h> |
| 32 |
#include <Gui/Application.h> |
| 33 |
#include <Gui/BitmapFactory.h> |
| 34 |
#include <Gui/Command.h> |
| 35 |
#include <Gui/Control.h> |
| 36 |
#include <Gui/UiLoader.h> |
| 37 |
#include <Gui/PythonWrapper.h> |
| 38 |
|
| 39 |
#include "TaskDialogPython.h" |
| 40 |
#include "TaskView.h" |
| 41 |
|
| 42 |
|
| 43 |
using namespace Gui; |
| 44 |
using namespace Gui::TaskView; |
| 45 |
|
| 46 |
ControlPy* ControlPy::instance = nullptr; |
| 47 |
|
| 48 |
ControlPy* ControlPy::getInstance() |
| 49 |
{ |
| 50 |
if (!instance) { |
| 51 |
instance = new ControlPy(); |
| 52 |
} |
| 53 |
return instance; |
| 54 |
} |
| 55 |
|
| 56 |
void ControlPy::init_type() |
| 57 |
{ |
| 58 |
behaviors().name("Control"); |
| 59 |
behaviors().doc("Control for task dialogs"); |
| 60 |
// you must have overwritten the virtual functions |
| 61 |
behaviors().supportRepr(); |
| 62 |
behaviors().supportGetattr(); |
| 63 |
behaviors().supportSetattr(); |
| 64 |
add_varargs_method( |
| 65 |
"showDialog", |
| 66 |
&ControlPy::showDialog, |
| 67 |
"show the given dialog in the task panel\n" |
| 68 |
"showDialog(dialog)\n" |
| 69 |
"--\n" |
| 70 |
"if a task is already active a RuntimeError is raised" |
| 71 |
); |
| 72 |
add_varargs_method( |
| 73 |
"activeDialog", |
| 74 |
&ControlPy::activeDialog, |
| 75 |
"check if a dialog is active in the task panel\n" |
| 76 |
"activeDialog() --> bool" |
| 77 |
); |
| 78 |
add_varargs_method( |
| 79 |
"activeTaskDialog", |
| 80 |
&ControlPy::activeTaskDialog, |
| 81 |
"return the active task dialog if there is one\n" |
| 82 |
"activeTaskDialog() --> TaskDialog or None" |
| 83 |
); |
| 84 |
add_varargs_method( |
| 85 |
"closeDialog", |
| 86 |
&ControlPy::closeDialog, |
| 87 |
"close the active dialog\n" |
| 88 |
"closeDialog()" |
| 89 |
); |
| 90 |
add_varargs_method( |
| 91 |
"addTaskWatcher", |
| 92 |
&ControlPy::addTaskWatcher, |
| 93 |
"install a (list of) TaskWatcher\n" |
| 94 |
"addTaskWatcher(TaskWatcher | list)" |
| 95 |
); |
| 96 |
add_varargs_method( |
| 97 |
"clearTaskWatcher", |
| 98 |
&ControlPy::clearTaskWatcher, |
| 99 |
"remove all TaskWatchers\n" |
| 100 |
"clearTaskWatcher()" |
| 101 |
); |
| 102 |
add_varargs_method( |
| 103 |
"isAllowedAlterDocument", |
| 104 |
&ControlPy::isAllowedAlterDocument, |
| 105 |
"return the permission to alter the current Document\n" |
| 106 |
"isAllowedAlterDocument() --> bool" |
| 107 |
); |
| 108 |
add_varargs_method( |
| 109 |
"isAllowedAlterView", |
| 110 |
&ControlPy::isAllowedAlterView, |
| 111 |
"return the permission to alter the current View\n" |
| 112 |
"isAllowedAlterView() --> bool" |
| 113 |
); |
| 114 |
add_varargs_method( |
| 115 |
"isAllowedAlterSelection", |
| 116 |
&ControlPy::isAllowedAlterSelection, |
| 117 |
"return the permission to alter the current Selection\n" |
| 118 |
"isAllowedAlterSelection() --> bool" |
| 119 |
); |
| 120 |
add_varargs_method( |
| 121 |
"showTaskView", |
| 122 |
&ControlPy::showTaskView, |
| 123 |
"show the Task panel\n" |
| 124 |
"showTaskView()" |
| 125 |
); |
| 126 |
add_varargs_method( |
| 127 |
"showModelView", |
| 128 |
&ControlPy::showModelView, |
| 129 |
"show the Model panel\n" |
| 130 |
"showModelView()" |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
ControlPy::ControlPy() = default; |
| 135 |
|
| 136 |
ControlPy::~ControlPy() = default; |
| 137 |
|
| 138 |
Py::Object ControlPy::repr() |
| 139 |
{ |
| 140 |
return Py::String("Control Task Dialog"); |
| 141 |
} |
| 142 |
|
| 143 |
Py::Object ControlPy::showDialog(const Py::Tuple& args) |
| 144 |
{ |
| 145 |
PyObject* arg0; |
| 146 |
if (!PyArg_ParseTuple(args.ptr(), "O", &arg0)) { |
| 147 |
throw Py::Exception(); |
| 148 |
} |
| 149 |
Gui::TaskView::TaskDialog* act = Gui::Control().activeDialog(); |
| 150 |
if (act) { |
| 151 |
throw Py::RuntimeError("Active task dialog found"); |
| 152 |
} |
| 153 |
auto dlg = new TaskDialogPython(Py::Object(arg0)); |
| 154 |
Gui::Control().showDialog(dlg); |
| 155 |
return (Py::asObject(new TaskDialogPy(dlg))); |
| 156 |
} |
| 157 |
|
| 158 |
Py::Object ControlPy::activeDialog(const Py::Tuple& args) |
| 159 |
{ |
| 160 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 161 |
throw Py::Exception(); |
| 162 |
} |
| 163 |
Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog(); |
| 164 |
return Py::Boolean(dlg != nullptr); |
| 165 |
} |
| 166 |
|
| 167 |
Py::Object ControlPy::activeTaskDialog(const Py::Tuple& args) |
| 168 |
{ |
| 169 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 170 |
throw Py::Exception(); |
| 171 |
} |
| 172 |
Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog(); |
| 173 |
return (dlg ? Py::asObject(new TaskDialogPy(dlg)) : Py::None()); |
| 174 |
} |
| 175 |
|
| 176 |
Py::Object ControlPy::closeDialog(const Py::Tuple& args) |
| 177 |
{ |
| 178 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 179 |
throw Py::Exception(); |
| 180 |
} |
| 181 |
Gui::Control().closeDialog(); |
| 182 |
return Py::None(); |
| 183 |
} |
| 184 |
|
| 185 |
Py::Object ControlPy::addTaskWatcher(const Py::Tuple& args) |
| 186 |
{ |
| 187 |
PyObject* arg0; |
| 188 |
if (!PyArg_ParseTuple(args.ptr(), "O", &arg0)) { |
| 189 |
throw Py::Exception(); |
| 190 |
} |
| 191 |
|
| 192 |
std::vector<Gui::TaskView::TaskWatcher*> watcher; |
| 193 |
Py::Sequence list(arg0); |
| 194 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { |
| 195 |
auto w = new TaskWatcherPython(*it); |
| 196 |
watcher.push_back(w); |
| 197 |
} |
| 198 |
|
| 199 |
Gui::TaskView::TaskView* taskView = Gui::Control().taskPanel(); |
| 200 |
if (taskView) { |
| 201 |
taskView->addTaskWatcher(watcher); |
| 202 |
} |
| 203 |
return Py::None(); |
| 204 |
} |
| 205 |
|
| 206 |
Py::Object ControlPy::clearTaskWatcher(const Py::Tuple& args) |
| 207 |
{ |
| 208 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 209 |
throw Py::Exception(); |
| 210 |
} |
| 211 |
Gui::TaskView::TaskView* taskView = Gui::Control().taskPanel(); |
| 212 |
if (taskView) { |
| 213 |
taskView->clearTaskWatcher(); |
| 214 |
} |
| 215 |
return Py::None(); |
| 216 |
} |
| 217 |
|
| 218 |
Py::Object ControlPy::isAllowedAlterDocument(const Py::Tuple& args) |
| 219 |
{ |
| 220 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 221 |
throw Py::Exception(); |
| 222 |
} |
| 223 |
bool ok = Gui::Control().isAllowedAlterDocument(); |
| 224 |
return Py::Boolean(ok); |
| 225 |
} |
| 226 |
|
| 227 |
Py::Object ControlPy::isAllowedAlterView(const Py::Tuple& args) |
| 228 |
{ |
| 229 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 230 |
throw Py::Exception(); |
| 231 |
} |
| 232 |
bool ok = Gui::Control().isAllowedAlterView(); |
| 233 |
return Py::Boolean(ok); |
| 234 |
} |
| 235 |
|
| 236 |
Py::Object ControlPy::isAllowedAlterSelection(const Py::Tuple& args) |
| 237 |
{ |
| 238 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 239 |
throw Py::Exception(); |
| 240 |
} |
| 241 |
bool ok = Gui::Control().isAllowedAlterSelection(); |
| 242 |
return Py::Boolean(ok); |
| 243 |
} |
| 244 |
|
| 245 |
Py::Object ControlPy::showTaskView(const Py::Tuple& args) |
| 246 |
{ |
| 247 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 248 |
throw Py::Exception(); |
| 249 |
} |
| 250 |
Gui::Control().showTaskView(); |
| 251 |
return Py::None(); |
| 252 |
} |
| 253 |
|
| 254 |
Py::Object ControlPy::showModelView(const Py::Tuple& args) |
| 255 |
{ |
| 256 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 257 |
throw Py::Exception(); |
| 258 |
} |
| 259 |
Gui::Control().showModelView(); |
| 260 |
return Py::None(); |
| 261 |
} |
| 262 |
|
| 263 |
// ------------------------------------------------------------------ |
| 264 |
|
| 265 |
TaskWatcherPython::TaskWatcherPython(const Py::Object& o) |
| 266 |
: TaskWatcher(nullptr) |
| 267 |
, watcher(o) |
| 268 |
{ |
| 269 |
QString title; |
| 270 |
if (watcher.hasAttr(std::string("title"))) { |
| 271 |
Py::String name(watcher.getAttr(std::string("title"))); |
| 272 |
std::string s = static_cast<std::string>(name); |
| 273 |
title = QString::fromUtf8(s.c_str()); |
| 274 |
} |
| 275 |
|
| 276 |
QPixmap icon; |
| 277 |
if (watcher.hasAttr(std::string("icon"))) { |
| 278 |
Py::String name(watcher.getAttr(std::string("icon"))); |
| 279 |
std::string s = static_cast<std::string>(name); |
| 280 |
icon = BitmapFactory().pixmap(s.c_str()); |
| 281 |
} |
| 282 |
|
| 283 |
Gui::TaskView::TaskBox* tb = nullptr; |
| 284 |
if (watcher.hasAttr(std::string("commands"))) { |
| 285 |
tb = new Gui::TaskView::TaskBox(icon, title, true, nullptr); |
| 286 |
Py::Sequence cmds(watcher.getAttr(std::string("commands"))); |
| 287 |
CommandManager& mgr = Gui::Application::Instance->commandManager(); |
| 288 |
for (Py::Sequence::iterator it = cmds.begin(); it != cmds.end(); ++it) { |
| 289 |
Py::String name(*it); |
| 290 |
std::string s = static_cast<std::string>(name); |
| 291 |
Command* c = mgr.getCommandByName(s.c_str()); |
| 292 |
if (c) { |
| 293 |
c->addTo(tb); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if (watcher.hasAttr(std::string("widgets"))) { |
| 299 |
if (!tb && !title.isEmpty()) { |
| 300 |
tb = new Gui::TaskView::TaskBox(icon, title, true, nullptr); |
| 301 |
} |
| 302 |
Py::Sequence list(watcher.getAttr(std::string("widgets"))); |
| 303 |
|
| 304 |
Gui::PythonWrapper wrap; |
| 305 |
if (wrap.loadCoreModule()) { |
| 306 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { |
| 307 |
QObject* object = wrap.toQObject(*it); |
| 308 |
if (object) { |
| 309 |
QWidget* w = qobject_cast<QWidget*>(object); |
| 310 |
if (w) { |
| 311 |
if (tb) { |
| 312 |
tb->groupLayout()->addWidget(w); |
| 313 |
} |
| 314 |
else { |
| 315 |
Content.push_back(w); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
if (tb) { |
| 324 |
Content.push_back(tb); |
| 325 |
} |
| 326 |
|
| 327 |
if (watcher.hasAttr(std::string("filter"))) { |
| 328 |
Py::String name(watcher.getAttr(std::string("filter"))); |
| 329 |
std::string s = static_cast<std::string>(name); |
| 330 |
this->setFilter(s.c_str()); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
TaskWatcherPython::~TaskWatcherPython() |
| 335 |
{ |
| 336 |
std::vector<QPointer<QWidget>> guarded; |
| 337 |
guarded.insert(guarded.begin(), Content.begin(), Content.end()); |
| 338 |
Content.clear(); |
| 339 |
Base::PyGILStateLocker lock; |
| 340 |
this->watcher = Py::None(); |
| 341 |
Content.insert(Content.begin(), guarded.begin(), guarded.end()); |
| 342 |
} |
| 343 |
|
| 344 |
bool TaskWatcherPython::shouldShow() |
| 345 |
{ |
| 346 |
Base::PyGILStateLocker lock; |
| 347 |
try { |
| 348 |
if (watcher.hasAttr(std::string("shouldShow"))) { |
| 349 |
Py::Callable method(watcher.getAttr(std::string("shouldShow"))); |
| 350 |
Py::Tuple args; |
| 351 |
Py::Boolean ret(method.apply(args)); |
| 352 |
return (bool)ret; |
| 353 |
} |
| 354 |
} |
| 355 |
catch (Py::Exception&) { |
| 356 |
Base::PyException e; // extract the Python error text |
| 357 |
e.reportException(); |
| 358 |
} |
| 359 |
|
| 360 |
if (!this->Filter.empty()) { |
| 361 |
return match(); |
| 362 |
} |
| 363 |
else { |
| 364 |
return TaskWatcher::shouldShow(); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
// ------------------------------------------------------------------ |
| 369 |
|
| 370 |
void TaskDialogPy::init_type() |
| 371 |
{ |
| 372 |
behaviors().name("TaskDialog"); |
| 373 |
behaviors().doc("Task dialog"); |
| 374 |
// you must have overwritten the virtual functions |
| 375 |
behaviors().supportRepr(); |
| 376 |
behaviors().supportGetattr(); |
| 377 |
behaviors().supportSetattr(); |
| 378 |
add_varargs_method( |
| 379 |
"getDialogContent", |
| 380 |
&TaskDialogPy::getDialogContent, |
| 381 |
"Returns the widgets of the task dialog -> list" |
| 382 |
); |
| 383 |
add_varargs_method( |
| 384 |
"getStandardButtons", |
| 385 |
&TaskDialogPy::getStandardButtons, |
| 386 |
"Get the standard buttons of the box -> flags" |
| 387 |
); |
| 388 |
add_varargs_method( |
| 389 |
"setEscapeButtonEnabled", |
| 390 |
&TaskDialogPy::setEscapeButtonEnabled, |
| 391 |
"Defines whether the task dialog can be rejected by pressing Esc" |
| 392 |
); |
| 393 |
add_varargs_method( |
| 394 |
"isEscapeButtonEnabled", |
| 395 |
&TaskDialogPy::isEscapeButtonEnabled, |
| 396 |
"Checks if the task dialog can be rejected by pressing Esc -> bool" |
| 397 |
); |
| 398 |
add_varargs_method( |
| 399 |
"setAutoCloseOnTransactionChange", |
| 400 |
&TaskDialogPy::setAutoCloseOnTransactionChange, |
| 401 |
"Defines whether a task dialog must be closed if the document changes the\n" |
| 402 |
"active transaction" |
| 403 |
); |
| 404 |
add_varargs_method( |
| 405 |
"isAutoCloseOnTransactionChange", |
| 406 |
&TaskDialogPy::isAutoCloseOnTransactionChange, |
| 407 |
"Checks if the task dialog will be closed when the active transaction has changed -> bool" |
| 408 |
); |
| 409 |
add_varargs_method( |
| 410 |
"setAutoCloseOnDeletedDocument", |
| 411 |
&TaskDialogPy::setAutoCloseOnDeletedDocument, |
| 412 |
"Defines whether a task dialog must be closed if the document is deleted" |
| 413 |
); |
| 414 |
add_varargs_method( |
| 415 |
"isAutoCloseOnDeletedDocument", |
| 416 |
&TaskDialogPy::isAutoCloseOnDeletedDocument, |
| 417 |
"Checks if the task dialog will be closed if the document is deleted -> bool" |
| 418 |
); |
| 419 |
add_varargs_method( |
| 420 |
"getDocumentName", |
| 421 |
&TaskDialogPy::getDocumentName, |
| 422 |
"Get the name of the document the task dialog is attached to -> str" |
| 423 |
); |
| 424 |
add_varargs_method( |
| 425 |
"setDocumentName", |
| 426 |
&TaskDialogPy::setDocumentName, |
| 427 |
"Set the name of the document the task dialog is attached to" |
| 428 |
); |
| 429 |
add_varargs_method( |
| 430 |
"isAllowedAlterDocument", |
| 431 |
&TaskDialogPy::isAllowedAlterDocument, |
| 432 |
"Indicates whether this task dialog allows other commands to modify\n" |
| 433 |
"the document while it is open -> bool" |
| 434 |
); |
| 435 |
add_varargs_method( |
| 436 |
"isAllowedAlterView", |
| 437 |
&TaskDialogPy::isAllowedAlterView, |
| 438 |
"Indicates whether this task dialog allows other commands to modify\n" |
| 439 |
"the 3d view while it is open -> bool" |
| 440 |
); |
| 441 |
add_varargs_method( |
| 442 |
"isAllowedAlterSelection", |
| 443 |
&TaskDialogPy::isAllowedAlterSelection, |
| 444 |
"Indicates whether this task dialog allows other commands to modify\n" |
| 445 |
"the selection while it is open -> bool" |
| 446 |
); |
| 447 |
add_varargs_method( |
| 448 |
"needsFullSpace", |
| 449 |
&TaskDialogPy::needsFullSpace, |
| 450 |
"Indicates whether the task dialog fully requires the available space -> bool" |
| 451 |
); |
| 452 |
add_varargs_method("accept", &TaskDialogPy::accept, "Accept the task dialog"); |
| 453 |
add_varargs_method("reject", &TaskDialogPy::reject, "Reject the task dialog"); |
| 454 |
} |
| 455 |
|
| 456 |
TaskDialogPy::TaskDialogPy(TaskDialog* dlg) |
| 457 |
: dialog(dlg) |
| 458 |
{} |
| 459 |
|
| 460 |
TaskDialogPy::~TaskDialogPy() = default; |
| 461 |
|
| 462 |
Py::Object TaskDialogPy::repr() |
| 463 |
{ |
| 464 |
std::stringstream str; |
| 465 |
str << "<Task Dialog for '" << dialog->getDocumentName() << "' >"; |
| 466 |
return Py::String(str.str()); |
| 467 |
} |
| 468 |
|
| 469 |
Py::Object TaskDialogPy::getattr(const char* attr) |
| 470 |
{ |
| 471 |
if (!dialog) { |
| 472 |
std::ostringstream s_out; |
| 473 |
s_out << "Cannot access attribute '" << attr << "' of deleted object"; |
| 474 |
throw Py::RuntimeError(s_out.str()); |
| 475 |
} |
| 476 |
return BaseType::getattr(attr); |
| 477 |
} |
| 478 |
|
| 479 |
int TaskDialogPy::setattr(const char* attr, const Py::Object& value) |
| 480 |
{ |
| 481 |
if (!dialog) { |
| 482 |
std::ostringstream s_out; |
| 483 |
s_out << "Cannot access attribute '" << attr << "' of deleted object"; |
| 484 |
throw Py::RuntimeError(s_out.str()); |
| 485 |
} |
| 486 |
return BaseType::setattr(attr, value); |
| 487 |
} |
| 488 |
|
| 489 |
Py::Object TaskDialogPy::getDialogContent(const Py::Tuple& args) |
| 490 |
{ |
| 491 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 492 |
throw Py::Exception(); |
| 493 |
} |
| 494 |
|
| 495 |
PythonWrapper wrap; |
| 496 |
wrap.loadWidgetsModule(); |
| 497 |
|
| 498 |
Py::List list; |
| 499 |
auto widgets = dialog->getDialogContent(); |
| 500 |
for (auto it : widgets) { |
| 501 |
list.append(wrap.fromQWidget(it)); |
| 502 |
} |
| 503 |
|
| 504 |
return list; |
| 505 |
} |
| 506 |
|
| 507 |
Py::Object TaskDialogPy::getStandardButtons(const Py::Tuple& args) |
| 508 |
{ |
| 509 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 510 |
throw Py::Exception(); |
| 511 |
} |
| 512 |
auto buttons = dialog->getStandardButtons(); |
| 513 |
return Py::Long(static_cast<int>(buttons)); |
| 514 |
} |
| 515 |
|
| 516 |
Py::Object TaskDialogPy::setEscapeButtonEnabled(const Py::Tuple& args) |
| 517 |
{ |
| 518 |
Py::Boolean value(args[0]); |
| 519 |
dialog->setEscapeButtonEnabled(static_cast<bool>(value)); |
| 520 |
return Py::None(); |
| 521 |
} |
| 522 |
|
| 523 |
Py::Object TaskDialogPy::isEscapeButtonEnabled(const Py::Tuple& args) |
| 524 |
{ |
| 525 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 526 |
throw Py::Exception(); |
| 527 |
} |
| 528 |
return Py::Boolean(dialog->isEscapeButtonEnabled()); |
| 529 |
} |
| 530 |
|
| 531 |
Py::Object TaskDialogPy::setAutoCloseOnTransactionChange(const Py::Tuple& args) |
| 532 |
{ |
| 533 |
Py::Boolean value(args[0]); |
| 534 |
dialog->setAutoCloseOnTransactionChange(static_cast<bool>(value)); |
| 535 |
return Py::None(); |
| 536 |
} |
| 537 |
|
| 538 |
Py::Object TaskDialogPy::isAutoCloseOnTransactionChange(const Py::Tuple& args) |
| 539 |
{ |
| 540 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 541 |
throw Py::Exception(); |
| 542 |
} |
| 543 |
return Py::Boolean(dialog->isAutoCloseOnTransactionChange()); |
| 544 |
} |
| 545 |
|
| 546 |
Py::Object TaskDialogPy::setAutoCloseOnDeletedDocument(const Py::Tuple& args) |
| 547 |
{ |
| 548 |
Py::Boolean value(args[0]); |
| 549 |
dialog->setAutoCloseOnDeletedDocument(static_cast<bool>(value)); |
| 550 |
return Py::None(); |
| 551 |
} |
| 552 |
|
| 553 |
Py::Object TaskDialogPy::isAutoCloseOnDeletedDocument(const Py::Tuple& args) |
| 554 |
{ |
| 555 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 556 |
throw Py::Exception(); |
| 557 |
} |
| 558 |
return Py::Boolean(dialog->isAutoCloseOnDeletedDocument()); |
| 559 |
} |
| 560 |
|
| 561 |
Py::Object TaskDialogPy::getDocumentName(const Py::Tuple& args) |
| 562 |
{ |
| 563 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 564 |
throw Py::Exception(); |
| 565 |
} |
| 566 |
return Py::String(dialog->getDocumentName()); |
| 567 |
} |
| 568 |
|
| 569 |
Py::Object TaskDialogPy::setDocumentName(const Py::Tuple& args) |
| 570 |
{ |
| 571 |
const char* name {""}; |
| 572 |
if (!PyArg_ParseTuple(args.ptr(), "s", &name)) { |
| 573 |
throw Py::Exception(); |
| 574 |
} |
| 575 |
dialog->setDocumentName(name); |
| 576 |
return Py::None(); |
| 577 |
} |
| 578 |
|
| 579 |
Py::Object TaskDialogPy::isAllowedAlterDocument(const Py::Tuple& args) |
| 580 |
{ |
| 581 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 582 |
throw Py::Exception(); |
| 583 |
} |
| 584 |
return Py::Boolean(dialog->isAllowedAlterDocument()); |
| 585 |
} |
| 586 |
|
| 587 |
Py::Object TaskDialogPy::isAllowedAlterView(const Py::Tuple& args) |
| 588 |
{ |
| 589 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 590 |
throw Py::Exception(); |
| 591 |
} |
| 592 |
return Py::Boolean(dialog->isAllowedAlterView()); |
| 593 |
} |
| 594 |
|
| 595 |
Py::Object TaskDialogPy::isAllowedAlterSelection(const Py::Tuple& args) |
| 596 |
{ |
| 597 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 598 |
throw Py::Exception(); |
| 599 |
} |
| 600 |
return Py::Boolean(dialog->isAllowedAlterSelection()); |
| 601 |
} |
| 602 |
|
| 603 |
Py::Object TaskDialogPy::needsFullSpace(const Py::Tuple& args) |
| 604 |
{ |
| 605 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 606 |
throw Py::Exception(); |
| 607 |
} |
| 608 |
return Py::Boolean(dialog->needsFullSpace()); |
| 609 |
} |
| 610 |
|
| 611 |
namespace |
| 612 |
{ |
| 613 |
auto clickButton = [](QDialogButtonBox* buttonBox, QDialogButtonBox::ButtonRole role) { |
| 614 |
if (buttonBox) { |
| 615 |
QList<QAbstractButton*> list = buttonBox->buttons(); |
| 616 |
for (auto pb : list) { |
| 617 |
if (buttonBox->buttonRole(pb) == role) { |
| 618 |
if (pb->isEnabled()) { |
| 619 |
pb->click(); |
| 620 |
break; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
}; |
| 626 |
} // namespace |
| 627 |
|
| 628 |
Py::Object TaskDialogPy::accept(const Py::Tuple& args) |
| 629 |
{ |
| 630 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 631 |
throw Py::Exception(); |
| 632 |
} |
| 633 |
auto buttonBox = TaskDialogAttorney::getButtonBox(dialog); |
| 634 |
clickButton(buttonBox, QDialogButtonBox::AcceptRole); |
| 635 |
return Py::None(); |
| 636 |
} |
| 637 |
|
| 638 |
Py::Object TaskDialogPy::reject(const Py::Tuple& args) |
| 639 |
{ |
| 640 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 641 |
throw Py::Exception(); |
| 642 |
} |
| 643 |
auto buttonBox = TaskDialogAttorney::getButtonBox(dialog); |
| 644 |
clickButton(buttonBox, QDialogButtonBox::RejectRole); |
| 645 |
return Py::None(); |
| 646 |
} |
| 647 |
|
| 648 |
// ------------------------------------------------------------------ |
| 649 |
|
| 650 |
TaskDialogPython::TaskDialogPython(const Py::Object& o) |
| 651 |
: dlg(o) |
| 652 |
{ |
| 653 |
if (!tryLoadUiFile()) { |
| 654 |
tryLoadForm(); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
TaskDialogPython::~TaskDialogPython() |
| 659 |
{ |
| 660 |
std::vector<QPointer<QWidget>> guarded; |
| 661 |
guarded.insert(guarded.begin(), Content.begin(), Content.end()); |
| 662 |
Content.clear(); |
| 663 |
|
| 664 |
Base::PyGILStateLocker lock; |
| 665 |
clearForm(); |
| 666 |
|
| 667 |
// Assigning None to 'dlg' may destroy some of the stored widgets. |
| 668 |
// By guarding them with QPointer their pointers will be set to null |
| 669 |
// so that the destructor of the base class can reliably call 'delete'. |
| 670 |
Content.insert(Content.begin(), guarded.begin(), guarded.end()); |
| 671 |
} |
| 672 |
|
| 673 |
bool TaskDialogPython::tryLoadUiFile() |
| 674 |
{ |
| 675 |
if (dlg.hasAttr(std::string("ui"))) { |
| 676 |
auto loader = UiLoader::newInstance(); |
| 677 |
QString fn, icon; |
| 678 |
Py::String ui(dlg.getAttr(std::string("ui"))); |
| 679 |
std::string path = static_cast<std::string>(ui); |
| 680 |
fn = QString::fromUtf8(path.c_str()); |
| 681 |
|
| 682 |
QFile file(fn); |
| 683 |
QWidget* form = nullptr; |
| 684 |
if (file.open(QFile::ReadOnly)) { |
| 685 |
form = loader->load(&file, nullptr); |
| 686 |
} |
| 687 |
file.close(); |
| 688 |
if (form) { |
| 689 |
appendForm(form, QPixmap(icon)); |
| 690 |
} |
| 691 |
else { |
| 692 |
Base::Console().error("Failed to load UI file from '%s'\n", (const char*)fn.toUtf8()); |
| 693 |
} |
| 694 |
|
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
return false; |
| 699 |
} |
| 700 |
|
| 701 |
bool TaskDialogPython::tryLoadForm() |
| 702 |
{ |
| 703 |
if (dlg.hasAttr(std::string("form"))) { |
| 704 |
Py::Object f(dlg.getAttr(std::string("form"))); |
| 705 |
Py::List widgets; |
| 706 |
if (f.isList()) { |
| 707 |
widgets = f; |
| 708 |
} |
| 709 |
else { |
| 710 |
widgets.append(f); |
| 711 |
} |
| 712 |
|
| 713 |
Gui::PythonWrapper wrap; |
| 714 |
if (wrap.loadCoreModule()) { |
| 715 |
for (Py::List::iterator it = widgets.begin(); it != widgets.end(); ++it) { |
| 716 |
QObject* object = wrap.toQObject(*it); |
| 717 |
if (object) { |
| 718 |
QWidget* form = qobject_cast<QWidget*>(object); |
| 719 |
if (form) { |
| 720 |
appendForm(form, form->windowIcon().pixmap(32)); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
return true; |
| 727 |
} |
| 728 |
|
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
void TaskDialogPython::appendForm(QWidget* form, const QPixmap& icon) |
| 733 |
{ |
| 734 |
form->installEventFilter(this); |
| 735 |
auto taskbox = new Gui::TaskView::TaskBox(icon, form->windowTitle(), true, nullptr); |
| 736 |
taskbox->groupLayout()->addWidget(form); |
| 737 |
Content.push_back(taskbox); |
| 738 |
} |
| 739 |
|
| 740 |
void TaskDialogPython::clearForm() |
| 741 |
{ |
| 742 |
Base::PyGILStateLocker lock; |
| 743 |
try { |
| 744 |
// The widgets stored in the 'form' attribute will be deleted. |
| 745 |
// Thus, set this attribute to None to make sure that when using |
| 746 |
// the same dialog instance for a task panel won't segfault. |
| 747 |
if (this->dlg.hasAttr(std::string("form"))) { |
| 748 |
this->dlg.setAttr(std::string("form"), Py::None()); |
| 749 |
} |
| 750 |
this->dlg = Py::None(); |
| 751 |
} |
| 752 |
catch (Py::AttributeError& e) { |
| 753 |
e.clear(); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
void TaskDialogPython::open() |
| 758 |
{ |
| 759 |
Base::PyGILStateLocker lock; |
| 760 |
try { |
| 761 |
if (dlg.hasAttr(std::string("open"))) { |
| 762 |
Py::Callable method(dlg.getAttr(std::string("open"))); |
| 763 |
Py::Tuple args; |
| 764 |
method.apply(args); |
| 765 |
} |
| 766 |
} |
| 767 |
catch (Py::Exception&) { |
| 768 |
Base::PyException e; // extract the Python error text |
| 769 |
e.reportException(); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
void TaskDialogPython::clicked(int i) |
| 774 |
{ |
| 775 |
Base::PyGILStateLocker lock; |
| 776 |
try { |
| 777 |
if (dlg.hasAttr(std::string("clicked"))) { |
| 778 |
Py::Callable method(dlg.getAttr(std::string("clicked"))); |
| 779 |
PythonWrapper wrap; |
| 780 |
Py::Tuple args(1); |
| 781 |
args.setItem(0, wrap.toStandardButton(i)); |
| 782 |
method.apply(args); |
| 783 |
} |
| 784 |
} |
| 785 |
catch (Py::Exception&) { |
| 786 |
Base::PyException e; // extract the Python error text |
| 787 |
e.reportException(); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
bool TaskDialogPython::accept() |
| 792 |
{ |
| 793 |
Base::PyGILStateLocker lock; |
| 794 |
try { |
| 795 |
if (dlg.hasAttr(std::string("accept"))) { |
| 796 |
Py::Callable method(dlg.getAttr(std::string("accept"))); |
| 797 |
Py::Tuple args; |
| 798 |
Py::Boolean ret(method.apply(args)); |
| 799 |
return (bool)ret; |
| 800 |
} |
| 801 |
} |
| 802 |
catch (Py::Exception&) { |
| 803 |
Base::PyException e; // extract the Python error text |
| 804 |
e.reportException(); |
| 805 |
} |
| 806 |
|
| 807 |
return TaskDialog::accept(); |
| 808 |
} |
| 809 |
|
| 810 |
bool TaskDialogPython::reject() |
| 811 |
{ |
| 812 |
Base::PyGILStateLocker lock; |
| 813 |
try { |
| 814 |
if (dlg.hasAttr(std::string("reject"))) { |
| 815 |
Py::Callable method(dlg.getAttr(std::string("reject"))); |
| 816 |
Py::Tuple args; |
| 817 |
Py::Boolean ret(method.apply(args)); |
| 818 |
return (bool)ret; |
| 819 |
} |
| 820 |
} |
| 821 |
catch (Py::Exception&) { |
| 822 |
Base::PyException e; // extract the Python error text |
| 823 |
e.reportException(); |
| 824 |
} |
| 825 |
|
| 826 |
return TaskDialog::reject(); |
| 827 |
} |
| 828 |
|
| 829 |
void TaskDialogPython::helpRequested() |
| 830 |
{ |
| 831 |
Base::PyGILStateLocker lock; |
| 832 |
try { |
| 833 |
if (dlg.hasAttr(std::string("helpRequested"))) { |
| 834 |
Py::Callable method(dlg.getAttr(std::string("helpRequested"))); |
| 835 |
Py::Tuple args; |
| 836 |
method.apply(args); |
| 837 |
} |
| 838 |
} |
| 839 |
catch (Py::Exception&) { |
| 840 |
Base::PyException e; // extract the Python error text |
| 841 |
e.reportException(); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
bool TaskDialogPython::eventFilter(QObject* watched, QEvent* event) |
| 846 |
{ |
| 847 |
if (event->type() == QEvent::LanguageChange) { |
| 848 |
Base::PyGILStateLocker lock; |
| 849 |
try { |
| 850 |
if (dlg.hasAttr(std::string("changeEvent"))) { |
| 851 |
Py::Callable method(dlg.getAttr(std::string("changeEvent"))); |
| 852 |
Py::Tuple args {1}; |
| 853 |
args.setItem(0, Py::Long(static_cast<int>(event->type()))); |
| 854 |
method.apply(args); |
| 855 |
} |
| 856 |
} |
| 857 |
catch (Py::Exception&) { |
| 858 |
Base::PyException e; // extract the Python error text |
| 859 |
e.reportException(); |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
return TaskDialog::eventFilter(watched, event); |
| 864 |
} |
| 865 |
|
| 866 |
QDialogButtonBox::StandardButtons TaskDialogPython::getStandardButtons() const |
| 867 |
{ |
| 868 |
Base::PyGILStateLocker lock; |
| 869 |
try { |
| 870 |
if (dlg.hasAttr(std::string("getStandardButtons"))) { |
| 871 |
Py::Callable method(dlg.getAttr(std::string("getStandardButtons"))); |
| 872 |
Py::Tuple args; |
| 873 |
Gui::PythonWrapper wrap; |
| 874 |
wrap.loadWidgetsModule(); |
| 875 |
int value = wrap.toEnum(method.apply(args)); |
| 876 |
return {value}; |
| 877 |
} |
| 878 |
} |
| 879 |
catch (Py::Exception&) { |
| 880 |
Base::PyException e; // extract the Python error text |
| 881 |
e.reportException(); |
| 882 |
} |
| 883 |
|
| 884 |
return TaskDialog::getStandardButtons(); |
| 885 |
} |
| 886 |
|
| 887 |
void TaskDialogPython::modifyStandardButtons(QDialogButtonBox* buttonBox) |
| 888 |
{ |
| 889 |
Base::PyGILStateLocker lock; |
| 890 |
try { |
| 891 |
if (dlg.hasAttr(std::string("modifyStandardButtons"))) { |
| 892 |
Gui::PythonWrapper wrap; |
| 893 |
wrap.loadGuiModule(); |
| 894 |
wrap.loadWidgetsModule(); |
| 895 |
Py::Callable method(dlg.getAttr(std::string("modifyStandardButtons"))); |
| 896 |
Py::Tuple args(1); |
| 897 |
args.setItem(0, wrap.fromQWidget(buttonBox, "QDialogButtonBox")); |
| 898 |
method.apply(args); |
| 899 |
} |
| 900 |
} |
| 901 |
catch (Py::Exception&) { |
| 902 |
Base::PyException e; // extract the Python error text |
| 903 |
e.reportException(); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
bool TaskDialogPython::isAllowedAlterDocument() const |
| 908 |
{ |
| 909 |
Base::PyGILStateLocker lock; |
| 910 |
try { |
| 911 |
if (dlg.hasAttr(std::string("isAllowedAlterDocument"))) { |
| 912 |
Py::Callable method(dlg.getAttr(std::string("isAllowedAlterDocument"))); |
| 913 |
Py::Tuple args; |
| 914 |
Py::Boolean ret(method.apply(args)); |
| 915 |
return (bool)ret; |
| 916 |
} |
| 917 |
} |
| 918 |
catch (Py::Exception&) { |
| 919 |
Base::PyException e; // extract the Python error text |
| 920 |
e.reportException(); |
| 921 |
} |
| 922 |
|
| 923 |
return TaskDialog::isAllowedAlterDocument(); |
| 924 |
} |
| 925 |
|
| 926 |
bool TaskDialogPython::isAllowedAlterView() const |
| 927 |
{ |
| 928 |
Base::PyGILStateLocker lock; |
| 929 |
try { |
| 930 |
if (dlg.hasAttr(std::string("isAllowedAlterView"))) { |
| 931 |
Py::Callable method(dlg.getAttr(std::string("isAllowedAlterView"))); |
| 932 |
Py::Tuple args; |
| 933 |
Py::Boolean ret(method.apply(args)); |
| 934 |
return (bool)ret; |
| 935 |
} |
| 936 |
} |
| 937 |
catch (Py::Exception&) { |
| 938 |
Base::PyException e; // extract the Python error text |
| 939 |
e.reportException(); |
| 940 |
} |
| 941 |
|
| 942 |
return TaskDialog::isAllowedAlterView(); |
| 943 |
} |
| 944 |
|
| 945 |
bool TaskDialogPython::isAllowedAlterSelection() const |
| 946 |
{ |
| 947 |
Base::PyGILStateLocker lock; |
| 948 |
try { |
| 949 |
if (dlg.hasAttr(std::string("isAllowedAlterSelection"))) { |
| 950 |
Py::Callable method(dlg.getAttr(std::string("isAllowedAlterSelection"))); |
| 951 |
Py::Tuple args; |
| 952 |
Py::Boolean ret(method.apply(args)); |
| 953 |
return (bool)ret; |
| 954 |
} |
| 955 |
} |
| 956 |
catch (Py::Exception&) { |
| 957 |
Base::PyException e; // extract the Python error text |
| 958 |
e.reportException(); |
| 959 |
} |
| 960 |
|
| 961 |
return TaskDialog::isAllowedAlterSelection(); |
| 962 |
} |
| 963 |
|
| 964 |
bool TaskDialogPython::needsFullSpace() const |
| 965 |
{ |
| 966 |
Base::PyGILStateLocker lock; |
| 967 |
try { |
| 968 |
if (dlg.hasAttr(std::string("needsFullSpace"))) { |
| 969 |
Py::Callable method(dlg.getAttr(std::string("needsFullSpace"))); |
| 970 |
Py::Tuple args; |
| 971 |
Py::Boolean ret(method.apply(args)); |
| 972 |
return (bool)ret; |
| 973 |
} |
| 974 |
} |
| 975 |
catch (Py::Exception&) { |
| 976 |
Base::PyException e; // extract the Python error text |
| 977 |
e.reportException(); |
| 978 |
} |
| 979 |
|
| 980 |
return TaskDialog::needsFullSpace(); |
| 981 |
} |
| 982 |
|
| 983 |
void TaskDialogPython::autoClosedOnTransactionChange() |
| 984 |
{ |
| 985 |
Base::PyGILStateLocker lock; |
| 986 |
try { |
| 987 |
if (dlg.hasAttr(std::string("autoClosedOnTransactionChange"))) { |
| 988 |
Py::Callable method(dlg.getAttr(std::string("autoClosedOnTransactionChange"))); |
| 989 |
Py::Tuple args; |
| 990 |
method.apply(args); |
| 991 |
} |
| 992 |
} |
| 993 |
catch (Py::Exception&) { |
| 994 |
Base::PyException e; // extract the Python error text |
| 995 |
e.reportException(); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
void TaskDialogPython::autoClosedOnDeletedDocument() |
| 1000 |
{ |
| 1001 |
Base::PyGILStateLocker lock; |
| 1002 |
try { |
| 1003 |
if (dlg.hasAttr(std::string("autoClosedOnDeletedDocument"))) { |
| 1004 |
Py::Callable method(dlg.getAttr(std::string("autoClosedOnDeletedDocument"))); |
| 1005 |
Py::Tuple args; |
| 1006 |
method.apply(args); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
catch (Py::Exception&) { |
| 1010 |
Base::PyException e; // extract the Python error text |
| 1011 |
e.reportException(); |
| 1012 |
} |
| 1013 |
} |