diff options
author | Renato Filho <renato.filho@openbossa.org> | 2011-05-17 17:36:04 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:54:23 -0300 |
commit | 8e221bfb9731589a4a4d4712745252e50b0a97c6 (patch) | |
tree | 8218a7ffe61da431312d7003b37547c089aca03f /PySide | |
parent | 952f9f0bda958afa36421c6af828f55f643a3deb (diff) |
Implemented convertion from Python types for QDate, QTime, QDateTime.
Fixes bug #680
Reviewer: pcacjr <pcacjr@gmail.com>
Hugo Parente <hugo.lima@openbossa.org>
Diffstat (limited to 'PySide')
-rw-r--r-- | PySide/QtCore/qdate_conversions.h | 64 | ||||
-rw-r--r-- | PySide/QtCore/qdatetime_conversions.h | 76 | ||||
-rw-r--r-- | PySide/QtCore/qtime_conversions.h | 65 | ||||
-rw-r--r-- | PySide/QtCore/typesystem_core.xml | 34 |
4 files changed, 144 insertions, 95 deletions
diff --git a/PySide/QtCore/qdate_conversions.h b/PySide/QtCore/qdate_conversions.h index e0f1dd953..7c45eece9 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 a3cdf0927..8ee4f4882 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 5e172fa07..080df5f2f 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 5a53261b7..37c246b93 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&,int&,int&)" 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> @@ -648,6 +655,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"/> @@ -946,6 +961,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"> @@ -962,10 +982,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"> |