mirror of
https://github.com/qgis/QGIS.git
synced 2025-06-19 00:02:48 -04:00
implement date() and time() in QgsDateTimeEdit to handle NULL values
This commit is contained in:
parent
c64c088465
commit
d61fab7783
@ -31,6 +31,7 @@ The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/read
|
|||||||
Constructor for QgsDateTimeEdit
|
Constructor for QgsDateTimeEdit
|
||||||
The current date and time is used by default.
|
The current date and time is used by default.
|
||||||
The widget is allowing null 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
|
%End
|
||||||
|
|
||||||
void setAllowNull( bool allowNull );
|
void setAllowNull( bool allowNull );
|
||||||
@ -58,17 +59,31 @@ setDateTime set the date time in the widget and handles null date times.
|
|||||||
|
|
||||||
QDateTime dateTime() const;
|
QDateTime dateTime() const;
|
||||||
%Docstring
|
%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::
|
.. 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::
|
.. note::
|
||||||
|
|
||||||
since QDateTimeEdit.dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit
|
since QDateTimeEdit.dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit
|
||||||
%End
|
%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();
|
virtual void clear();
|
||||||
|
|
||||||
%Docstring
|
%Docstring
|
||||||
|
@ -275,3 +275,27 @@ QDateTime QgsDateTimeEdit::dateTime() const
|
|||||||
return QDateTimeEdit::dateTime();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -40,6 +40,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
|
|||||||
* Constructor for QgsDateTimeEdit
|
* Constructor for QgsDateTimeEdit
|
||||||
* The current date and time is used by default.
|
* The current date and time is used by default.
|
||||||
* The widget is allowing null 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 );
|
explicit QgsDateTimeEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
|
||||||
|
|
||||||
@ -62,12 +63,24 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
|
|||||||
void setDateTime( const QDateTime &dateTime );
|
void setDateTime( const QDateTime &dateTime );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief dateTime returns the date time which can eventually be a null date/time
|
* \brief 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.
|
* \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
|
* \note since QDateTimeEdit::dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit
|
||||||
*/
|
*/
|
||||||
QDateTime dateTime() const;
|
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
|
* Set the current date as NULL
|
||||||
* \note if the widget is not configured to accept NULL dates, this will have no effect
|
* \note if the widget is not configured to accept NULL dates, this will have no effect
|
||||||
|
@ -127,6 +127,7 @@ ADD_QGIS_TEST(datumtransformdialog testqgsdatumtransformdialog.cpp)
|
|||||||
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)
|
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)
|
||||||
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp)
|
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp)
|
||||||
ADD_QGIS_TEST(attributeformtest testqgsattributeform.cpp)
|
ADD_QGIS_TEST(attributeformtest testqgsattributeform.cpp)
|
||||||
|
ADD_QGIS_TEST(datetimedittest testqgsdatetimeedit.cpp)
|
||||||
ADD_QGIS_TEST(dockwidget testqgsdockwidget.cpp)
|
ADD_QGIS_TEST(dockwidget testqgsdockwidget.cpp)
|
||||||
ADD_QGIS_TEST(fieldexpressionwidget testqgsfieldexpressionwidget.cpp)
|
ADD_QGIS_TEST(fieldexpressionwidget testqgsfieldexpressionwidget.cpp)
|
||||||
ADD_QGIS_TEST(filewidget testqgsfilewidget.cpp)
|
ADD_QGIS_TEST(filewidget testqgsfilewidget.cpp)
|
||||||
|
83
tests/src/gui/testqgsdatetimeedit.cpp
Normal file
83
tests/src/gui/testqgsdatetimeedit.cpp
Normal 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"
|
Loading…
x
Reference in New Issue
Block a user