Implemented convertion from Python types for QDate, QTime, QDateTime.
Fixes bug #680 Reviewer: pcacjr <pcacjr@gmail.com> Hugo Parente <hugo.lima@openbossa.org>
This commit is contained in:
parent
75d27311e5
commit
3546a3674f
6 changed files with 177 additions and 109 deletions
|
|
@ -1,35 +1,45 @@
|
||||||
namespace Shiboken {
|
namespace Shiboken {
|
||||||
template <>
|
|
||||||
struct PythonConverter<QDate>
|
inline bool Converter<QDate>::checkType(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
static bool isPythonConvertible(PyObject* pyObj)
|
return ValueTypeConverter<QDate>::checkType(pyObj);
|
||||||
{
|
}
|
||||||
if (!PyDateTimeAPI)
|
|
||||||
PyDateTime_IMPORT;
|
inline PyObject* Converter<QDate>::toPython(const ::QDate& cppObj)
|
||||||
|
{
|
||||||
return pyObj && PyDate_Check(pyObj);
|
return ValueTypeConverter<QDate>::toPython(cppObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static QDate* transformFromPython(PyObject* obj)
|
inline bool Converter<QDate>::isConvertible(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
if (isPythonConvertible(obj)) {
|
if (ValueTypeConverter<QDate>::isConvertible(pyObj))
|
||||||
int day = PyDateTime_GET_DAY(obj);
|
return true;
|
||||||
int month = PyDateTime_GET_MONTH(obj);
|
|
||||||
int year = PyDateTime_GET_YEAR(obj);
|
if (!PyDateTimeAPI)
|
||||||
return new QDate(year, month, day);
|
PyDateTime_IMPORT;
|
||||||
}
|
|
||||||
return 0;
|
SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType< ::QDate >());
|
||||||
}
|
return PyDate_Check(pyObj) || ObjectType::isExternalConvertible(shiboType, pyObj);
|
||||||
|
}
|
||||||
static PyObject* transformToPython(QDate* d)
|
|
||||||
{
|
inline QDate Converter<QDate>::toCpp(PyObject* pyObj)
|
||||||
if (d) {
|
{
|
||||||
if (!PyDateTimeAPI)
|
if (!PyDateTimeAPI)
|
||||||
PyDateTime_IMPORT;
|
PyDateTime_IMPORT;
|
||||||
|
|
||||||
return PyDate_FromDate(d->year(), d->month(), d->day());
|
if (pyObj == Py_None) {
|
||||||
}
|
return QDate();
|
||||||
return 0;
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,41 +1,49 @@
|
||||||
namespace Shiboken {
|
namespace Shiboken {
|
||||||
template <>
|
|
||||||
struct PythonConverter<QDateTime>
|
inline bool Converter<QDateTime>::checkType(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
static bool isPythonConvertible(PyObject* pyObj)
|
return ValueTypeConverter<QDateTime>::checkType(pyObj);
|
||||||
{
|
}
|
||||||
if (!PyDateTimeAPI)
|
|
||||||
PyDateTime_IMPORT;
|
inline PyObject* Converter<QDateTime>::toPython(const ::QDateTime& cppObj)
|
||||||
|
{
|
||||||
return pyObj && PyDateTime_Check(pyObj);
|
return ValueTypeConverter<QDateTime>::toPython(cppObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static QDateTime* transformFromPython(PyObject* obj)
|
inline bool Converter<QDateTime>::isConvertible(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
if (isPythonConvertible(obj)) {
|
if (ValueTypeConverter<QDateTime>::isConvertible(pyObj))
|
||||||
int day = PyDateTime_GET_DAY(obj);
|
return true;
|
||||||
int month = PyDateTime_GET_MONTH(obj);
|
|
||||||
int year = PyDateTime_GET_YEAR(obj);
|
if (!PyDateTimeAPI)
|
||||||
int hour = PyDateTime_DATE_GET_HOUR(obj);
|
PyDateTime_IMPORT;
|
||||||
int min = PyDateTime_DATE_GET_MINUTE(obj);
|
|
||||||
int sec = PyDateTime_DATE_GET_SECOND(obj);
|
SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType< ::QDateTime >());
|
||||||
int msec = PyDateTime_DATE_GET_MICROSECOND(obj);
|
return PyDateTime_Check(pyObj) || ObjectType::isExternalConvertible(shiboType, pyObj);
|
||||||
return new QDateTime(QDate(year, month, day), QTime(hour, min, sec, msec));
|
}
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
inline QDateTime Converter<QDateTime>::toCpp(PyObject* pyObj)
|
||||||
|
{
|
||||||
static PyObject* transformToPython(QDateTime* d)
|
if (!PyDateTimeAPI)
|
||||||
{
|
PyDateTime_IMPORT;
|
||||||
if (d) {
|
|
||||||
if (!PyDateTimeAPI)
|
if (pyObj == Py_None) {
|
||||||
PyDateTime_IMPORT;
|
return QDateTime();
|
||||||
|
} else if (PyObject_TypeCheck(pyObj, SbkType<QDateTime>())) {
|
||||||
QDate date = d->date();
|
return *Converter<QDateTime*>::toCpp(pyObj);
|
||||||
QTime time = d->time();
|
} else if (PyDateTime_Check(pyObj)) {
|
||||||
return PyDateTime_FromDateAndTime(date.year(), date.month(), date.day(), time.hour(), time.minute(), time.second(), time.msec());
|
int day = PyDateTime_GET_DAY(pyObj);
|
||||||
}
|
int month = PyDateTime_GET_MONTH(pyObj);
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,45 @@
|
||||||
namespace Shiboken {
|
namespace Shiboken {
|
||||||
template <>
|
|
||||||
struct PythonConverter<QTime>
|
inline bool Converter<QTime>::checkType(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
static bool isPythonConvertible(PyObject* pyObj)
|
return ValueTypeConverter<QTime>::checkType(pyObj);
|
||||||
{
|
}
|
||||||
if (!PyDateTimeAPI)
|
|
||||||
PyDateTime_IMPORT;
|
inline PyObject* Converter<QTime>::toPython(const ::QTime& cppObj)
|
||||||
|
{
|
||||||
return pyObj && PyTime_Check(pyObj);
|
return ValueTypeConverter<QTime>::toPython(cppObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static QTime* transformFromPython(PyObject* obj)
|
inline bool Converter<QTime>::isConvertible(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
if (isPythonConvertible(obj)) {
|
if (ValueTypeConverter<QTime>::isConvertible(pyObj))
|
||||||
int hour = PyDateTime_TIME_GET_HOUR(obj);
|
return true;
|
||||||
int min = PyDateTime_TIME_GET_MINUTE(obj);
|
|
||||||
int sec = PyDateTime_TIME_GET_SECOND(obj);
|
if (!PyDateTimeAPI)
|
||||||
int msec = PyDateTime_TIME_GET_MICROSECOND(obj);
|
PyDateTime_IMPORT;
|
||||||
return new QTime(hour, min, sec, msec);
|
|
||||||
}
|
SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType< ::QTime >());
|
||||||
return 0;
|
return PyTime_Check(pyObj) || ObjectType::isExternalConvertible(shiboType, pyObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject* transformToPython(QTime* d)
|
inline QTime Converter<QTime>::toCpp(PyObject* pyObj)
|
||||||
{
|
{
|
||||||
if (d) {
|
if (!PyDateTimeAPI)
|
||||||
if (!PyDateTimeAPI)
|
PyDateTime_IMPORT;
|
||||||
PyDateTime_IMPORT;
|
|
||||||
|
if (pyObj == Py_None) {
|
||||||
return PyTime_FromTime(d->hour(), d->minute(), d->second(), d->msec());
|
return QTime();
|
||||||
}
|
} else if (PyObject_TypeCheck(pyObj, SbkType<QTime>())) {
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -140,6 +140,7 @@
|
||||||
<conversion-rule file="qstringlist_conversions.h" />
|
<conversion-rule file="qstringlist_conversions.h" />
|
||||||
</container-type>
|
</container-type>
|
||||||
<primitive-type name="QStringRef">
|
<primitive-type name="QStringRef">
|
||||||
|
<include file-name="datetime.h" location="global"/>
|
||||||
<conversion-rule file="qstringref_conversions.h" />
|
<conversion-rule file="qstringref_conversions.h" />
|
||||||
</primitive-type>
|
</primitive-type>
|
||||||
<primitive-type name="QChar">
|
<primitive-type name="QChar">
|
||||||
|
|
@ -559,7 +560,7 @@
|
||||||
<value-type name="QBasicTimer"/>
|
<value-type name="QBasicTimer"/>
|
||||||
<value-type name="QByteArrayMatcher"/>
|
<value-type name="QByteArrayMatcher"/>
|
||||||
<value-type name="QDate" hash-function="PySide::hash" >
|
<value-type name="QDate" hash-function="PySide::hash" >
|
||||||
<conversion-rule class="target" file="qdate_conversions.h"/>
|
<conversion-rule file="qdate_conversions.h"/>
|
||||||
<extra-includes>
|
<extra-includes>
|
||||||
<include file-name="datetime.h" location="global"/>
|
<include file-name="datetime.h" location="global"/>
|
||||||
</extra-includes>
|
</extra-includes>
|
||||||
|
|
@ -581,6 +582,12 @@
|
||||||
</insert-template>
|
</insert-template>
|
||||||
</inject-code>
|
</inject-code>
|
||||||
</add-function>
|
</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-function signature="getDate(int*,int*,int*)" >
|
||||||
<modify-argument index="1">
|
<modify-argument index="1">
|
||||||
<remove-argument/>
|
<remove-argument/>
|
||||||
|
|
@ -617,7 +624,7 @@
|
||||||
<modify-function signature="julianToGregorian(uint,int&,int&,int&)" remove="all"/>
|
<modify-function signature="julianToGregorian(uint,int&,int&,int&)" remove="all"/>
|
||||||
</value-type>
|
</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"/>
|
<conversion-rule file="qdatetime_conversions.h"/>
|
||||||
<extra-includes>
|
<extra-includes>
|
||||||
<include file-name="datetime.h" location="global"/>
|
<include file-name="datetime.h" location="global"/>
|
||||||
</extra-includes>
|
</extra-includes>
|
||||||
|
|
@ -656,6 +663,14 @@
|
||||||
</insert-template>
|
</insert-template>
|
||||||
</inject-code>
|
</inject-code>
|
||||||
</add-function>
|
</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>
|
||||||
<value-type name="QDir">
|
<value-type name="QDir">
|
||||||
<enum-type name="Filter" flags="Filters"/>
|
<enum-type name="Filter" flags="Filters"/>
|
||||||
|
|
@ -954,6 +969,11 @@
|
||||||
</value-type>
|
</value-type>
|
||||||
|
|
||||||
<value-type name="QTime" hash-function="PySide::hash">
|
<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*">
|
<add-function signature="__repr__" return-type="PyObject*">
|
||||||
<inject-code class="target" position="beginning">
|
<inject-code class="target" position="beginning">
|
||||||
<insert-template name="repr_code">
|
<insert-template name="repr_code">
|
||||||
|
|
@ -970,10 +990,12 @@
|
||||||
</insert-template>
|
</insert-template>
|
||||||
</inject-code>
|
</inject-code>
|
||||||
</add-function>
|
</add-function>
|
||||||
<conversion-rule class="target" file="qtime_conversions.h"/>
|
<add-function signature="toPython()" return-type="PyObject">
|
||||||
<extra-includes>
|
<inject-code class="target" position="beginning">
|
||||||
<include file-name="datetime.h" location="global"/>
|
if (!PyDateTimeAPI) PyDateTime_IMPORT;
|
||||||
</extra-includes>
|
%PYARG_0 = PyTime_FromTime(%CPPSELF.hour(), %CPPSELF.minute(), %CPPSELF.second(), %CPPSELF.msec());
|
||||||
|
</inject-code>
|
||||||
|
</add-function>
|
||||||
</value-type>
|
</value-type>
|
||||||
<value-type name="QPersistentModelIndex">
|
<value-type name="QPersistentModelIndex">
|
||||||
<modify-function signature="internalPointer()const">
|
<modify-function signature="internalPointer()const">
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ PYSIDE_TEST(qbytearray_operator_test.py)
|
||||||
PYSIDE_TEST(qbytearray_test.py)
|
PYSIDE_TEST(qbytearray_test.py)
|
||||||
PYSIDE_TEST(qcoreapplication_instance_test.py)
|
PYSIDE_TEST(qcoreapplication_instance_test.py)
|
||||||
PYSIDE_TEST(qdatastream_test.py)
|
PYSIDE_TEST(qdatastream_test.py)
|
||||||
|
PYSIDE_TEST(qdatetime_test.py)
|
||||||
PYSIDE_TEST(qdate_test.py)
|
PYSIDE_TEST(qdate_test.py)
|
||||||
PYSIDE_TEST(qenum_test.py)
|
PYSIDE_TEST(qenum_test.py)
|
||||||
PYSIDE_TEST(qevent_test.py)
|
PYSIDE_TEST(qevent_test.py)
|
||||||
|
|
|
||||||
18
tests/QtCore/qdatetime_test.py
Normal file
18
tests/QtCore/qdatetime_test.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue