| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 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 |
// inclusion of the generated files (generated out of PythonWorkbenchPy.pyi) |
| 25 |
#include "PythonWorkbenchPy.h" |
| 26 |
#include "PythonWorkbenchPy.cpp" |
| 27 |
|
| 28 |
|
| 29 |
using namespace Gui; |
| 30 |
|
| 31 |
/** @class PythonWorkbenchPy |
| 32 |
* The workbench Python class provides additional methods for manipulation of python |
| 33 |
* workbench objects. |
| 34 |
* From the view of Python PythonWorkbenchPy is also derived from WorkbenchPy as in C++. |
| 35 |
* @see Workbench |
| 36 |
* @see WorkbenchPy |
| 37 |
* @see PythonWorkbench |
| 38 |
* @author Werner Mayer |
| 39 |
*/ |
| 40 |
|
| 41 |
// returns a string which represent the object e.g. when printed in python |
| 42 |
std::string PythonWorkbenchPy::representation() const |
| 43 |
{ |
| 44 |
return {"<Workbench object>"}; |
| 45 |
} |
| 46 |
|
| 47 |
/** Appends a new menu */ |
| 48 |
PyObject* PythonWorkbenchPy::appendMenu(PyObject* args) |
| 49 |
{ |
| 50 |
PY_TRY |
| 51 |
{ |
| 52 |
PyObject* pPath; |
| 53 |
PyObject* pItems; |
| 54 |
if (!PyArg_ParseTuple(args, "OO", &pPath, &pItems)) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
|
| 58 |
// menu path |
| 59 |
std::list<std::string> path; |
| 60 |
if (PyList_Check(pPath)) { |
| 61 |
int nDepth = PyList_Size(pPath); |
| 62 |
for (int j = 0; j < nDepth; ++j) { |
| 63 |
PyObject* item = PyList_GetItem(pPath, j); |
| 64 |
if (PyUnicode_Check(item)) { |
| 65 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 66 |
path.emplace_back(pItem); |
| 67 |
} |
| 68 |
else { |
| 69 |
continue; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
else if (PyUnicode_Check(pPath)) { |
| 74 |
const char* pItem = PyUnicode_AsUTF8(pPath); |
| 75 |
path.emplace_back(pItem); |
| 76 |
} |
| 77 |
else { |
| 78 |
PyErr_SetString( |
| 79 |
PyExc_AssertionError, |
| 80 |
"Expected either a string or a stringlist as first argument" |
| 81 |
); |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
// menu items |
| 86 |
std::list<std::string> items; |
| 87 |
if (PyList_Check(pItems)) { |
| 88 |
int nItems = PyList_Size(pItems); |
| 89 |
for (int i = 0; i < nItems; ++i) { |
| 90 |
PyObject* item = PyList_GetItem(pItems, i); |
| 91 |
if (PyUnicode_Check(item)) { |
| 92 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 93 |
items.emplace_back(pItem); |
| 94 |
} |
| 95 |
else { |
| 96 |
continue; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
else if (PyUnicode_Check(pItems)) { |
| 101 |
const char* pItem = PyUnicode_AsUTF8(pItems); |
| 102 |
items.emplace_back(pItem); |
| 103 |
} |
| 104 |
else { |
| 105 |
PyErr_SetString( |
| 106 |
PyExc_AssertionError, |
| 107 |
"Expected either a string or a stringlist as first argument" |
| 108 |
); |
| 109 |
return nullptr; |
| 110 |
} |
| 111 |
|
| 112 |
getPythonBaseWorkbenchPtr()->appendMenu(path, items); |
| 113 |
|
| 114 |
Py_Return; |
| 115 |
} |
| 116 |
PY_CATCH; |
| 117 |
} |
| 118 |
|
| 119 |
/** Removes a menu */ |
| 120 |
PyObject* PythonWorkbenchPy::removeMenu(PyObject* args) |
| 121 |
{ |
| 122 |
PY_TRY |
| 123 |
{ |
| 124 |
char* psMenu; |
| 125 |
if (!PyArg_ParseTuple(args, "s", &psMenu)) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
getPythonBaseWorkbenchPtr()->removeMenu(psMenu); |
| 130 |
Py_Return; |
| 131 |
} |
| 132 |
PY_CATCH; |
| 133 |
} |
| 134 |
|
| 135 |
/** Appends new context menu items */ |
| 136 |
PyObject* PythonWorkbenchPy::appendContextMenu(PyObject* args) |
| 137 |
{ |
| 138 |
PY_TRY |
| 139 |
{ |
| 140 |
PyObject* pPath; |
| 141 |
PyObject* pItems; |
| 142 |
if (!PyArg_ParseTuple(args, "OO", &pPath, &pItems)) { |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
|
| 146 |
// menu path |
| 147 |
std::list<std::string> path; |
| 148 |
if (PyList_Check(pPath)) { |
| 149 |
int nDepth = PyList_Size(pPath); |
| 150 |
for (int j = 0; j < nDepth; ++j) { |
| 151 |
PyObject* item = PyList_GetItem(pPath, j); |
| 152 |
if (PyUnicode_Check(item)) { |
| 153 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 154 |
path.emplace_back(pItem); |
| 155 |
} |
| 156 |
else { |
| 157 |
continue; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
else if (PyUnicode_Check(pPath)) { |
| 162 |
const char* pItem = PyUnicode_AsUTF8(pPath); |
| 163 |
path.emplace_back(pItem); |
| 164 |
} |
| 165 |
else { |
| 166 |
PyErr_SetString( |
| 167 |
PyExc_AssertionError, |
| 168 |
"Expected either a string or a stringlist as first argument" |
| 169 |
); |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
|
| 173 |
// menu items |
| 174 |
std::list<std::string> items; |
| 175 |
if (PyList_Check(pItems)) { |
| 176 |
int nItems = PyList_Size(pItems); |
| 177 |
for (int i = 0; i < nItems; ++i) { |
| 178 |
PyObject* item = PyList_GetItem(pItems, i); |
| 179 |
if (PyUnicode_Check(item)) { |
| 180 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 181 |
items.emplace_back(pItem); |
| 182 |
} |
| 183 |
else { |
| 184 |
continue; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
else if (PyUnicode_Check(pItems)) { |
| 189 |
const char* pItem = PyUnicode_AsUTF8(pItems); |
| 190 |
items.emplace_back(pItem); |
| 191 |
} |
| 192 |
else { |
| 193 |
PyErr_SetString( |
| 194 |
PyExc_AssertionError, |
| 195 |
"Expected either a string or a stringlist as first argument" |
| 196 |
); |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
getPythonBaseWorkbenchPtr()->appendContextMenu(path, items); |
| 201 |
|
| 202 |
Py_Return; |
| 203 |
} |
| 204 |
PY_CATCH; |
| 205 |
} |
| 206 |
|
| 207 |
/** Removes a context menu */ |
| 208 |
PyObject* PythonWorkbenchPy::removeContextMenu(PyObject* args) |
| 209 |
{ |
| 210 |
PY_TRY |
| 211 |
{ |
| 212 |
char* psMenu; |
| 213 |
if (!PyArg_ParseTuple(args, "s", &psMenu)) { |
| 214 |
return nullptr; |
| 215 |
} |
| 216 |
|
| 217 |
getPythonBaseWorkbenchPtr()->removeContextMenu(psMenu); |
| 218 |
Py_Return; |
| 219 |
} |
| 220 |
PY_CATCH; |
| 221 |
} |
| 222 |
|
| 223 |
/** Appends a new toolbar */ |
| 224 |
PyObject* PythonWorkbenchPy::appendToolbar(PyObject* args) |
| 225 |
{ |
| 226 |
PY_TRY |
| 227 |
{ |
| 228 |
PyObject* pObject; |
| 229 |
char* psToolBar; |
| 230 |
if (!PyArg_ParseTuple(args, "sO", &psToolBar, &pObject)) { |
| 231 |
return nullptr; |
| 232 |
} |
| 233 |
if (!PyList_Check(pObject)) { |
| 234 |
PyErr_SetString(PyExc_AssertionError, "Expected a list as second argument"); |
| 235 |
return nullptr; |
| 236 |
} |
| 237 |
|
| 238 |
std::list<std::string> items; |
| 239 |
int nSize = PyList_Size(pObject); |
| 240 |
for (int i = 0; i < nSize; ++i) { |
| 241 |
PyObject* item = PyList_GetItem(pObject, i); |
| 242 |
if (PyUnicode_Check(item)) { |
| 243 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 244 |
items.emplace_back(pItem); |
| 245 |
} |
| 246 |
else { |
| 247 |
continue; |
| 248 |
} |
| 249 |
} |
| 250 |
getPythonBaseWorkbenchPtr()->appendToolbar(psToolBar, items); |
| 251 |
|
| 252 |
Py_Return; |
| 253 |
} |
| 254 |
PY_CATCH; |
| 255 |
} |
| 256 |
|
| 257 |
/** Removes a toolbar */ |
| 258 |
PyObject* PythonWorkbenchPy::removeToolbar(PyObject* args) |
| 259 |
{ |
| 260 |
PY_TRY |
| 261 |
{ |
| 262 |
char* psToolBar; |
| 263 |
if (!PyArg_ParseTuple(args, "s", &psToolBar)) { |
| 264 |
return nullptr; |
| 265 |
} |
| 266 |
|
| 267 |
getPythonBaseWorkbenchPtr()->removeToolbar(psToolBar); |
| 268 |
Py_Return; |
| 269 |
} |
| 270 |
PY_CATCH; |
| 271 |
} |
| 272 |
|
| 273 |
/** Appends a new command bar */ |
| 274 |
PyObject* PythonWorkbenchPy::appendCommandbar(PyObject* args) |
| 275 |
{ |
| 276 |
PY_TRY |
| 277 |
{ |
| 278 |
PyObject* pObject; |
| 279 |
char* psToolBar; |
| 280 |
if (!PyArg_ParseTuple(args, "sO", &psToolBar, &pObject)) { |
| 281 |
return nullptr; |
| 282 |
} |
| 283 |
if (!PyList_Check(pObject)) { |
| 284 |
PyErr_SetString(PyExc_AssertionError, "Expected a list as second argument"); |
| 285 |
return nullptr; |
| 286 |
} |
| 287 |
|
| 288 |
std::list<std::string> items; |
| 289 |
int nSize = PyList_Size(pObject); |
| 290 |
for (int i = 0; i < nSize; ++i) { |
| 291 |
PyObject* item = PyList_GetItem(pObject, i); |
| 292 |
if (PyUnicode_Check(item)) { |
| 293 |
const char* pItem = PyUnicode_AsUTF8(item); |
| 294 |
items.emplace_back(pItem); |
| 295 |
} |
| 296 |
else { |
| 297 |
continue; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
getPythonBaseWorkbenchPtr()->appendCommandbar(psToolBar, items); |
| 302 |
|
| 303 |
Py_Return; |
| 304 |
} |
| 305 |
PY_CATCH; |
| 306 |
} |
| 307 |
|
| 308 |
/** Removes a command bar */ |
| 309 |
PyObject* PythonWorkbenchPy::removeCommandbar(PyObject* args) |
| 310 |
{ |
| 311 |
PY_TRY |
| 312 |
{ |
| 313 |
char* psToolBar; |
| 314 |
if (!PyArg_ParseTuple(args, "s", &psToolBar)) { |
| 315 |
return nullptr; |
| 316 |
} |
| 317 |
|
| 318 |
getPythonBaseWorkbenchPtr()->removeCommandbar(psToolBar); |
| 319 |
Py_Return; |
| 320 |
} |
| 321 |
PY_CATCH; |
| 322 |
} |
| 323 |
|
| 324 |
PyObject* PythonWorkbenchPy::getCustomAttributes(const char*) const |
| 325 |
{ |
| 326 |
return nullptr; |
| 327 |
} |
| 328 |
|
| 329 |
int PythonWorkbenchPy::setCustomAttributes(const char*, PyObject*) |
| 330 |
{ |
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
// deprecated methods |
| 335 |
|
| 336 |
PyObject* PythonWorkbenchPy::AppendMenu(PyObject* args) |
| 337 |
{ |
| 338 |
return appendMenu(args); |
| 339 |
} |
| 340 |
|
| 341 |
PyObject* PythonWorkbenchPy::RemoveMenu(PyObject* args) |
| 342 |
{ |
| 343 |
return removeMenu(args); |
| 344 |
} |
| 345 |
|
| 346 |
PyObject* PythonWorkbenchPy::ListMenus(PyObject* args) |
| 347 |
{ |
| 348 |
return listMenus(args); |
| 349 |
} |
| 350 |
|
| 351 |
PyObject* PythonWorkbenchPy::AppendContextMenu(PyObject* args) |
| 352 |
{ |
| 353 |
return appendContextMenu(args); |
| 354 |
} |
| 355 |
|
| 356 |
PyObject* PythonWorkbenchPy::RemoveContextMenu(PyObject* args) |
| 357 |
{ |
| 358 |
return removeContextMenu(args); |
| 359 |
} |
| 360 |
|
| 361 |
PyObject* PythonWorkbenchPy::AppendToolbar(PyObject* args) |
| 362 |
{ |
| 363 |
return appendToolbar(args); |
| 364 |
} |
| 365 |
|
| 366 |
PyObject* PythonWorkbenchPy::RemoveToolbar(PyObject* args) |
| 367 |
{ |
| 368 |
return removeToolbar(args); |
| 369 |
} |
| 370 |
|
| 371 |
PyObject* PythonWorkbenchPy::ListToolbars(PyObject* args) |
| 372 |
{ |
| 373 |
return listToolbars(args); |
| 374 |
} |
| 375 |
|
| 376 |
PyObject* PythonWorkbenchPy::AppendCommandbar(PyObject* args) |
| 377 |
{ |
| 378 |
return appendCommandbar(args); |
| 379 |
} |
| 380 |
|
| 381 |
PyObject* PythonWorkbenchPy::RemoveCommandbar(PyObject* args) |
| 382 |
{ |
| 383 |
return removeCommandbar(args); |
| 384 |
} |
| 385 |
|
| 386 |
PyObject* PythonWorkbenchPy::ListCommandbars(PyObject* args) |
| 387 |
{ |
| 388 |
return listCommandbars(args); |
| 389 |
} |