Skip to content

Commit

Permalink
[QgsDateTimeEdit] use special value text to handle NULL values
Browse files Browse the repository at this point in the history
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
  • Loading branch information
3nids committed Jan 2, 2018
1 parent 577c667 commit 86feed4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
1 change: 0 additions & 1 deletion python/gui/editorwidgets/qgsdatetimeedit.sip
Expand Up @@ -73,7 +73,6 @@ Resets the widget to show no value (ie, an "unknown" state).
virtual void mousePressEvent( QMouseEvent *event );



};

/************************************************************************
Expand Down
61 changes: 36 additions & 25 deletions src/gui/editorwidgets/qgsdatetimeedit.cpp
Expand Up @@ -19,6 +19,7 @@
#include <QSettings>
#include <QStyle>
#include <QToolButton>
#include <QStyleOptionSpinBox>


#include "qgsdatetimeedit.h"
Expand All @@ -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();
Expand All @@ -65,50 +61,70 @@ 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 );
QDateTimeEdit::setDateTime( minimumDateTime() );
QDateTimeEdit::blockSignals( false );
}
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 );
}

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()
Expand Down Expand Up @@ -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() );
}
4 changes: 1 addition & 3 deletions src/gui/editorwidgets/qgsdatetimeedit.h
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 86feed4

Please sign in to comment.