From 0eb8d670c240b5ea621dba6271139f30cc97790a Mon Sep 17 00:00:00 2001 From: Hugo Parente Lima Date: Tue, 15 Jun 2010 20:26:24 -0300 Subject: Added QVariant::Type as a primitive type. Reviewer: Marcelo Lira Luciano Wolf --- PySide/QtCore/qstring_conversions.h | 6 ++++ PySide/QtCore/qvariant_type_conversions.h | 53 +++++++++++++++++++++++++++++++ PySide/QtCore/typesystem_core.xml | 3 ++ tests/QtSql/CMakeLists.txt | 1 + tests/QtSql/qvarianttype_test.py | 36 +++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 PySide/QtCore/qvariant_type_conversions.h create mode 100644 tests/QtSql/qvarianttype_test.py 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,5 +1,11 @@ namespace Shiboken { +template<> +inline PyTypeObject* SbkType() +{ + return &PyUnicode_Type; +} + template<> struct Converter { 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 +{ + static bool checkType(PyObject* pyObj) + { + return pyObj == Py_None || PyType_Check(pyObj) || Converter::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(&PyString_Type) || pyObj == reinterpret_cast(&PyUnicode_Type)) + typeName = "QString"; + else if (pyObj == reinterpret_cast(&PyFloat_Type)) + typeName = "double"; // float is a UserType in QVariant. + else if (pyObj == reinterpret_cast(&PyLong_Type)) + typeName = "int"; // long is a UserType in QVariant. + else if (PyType_Check(pyObj)) + typeName = reinterpret_cast(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(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(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 @@ + + + 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() -- cgit v1.2.3