summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRenato Filho <renato.filho@openbossa.org>2011-05-17 17:36:04 -0300
committerRenato Filho <renato.filho@openbossa.org>2011-05-18 11:00:03 -0300
commit3546a3674f78abb1f3455e56456a571c6bcb4bef (patch)
tree884983b7d9ac5bbfb01dc061629819b3e4c65ab7
parent75d27311e5780bd12ab26380d31b100144d9aaf7 (diff)
downloadpyside-3546a3674f78abb1f3455e56456a571c6bcb4bef.tar.gz
pyside-3546a3674f78abb1f3455e56456a571c6bcb4bef.tar.xz
pyside-3546a3674f78abb1f3455e56456a571c6bcb4bef.zip
Implemented convertion from Python types for QDate, QTime, QDateTime.
Fixes bug #680 Reviewer: pcacjr <pcacjr@gmail.com> Hugo Parente <hugo.lima@openbossa.org>
-rw-r--r--PySide/QtCore/qdate_conversions.h64
-rw-r--r--PySide/QtCore/qdatetime_conversions.h76
-rw-r--r--PySide/QtCore/qtime_conversions.h65
-rw-r--r--PySide/QtCore/typesystem_core.xml34
-rw-r--r--tests/QtCore/CMakeLists.txt1
-rw-r--r--tests/QtCore/qdatetime_test.py18
6 files changed, 163 insertions, 95 deletions
diff --git a/PySide/QtCore/qdate_conversions.h b/PySide/QtCore/qdate_conversions.h
index e0f1dd9..7c45eec 100644
--- a/PySide/QtCore/qdate_conversions.h
+++ b/PySide/QtCore/qdate_conversions.h
@@ -1,35 +1,45 @@
namespace Shiboken {
-template <>
-struct PythonConverter<QDate>
+
+inline bool Converter<QDate>::checkType(PyObject* pyObj)
{
- static bool isPythonConvertible(PyObject* pyObj)
- {
- if (!PyDateTimeAPI)
- PyDateTime_IMPORT;
+ return ValueTypeConverter<QDate>::checkType(pyObj);
+}
- return pyObj && PyDate_Check(pyObj);
- }
+inline PyObject* Converter<QDate>::toPython(const ::QDate& cppObj)
+{
+ return ValueTypeConverter<QDate>::toPython(cppObj);
+}
- static QDate* transformFromPython(PyObject* obj)
- {
- if (isPythonConvertible(obj)) {
- int day = PyDateTime_GET_DAY(obj);
- int month = PyDateTime_GET_MONTH(obj);
- int year = PyDateTime_GET_YEAR(obj);
- return new QDate(year, month, day);
- }
- return 0;
- }
+inline bool Converter<QDate>::isConvertible(PyObject* pyObj)
+{
+ if (ValueTypeConverter<QDate>::isConvertible(pyObj))
+ return true;
- static PyObject* transformToPython(QDate* d)
- {
- if (d) {
- if (!PyDateTimeAPI)
- PyDateTime_IMPORT;
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
- return PyDate_FromDate(d->year(), d->month(), d->day());
- }
- return 0;
+ SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType< ::QDate >());
+ return PyDate_Check(pyObj) || ObjectType::isExternalConvertible(shiboType, pyObj);
+}
+
+inline QDate Converter<QDate>::toCpp(PyObject* pyObj)
+{
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ if (pyObj == Py_None) {
+ return QDate();
+ } else if (PyObject_TypeCheck(pyObj, SbkType<QDate>())) {
+ return *Converter<QDate*>::toCpp(pyObj);
+ } else if (PyDate_Check(pyObj)) {
+ int day = PyDateTime_GET_DAY(pyObj);
+ int month = PyDateTime_GET_MONTH(pyObj);
+ int year = PyDateTime_GET_YEAR(pyObj);
+ return QDate(year, month, day);
+ } else {
+ return ValueTypeConverter<QDate>::toCpp(pyObj);
}
-};
+}
+
+
}
diff --git a/PySide/QtCore/qdatetime_conversions.h b/PySide/QtCore/qdatetime_conversions.h
index a3cdf09..8ee4f48 100644
--- a/PySide/QtCore/qdatetime_conversions.h
+++ b/PySide/QtCore/qdatetime_conversions.h
@@ -1,41 +1,49 @@
namespace Shiboken {
-template <>
-struct PythonConverter<QDateTime>
+
+inline bool Converter<QDateTime>::checkType(PyObject* pyObj)
{
- static bool isPythonConvertible(PyObject* pyObj)
- {
- if (!PyDateTimeAPI)
- PyDateTime_IMPORT;
+ return ValueTypeConverter<QDateTime>::checkType(pyObj);
+}
- return pyObj && PyDateTime_Check(pyObj);
- }
+inline PyObject* Converter<QDateTime>::toPython(const ::QDateTime& cppObj)
+{
+ return ValueTypeConverter<QDateTime>::toPython(cppObj);
+}
- static QDateTime* transformFromPython(PyObject* obj)
- {
- if (isPythonConvertible(obj)) {
- int day = PyDateTime_GET_DAY(obj);
- int month = PyDateTime_GET_MONTH(obj);
- int year = PyDateTime_GET_YEAR(obj);
- int hour = PyDateTime_DATE_GET_HOUR(obj);
- int min = PyDateTime_DATE_GET_MINUTE(obj);
- int sec = PyDateTime_DATE_GET_SECOND(obj);
- int msec = PyDateTime_DATE_GET_MICROSECOND(obj);
- return new QDateTime(QDate(year, month, day), QTime(hour, min, sec, msec));
- }
- return 0;
- }
+inline bool Converter<QDateTime>::isConvertible(PyObject* pyObj)
+{
+ if (ValueTypeConverter<QDateTime>::isConvertible(pyObj))
+ return true;
+
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
- static PyObject* transformToPython(QDateTime* d)
- {
- if (d) {
- if (!PyDateTimeAPI)
- PyDateTime_IMPORT;
-
- QDate date = d->date();
- QTime time = d->time();
- return PyDateTime_FromDateAndTime(date.year(), date.month(), date.day(), time.hour(), time.minute(), time.second(), time.msec());
- }
- return 0;
+ SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType< ::QDateTime >());
+ return PyDateTime_Check(pyObj) || ObjectType::isExternalConvertible(shiboType, pyObj);
+}
+
+
+inline QDateTime Converter<QDateTime>::toCpp(PyObject* pyObj)
+{
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ if (pyObj == Py_None) {
+ return QDateTime();
+ } else if (PyObject_TypeCheck(pyObj, SbkType<QDateTime>())) {
+ return *Converter<QDateTime*>::toCpp(pyObj);
+ } else if (PyDateTime_Check(pyObj)) {
+ int day = PyDateTime_GET_DAY(pyObj);
+ int month = PyDateTime_GET_MONTH(pyObj);
+ int year = PyDateTime_GET_YEAR(pyObj);
+ int hour = PyDateTime_DATE_GET_HOUR(pyObj);
+ int min = PyDateTime_DATE_GET_MINUTE(pyObj);
+ int sec = PyDateTime_DATE_GET_SECOND(pyObj);
+ int msec = PyDateTime_DATE_GET_MICROSECOND(pyObj);
+ return QDateTime(QDate(year, month, day), QTime(hour, min, sec, msec));
+ } else {
+ return ValueTypeConverter<QDateTime>::toCpp(pyObj);
}
-};
+}
+
}
diff --git a/PySide/QtCore/qtime_conversions.h b/PySide/QtCore/qtime_conversions.h
index 5e172fa..080df5f 100644
--- a/PySide/QtCore/qtime_conversions.h
+++ b/PySide/QtCore/qtime_conversions.h
@@ -1,36 +1,45 @@
namespace Shiboken {
-template <>
-struct PythonConverter<QTime>
+
+inline bool Converter<QTime>::checkType(PyObject* pyObj)
{
- static bool isPythonConvertible(PyObject* pyObj)
- {
- if (!PyDateTimeAPI)
- PyDateTime_IMPORT;
+ return ValueTypeConverter<QTime>::checkType(pyObj);
+}
- return pyObj && PyTime_Check(pyObj);
- }
+inline PyObject* Converter<QTime>::toPython(const ::QTime& cppObj)
+{
+ return ValueTypeConverter<QTime>::toPython(cppObj);
+}
- static QTime* transformFromPython(PyObject* obj)
- {
- if (isPythonConvertible(obj)) {
- int hour = PyDateTime_TIME_GET_HOUR(obj);
- int min = PyDateTime_TIME_GET_MINUTE(obj);
- int sec = PyDateTime_TIME_GET_SECOND(obj);
- int msec = PyDateTime_TIME_GET_MICROSECOND(obj);
- return new QTime(hour, min, sec, msec);
- }
- return 0;
- }
+inline bool Converter<QTime>::isConvertible(PyObject* pyObj)
+{
+ if (ValueTypeConverter<QTime>::isConvertible(pyObj))
+ return true;
- static PyObject* transformToPython(QTime* d)
- {
- if (d) {
- if (!PyDateTimeAPI)
- PyDateTime_IMPORT;
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
- return PyTime_FromTime(d->hour(), d->minute(), d->second(), d->msec());
- }
- return 0;
+ SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType< ::QTime >());
+ return PyTime_Check(pyObj) || ObjectType::isExternalConvertible(shiboType, pyObj);
+}
+
+inline QTime Converter<QTime>::toCpp(PyObject* pyObj)
+{
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ if (pyObj == Py_None) {
+ return QTime();
+ } else if (PyObject_TypeCheck(pyObj, SbkType<QTime>())) {
+ return *Converter<QTime*>::toCpp(pyObj);
+ } else if (PyTime_Check(pyObj)) {
+ int hour = PyDateTime_TIME_GET_HOUR(pyObj);
+ int min = PyDateTime_TIME_GET_MINUTE(pyObj);
+ int sec = PyDateTime_TIME_GET_SECOND(pyObj);
+ int msec = PyDateTime_TIME_GET_MICROSECOND(pyObj);
+ return QTime(hour, min, sec, msec);
+ } else {
+ return ValueTypeConverter<QTime>::toCpp(pyObj);
}
-};
+}
+
}
diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml
index 59a8ae1..de6a11b 100644
--- a/PySide/QtCore/typesystem_core.xml
+++ b/PySide/QtCore/typesystem_core.xml
@@ -140,6 +140,7 @@
<conversion-rule file="qstringlist_conversions.h" />
</container-type>
<primitive-type name="QStringRef">
+ <include file-name="datetime.h" location="global"/>
<conversion-rule file="qstringref_conversions.h" />
</primitive-type>
<primitive-type name="QChar">
@@ -559,7 +560,7 @@
<value-type name="QBasicTimer"/>
<value-type name="QByteArrayMatcher"/>
<value-type name="QDate" hash-function="PySide::hash" >
- <conversion-rule class="target" file="qdate_conversions.h"/>
+ <conversion-rule file="qdate_conversions.h"/>
<extra-includes>
<include file-name="datetime.h" location="global"/>
</extra-includes>
@@ -581,6 +582,12 @@
</insert-template>
</inject-code>
</add-function>
+ <add-function signature="toPython()" return-type="PyObject">
+ <inject-code class="target" position="beginning">
+ if (!PyDateTimeAPI) PyDateTime_IMPORT;
+ %PYARG_0 = PyDate_FromDate(%CPPSELF.year(), %CPPSELF.month(), %CPPSELF.day());
+ </inject-code>
+ </add-function>
<modify-function signature="getDate(int*,int*,int*)" >
<modify-argument index="1">
<remove-argument/>
@@ -617,7 +624,7 @@
<modify-function signature="julianToGregorian(uint,int&amp;,int&amp;,int&amp;)" remove="all"/>
</value-type>
<value-type name="QDateTime" hash-function="PySide::hash">
- <conversion-rule class="target" file="qdatetime_conversions.h"/>
+ <conversion-rule file="qdatetime_conversions.h"/>
<extra-includes>
<include file-name="datetime.h" location="global"/>
</extra-includes>
@@ -656,6 +663,14 @@
</insert-template>
</inject-code>
</add-function>
+ <add-function signature="toPython()" return-type="PyObject">
+ <inject-code class="target" position="beginning">
+ QDate date = %CPPSELF.date();
+ QTime time = %CPPSELF.time();
+ if (!PyDateTimeAPI) PyDateTime_IMPORT;
+ %PYARG_0 = PyDateTime_FromDateAndTime(date.year(), date.month(), date.day(), time.hour(), time.minute(), time.second(), time.msec());
+ </inject-code>
+ </add-function>
</value-type>
<value-type name="QDir">
<enum-type name="Filter" flags="Filters"/>
@@ -954,6 +969,11 @@
</value-type>
<value-type name="QTime" hash-function="PySide::hash">
+ <conversion-rule file="qtime_conversions.h"/>
+ <extra-includes>
+ <include file-name="datetime.h" location="global"/>
+ </extra-includes>
+
<add-function signature="__repr__" return-type="PyObject*">
<inject-code class="target" position="beginning">
<insert-template name="repr_code">
@@ -970,10 +990,12 @@
</insert-template>
</inject-code>
</add-function>
- <conversion-rule class="target" file="qtime_conversions.h"/>
- <extra-includes>
- <include file-name="datetime.h" location="global"/>
- </extra-includes>
+ <add-function signature="toPython()" return-type="PyObject">
+ <inject-code class="target" position="beginning">
+ if (!PyDateTimeAPI) PyDateTime_IMPORT;
+ %PYARG_0 = PyTime_FromTime(%CPPSELF.hour(), %CPPSELF.minute(), %CPPSELF.second(), %CPPSELF.msec());
+ </inject-code>
+ </add-function>
</value-type>
<value-type name="QPersistentModelIndex">
<modify-function signature="internalPointer()const">
diff --git a/tests/QtCore/CMakeLists.txt b/tests/QtCore/CMakeLists.txt
index e49e390..c0829f4 100644
--- a/tests/QtCore/CMakeLists.txt
+++ b/tests/QtCore/CMakeLists.txt
@@ -38,6 +38,7 @@ PYSIDE_TEST(qbytearray_operator_test.py)
PYSIDE_TEST(qbytearray_test.py)
PYSIDE_TEST(qcoreapplication_instance_test.py)
PYSIDE_TEST(qdatastream_test.py)
+PYSIDE_TEST(qdatetime_test.py)
PYSIDE_TEST(qdate_test.py)
PYSIDE_TEST(qenum_test.py)
PYSIDE_TEST(qevent_test.py)
diff --git a/tests/QtCore/qdatetime_test.py b/tests/QtCore/qdatetime_test.py
new file mode 100644
index 0000000..b732477
--- /dev/null
+++ b/tests/QtCore/qdatetime_test.py
@@ -0,0 +1,18 @@
+import unittest
+import datetime
+
+from PySide.QtCore import QDateTime, QDate, QTime
+
+class TestQDate (unittest.TestCase):
+ def testDateConversion(self):
+ dateTime = QDateTime(QDate(2011, 05, 17), QTime(11, 01, 14, 15))
+ dateTimePy = QDateTime(datetime.date(2011, 05, 17), datetime.time(11, 01, 14, 15))
+ self.assertEqual(dateTime, dateTimePy)
+
+ def testDateTimeConversion(self):
+ dateTime = QDateTime(QDate(2011, 05, 17), QTime(11, 01, 14, 15))
+ dateTimePy = QDateTime(datetime.datetime(2011, 05, 17, 11, 01, 14, 15))
+ self.assertEqual(dateTime, dateTimePy)
+
+if __name__ == '__main__':
+ unittest.main()