aboutsummaryrefslogtreecommitdiffstats
path: root/PySide/QtCore/qvariant_type_conversions.h
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.lima@openbossa.org>2010-06-15 20:26:24 -0300
committerHugo Parente Lima <hugo.lima@openbossa.org>2010-06-15 20:34:02 -0300
commit0eb8d670c240b5ea621dba6271139f30cc97790a (patch)
tree312c89435590558ea08f75fc909764b5cd2cfacb /PySide/QtCore/qvariant_type_conversions.h
parent13b61ca7e55f52b501de9e028d2d7048836979ba (diff)
Added QVariant::Type as a primitive type.
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'PySide/QtCore/qvariant_type_conversions.h')
-rw-r--r--PySide/QtCore/qvariant_type_conversions.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/PySide/QtCore/qvariant_type_conversions.h b/PySide/QtCore/qvariant_type_conversions.h
new file mode 100644
index 000000000..107fb4203
--- /dev/null
+++ b/PySide/QtCore/qvariant_type_conversions.h
@@ -0,0 +1,53 @@
+namespace Shiboken {
+
+template<>
+struct Converter<QVariant::Type>
+{
+ static bool checkType(PyObject* pyObj)
+ {
+ return pyObj == Py_None || PyType_Check(pyObj) || Converter<QString>::checkType(pyObj);
+ }
+
+ static bool isConvertible(PyObject* pyObj)
+ {
+ return checkType(pyObj);
+ }
+
+ static QVariant::Type toCpp(PyObject* pyObj)
+ {
+ const char* typeName;
+ if (pyObj == Py_None)
+ return QVariant::Invalid;
+
+ if (pyObj == reinterpret_cast<PyObject*>(&PyString_Type) || pyObj == reinterpret_cast<PyObject*>(&PyUnicode_Type))
+ typeName = "QString";
+ else if (pyObj == reinterpret_cast<PyObject*>(&PyFloat_Type))
+ typeName = "double"; // float is a UserType in QVariant.
+ else if (pyObj == reinterpret_cast<PyObject*>(&PyLong_Type))
+ typeName = "int"; // long is a UserType in QVariant.
+ else if (PyType_Check(pyObj))
+ typeName = reinterpret_cast<PyTypeObject*>(pyObj)->tp_name;
+ else if (PyString_Check(pyObj))
+ typeName = PyString_AS_STRING(pyObj);
+ else if (PyUnicode_Check(pyObj))
+ typeName = PyString_AsString(pyObj);
+
+ return QVariant::nameToType(typeName);
+ }
+
+ static PyObject* toPython(void* cppObj) { return toPython(*reinterpret_cast<QVariant::Type*>(cppObj)); }
+ static PyObject* toPython(const QVariant::Type& cppObj)
+ {
+ const char* typeName = QVariant::typeToName(cppObj);
+ PyObject* pyObj;
+ if (!typeName) {
+ pyObj = Py_None;
+ } else {
+ Shiboken::TypeResolver* tr = Shiboken::TypeResolver::get(typeName);
+ pyObj = tr ? reinterpret_cast<PyObject*>(tr->pythonType()) : Py_None;
+ }
+ Py_INCREF(pyObj);
+ return pyObj;
+ }
+};
+}