[QgsDateTimeEdit] use special value text to handle NULL values

the widget now uses a special value text to display NULL rather than painting a QLineEdit on top

a press on the spinbox buttons (non-calendar mode) is catch to set to current date to avoid starting at the minimum date
This commit is contained in:
Denis Rouzaud 2017-12-29 14:13:20 -04:00
parent 577c667413
commit 86feed4955
3 changed files with 37 additions and 29 deletions

View File

@ -73,7 +73,6 @@ Resets the widget to show no value (ie, an "unknown" state).
virtual void mousePressEvent( QMouseEvent *event );
};
/************************************************************************

View File

@ -19,6 +19,7 @@
#include <QSettings>
#include <QStyle>
#include <QToolButton>
#include <QStyleOptionSpinBox>
#include "qgsdatetimeedit.h"
@ -36,11 +37,6 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
mClearButton->hide();
connect( mClearButton, &QAbstractButton::clicked, this, &QgsDateTimeEdit::clear );
mNullLabel = new QLineEdit( QgsApplication::nullRepresentation(), this );
mNullLabel->setReadOnly( true );
mNullLabel->setStyleSheet( QStringLiteral( "position: absolute; border: none; font-style: italic; color: grey;" ) );
mNullLabel->hide();
setStyleSheet( QStringLiteral( ".QWidget, QLineEdit, QToolButton { padding-right: %1px; }" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );
QSize msz = minimumSizeHint();
@ -65,39 +61,45 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
void QgsDateTimeEdit::setAllowNull( bool allowNull )
{
mAllowNull = allowNull;
mNullLabel->setVisible( ( mAllowNull && mIsNull ) && !mIsEmpty );
mClearButton->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
lineEdit()->setVisible( ( !mAllowNull || !mIsNull ) && !mIsEmpty );
}
void QgsDateTimeEdit::clear()
{
if ( calendarPopup() )
{
QDateTimeEdit::blockSignals( true );
setSpecialValueText( QgsApplication::nullRepresentation() );
QDateTimeEdit::setDateTime( minimumDateTime() );
QDateTimeEdit::blockSignals( false );
}
changed( QDateTime() );
emit dateTimeChanged( QDateTime() );
}
void QgsDateTimeEdit::setEmpty()
{
mNullLabel->setVisible( false );
lineEdit()->setVisible( false );
mClearButton->setVisible( mAllowNull );
mIsEmpty = true;
}
void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
{
QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
const QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
return;
if ( mIsNull && !calendarPopup() )
{
QStyleOptionSpinBox opt;
this->initStyleOption( &opt );
const QRect buttonUpRect = style()->subControlRect( QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxUp );
const QRect buttonDownRect = style()->subControlRect( QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxDown );
if ( buttonUpRect.contains( event->pos() ) || buttonDownRect.contains( event->pos() ) )
{
blockSignals( true );
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
blockSignals( false );
}
}
QDateTimeEdit::mousePressEvent( event );
}
@ -105,10 +107,24 @@ void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
void QgsDateTimeEdit::changed( const QDateTime &dateTime )
{
mIsEmpty = false;
mIsNull = dateTime.isNull();
mNullLabel->setVisible( mAllowNull && mIsNull );
bool isNull = dateTime.isNull() || dateTime == minimumDateTime();
if ( mIsNull != isNull )
{
mIsNull = isNull;
if ( mIsNull )
{
if ( mOriginalStyleSheet.isNull() )
{
mOriginalStyleSheet = lineEdit()->styleSheet();
}
lineEdit()->setStyleSheet( QStringLiteral( "font-style: italic; color: grey; }" ) );
}
else
{
lineEdit()->setStyleSheet( mOriginalStyleSheet );
}
}
mClearButton->setVisible( mAllowNull && !mIsNull );
lineEdit()->setVisible( !mAllowNull || !mIsNull );
}
void QgsDateTimeEdit::calendarSelectionChanged()
@ -164,11 +180,6 @@ void QgsDateTimeEdit::resizeEvent( QResizeEvent *event )
QSize sz = mClearButton->sizeHint();
mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
( rect().bottom() + 1 - sz.height() ) / 2 );
mNullLabel->move( 0, 0 );
mNullLabel->setMinimumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
mNullLabel->setMaximumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
}

View File

@ -70,13 +70,11 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
void mousePressEvent( QMouseEvent *event ) override;
private slots:
void changed( const QDateTime &dateTime );
void calendarSelectionChanged();
private:
int spinButtonWidth() const;
int frameWidth() const;
@ -85,8 +83,8 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
bool mIsNull = true;
bool mIsEmpty = false;
QLineEdit *mNullLabel = nullptr;
QToolButton *mClearButton = nullptr;
QString mOriginalStyleSheet = QString();
/**
* Set the lowest Date that can be displayed with the Qt::ISODate format