aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrenatofilho <renato.filho@openbossa.org>2010-10-01 19:00:36 -0300
committerrenatofilho <renato.filho@openbossa.org>2010-10-01 19:45:30 -0300
commit940b90e80a7ec21e513ac16ff73d8fa8cf23d489 (patch)
treee75a0dd76faab2379d23f72a1e8ac6ab82e98b75
parent288a53369fa8df74a92c7517e8744c5139797c78 (diff)
Implement python conversion to QTime, QDate, QDateTime
Fixes bug #371. Reviewer: Hugo Parente Lima <hugo.pl@gmail.com> Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--PySide/QtCore/qdate_conversions.h35
-rw-r--r--PySide/QtCore/qdatetime_conversions.h41
-rw-r--r--PySide/QtCore/qtime_conversions.h36
-rw-r--r--PySide/QtCore/typesystem_core.xml20
-rw-r--r--tests/QtCore/CMakeLists.txt1
-rw-r--r--tests/QtCore/python_conversion.py50
6 files changed, 181 insertions, 2 deletions
diff --git a/PySide/QtCore/qdate_conversions.h b/PySide/QtCore/qdate_conversions.h
new file mode 100644
index 000000000..e0f1dd953
--- /dev/null
+++ b/PySide/QtCore/qdate_conversions.h
@@ -0,0 +1,35 @@
+namespace Shiboken {
+template <>
+struct PythonConverter<QDate>
+{
+ static bool isPythonConvertible(PyObject* pyObj)
+ {
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ return pyObj && PyDate_Check(pyObj);
+ }
+
+ 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;
+ }
+
+ static PyObject* transformToPython(QDate* d)
+ {
+ if (d) {
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ return PyDate_FromDate(d->year(), d->month(), d->day());
+ }
+ return 0;
+ }
+};
+}
diff --git a/PySide/QtCore/qdatetime_conversions.h b/PySide/QtCore/qdatetime_conversions.h
new file mode 100644
index 000000000..a3cdf0927
--- /dev/null
+++ b/PySide/QtCore/qdatetime_conversions.h
@@ -0,0 +1,41 @@
+namespace Shiboken {
+template <>
+struct PythonConverter<QDateTime>
+{
+ static bool isPythonConvertible(PyObject* pyObj)
+ {
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ return pyObj && PyDateTime_Check(pyObj);
+ }
+
+ 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;
+ }
+
+ 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;
+ }
+};
+}
diff --git a/PySide/QtCore/qtime_conversions.h b/PySide/QtCore/qtime_conversions.h
new file mode 100644
index 000000000..5e172fa07
--- /dev/null
+++ b/PySide/QtCore/qtime_conversions.h
@@ -0,0 +1,36 @@
+namespace Shiboken {
+template <>
+struct PythonConverter<QTime>
+{
+ static bool isPythonConvertible(PyObject* pyObj)
+ {
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ return pyObj && PyTime_Check(pyObj);
+ }
+
+ 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;
+ }
+
+ static PyObject* transformToPython(QTime* d)
+ {
+ if (d) {
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+
+ return PyTime_FromTime(d->hour(), d->minute(), d->second(), d->msec());
+ }
+ return 0;
+ }
+};
+}
diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml
index 68bd04cdd..704e8d468 100644
--- a/PySide/QtCore/typesystem_core.xml
+++ b/PySide/QtCore/typesystem_core.xml
@@ -721,6 +721,10 @@
<value-type name="QBasicTimer"/>
<value-type name="QByteArrayMatcher"/>
<value-type name="QDate" hash-function="PySide::hash" >
+ <conversion-rule class="target" file="qdate_conversions.h"/>
+ <extra-includes>
+ <include file-name="datetime.h" location="global"/>
+ </extra-includes>
<enum-type name="MonthNameType"/>
<modify-function signature="julianToGregorian(uint,int&amp;,int&amp;,int&amp;)" remove="all"/>
<modify-function signature="getDate(int*,int*,int*)" >
@@ -756,7 +760,14 @@
</inject-code>
</modify-function>
</value-type>
- <value-type name="QDateTime" hash-function="PySide::hash"/>
+
+ <value-type name="QDateTime" hash-function="PySide::hash">
+ <conversion-rule class="target" file="qdatetime_conversions.h"/>
+ <extra-includes>
+ <include file-name="datetime.h" location="global"/>
+ </extra-includes>
+ </value-type>
+
<value-type name="QDir">
<enum-type name="Filter" flags="QDir::Filters"/>
<enum-type name="SortFlag" flags="QDir::SortFlags" />
@@ -818,7 +829,12 @@
<modify-function signature="rwidth()" remove="all" />
</value-type>
- <value-type name="QTime" hash-function="PySide::hash"/>
+ <value-type name="QTime" hash-function="PySide::hash">
+ <conversion-rule class="target" file="qtime_conversions.h"/>
+ <extra-includes>
+ <include file-name="datetime.h" location="global"/>
+ </extra-includes>
+ </value-type>
<value-type name="QPersistentModelIndex">
<modify-function signature="internalPointer()const" remove="all"/>
<modify-function signature="operator const QModelIndex&amp;()const" remove="all" /> <!-- FIXME Removed due to a shiboken bug-->
diff --git a/tests/QtCore/CMakeLists.txt b/tests/QtCore/CMakeLists.txt
index 210aa01de..30f3ff697 100644
--- a/tests/QtCore/CMakeLists.txt
+++ b/tests/QtCore/CMakeLists.txt
@@ -6,6 +6,7 @@ PYSIDE_TEST(deletelater_test.py)
PYSIDE_TEST(duck_punching_test.py)
PYSIDE_TEST(hash_test.py)
PYSIDE_TEST(missing_symbols_test.py)
+PYSIDE_TEST(python_conversion.py)
PYSIDE_TEST(qabs_test.py)
PYSIDE_TEST(qabstractitemmodel_test.py)
PYSIDE_TEST(qabstracttransition_test.py)
diff --git a/tests/QtCore/python_conversion.py b/tests/QtCore/python_conversion.py
new file mode 100644
index 000000000..43dfccfb6
--- /dev/null
+++ b/tests/QtCore/python_conversion.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+'''Test cases for QLineF'''
+
+import unittest
+import os
+import datetime
+
+from PySide.QtCore import *
+
+class TestDateTimeConversions (unittest.TestCase):
+ def testQDate(self):
+ date = datetime.date(2010, 04, 23)
+ other = QDate(date)
+ self.assertEqual(date.year, other.year())
+ self.assertEqual(date.month, other.month())
+ self.assertEqual(date.day, other.day())
+
+ self.assertEqual(date, other.toPython())
+
+ def testQTime(self):
+ time = datetime.time(11, 14, 00, 01)
+ other = QTime(time)
+ self.assertEqual(time.hour, other.hour())
+ self.assertEqual(time.minute, other.minute())
+ self.assertEqual(time.second, other.second())
+ self.assertEqual(time.microsecond, other.msec())
+
+ self.assertEqual(time, other.toPython())
+
+ def testQDateTime(self):
+ dateTime = datetime.datetime(2010, 04, 23, 11, 14, 00, 01)
+ other = QDateTime(dateTime)
+
+ otherDate = other.date()
+ self.assertEqual(dateTime.year, otherDate.year())
+ self.assertEqual(dateTime.month, otherDate.month())
+ self.assertEqual(dateTime.day, otherDate.day())
+
+ otherTime = other.time()
+ self.assertEqual(dateTime.hour, otherTime.hour())
+ self.assertEqual(dateTime.minute, otherTime.minute())
+ self.assertEqual(dateTime.second, otherTime.second())
+ self.assertEqual(dateTime.microsecond, otherTime.msec())
+
+ self.assertEqual(dateTime, other.toPython())
+
+
+
+if __name__ == '__main__':
+ unittest.main()