| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2019 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 <App/DocumentObject.h> |
| 25 |
#include <App/DocumentObjectPy.h> |
| 26 |
#include <Base/Exception.h> |
| 27 |
|
| 28 |
#include "MDIViewPy.h" |
| 29 |
#include "MDIView.h" |
| 30 |
|
| 31 |
|
| 32 |
using namespace Gui; |
| 33 |
|
| 34 |
|
| 35 |
void MDIViewPy::init_type() |
| 36 |
{ |
| 37 |
behaviors().name("MDIViewPy"); |
| 38 |
behaviors().doc("Python binding class for the MDI view class"); |
| 39 |
// you must have overwritten the virtual functions |
| 40 |
behaviors().supportRepr(); |
| 41 |
behaviors().supportGetattr(); |
| 42 |
behaviors().supportSetattr(); |
| 43 |
behaviors().set_tp_new(extension_object_new); |
| 44 |
|
| 45 |
add_varargs_method("printView", &MDIViewPy::printView, "printView()"); |
| 46 |
add_varargs_method("printPdf", &MDIViewPy::printPdf, "printPdf()"); |
| 47 |
add_varargs_method("printPreview", &MDIViewPy::printPreview, "printPreview()"); |
| 48 |
|
| 49 |
add_varargs_method("undoActions", &MDIViewPy::undoActions, "undoActions()"); |
| 50 |
add_varargs_method("redoActions", &MDIViewPy::redoActions, "redoActions()"); |
| 51 |
|
| 52 |
add_varargs_method("message", &MDIViewPy::sendMessage, "deprecated: use sendMessage"); |
| 53 |
add_varargs_method("sendMessage", &MDIViewPy::sendMessage, "sendMessage(str)"); |
| 54 |
add_varargs_method("supportMessage", &MDIViewPy::supportMessage, "supportMessage(str)"); |
| 55 |
add_varargs_method("fitAll", &MDIViewPy::fitAll, "fitAll()"); |
| 56 |
add_varargs_method( |
| 57 |
"setActiveObject", |
| 58 |
&MDIViewPy::setActiveObject, |
| 59 |
"setActiveObject(name,object,subname=None)\nadd or set a new active object" |
| 60 |
); |
| 61 |
add_varargs_method( |
| 62 |
"getActiveObject", |
| 63 |
&MDIViewPy::getActiveObject, |
| 64 |
"getActiveObject(name,resolve=True)\nreturns the active object for the given type" |
| 65 |
); |
| 66 |
add_varargs_method("cast_to_base", &MDIViewPy::cast_to_base, "cast_to_base() cast to MDIView class"); |
| 67 |
} |
| 68 |
|
| 69 |
PyObject* MDIViewPy::extension_object_new(struct _typeobject* /*type*/, PyObject* /*args*/, PyObject* /*kwds*/) |
| 70 |
{ |
| 71 |
return new MDIViewPy(nullptr); |
| 72 |
} |
| 73 |
|
| 74 |
Py::Object MDIViewPy::type() |
| 75 |
{ |
| 76 |
return Py::Object(reinterpret_cast<PyObject*>(behaviors().type_object())); |
| 77 |
} |
| 78 |
|
| 79 |
Py::ExtensionObject<MDIViewPy> MDIViewPy::create(MDIView* mdi) |
| 80 |
{ |
| 81 |
Py::Callable class_type(type()); |
| 82 |
Py::Tuple arg; |
| 83 |
auto inst = Py::ExtensionObject<MDIViewPy>(class_type.apply(arg, Py::Dict())); |
| 84 |
inst.extensionObject()->_view = mdi; |
| 85 |
return inst; |
| 86 |
} |
| 87 |
|
| 88 |
MDIViewPy::MDIViewPy(MDIView* mdi) |
| 89 |
: _view(mdi) |
| 90 |
{} |
| 91 |
|
| 92 |
MDIViewPy::~MDIViewPy() |
| 93 |
{ |
| 94 |
// in case the class is instantiated on the stack |
| 95 |
ob_refcnt = 0; |
| 96 |
} |
| 97 |
|
| 98 |
Py::Object MDIViewPy::repr() |
| 99 |
{ |
| 100 |
if (!_view) { |
| 101 |
throw Py::RuntimeError("Cannot print representation of deleted object"); |
| 102 |
} |
| 103 |
|
| 104 |
return Py::String(_view->getTypeId().getName()); |
| 105 |
} |
| 106 |
|
| 107 |
Py::Object MDIViewPy::printView(const Py::Tuple& args) |
| 108 |
{ |
| 109 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 110 |
throw Py::Exception(); |
| 111 |
} |
| 112 |
|
| 113 |
if (_view) { |
| 114 |
_view->print(); |
| 115 |
} |
| 116 |
|
| 117 |
return Py::None(); |
| 118 |
} |
| 119 |
|
| 120 |
Py::Object MDIViewPy::printPdf(const Py::Tuple& args) |
| 121 |
{ |
| 122 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 123 |
throw Py::Exception(); |
| 124 |
} |
| 125 |
|
| 126 |
if (_view) { |
| 127 |
_view->printPdf(); |
| 128 |
} |
| 129 |
|
| 130 |
return Py::None(); |
| 131 |
} |
| 132 |
|
| 133 |
Py::Object MDIViewPy::printPreview(const Py::Tuple& args) |
| 134 |
{ |
| 135 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 136 |
throw Py::Exception(); |
| 137 |
} |
| 138 |
|
| 139 |
if (_view) { |
| 140 |
_view->printPreview(); |
| 141 |
} |
| 142 |
|
| 143 |
return Py::None(); |
| 144 |
} |
| 145 |
|
| 146 |
Py::Object MDIViewPy::undoActions(const Py::Tuple& args) |
| 147 |
{ |
| 148 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 149 |
throw Py::Exception(); |
| 150 |
} |
| 151 |
|
| 152 |
Py::List list; |
| 153 |
if (_view) { |
| 154 |
QStringList undo = _view->undoActions(); |
| 155 |
for (const auto& it : undo) { |
| 156 |
list.append(Py::String(it.toStdString())); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return list; |
| 161 |
} |
| 162 |
|
| 163 |
Py::Object MDIViewPy::redoActions(const Py::Tuple& args) |
| 164 |
{ |
| 165 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 166 |
throw Py::Exception(); |
| 167 |
} |
| 168 |
|
| 169 |
Py::List list; |
| 170 |
if (_view) { |
| 171 |
QStringList redo = _view->redoActions(); |
| 172 |
for (const auto& it : redo) { |
| 173 |
list.append(Py::String(it.toStdString())); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return list; |
| 178 |
} |
| 179 |
|
| 180 |
Py::Object MDIViewPy::sendMessage(const Py::Tuple& args) |
| 181 |
{ |
| 182 |
const char** ppReturn = nullptr; |
| 183 |
char* psMsgStr; |
| 184 |
if (!PyArg_ParseTuple(args.ptr(), "s;Message string needed (string)", &psMsgStr)) { |
| 185 |
throw Py::Exception(); |
| 186 |
} |
| 187 |
|
| 188 |
try { |
| 189 |
bool ok = false; |
| 190 |
if (_view) { |
| 191 |
ok = _view->onMsg(psMsgStr, ppReturn); |
| 192 |
} |
| 193 |
return Py::Boolean(ok); |
| 194 |
} |
| 195 |
catch (const Base::Exception& e) { |
| 196 |
throw Py::RuntimeError(e.what()); |
| 197 |
} |
| 198 |
catch (const std::exception& e) { |
| 199 |
throw Py::RuntimeError(e.what()); |
| 200 |
} |
| 201 |
catch (...) { |
| 202 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
Py::Object MDIViewPy::supportMessage(const Py::Tuple& args) |
| 207 |
{ |
| 208 |
char* psMsgStr; |
| 209 |
if (!PyArg_ParseTuple(args.ptr(), "s;Message string needed (string)", &psMsgStr)) { |
| 210 |
throw Py::Exception(); |
| 211 |
} |
| 212 |
|
| 213 |
try { |
| 214 |
bool ok = false; |
| 215 |
if (_view) { |
| 216 |
_view->onHasMsg(psMsgStr); |
| 217 |
} |
| 218 |
return Py::Boolean(ok); |
| 219 |
} |
| 220 |
catch (const Base::Exception& e) { |
| 221 |
throw Py::RuntimeError(e.what()); |
| 222 |
} |
| 223 |
catch (const std::exception& e) { |
| 224 |
throw Py::RuntimeError(e.what()); |
| 225 |
} |
| 226 |
catch (...) { |
| 227 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
Py::Object MDIViewPy::fitAll(const Py::Tuple& args) |
| 232 |
{ |
| 233 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 234 |
throw Py::Exception(); |
| 235 |
} |
| 236 |
|
| 237 |
try { |
| 238 |
if (_view) { |
| 239 |
_view->viewAll(); |
| 240 |
} |
| 241 |
} |
| 242 |
catch (const Base::Exception& e) { |
| 243 |
throw Py::RuntimeError(e.what()); |
| 244 |
} |
| 245 |
catch (const std::exception& e) { |
| 246 |
throw Py::RuntimeError(e.what()); |
| 247 |
} |
| 248 |
catch (...) { |
| 249 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 250 |
} |
| 251 |
return Py::None(); |
| 252 |
} |
| 253 |
|
| 254 |
Py::Object MDIViewPy::setActiveObject(const Py::Tuple& args) |
| 255 |
{ |
| 256 |
PyObject* docObject = Py_None; |
| 257 |
char* name; |
| 258 |
char* subname = nullptr; |
| 259 |
if (!PyArg_ParseTuple(args.ptr(), "s|Os", &name, &docObject, &subname)) { |
| 260 |
throw Py::Exception(); |
| 261 |
} |
| 262 |
|
| 263 |
try { |
| 264 |
Base::PyTypeCheck( |
| 265 |
&docObject, |
| 266 |
&App::DocumentObjectPy::Type, |
| 267 |
"Expect the second argument to be a document object or None" |
| 268 |
); |
| 269 |
if (_view) { |
| 270 |
App::DocumentObject* obj = docObject |
| 271 |
? static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr() |
| 272 |
: nullptr; |
| 273 |
_view->setActiveObject(obj, name, subname); |
| 274 |
} |
| 275 |
} |
| 276 |
catch (const Base::Exception& e) { |
| 277 |
throw Py::Exception(e.getPyExceptionType(), e.what()); |
| 278 |
} |
| 279 |
|
| 280 |
return Py::None(); |
| 281 |
} |
| 282 |
|
| 283 |
Py::Object MDIViewPy::getActiveObject(const Py::Tuple& args) |
| 284 |
{ |
| 285 |
const char* name {}; |
| 286 |
PyObject* resolve = Py_True; // NOLINT |
| 287 |
if (!PyArg_ParseTuple(args.ptr(), "s|O!", &name, &PyBool_Type, &resolve)) { |
| 288 |
throw Py::Exception(); |
| 289 |
} |
| 290 |
|
| 291 |
App::DocumentObject* parent = nullptr; |
| 292 |
std::string subname; |
| 293 |
App::DocumentObject* obj = nullptr; |
| 294 |
if (_view) { |
| 295 |
obj = _view->getActiveObject<App::DocumentObject*>(name, &parent, &subname); |
| 296 |
} |
| 297 |
|
| 298 |
if (Base::asBoolean(resolve)) { |
| 299 |
if (obj) { |
| 300 |
return Py::asObject(obj->getPyObject()); |
| 301 |
} |
| 302 |
|
| 303 |
return Py::None(); |
| 304 |
} |
| 305 |
|
| 306 |
// NOLINTBEGIN(cppcoreguidelines-slicing) |
| 307 |
if (obj) { |
| 308 |
return Py::TupleN( |
| 309 |
Py::asObject(obj->getPyObject()), |
| 310 |
Py::asObject(parent->getPyObject()), |
| 311 |
Py::String(subname.c_str()) |
| 312 |
); |
| 313 |
} |
| 314 |
|
| 315 |
return Py::TupleN(Py::None(), Py::None(), Py::String()); |
| 316 |
// NOLINTEND(cppcoreguidelines-slicing) |
| 317 |
} |
| 318 |
|
| 319 |
Py::Object MDIViewPy::cast_to_base(const Py::Tuple&) |
| 320 |
{ |
| 321 |
return Py::Object(this); |
| 322 |
} |