FreeCAD

Форк
0
/
PythonWorkbenchPyImp.cpp 
340 строк · 10.6 Кб
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
#include "PreCompiled.h"
24

25
// inclusion of the generated files (generated out of PythonWorkbenchPy.xml)
26
#include "PythonWorkbenchPy.h"
27
#include "PythonWorkbenchPy.cpp"
28

29

30
using namespace Gui;
31

32
/** @class PythonWorkbenchPy
33
 * The workbench Python class provides additional methods for manipulation of python
34
 * workbench objects.
35
 * From the view of Python PythonWorkbenchPy is also derived from WorkbenchPy as in C++.
36
 * @see Workbench
37
 * @see WorkbenchPy
38
 * @see PythonWorkbench
39
 * @author Werner Mayer
40
 */
41

42
// returns a string which represent the object e.g. when printed in python
43
std::string PythonWorkbenchPy::representation() const
44
{
45
    return {"<Workbench object>"};
46
}
47

48
/** Appends a new menu */
49
PyObject*  PythonWorkbenchPy::appendMenu(PyObject *args)
50
{
51
    PY_TRY {
52
        PyObject* pPath;
53
        PyObject* pItems;
54
        if ( !PyArg_ParseTuple(args, "OO", &pPath, &pItems) )
55
            return nullptr;
56

57
        // menu path
58
        std::list<std::string> path;
59
        if (PyList_Check(pPath)) {
60
            int nDepth = PyList_Size(pPath);
61
            for (int j=0; j<nDepth;++j) {
62
                PyObject* item = PyList_GetItem(pPath, j);
63
                if (PyUnicode_Check(item)) {
64
                    const char* pItem = PyUnicode_AsUTF8(item);
65
                    path.emplace_back(pItem);
66
                } else {
67
                    continue;
68
                }
69
            }
70
        } else if (PyUnicode_Check(pPath)) {
71
            const char* pItem = PyUnicode_AsUTF8(pPath);
72
            path.emplace_back(pItem);
73
        } else {
74
            PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument");
75
            return nullptr;
76
        }
77

78
        // menu items
79
        std::list<std::string> items;
80
        if (PyList_Check(pItems)) {
81
            int nItems = PyList_Size(pItems);
82
            for (int i=0; i<nItems;++i) {
83
                PyObject* item = PyList_GetItem(pItems, i);
84
                if (PyUnicode_Check(item)) {
85
                    const char* pItem = PyUnicode_AsUTF8(item);
86
                    items.emplace_back(pItem);
87
                } else {
88
                    continue;
89
                }
90
            }
91
        } else if (PyUnicode_Check(pItems)) {
92
            const char* pItem = PyUnicode_AsUTF8(pItems);
93
            items.emplace_back(pItem);
94
        } else {
95
            PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument");
96
            return nullptr;
97
        }
98

99
        getPythonBaseWorkbenchPtr()->appendMenu( path, items );
100

101
        Py_Return;
102
    } PY_CATCH;
103
}
104

105
/** Removes a menu */
106
PyObject*  PythonWorkbenchPy::removeMenu(PyObject *args)
107
{
108
    PY_TRY {
109
        char *psMenu;
110
        if (!PyArg_ParseTuple(args, "s", &psMenu))
111
            return nullptr;
112

113
        getPythonBaseWorkbenchPtr()->removeMenu( psMenu );
114
        Py_Return;
115
    } PY_CATCH;
116
}
117

118
/** Appends new context menu items */
119
PyObject*  PythonWorkbenchPy::appendContextMenu(PyObject *args)
120
{
121
    PY_TRY {
122
        PyObject* pPath;
123
        PyObject* pItems;
124
        if ( !PyArg_ParseTuple(args, "OO", &pPath, &pItems) )
125
            return nullptr;
126

127
        // menu path
128
        std::list<std::string> path;
129
        if (PyList_Check(pPath)) {
130
            int nDepth = PyList_Size(pPath);
131
            for (int j=0; j<nDepth;++j) {
132
                PyObject* item = PyList_GetItem(pPath, j);
133
                if (PyUnicode_Check(item)) {
134
                    const char* pItem = PyUnicode_AsUTF8(item);
135
                    path.emplace_back(pItem);
136
                } else {
137
                    continue;
138
                }
139
            }
140
        } else if (PyUnicode_Check(pPath)) {
141
            const char* pItem = PyUnicode_AsUTF8(pPath);
142
            path.emplace_back(pItem);
143
        } else {
144
            PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument");
145
            return nullptr;
146
        }
147

148
        // menu items
149
        std::list<std::string> items;
150
        if (PyList_Check(pItems)) {
151
            int nItems = PyList_Size(pItems);
152
            for (int i=0; i<nItems;++i) {
153
                PyObject* item = PyList_GetItem(pItems, i);
154
                if (PyUnicode_Check(item)) {
155
                    const char* pItem = PyUnicode_AsUTF8(item);
156
                    items.emplace_back(pItem);
157
                } else {
158
                    continue;
159
                }
160
            }
161
        } else if (PyUnicode_Check(pItems)) {
162
            const char* pItem = PyUnicode_AsUTF8(pItems);
163
            items.emplace_back(pItem);
164
        } else {
165
            PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument");
166
            return nullptr;
167
        }
168

169
        getPythonBaseWorkbenchPtr()->appendContextMenu( path, items );
170

171
        Py_Return;
172
    } PY_CATCH;
173
}
174

