Skip to content

Commit 6d86859

Browse files
committed
Add an interderminant "empty" state for QgsDateTimeEdit
1 parent 1e77839 commit 6d86859

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

python/gui/editorwidgets/qgsdatetimeedit.sip

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class QgsDateTimeEdit : QDateTimeEdit
3030
//! @note if the widget is not configured to accept NULL dates, this will have no effect
3131
virtual void clear();
3232

33+
/** Resets the widget to show no value (ie, an "unknown" state).
34+
* @note added in QGIS 2.16
35+
*/
36+
void setEmpty();
3337

3438
protected:
3539
virtual void resizeEvent( QResizeEvent* event );

src/gui/editorwidgets/qgsdatetimeedit.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
2828
: QDateTimeEdit( parent )
2929
, mAllowNull( true )
3030
, mIsNull( true )
31+
, mIsEmpty( false )
3132
{
3233
mClearButton = new QToolButton( this );
3334
mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
@@ -57,9 +58,9 @@ void QgsDateTimeEdit::setAllowNull( bool allowNull )
5758
{
5859
mAllowNull = allowNull;
5960

60-
mNullLabel->setVisible( mAllowNull && mIsNull );
61-
mClearButton->setVisible( mAllowNull && !mIsNull );
62-
lineEdit()->setVisible( !mAllowNull || !mIsNull );
61+
mNullLabel->setVisible(( mAllowNull && mIsNull ) && !mIsEmpty );
62+
mClearButton->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
63+
lineEdit()->setVisible(( !mAllowNull || !mIsNull ) && !mIsEmpty );
6364
}
6465

6566

@@ -69,6 +70,13 @@ void QgsDateTimeEdit::clear()
6970
emit dateTimeChanged( QDateTime() );
7071
}
7172

73+
void QgsDateTimeEdit::setEmpty()
74+
{
75+
mNullLabel->setVisible( false );
76+
lineEdit()->setVisible( false );
77+
mClearButton->setVisible( mAllowNull );
78+
}
79+
7280
void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
7381
{
7482
QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
@@ -80,6 +88,7 @@ void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
8088

8189
void QgsDateTimeEdit::changed( const QDateTime & dateTime )
8290
{
91+
mIsEmpty = false;
8392
mIsNull = dateTime.isNull();
8493
mNullLabel->setVisible( mAllowNull && mIsNull );
8594
mClearButton->setVisible( mAllowNull && !mIsNull );
@@ -98,6 +107,8 @@ int QgsDateTimeEdit::frameWidth() const
98107

99108
void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
100109
{
110+
mIsEmpty = false;
111+
101112
// set an undefined date
102113
if ( !dateTime.isValid() || dateTime.isNull() )
103114
{
@@ -107,6 +118,7 @@ void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
107118
{
108119
QDateTimeEdit::setDateTime( dateTime );
109120
mIsNull = false;
121+
changed( dateTime );
110122
}
111123
}
112124

src/gui/editorwidgets/qgsdatetimeedit.h

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
5252
//! @note if the widget is not configured to accept NULL dates, this will have no effect
5353
virtual void clear() override;
5454

55+
/** Resets the widget to show no value (ie, an "unknown" state).
56+
* @note added in QGIS 2.16
57+
*/
58+
void setEmpty();
5559

5660
protected:
5761
virtual void resizeEvent( QResizeEvent* event ) override;
@@ -69,6 +73,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
6973

7074
bool mAllowNull;
7175
bool mIsNull;
76+
bool mIsEmpty;
7277

7378
QLineEdit* mNullLabel;
7479
QToolButton* mClearButton;

0 commit comments

Comments
 (0)