Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement date() and time() in QgsDateTimeEdit to handle NULL values
  • Loading branch information
Gustry authored and nyalldawson committed Oct 3, 2019
1 parent c64c088 commit d61fab7
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
19 changes: 17 additions & 2 deletions python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeedit.cpp
Expand Up @@ -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();
}
}
17 changes: 15 additions & 2 deletions src/gui/editorwidgets/qgsdatetimeedit.h
Expand Up @@ -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 );

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Expand Up @@ -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)
Expand Down
83 changes: 83 additions & 0 deletions 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 <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"

0 comments on commit d61fab7

Please sign in to comment.