Add an interderminant "empty" state for QgsDateTimeEdit

This commit is contained in:
Nyall Dawson 2016-05-23 07:19:32 +10:00
parent 1e778393c6
commit 6d868598f5
3 changed files with 24 additions and 3 deletions

View File

@ -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 );

View File

@ -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" ) );
@ -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 );
}
@ -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 );
@ -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 );
@ -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() )
{
@ -107,6 +118,7 @@ void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
{
QDateTimeEdit::setDateTime( dateTime );
mIsNull = false;
changed( dateTime );
}
}

View File

@ -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;
@ -69,6 +73,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
bool mAllowNull;
bool mIsNull;
bool mIsEmpty;
QLineEdit* mNullLabel;
QToolButton* mClearButton;