Skip to content

Commit

Permalink
Add an interderminant "empty" state for QgsDateTimeEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 23, 2016
1 parent 1e77839 commit 6d86859
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions python/gui/editorwidgets/qgsdatetimeedit.sip
Expand Up @@ -30,6 +30,10 @@ class QgsDateTimeEdit : QDateTimeEdit
//! @note if the widget is not configured to accept NULL dates, this will have no effect
virtual void clear();

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

protected:
virtual void resizeEvent( QResizeEvent* event );
Expand Down
18 changes: 15 additions & 3 deletions src/gui/editorwidgets/qgsdatetimeedit.cpp
Expand Up @@ -28,6 +28,7 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
: QDateTimeEdit( parent )
, mAllowNull( true )
, mIsNull( true )
, mIsEmpty( false )
{
mClearButton = new QToolButton( this );
mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
Expand Down Expand Up @@ -57,9 +58,9 @@ void QgsDateTimeEdit::setAllowNull( bool allowNull )
{
mAllowNull = allowNull;

mNullLabel->setVisible( mAllowNull && mIsNull );
mClearButton->setVisible( mAllowNull && !mIsNull );
lineEdit()->setVisible( !mAllowNull || !mIsNull );
mNullLabel->setVisible(( mAllowNull && mIsNull ) && !mIsEmpty );
mClearButton->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
lineEdit()->setVisible(( !mAllowNull || !mIsNull ) && !mIsEmpty );
}


Expand All @@ -69,6 +70,13 @@ void QgsDateTimeEdit::clear()
emit dateTimeChanged( QDateTime() );
}

void QgsDateTimeEdit::setEmpty()
{
mNullLabel->setVisible( false );
lineEdit()->setVisible( false );
mClearButton->setVisible( mAllowNull );
}

void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
{
QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
Expand All @@ -80,6 +88,7 @@ void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )

void QgsDateTimeEdit::changed( const QDateTime & dateTime )
{
mIsEmpty = false;
mIsNull = dateTime.isNull();
mNullLabel->setVisible( mAllowNull && mIsNull );
mClearButton->setVisible( mAllowNull && !mIsNull );
Expand All @@ -98,6 +107,8 @@ int QgsDateTimeEdit::frameWidth() const

void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
{
mIsEmpty = false;

// set an undefined date
if ( !dateTime.isValid() || dateTime.isNull() )
{
Expand All @@ -107,6 +118,7 @@ void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
{
QDateTimeEdit::setDateTime( dateTime );
mIsNull = false;
changed( dateTime );
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeedit.h
Expand Up @@ -52,6 +52,10 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
//! @note if the widget is not configured to accept NULL dates, this will have no effect
virtual void clear() override;

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

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

bool mAllowNull;
bool mIsNull;
bool mIsEmpty;

QLineEdit* mNullLabel;
QToolButton* mClearButton;
Expand Down

1 comment on commit 6d86859

@3nids
Copy link
Member

@3nids 3nids commented on 6d86859 Dec 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyalldawson I don't see the point here. mIsEmpty is always false.

does it miss mIsEmpty = true; in setEmpty()?

Please sign in to comment.