diff options
author | Hugo Parente Lima <hugo.lima@openbossa.org> | 2010-06-15 20:26:24 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.lima@openbossa.org> | 2010-06-15 20:34:02 -0300 |
commit | 0eb8d670c240b5ea621dba6271139f30cc97790a (patch) | |
tree | 312c89435590558ea08f75fc909764b5cd2cfacb | |
parent | 13b61ca7e55f52b501de9e028d2d7048836979ba (diff) |
Added QVariant::Type as a primitive type.
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org>
Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r-- | PySide/QtCore/qstring_conversions.h | 6 | ||||
-rw-r--r-- | PySide/QtCore/qvariant_type_conversions.h | 53 | ||||
-rw-r--r-- | PySide/QtCore/typesystem_core.xml | 3 | ||||
-rw-r--r-- | tests/QtSql/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/QtSql/qvarianttype_test.py | 36 |
5 files changed, 99 insertions, 0 deletions
diff --git a/PySide/QtCore/qstring_conversions.h b/PySide/QtCore/qstring_conversions.h index 04b46f4ea..d0006b889 100644 --- a/PySide/QtCore/qstring_conversions.h +++ b/PySide/QtCore/qstring_conversions.h @@ -1,6 +1,12 @@ namespace Shiboken { template<> +inline PyTypeObject* SbkType<QString>() +{ + return &PyUnicode_Type; +} + +template<> struct Converter<QString> { static bool checkType(PyObject* pyObj) 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; + } +}; +} diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml index 8555f65c2..5331fa94a 100644 --- a/PySide/QtCore/typesystem_core.xml +++ b/PySide/QtCore/typesystem_core.xml @@ -161,6 +161,9 @@ <include file-name="typeresolver.h" location="global"/> </primitive-type> + <primitive-type name="QVariant::Type"> + <conversion-rule file="qvariant_type_conversions.h" /> + </primitive-type> <container-type name="QSet" type="set"> <conversion-rule file="qset_conversions.h"/> <include file-name="QSet" location="global"/> diff --git a/tests/QtSql/CMakeLists.txt b/tests/QtSql/CMakeLists.txt index a94469109..25fba44a0 100644 --- a/tests/QtSql/CMakeLists.txt +++ b/tests/QtSql/CMakeLists.txt @@ -1 +1,2 @@ PYSIDE_TEST(qsqldatabaseandqueries_test.py) +PYSIDE_TEST(qvarianttype_test.py) diff --git a/tests/QtSql/qvarianttype_test.py b/tests/QtSql/qvarianttype_test.py new file mode 100644 index 000000000..7eae67fd8 --- /dev/null +++ b/tests/QtSql/qvarianttype_test.py @@ -0,0 +1,36 @@ +'''Test cases for QVariant::Type converter''' +import unittest +from PySide.QtCore import * +from PySide.QtSql import * + +class QVariantTypeTest(unittest.TestCase): + def testQVariantType(self): + f = QSqlField("name", unicode) + self.assertEqual(f.type(), unicode) + + f = QSqlField("name", str) + self.assertEqual(f.type(), unicode) + + f = QSqlField("name", "QString") + self.assertEqual(f.type(), unicode) + + f = QSqlField("name", "double") + self.assertEqual(f.type(), float) + + f = QSqlField("name", float) + self.assertEqual(f.type(), float) + + f = QSqlField("name", int) + self.assertEqual(f.type(), int) + + f = QSqlField("name", long) + self.assertEqual(f.type(), int) # long isn't registered in QVariant:Type, just in QMetaType::Type + + #f = QSqlField("name", QObject) + #self.assertEqual(f.type(), None) + + f = QSqlField("name", None) + self.assertEqual(f.type(), None) + +if __name__ == '__main__': + unittest.main() |