mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
[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:
parent
577c667413
commit
86feed4955
@ -73,7 +73,6 @@ Resets the widget to show no value (ie, an "unknown" state).
|
|||||||
virtual void mousePressEvent( QMouseEvent *event );
|
virtual void mousePressEvent( QMouseEvent *event );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QStyle>
|
#include <QStyle>
|
||||||
#include <QToolButton>
|
#include <QToolButton>
|
||||||
|
#include <QStyleOptionSpinBox>
|
||||||
|
|
||||||
|
|
||||||
#include "qgsdatetimeedit.h"
|
#include "qgsdatetimeedit.h"
|
||||||
@ -36,11 +37,6 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
|
|||||||
mClearButton->hide();
|
mClearButton->hide();
|
||||||
connect( mClearButton, &QAbstractButton::clicked, this, &QgsDateTimeEdit::clear );
|
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 ) );
|
setStyleSheet( QStringLiteral( ".QWidget, QLineEdit, QToolButton { padding-right: %1px; }" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );
|
||||||
|
|
||||||
QSize msz = minimumSizeHint();
|
QSize msz = minimumSizeHint();
|
||||||
@ -65,39 +61,45 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
|
|||||||
void QgsDateTimeEdit::setAllowNull( bool allowNull )
|
void QgsDateTimeEdit::setAllowNull( bool allowNull )
|
||||||
{
|
{
|
||||||
mAllowNull = allowNull;
|
mAllowNull = allowNull;
|
||||||
|
|
||||||
mNullLabel->setVisible( ( mAllowNull && mIsNull ) && !mIsEmpty );
|
|
||||||
mClearButton->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
|
mClearButton->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
|
||||||
lineEdit()->setVisible( ( !mAllowNull || !mIsNull ) && !mIsEmpty );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QgsDateTimeEdit::clear()
|
void QgsDateTimeEdit::clear()
|
||||||
{
|
{
|
||||||
if ( calendarPopup() )
|
QDateTimeEdit::blockSignals( true );
|
||||||
{
|
setSpecialValueText( QgsApplication::nullRepresentation() );
|
||||||
QDateTimeEdit::blockSignals( true );
|
QDateTimeEdit::setDateTime( minimumDateTime() );
|
||||||
QDateTimeEdit::setDateTime( minimumDateTime() );
|
QDateTimeEdit::blockSignals( false );
|
||||||
QDateTimeEdit::blockSignals( false );
|
|
||||||
}
|
|
||||||
changed( QDateTime() );
|
changed( QDateTime() );
|
||||||
emit dateTimeChanged( QDateTime() );
|
emit dateTimeChanged( QDateTime() );
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsDateTimeEdit::setEmpty()
|
void QgsDateTimeEdit::setEmpty()
|
||||||
{
|
{
|
||||||
mNullLabel->setVisible( false );
|
|
||||||
lineEdit()->setVisible( false );
|
|
||||||
mClearButton->setVisible( mAllowNull );
|
mClearButton->setVisible( mAllowNull );
|
||||||
mIsEmpty = true;
|
mIsEmpty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
|
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() ) )
|
if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
|
||||||
return;
|
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 );
|
QDateTimeEdit::mousePressEvent( event );
|
||||||
}
|
}
|
||||||
@ -105,10 +107,24 @@ void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
|
|||||||
void QgsDateTimeEdit::changed( const QDateTime &dateTime )
|
void QgsDateTimeEdit::changed( const QDateTime &dateTime )
|
||||||
{
|
{
|
||||||
mIsEmpty = false;
|
mIsEmpty = false;
|
||||||
mIsNull = dateTime.isNull();
|
bool isNull = dateTime.isNull() || dateTime == minimumDateTime();
|
||||||
mNullLabel->setVisible( mAllowNull && mIsNull );
|
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 );
|
mClearButton->setVisible( mAllowNull && !mIsNull );
|
||||||
lineEdit()->setVisible( !mAllowNull || !mIsNull );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsDateTimeEdit::calendarSelectionChanged()
|
void QgsDateTimeEdit::calendarSelectionChanged()
|
||||||
@ -164,11 +180,6 @@ void QgsDateTimeEdit::resizeEvent( QResizeEvent *event )
|
|||||||
|
|
||||||
QSize sz = mClearButton->sizeHint();
|
QSize sz = mClearButton->sizeHint();
|
||||||
|
|
||||||
|
|
||||||
mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
|
mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
|
||||||
( rect().bottom() + 1 - sz.height() ) / 2 );
|
( 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() );
|
|
||||||
}
|
}
|
||||||
|
@ -70,13 +70,11 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
|
|||||||
|
|
||||||
void mousePressEvent( QMouseEvent *event ) override;
|
void mousePressEvent( QMouseEvent *event ) override;
|
||||||
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void changed( const QDateTime &dateTime );
|
void changed( const QDateTime &dateTime );
|
||||||
|
|
||||||
void calendarSelectionChanged();
|
void calendarSelectionChanged();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int spinButtonWidth() const;
|
int spinButtonWidth() const;
|
||||||
int frameWidth() const;
|
int frameWidth() const;
|
||||||
@ -85,8 +83,8 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
|
|||||||
bool mIsNull = true;
|
bool mIsNull = true;
|
||||||
bool mIsEmpty = false;
|
bool mIsEmpty = false;
|
||||||
|
|
||||||
QLineEdit *mNullLabel = nullptr;
|
|
||||||
QToolButton *mClearButton = nullptr;
|
QToolButton *mClearButton = nullptr;
|
||||||
|
QString mOriginalStyleSheet = QString();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the lowest Date that can be displayed with the Qt::ISODate format
|
* Set the lowest Date that can be displayed with the Qt::ISODate format
|
||||||
|
Loading…
x
Reference in New Issue
Block a user