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
fix #16579
  • Loading branch information
3nids committed Jan 2, 2018
1 parent 6508543 commit 577c667
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
* *
***************************************************************************/

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


#include "qgsdatetimeedit.h"

#include "qgsapplication.h"
Expand Down Expand Up @@ -47,8 +49,16 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )

connect( this, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeEdit::changed );

// 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(), &QCalendarWidget::selectionChanged, this, &QgsDateTimeEdit::calendarSelectionChanged );

// init with current time so mIsNull is properly initialized
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );

setMinimumEditDateTime();
}

Expand All @@ -64,6 +74,12 @@ void QgsDateTimeEdit::setAllowNull( bool allowNull )

void QgsDateTimeEdit::clear()
{
if ( calendarPopup() )
{
QDateTimeEdit::blockSignals( true );
QDateTimeEdit::setDateTime( minimumDateTime() );
QDateTimeEdit::blockSignals( false );
}
changed( QDateTime() );
emit dateTimeChanged( QDateTime() );
}
Expand All @@ -73,6 +89,7 @@ void QgsDateTimeEdit::setEmpty()
mNullLabel->setVisible( false );
lineEdit()->setVisible( false );
mClearButton->setVisible( mAllowNull );
mIsEmpty = true;
}

void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
Expand All @@ -81,6 +98,7 @@ void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
return;


QDateTimeEdit::mousePressEvent( event );
}

Expand All @@ -93,6 +111,14 @@ void QgsDateTimeEdit::changed( const QDateTime &dateTime )
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
{
return calendarPopup() ? 25 : 18;
Expand Down
3 changes: 2 additions & 1 deletion src/gui/editorwidgets/qgsdatetimeedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
private slots:
void changed( const QDateTime &dateTime );

void calendarSelectionChanged();


private:
int spinButtonWidth() const;
Expand Down Expand Up @@ -101,7 +103,6 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
{
setMinimumDateTime( QDateTime::fromString( QStringLiteral( "0100-01-01" ), Qt::ISODate ) );
}

};

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

const bool calendar = config( QStringLiteral( "calendar_popup" ), true ).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 577c667

Please sign in to comment.