improve unicode support for enumeration properties

This commit is contained in:
wmayer
2016-11-14 15:22:21 +01:00
parent cd6e96b588
commit 365a50603b
2 changed files with 35 additions and 13 deletions

View File

@@ -441,27 +441,48 @@ void PropertyEnumeration::setPyObject(PyObject *value)
throw Base::ValueError(out.str());
}
}
else if (PyList_Check(value)) {
Py_ssize_t nSize = PyList_Size(value);
else if (PyUnicode_Check(value)) {
PyObject* unicode = PyUnicode_AsUTF8String(value);
std::string str = PyString_AsString(unicode);
Py_DECREF(unicode);
if (_enum.contains(str.c_str())) {
aboutToSetValue();
_enum.setValue(str);
hasSetValue();
}
else {
std::stringstream out;
out << "'" << str << "' is not part of the enumeration";
throw Base::ValueError(out.str());
}
}
else if (PySequence_Check(value)) {
Py_ssize_t nSize = PySequence_Size(value);
std::vector<std::string> values;
values.resize(nSize);
for (Py_ssize_t i = 0; i < nSize; ++i) {
PyObject *item = PyList_GetItem(value, i);
PyObject *item = PySequence_GetItem(value, i);
if ( !PyString_Check(item) ) {
std::string error = std::string("type in list must be str, not ");
if (PyString_Check(item)) {
values[i] = PyString_AsString(item);
}
else if (PyUnicode_Check(item)) {
PyObject* unicode = PyUnicode_AsUTF8String(item);
values[i] = PyString_AsString(unicode);
Py_DECREF(unicode);
}
else {
std::string error = std::string("type in list must be str or unicode, not ");
throw Base::TypeError(error + item->ob_type->tp_name);
}
values[i] = PyString_AsString(item);
}
_enum.setEnums(values);
setValue((long)0);
}
else {
std::string error = std::string("type must be int or str, not ");
std::string error = std::string("type must be int, str or unicode not ");
throw Base::TypeError(error + value->ob_type->tp_name);
}
}

View File

@@ -1977,8 +1977,9 @@ void PropertyEnumItem::setValue(const QVariant& value)
return;
QStringList items = value.toStringList();
if (!items.isEmpty()) {
QString val = items.front();
QString data = QString::fromLatin1("\"%1\"").arg(val);
QByteArray val = items.front().toUtf8();
std::string str = Base::Tools::escapedUnicodeFromUtf8(val);
QString data = QString::fromLatin1("u\"%1\"").arg(QString::fromStdString(str));
setPropertyValue(data);
}
}
@@ -2007,12 +2008,12 @@ void PropertyEnumItem::setEditorData(QWidget *editor, const QVariant& data) cons
const std::vector<std::string>& value = prop->getEnumVector();
if (it == items.begin()) {
for (std::vector<std::string>::const_iterator jt = value.begin(); jt != value.end(); ++jt)
commonModes << QLatin1String(jt->c_str());
commonModes << QString::fromUtf8(jt->c_str());
}
else {
for (std::vector<std::string>::const_iterator jt = value.begin(); jt != value.end(); ++jt) {
if (commonModes.contains(QLatin1String(jt->c_str())))
modes << QLatin1String(jt->c_str());
if (commonModes.contains(QString::fromUtf8(jt->c_str())))
modes << QString::fromUtf8(jt->c_str());
}
commonModes = modes;