summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Kiagiadakis <george.kiagiadakis@collabora.co.uk>2011-01-16 16:45:44 +0200
committerGeorge Kiagiadakis <george.kiagiadakis@collabora.co.uk>2011-01-16 17:24:58 +0200
commitc4be24c1040e228c31338503dc3fecdf6a10fee1 (patch)
treef5925eb442434afaf9e177b2ff7323a6f359b5fd
parent974b1eaf0dca68a1d3419ebdfba07cd520193ae9 (diff)
Add ValueVTable for QDate and QDateTime.
-rw-r--r--src/QGst/global.h6
-rw-r--r--src/QGst/value.cpp92
-rw-r--r--tests/auto/valuetest.cpp33
3 files changed, 131 insertions, 0 deletions
diff --git a/src/QGst/global.h b/src/QGst/global.h
index 1153849..ae64cfd 100644
--- a/src/QGst/global.h
+++ b/src/QGst/global.h
@@ -19,6 +19,7 @@
#include "../QGlib/type.h"
#include <QtCore/QtGlobal>
+#include <QtCore/QDate>
#include <QtCore/QSharedPointer>
/* defined by cmake when building this library */
@@ -41,6 +42,11 @@
//cyclic dependency, must include after defining the above
#include "enums.h"
+//registered in value.cpp
+QGST_REGISTER_TYPE(QDate) //codegen: skip=true
+QGST_REGISTER_TYPE(QDateTime) //codegen: skip=true
+
+
#define QGST_WRAPPER_GSTCLASS_DECLARATION(Class) \
typedef struct _Gst##Class Gst##Class;
diff --git a/src/QGst/value.cpp b/src/QGst/value.cpp
index a86d704..3a26380 100644
--- a/src/QGst/value.cpp
+++ b/src/QGst/value.cpp
@@ -19,8 +19,17 @@
#include "miniobject.h"
#include "structure.h"
#include "../QGlib/value.h"
+#include <cmath>
#include <gst/gstvalue.h>
#include <gst/gstminiobject.h>
+#include <gst/gstdatetime.h>
+
+namespace QGlib {
+
+GetTypeImpl<QDate>::operator Type() { return GST_TYPE_DATE; }
+GetTypeImpl<QDateTime>::operator Type() { return GST_TYPE_DATE_TIME; }
+
+} //namespace QGlib
namespace QGst {
namespace Private {
@@ -153,6 +162,89 @@ void registerValueVTables()
};
QGlib::Value::registerValueVTable(QGlib::GetType<Structure>(),
QGlib::ValueVTable(ValueVTable_Structure::set, ValueVTable_Structure::get));
+
+ struct ValueVTable_QDate
+ {
+ static void get(const QGlib::Value & value, void *data)
+ {
+ const GDate *gdate = gst_value_get_date(value);
+ *reinterpret_cast<QDate*>(data) = QDate(g_date_get_year(gdate),
+ g_date_get_month(gdate),
+ g_date_get_day(gdate));
+ }
+
+ static void set(QGlib::Value & value, const void *data)
+ {
+ const QDate *qdate = reinterpret_cast<QDate const *>(data);
+ GDate *gdate = g_date_new_dmy(qdate->day(),
+ static_cast<GDateMonth>(qdate->month()),
+ qdate->year());
+ gst_value_set_date(value, gdate);
+ g_date_free(gdate);
+ }
+ };
+ QGlib::Value::registerValueVTable(QGlib::GetType<QDate>(),
+ QGlib::ValueVTable(ValueVTable_QDate::set, ValueVTable_QDate::get));
+
+ struct ValueVTable_QDateTime
+ {
+ static void get(const QGlib::Value & value, void *data)
+ {
+ const GstDateTime *gdatetime = static_cast<GstDateTime*>(g_value_get_boxed(value));
+
+ QDate date = QDate(gst_date_time_get_year(gdatetime),
+ gst_date_time_get_month(gdatetime),
+ gst_date_time_get_day(gdatetime));
+
+ /* timezone conversion */
+ float tzoffset = gst_date_time_get_time_zone_offset(gdatetime);
+ float hourOffset;
+ float minutesOffset = std::modf(tzoffset, &hourOffset);
+
+ int hour = gst_date_time_get_hour(gdatetime) - hourOffset;
+ int minute = gst_date_time_get_minute(gdatetime) - (minutesOffset * 60);
+
+ /* handle overflow */
+ if (minute >= 60) {
+ hour++;
+ minute -= 60;
+ } else if (minute < 0) {
+ hour--;
+ minute = 60 + minute;
+ }
+
+ if (hour >= 24) {
+ date = date.addDays(1);
+ hour -= 24;
+ } else if (hour < 0) {
+ date = date.addDays(-1);
+ hour = 24 + hour;
+ }
+
+ QTime time = QTime(hour, minute,
+ gst_date_time_get_second(gdatetime),
+ gst_date_time_get_microsecond(gdatetime)/1000);
+
+ *reinterpret_cast<QDateTime*>(data) = QDateTime(date, time, Qt::UTC);
+ }
+
+ static void set(QGlib::Value & value, const void *data)
+ {
+ QDateTime qdatetime = reinterpret_cast<QDateTime const *>(data)->toUTC();
+ GstDateTime *gdatetime = gst_date_time_new(0.0f,
+ qdatetime.date().year(),
+ qdatetime.date().month(),
+ qdatetime.date().day(),
+ qdatetime.time().hour(),
+ qdatetime.time().minute(),
+ qdatetime.time().second() + (qdatetime.time().msec()/1000.0)
+ );
+
+ g_value_take_boxed(value, gdatetime);
+ }
+ };
+ QGlib::Value::registerValueVTable(QGlib::GetType<QDateTime>(),
+ QGlib::ValueVTable(ValueVTable_QDateTime::set, ValueVTable_QDateTime::get));
}
} //namespace Private
diff --git a/tests/auto/valuetest.cpp b/tests/auto/valuetest.cpp
index 47ff9a4..22f4aea 100644
--- a/tests/auto/valuetest.cpp
+++ b/tests/auto/valuetest.cpp
@@ -41,6 +41,7 @@ private Q_SLOTS:
void copyTest();
void castTest();
void qdebugTest();
+ void datetimeTest();
};
void ValueTest::intTest()
@@ -225,6 +226,38 @@ void ValueTest::qdebugTest()
qDebug() << QGlib::Value::create(QGlib::ObjectPtr());
}
+void ValueTest::datetimeTest()
+{
+ {
+ QDateTime d = QDateTime::currentDateTime();
+ QGlib::Value v = QGlib::Value::create(d);
+ QCOMPARE(v.get<QDateTime>(), d);
+ }
+
+ {
+ GstDateTime *gstDateTime = gst_date_time_new(1.0f,
+ 2011, 01, 16,
+ 15, 15, 23.132);
+ QGlib::Value v;
+ v.init(GST_TYPE_DATE_TIME);
+ g_value_take_boxed(v, gstDateTime);
+
+ QDateTime d = v.get<QDateTime>();
+ QCOMPARE(d, QDateTime(QDate(2011, 01, 16), QTime(14, 15, 23, 132), Qt::UTC));
+ }
+
+ {
+ GstDateTime *gstDateTime = gst_date_time_new(-10.0f,
+ 2011, 01, 16,
+ 20, 50, 43.592);
+ QGlib::Value v;
+ v.init(GST_TYPE_DATE_TIME);
+ g_value_take_boxed(v, gstDateTime);
+
+ QDateTime d = v.get<QDateTime>();
+ QCOMPARE(d, QDateTime(QDate(2011, 01, 17), QTime(6, 50, 43, 592), Qt::UTC));
+ }
+}
QTEST_APPLESS_MAIN(ValueTest)