implement date() and time() in QgsDateTimeEdit to handle NULL values

This commit is contained in:
Etienne Trimaille 2019-09-25 10:04:46 +02:00 committed by Nyall Dawson
parent c64c088465
commit d61fab7783
5 changed files with 140 additions and 4 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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

View File

@ -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)

View File

@ -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 <qgsdatetimeedit.h>
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"