| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include "Exception.h" |
| 26 |
#include "ProgressIndicatorPy.h" |
| 27 |
|
| 28 |
|
| 29 |
using namespace Base; |
| 30 |
|
| 31 |
void ProgressIndicatorPy::init_type() |
| 32 |
{ |
| 33 |
behaviors().name("ProgressIndicator"); |
| 34 |
behaviors().doc("Progress indicator"); |
| 35 |
// you must have overwritten the virtual functions |
| 36 |
behaviors().supportRepr(); |
| 37 |
behaviors().supportGetattr(); |
| 38 |
behaviors().supportSetattr(); |
| 39 |
behaviors().set_tp_new(PyMake); |
| 40 |
|
| 41 |
add_varargs_method("start", &ProgressIndicatorPy::start, "start(string,int)"); |
| 42 |
add_varargs_method("next", &ProgressIndicatorPy::next, "next()"); |
| 43 |
add_varargs_method("stop", &ProgressIndicatorPy::stop, "stop()"); |
| 44 |
} |
| 45 |
|
| 46 |
Py::PythonType& ProgressIndicatorPy::behaviors() |
| 47 |
{ |
| 48 |
return Py::PythonExtension<ProgressIndicatorPy>::behaviors(); |
| 49 |
} |
| 50 |
|
| 51 |
PyTypeObject* ProgressIndicatorPy::type_object() |
| 52 |
{ |
| 53 |
return Py::PythonExtension<ProgressIndicatorPy>::type_object(); |
| 54 |
} |
| 55 |
|
| 56 |
bool ProgressIndicatorPy::check(PyObject* py) |
| 57 |
{ |
| 58 |
return Py::PythonExtension<ProgressIndicatorPy>::check(py); |
| 59 |
} |
| 60 |
|
| 61 |
PyObject* ProgressIndicatorPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/) |
| 62 |
{ |
| 63 |
return new ProgressIndicatorPy(); |
| 64 |
} |
| 65 |
|
| 66 |
ProgressIndicatorPy::ProgressIndicatorPy() = default; |
| 67 |
|
| 68 |
ProgressIndicatorPy::~ProgressIndicatorPy() = default; |
| 69 |
|
| 70 |
Py::Object ProgressIndicatorPy::repr() |
| 71 |
{ |
| 72 |
return Py::String("Base.ProgressIndicator"); |
| 73 |
} |
| 74 |
|
| 75 |
Py::Object ProgressIndicatorPy::start(const Py::Tuple& args) |
| 76 |
{ |
| 77 |
char* text = nullptr; |
| 78 |
unsigned int steps = 0; |
| 79 |
if (!PyArg_ParseTuple(args.ptr(), "sI", &text, &steps)) { |
| 80 |
throw Py::Exception(); |
| 81 |
} |
| 82 |
if (!_seq) { |
| 83 |
_seq = std::make_unique<SequencerLauncher>(text, steps); |
| 84 |
} |
| 85 |
return Py::None(); |
| 86 |
} |
| 87 |
|
| 88 |
Py::Object ProgressIndicatorPy::next(const Py::Tuple& args) |
| 89 |
{ |
| 90 |
int b = 0; |
| 91 |
if (!PyArg_ParseTuple(args.ptr(), "|i", &b)) { |
| 92 |
throw Py::Exception(); |
| 93 |
} |
| 94 |
if (_seq) { |
| 95 |
try { |
| 96 |
_seq->next(b != 0); |
| 97 |
} |
| 98 |
catch (const Base::AbortException&) { |
| 99 |
_seq.reset(); |
| 100 |
throw Py::RuntimeError("abort progress indicator"); |
| 101 |
} |
| 102 |
} |
| 103 |
return Py::None(); |
| 104 |
} |
| 105 |
|
| 106 |
Py::Object ProgressIndicatorPy::stop(const Py::Tuple& args) |
| 107 |
{ |
| 108 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 109 |
throw Py::Exception(); |
| 110 |
} |
| 111 |
_seq.reset(); |
| 112 |
return Py::None(); |
| 113 |
} |