175
/** Removes a context menu */
176
PyObject*  PythonWorkbenchPy::removeContextMenu(PyObject *args)
177
{
178
    PY_TRY {
179
        char *psMenu;
180
        if (!PyArg_ParseTuple(args, "s", &psMenu))
181
            return nullptr;
182

183
        getPythonBaseWorkbenchPtr()->removeContextMenu( psMenu );
184
        Py_Return;
185
    } PY_CATCH;
186
}
187

188
/** Appends a new toolbar */
189
PyObject*  PythonWorkbenchPy::appendToolbar(PyObject *args)
190
{
191
    PY_TRY {
192
        PyObject* pObject;
193
        char* psToolBar;
194
        if ( !PyArg_ParseTuple(args, "sO", &psToolBar, &pObject) )
195
            return nullptr;
196
        if (!PyList_Check(pObject)) {
197
            PyErr_SetString(PyExc_AssertionError, "Expected a list as second argument");
198
            return nullptr;
199
        }
200

201
        std::list<std::string> items;
202
        int nSize = PyList_Size(pObject);
203
        for (int i=0; i<nSize;++i) {
204
            PyObject* item = PyList_GetItem(pObject, i);
205
            if (PyUnicode_Check(item)) {
206
                const char* pItem = PyUnicode_AsUTF8(item);
207
                items.emplace_back(pItem);
208
            } else {
209
                continue;
210
            }
211
        }
212
        getPythonBaseWorkbenchPtr()->appendToolbar( psToolBar, items );
213

214
        Py_Return;
215
    } PY_CATCH;
216
}
217

218
/** Removes a toolbar */
219
PyObject*  PythonWorkbenchPy::removeToolbar(PyObject *args)
220
{
221
    PY_TRY {
222
        char *psToolBar;
223
        if (!PyArg_ParseTuple(args, "s", &psToolBar))
224
            return nullptr;
225

226
        getPythonBaseWorkbenchPtr()->removeToolbar( psToolBar );
227
        Py_Return;
228
    } PY_CATCH;
229
}
230

231
/** Appends a new command bar */
232
PyObject*  PythonWorkbenchPy::appendCommandbar(PyObject *args)
233
{
234
    PY_TRY {
235
        PyObject* pObject;
236
        char* psToolBar;
237
        if ( !PyArg_ParseTuple(args, "sO", &psToolBar, &pObject) )
238
            return nullptr;
239
        if (!PyList_Check(pObject)) {
240
            PyErr_SetString(PyExc_AssertionError, "Expected a list as second argument");
241
            return nullptr;
242
        }
243

244
        std::list<std::string> items;
245
        int nSize = PyList_Size(pObject);
246
        for (int i=0; i<nSize;++i) {
247
            PyObject* item = PyList_GetItem(pObject, i);
248
            if (PyUnicode_Check(item)) {
249
                const char* pItem = PyUnicode_AsUTF8(item);
250
                items.emplace_back(pItem);
251
            } else {
252
                continue;
253
            }
254
        }
255

256
        getPythonBaseWorkbenchPtr()->appendCommandbar( psToolBar, items );
257

258
        Py_Return;
259
    } PY_CATCH;
260
}
261

262
/** Removes a command bar */
263
PyObject*  PythonWorkbenchPy::removeCommandbar(PyObject *args)
264
{
265
    PY_TRY {
266
        char *psToolBar;
267
        if (!PyArg_ParseTuple(args, "s", &psToolBar))
268
            return nullptr;
269

270
        getPythonBaseWorkbenchPtr()->removeCommandbar( psToolBar );
271
        Py_Return;
272
    } PY_CATCH;
273
}
274

275
PyObject* PythonWorkbenchPy::getCustomAttributes(const char* ) const
276
{
277
    return nullptr;
278
}
279

280
int PythonWorkbenchPy::setCustomAttributes(const char* , PyObject *)
281
{
282
    return 0;
283
}
284

285
// deprecated methods
286

287
PyObject*  PythonWorkbenchPy::AppendMenu(PyObject *args)
288
{
289
    return appendMenu(args);
290
}
291

292
PyObject*  PythonWorkbenchPy::RemoveMenu(PyObject *args)
293
{
294
    return removeMenu(args);
295
}
296

297
PyObject*  PythonWorkbenchPy::ListMenus(PyObject *args)
298
{
299
    return listMenus(args);
300
}
301

302
PyObject*  PythonWorkbenchPy::AppendContextMenu(PyObject *args)
303
{
304
    return appendContextMenu(args);
305
}
306

307
PyObject*  PythonWorkbenchPy::RemoveContextMenu(PyObject *args)
308
{
309
    return removeContextMenu(args);
310
}
311

312
PyObject*  PythonWorkbenchPy::AppendToolbar(PyObject *args)
313
{
314
    return appendToolbar(args);
315
}
316

317
PyObject*  PythonWorkbenchPy::RemoveToolbar(PyObject *args)
318
{
319
    return removeToolbar(args);
320
}
321

322
PyObject*  PythonWorkbenchPy::ListToolbars(PyObject *args)
323
{
324
    return listToolbars(args);
325
}
326

327
PyObject*  PythonWorkbenchPy::AppendCommandbar(PyObject *args)
328
{
329
    return appendCommandbar(args);
330
}
331

332
PyObject*  PythonWorkbenchPy::RemoveCommandbar(PyObject *args)
333
{
334
    return removeCommandbar(args);
335
}
336

337
PyObject*  PythonWorkbenchPy::ListCommandbars(PyObject *args)
338
{
339
    return listCommandbars(args);
340
}
341

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.