From 1d5cfbc9ccc79e89f4dffe3a8408f5430622f279 Mon Sep 17 00:00:00 2001 From: jriegel Date: Sun, 16 Feb 2014 20:02:17 +0100 Subject: [PATCH] PropertyUnits takes Unicode python strings --- src/App/PropertyUnits.cpp | 33 +++++++++++++++++---------------- src/App/PropertyUnits.h | 3 ++- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/App/PropertyUnits.cpp b/src/App/PropertyUnits.cpp index ae9849baf..578b3e643 100644 --- a/src/App/PropertyUnits.cpp +++ b/src/App/PropertyUnits.cpp @@ -70,13 +70,19 @@ PyObject *PropertyQuantity::getPyObject(void) return new QuantityPy (new Quantity(_dValue,_Unit)); } -void PropertyQuantity::setPyObject(PyObject *value) +Base::Quantity PropertyQuantity::createQuantityFromPy(PyObject *value) { Base::Quantity quant; if (PyString_Check(value)) quant = Quantity::parse(QString::fromLatin1(PyString_AsString(value))); - else if (PyFloat_Check(value)) + else if (PyUnicode_Check(value)){ + PyObject* unicode = PyUnicode_AsUTF8String(value); + std::string Str; + Str = PyString_AsString(unicode); + quant = Quantity::parse(QString::fromUtf8(Str.c_str())); + Py_DECREF(unicode); + }else if (PyFloat_Check(value)) quant = Quantity(PyFloat_AsDouble(value),_Unit); else if (PyInt_Check(value)) quant = Quantity((double)PyInt_AsLong(value),_Unit); @@ -87,6 +93,14 @@ void PropertyQuantity::setPyObject(PyObject *value) else throw Base::Exception("Wrong type!"); + return quant; +} + + +void PropertyQuantity::setPyObject(PyObject *value) +{ + Base::Quantity quant= createQuantityFromPy(value); + Unit unit = quant.getUnit(); if (unit.isEmpty()){ PropertyFloat::setValue(quant.getValue()); @@ -120,20 +134,7 @@ const PropertyQuantityConstraint::Constraints* PropertyQuantityConstraint::getC void PropertyQuantityConstraint::setPyObject(PyObject *value) { - Base::Quantity quant; - - if (PyString_Check(value)) - quant = Quantity::parse(QString::fromLatin1(PyString_AsString(value))); - else if (PyFloat_Check(value)) - quant = Quantity(PyFloat_AsDouble(value),_Unit); - else if (PyInt_Check(value)) - quant = Quantity((double)PyInt_AsLong(value),_Unit); - else if (PyObject_TypeCheck(value, &(QuantityPy::Type))) { - Base::QuantityPy *pcObject = static_cast(value); - quant = *(pcObject->getQuantityPtr()); - } - else - throw Base::Exception("Wrong type!"); + Base::Quantity quant= createQuantityFromPy(value); Unit unit = quant.getUnit(); double temp = quant.getValue(); diff --git a/src/App/PropertyUnits.h b/src/App/PropertyUnits.h index f82f97f73..c4fe3641b 100644 --- a/src/App/PropertyUnits.h +++ b/src/App/PropertyUnits.h @@ -66,6 +66,7 @@ public: const Base::Unit &getUnit(void) const {return _Unit;} protected: + Base::Quantity createQuantityFromPy(PyObject *value); Base::Unit _Unit; }; @@ -77,7 +78,7 @@ class AppExport PropertyQuantityConstraint : public PropertyQuantity TYPESYSTEM_HEADER(); public: - PropertyQuantityConstraint(void){} + PropertyQuantityConstraint(void):_ConstStruct(0){} virtual ~PropertyQuantityConstraint(){} /// Constraint methods