Skip to content

Commit

Permalink
[date widget] fix current date can't be picked
Browse files Browse the repository at this point in the history
better solution has been found for Qt5 but can't be used in Qt4
see #16579
  • Loading branch information
3nids committed Jan 2, 2018
1 parent a947f6d commit 8234060
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
71 changes: 54 additions & 17 deletions src/gui/editorwidgets/qgsdatetimeedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
* *
***************************************************************************/

#include <QCalendarWidget>
#include <QLineEdit>
#include <QMouseEvent>
#include <QSettings>
#include <QStyle>
#include <QStyleOptionSpinBox>
#include <QToolButton>

#include "qgsdatetimeedit.h"
Expand All @@ -37,11 +39,6 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
mClearButton->hide();
connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );

mNullLabel = new QLineEdit( QSettings().value( "qgis/nullValue", "NULL" ).toString(), this );
mNullLabel->setReadOnly( true );
mNullLabel->setStyleSheet( "position: absolute; border: none; font-style: italic; color: grey;" );
mNullLabel->hide();

setStyleSheet( QString( ".QWidget, QLineEdit, QToolButton { padding-right: %1px; }" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );

QSize msz = minimumSizeHint();
Expand All @@ -50,6 +47,13 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )

connect( this, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( changed( QDateTime ) ) );

// set this by defaut to properly connect the calendar widget
setCalendarPopup( true );
// when clearing the widget, date of the QDateTimeEdit will be set to minimum date
// hence when the calendar popups, on selection changed if it set to the minimum date,
// the page of the current date will be shown
connect( calendarWidget(), SIGNAL( selectionChanged() ), this, SLOT( calendarSelectionChanged() ) );

// init with current time so mIsNull is properly initialized
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
}
Expand All @@ -58,41 +62,78 @@ 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()
{
QDateTimeEdit::blockSignals( true );
setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
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( "font-style: italic; color: grey; }" );
}
else
{
lineEdit()->setStyleSheet( mOriginalStyleSheet );
}
}
mClearButton->setVisible( mAllowNull && !mIsNull );
lineEdit()->setVisible( !mAllowNull || !mIsNull );
}

void QgsDateTimeEdit::calendarSelectionChanged()
{
if ( mAllowNull && calendarWidget() && calendarWidget()->selectedDate() == minimumDate() )
{
calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
}
}

int QgsDateTimeEdit::spinButtonWidth() const
Expand Down Expand Up @@ -143,8 +184,4 @@ void QgsDateTimeEdit::resizeEvent( QResizeEvent * event )

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() );
}
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
private slots:
void changed( const QDateTime & dateTime );

void calendarSelectionChanged();

private:
int spinButtonWidth() const;
Expand All @@ -77,6 +78,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit

QLineEdit* mNullLabel;
QToolButton* mClearButton;
QString mOriginalStyleSheet = QString();

};

Expand Down
6 changes: 5 additions & 1 deletion src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
mQDateTimeEdit->setDisplayFormat( displayFormat );

const bool calendar = config( "calendar_popup", false ).toBool();
mQDateTimeEdit->setCalendarPopup( calendar );

if ( calendar != mQDateTimeEdit->calendarPopup() )
{
mQDateTimeEdit->setCalendarPopup( calendar );
}
if ( calendar && mQDateTimeEdit->calendarWidget() )
{
// highlight today's date
Expand Down

0 comments on commit 8234060

Please sign in to comment.