From a8fe58d93b57399ce9b09c06b95914cd478e2dbc Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 1 Apr 2014 15:43:40 +0200 Subject: [PATCH] + use Quantity type as QVariant, add quantity property to InputField and implement a PySide converter --- src/Gui/InputField.h | 21 +++++----- src/Gui/SoFCDB.cpp | 1 + src/Gui/WidgetFactory.cpp | 58 +++++++++++++++++++++++++++ src/Gui/propertyeditor/PropertyItem.h | 2 +- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/Gui/InputField.h b/src/Gui/InputField.h index 5ed640bffa..53b58ae667 100644 --- a/src/Gui/InputField.h +++ b/src/Gui/InputField.h @@ -31,6 +31,8 @@ #include "SpinBox.h" #include "FileDialog.h" +Q_DECLARE_METATYPE(Base::Quantity) + namespace Gui { @@ -47,24 +49,25 @@ class GuiExport InputField : public QLineEdit { Q_OBJECT - Q_PROPERTY( QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath ) + Q_PROPERTY(QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath ) Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep ) Q_PROPERTY(double maximum READ maximum WRITE setMaximum ) Q_PROPERTY(double minimum READ minimum WRITE setMinimum ) Q_PROPERTY(int historySize READ historySize WRITE setHistorySize ) Q_PROPERTY(QString unit READ getUnitText WRITE setUnitText ) + Q_PROPERTY(Base::Quantity quantity READ getQuantity WRITE setValue ) public: InputField ( QWidget * parent = 0 ); virtual ~InputField(); - /// sets the field with a quantity + /// set the field with a quantity void setValue(const Base::Quantity&); - /// get the actual value + /// get the current value Base::Quantity getQuantity(void)const{return this->actQuantity;} - /** sets the Unit this field working with. - * After seting the Unit the field will only acceppt + /** sets the Unit this field is working with. + * After setting the Unit the field will only accept * user input with this unit type. Or if the user input * a value without unit, this one will be added to the resulting * Quantity. @@ -98,9 +101,9 @@ public: /** @name history and default management */ //@{ - /// the param group path where the widget write and read the dafault values + /// the param group path where the widget writes and reads the default values QByteArray paramGrpPath () const; - /// set the param group path where the widget write and read the dafault values + /// set the param group path where the widget writes and reads the default values void setParamGrpPath ( const QByteArray& name ); /// push a new value to the history, if no string given the actual text of the input field is used. void pushToHistory(const QString &valueq = QString()); @@ -114,14 +117,14 @@ public: Q_SIGNALS: /** gets emitted if the user has entered a VALID input - * Valid means the user inputted string obays all restrictions + * Valid means the user inputted string obeys all restrictions * like: minimum, maximum and/or the right Unit (if specified). * If you want the unfiltered/unvalidated input use textChanged(const QString&) * instead: */ void valueChanged(const Base::Quantity&); /** gets emitted if the user has entered a VALID input - * Valid means the user inputted string obays all restrictions + * Valid means the user inputted string obeys all restrictions * like: minimum, maximum and/or the right Unit (if specified). * If you want the unfiltered/unvalidated input use textChanged(const QString&) * instead: diff --git a/src/Gui/SoFCDB.cpp b/src/Gui/SoFCDB.cpp index 963f59c197..34437aadca 100644 --- a/src/Gui/SoFCDB.cpp +++ b/src/Gui/SoFCDB.cpp @@ -135,6 +135,7 @@ void Gui::SoFCDB::init() qRegisterMetaType("Base::Vector3f"); qRegisterMetaType("Base::Vector3d"); + qRegisterMetaType("Base::Quantity"); init_done = TRUE; } diff --git a/src/Gui/WidgetFactory.cpp b/src/Gui/WidgetFactory.cpp index 1a5511517c..1ad9c6f41d 100644 --- a/src/Gui/WidgetFactory.cpp +++ b/src/Gui/WidgetFactory.cpp @@ -59,6 +59,8 @@ PyTypeObject** SbkPySide_QtGuiTypes=NULL; #include #include #include +#include +#include #include "WidgetFactory.h" @@ -68,9 +70,65 @@ PyTypeObject** SbkPySide_QtGuiTypes=NULL; using namespace Gui; +#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) +namespace Shiboken { +template<> struct Converter +{ + static inline bool checkType(PyObject* pyObj) { + return PyObject_TypeCheck(pyObj, &(Base::QuantityPy::Type)); + } + static inline bool isConvertible(PyObject* pyObj) { + return PyObject_TypeCheck(pyObj, &(Base::QuantityPy::Type)); + } + static inline PyObject* toPython(void* cppobj) { + return toPython(*reinterpret_cast(cppobj)); + } + static inline PyObject* toPython(Base::Quantity cpx) { + return new Base::QuantityPy(new Base::Quantity(cpx)); + } + static inline Base::Quantity toCpp(PyObject* pyobj) { + Base::Quantity q = *static_cast(pyobj)->getQuantityPtr(); + return q; + } +}; +} + +PyObject* toPythonFuncQuantity(const void* cpp) +{ + return Shiboken::Converter::toPython(const_cast(cpp)); +} + +void toCppPointerConvFuncQuantity(PyObject*,void*) +{ +} + +PythonToCppFunc toCppPointerCheckFuncQuantity(PyObject* obj) +{ + if (Shiboken::Converter::isConvertible(obj)) + return toCppPointerConvFuncQuantity; + else + return 0; +} + +void registerTypes() +{ + SbkConverter* convert = Shiboken::Conversions::createConverter(&Base::QuantityPy::Type, toPythonFuncQuantity); + Shiboken::Conversions::setPythonToCppPointerFunctions(convert, toCppPointerConvFuncQuantity, toCppPointerCheckFuncQuantity); + Shiboken::Conversions::registerConverterName(convert, "Base::Quantity"); +} +#endif + +// -------------------------------------------------------- PythonWrapper::PythonWrapper() { +#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) + static bool init = false; + if (!init) { + init = true; + registerTypes(); + } +#endif } bool PythonWrapper::toCString(const Py::Object& pyobject, std::string& str) diff --git a/src/Gui/propertyeditor/PropertyItem.h b/src/Gui/propertyeditor/PropertyItem.h index d4c2cf8562..5e1cadcc58 100644 --- a/src/Gui/propertyeditor/PropertyItem.h +++ b/src/Gui/propertyeditor/PropertyItem.h @@ -36,12 +36,12 @@ #include #include #include +#include Q_DECLARE_METATYPE(Base::Vector3f) Q_DECLARE_METATYPE(Base::Vector3d) Q_DECLARE_METATYPE(Base::Matrix4D) Q_DECLARE_METATYPE(Base::Placement) -Q_DECLARE_METATYPE(Base::Quantity) namespace Gui { namespace Dialog { class TaskPlacement; }