From 1facaf51492cc77ed225fbb659015e9e60cc13df Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 23 Feb 2020 17:42:31 +1000 Subject: [PATCH] Allow null string to be customised for QgsDateTimeEdit E.g. for processing use this needs to be "Not specified" --- .../editorwidgets/qgsdatetimeedit.sip.in | 20 ++++++++++++++++++ src/gui/editorwidgets/qgsdatetimeedit.cpp | 21 ++++++++++++++++--- src/gui/editorwidgets/qgsdatetimeedit.h | 19 +++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in b/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in index 4ea6bec00b7..8e837dec0a8 100644 --- a/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in +++ b/python/gui/auto_generated/editorwidgets/qgsdatetimeedit.sip.in @@ -103,6 +103,26 @@ Set the current date as NULL. Resets the widget to show no value (ie, an "unknown" state). .. versionadded:: 2.16 +%End + + QString nullRepresentation() const; +%Docstring +Returns the widget's NULL representation, which defaults +to :py:func:`QgsApplication.nullRepresentation()` + +.. seealso:: :py:func:`setNullRepresentation` + +.. versionadded:: 3.14 +%End + + void setNullRepresentation( const QString &null ); +%Docstring +Sets the widget's ``null`` representation, which defaults +to :py:func:`QgsApplication.nullRepresentation()` + +.. seealso:: :py:func:`nullRepresentation` + +.. versionadded:: 3.14 %End signals: diff --git a/src/gui/editorwidgets/qgsdatetimeedit.cpp b/src/gui/editorwidgets/qgsdatetimeedit.cpp index 7b7894781a7..59d5fdec9dc 100644 --- a/src/gui/editorwidgets/qgsdatetimeedit.cpp +++ b/src/gui/editorwidgets/qgsdatetimeedit.cpp @@ -36,6 +36,7 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent ) QgsDateTimeEdit::QgsDateTimeEdit( const QVariant &var, QVariant::Type parserType, QWidget *parent ) : QDateTimeEdit( var, parserType, parent ) + , mNullRepresentation( QgsApplication::nullRepresentation() ) { QIcon clearIcon = QgsApplication::getThemeIcon( "/mIconClearText.svg" ); mClearAction = new QAction( clearIcon, tr( "clear" ), this ); @@ -151,7 +152,7 @@ void QgsDateTimeEdit::focusOutEvent( QFocusEvent *event ) if ( mAllowNull && mIsNull && !mCurrentPressEvent ) { QAbstractSpinBox::focusOutEvent( event ); - if ( lineEdit()->text() != QgsApplication::nullRepresentation() ) + if ( lineEdit()->text() != mNullRepresentation ) { displayNull(); } @@ -191,7 +192,7 @@ void QgsDateTimeEdit::showEvent( QShowEvent *event ) { QDateTimeEdit::showEvent( event ); if ( mAllowNull && mIsNull && - lineEdit()->text() != QgsApplication::nullRepresentation() ) + lineEdit()->text() != mNullRepresentation ) { displayNull(); } @@ -222,6 +223,20 @@ void QgsDateTimeEdit::changed( const QVariant &dateTime ) emitValueChanged( dateTime ); } +QString QgsDateTimeEdit::nullRepresentation() const +{ + return mNullRepresentation; +} + +void QgsDateTimeEdit::setNullRepresentation( const QString &nullRepresentation ) +{ + mNullRepresentation = nullRepresentation; + if ( mIsNull ) + { + lineEdit()->setText( mNullRepresentation ); + } +} + void QgsDateTimeEdit::displayNull( bool updateCalendar ) { disconnect( this, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeEdit::changed ); @@ -232,7 +247,7 @@ void QgsDateTimeEdit::displayNull( bool updateCalendar ) QDateTimeEdit::setDateTime( minimumDateTime() ); } lineEdit()->setCursorPosition( lineEdit()->text().length() ); - lineEdit()->setText( QgsApplication::nullRepresentation() ); + lineEdit()->setText( mNullRepresentation ); connect( this, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeEdit::changed ); } diff --git a/src/gui/editorwidgets/qgsdatetimeedit.h b/src/gui/editorwidgets/qgsdatetimeedit.h index 398867a2d26..12eed613a16 100644 --- a/src/gui/editorwidgets/qgsdatetimeedit.h +++ b/src/gui/editorwidgets/qgsdatetimeedit.h @@ -97,6 +97,24 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit */ void setEmpty(); + /** + * Returns the widget's NULL representation, which defaults + * to QgsApplication::nullRepresentation(). + * + * \see setNullRepresentation() + * \since QGIS 3.14 + */ + QString nullRepresentation() const; + + /** + * Sets the widget's \a null representation, which defaults + * to QgsApplication::nullRepresentation(). + * + * \see nullRepresentation() + * \since QGIS 3.14 + */ + void setNullRepresentation( const QString &null ); + signals: /** @@ -141,6 +159,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit QString mOriginalStyleSheet = QString(); QAction *mClearAction; + QString mNullRepresentation; //! TRUE if the widget allows null values bool mAllowNull = true;