From d61fab77831cb9e16cf8bc13b6848e22a5796a61 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Wed, 25 Sep 2019 10:04:46 +0200 Subject: [PATCH] implement date() and time() in QgsDateTimeEdit to handle NULL values --- .../editorwidgets/qgsdatetimeedit.sip.in | 19 ++++- src/gui/editorwidgets/qgsdatetimeedit.cpp | 24 ++++++ src/gui/editorwidgets/qgsdatetimeedit.h | 17 +++- tests/src/gui/CMakeLists.txt | 1 + tests/src/gui/testqgsdatetimeedit.cpp | 83 +++++++++++++++++++ 5 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 tests/src/gui/testqgsdatetimeedit.cpp diff --git a/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in b/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in index ef66832faf2..2b85f86121e 100644 --- a/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in +++ b/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in @@ -31,6 +31,7 @@ The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/read Constructor for QgsDateTimeEdit The current date and time is used by default. The widget is allowing null by default. +If allow null is disabled, you should check allowNull before getting values from the widget. %End void setAllowNull( bool allowNull ); @@ -58,17 +59,31 @@ setDateTime set the date time in the widget and handles null date times. QDateTime dateTime() const; %Docstring -dateTime returns the date time which can eventually be a null date/time +dateTime returns the date time which can be a null date/time .. note:: - You mustn't call date() or time() because they can't return a NULL value. + Before QGIS 3.10, you mustn't call date() or time() because they can't return a NULL value. .. note:: since QDateTimeEdit.dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit %End + QTime time() const; +%Docstring +time returns the time which can be a null time. + +.. versionadded:: 3.10 +%End + + QDate date() const; +%Docstring +date returns the date which can be a null date. + +.. versionadded:: 3.10 +%End + virtual void clear(); %Docstring diff --git a/src/gui/editorwidgets/qgsdatetimeedit.cpp b/src/gui/editorwidgets/qgsdatetimeedit.cpp index 5f7b4dbcac2..a2e08d8daa2 100644 --- a/src/gui/editorwidgets/qgsdatetimeedit.cpp +++ b/src/gui/editorwidgets/qgsdatetimeedit.cpp @@ -275,3 +275,27 @@ QDateTime QgsDateTimeEdit::dateTime() const return QDateTimeEdit::dateTime(); } } + +QTime QgsDateTimeEdit::time() const +{ + if ( mAllowNull && mIsNull ) + { + return QTime(); + } + else + { + return QDateTimeEdit::time(); + } +} + +QDate QgsDateTimeEdit::date() const +{ + if ( mAllowNull && mIsNull ) + { + return QDate(); + } + else + { + return QDateTimeEdit::date(); + } +} diff --git a/src/gui/editorwidgets/qgsdatetimeedit.h b/src/gui/editorwidgets/qgsdatetimeedit.h index 973dafc98d0..d72d9d6903f 100644 --- a/src/gui/editorwidgets/qgsdatetimeedit.h +++ b/src/gui/editorwidgets/qgsdatetimeedit.h @@ -40,6 +40,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit * Constructor for QgsDateTimeEdit * The current date and time is used by default. * The widget is allowing null by default. + * If allow null is disabled, you should check allowNull before getting values from the widget. */ explicit QgsDateTimeEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr ); @@ -62,12 +63,24 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit void setDateTime( const QDateTime &dateTime ); /** - * \brief dateTime returns the date time which can eventually be a null date/time - * \note You mustn't call date() or time() because they can't return a NULL value. + * \brief dateTime returns the date time which can be a null date/time + * \note Before QGIS 3.10, you mustn't call date() or time() because they can't return a NULL value. * \note since QDateTimeEdit::dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit */ QDateTime dateTime() const; + /** + * \brief time returns the time which can be a null time. + * \since QGIS 3.10 + */ + QTime time() const; + + /** + * \brief date returns the date which can be a null date. + * \since QGIS 3.10 + */ + QDate date() const; + /** * Set the current date as NULL * \note if the widget is not configured to accept NULL dates, this will have no effect diff --git a/tests/src/gui/CMakeLists.txt b/tests/src/gui/CMakeLists.txt index ce2a0864fdb..64114e5bfc9 100644 --- a/tests/src/gui/CMakeLists.txt +++ b/tests/src/gui/CMakeLists.txt @@ -127,6 +127,7 @@ ADD_QGIS_TEST(datumtransformdialog testqgsdatumtransformdialog.cpp) ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp) ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp) ADD_QGIS_TEST(attributeformtest testqgsattributeform.cpp) +ADD_QGIS_TEST(datetimedittest testqgsdatetimeedit.cpp) ADD_QGIS_TEST(dockwidget testqgsdockwidget.cpp) ADD_QGIS_TEST(fieldexpressionwidget testqgsfieldexpressionwidget.cpp) ADD_QGIS_TEST(filewidget testqgsfilewidget.cpp) diff --git a/tests/src/gui/testqgsdatetimeedit.cpp b/tests/src/gui/testqgsdatetimeedit.cpp new file mode 100644 index 00000000000..2bc9ef08387 --- /dev/null +++ b/tests/src/gui/testqgsdatetimeedit.cpp @@ -0,0 +1,83 @@ +/*************************************************************************** + testqgsdatetimeedit.cpp + -------------------------------------- + Date : September 2019 + Copyright : (C) 2019 Etienne Trimaille + Email : etienne dot trimaille at gmail dot com + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + + +#include "qgstest.h" +#include "qdatetime.h" + +#include + +class TestQgsDateTimeEdit: public QObject +{ + Q_OBJECT + private slots: + void initTestCase(); // will be called before the first testfunction is executed. + void cleanupTestCase(); // will be called after the last testfunction was executed. + void init(); // will be called before each testfunction is executed. + void cleanup(); // will be called after every testfunction. + + void nullValues(); + +}; + +void TestQgsDateTimeEdit::initTestCase() +{ +} + +void TestQgsDateTimeEdit::cleanupTestCase() +{ +} + +void TestQgsDateTimeEdit::init() +{ +} + +void TestQgsDateTimeEdit::cleanup() +{ +} + +void TestQgsDateTimeEdit::nullValues() +{ + QgsDateTimeEdit *timeEdit = new QgsDateTimeEdit(); + + // Allow null with a null datetime + QVERIFY( timeEdit->allowNull() ); + timeEdit->setDateTime( QDateTime() ); + QCOMPARE( timeEdit->dateTime(), QDateTime() ); + QCOMPARE( timeEdit->time(), QTime() ); + QCOMPARE( timeEdit->date(), QDate() ); + + // Not null with not null datetime + QDateTime date( QDate( 2019, 7, 6 ), QTime( 8, 30, 0 ) ); + timeEdit->setAllowNull( false ); + QVERIFY( !timeEdit->allowNull() ); + timeEdit->setDateTime( date ); + QCOMPARE( timeEdit->dateTime(), date ); + QCOMPARE( timeEdit->time(), date.time() ); + QCOMPARE( timeEdit->date(), date.date() ); + + // Not null with null date + timeEdit->setAllowNull( false ); + QVERIFY( !timeEdit->allowNull() ); + timeEdit->setDateTime( QDateTime() ); + QCOMPARE( timeEdit->dateTime(), date ); + QCOMPARE( timeEdit->time(), date.time() ); + QCOMPARE( timeEdit->date(), date.date() ); + + delete timeEdit; +} + +QGSTEST_MAIN( TestQgsDateTimeEdit ) +#include "testqgsdatetimeedit.moc"