Skip to content

Commit 577c667

Browse files
committedJan 2, 2018
[date widget] fix current date can't be picked
fix #16579
1 parent 6508543 commit 577c667

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed
 

‎src/gui/editorwidgets/qgsdatetimeedit.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
* *
1414
***************************************************************************/
1515

16+
#include <QCalendarWidget>
1617
#include <QLineEdit>
1718
#include <QMouseEvent>
1819
#include <QSettings>
1920
#include <QStyle>
2021
#include <QToolButton>
2122

23+
2224
#include "qgsdatetimeedit.h"
2325

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

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

52+
// set this by defaut to properly connect the calendar widget
53+
setCalendarPopup( true );
54+
// when clearing the widget, date of the QDateTimeEdit will be set to minimum date
55+
// hence when the calendar popups, on selection changed if it set to the minimum date,
56+
// the page of the current date will be shown
57+
connect( calendarWidget(), &QCalendarWidget::selectionChanged, this, &QgsDateTimeEdit::calendarSelectionChanged );
58+
5059
// init with current time so mIsNull is properly initialized
5160
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
61+
5262
setMinimumEditDateTime();
5363
}
5464

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

6575
void QgsDateTimeEdit::clear()
6676
{
77+
if ( calendarPopup() )
78+
{
79+
QDateTimeEdit::blockSignals( true );
80+
QDateTimeEdit::setDateTime( minimumDateTime() );
81+
QDateTimeEdit::blockSignals( false );
82+
}
6783
changed( QDateTime() );
6884
emit dateTimeChanged( QDateTime() );
6985
}
@@ -73,6 +89,7 @@ void QgsDateTimeEdit::setEmpty()
7389
mNullLabel->setVisible( false );
7490
lineEdit()->setVisible( false );
7591
mClearButton->setVisible( mAllowNull );
92+
mIsEmpty = true;
7693
}
7794

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

101+
84102
QDateTimeEdit::mousePressEvent( event );
85103
}
86104

@@ -93,6 +111,14 @@ void QgsDateTimeEdit::changed( const QDateTime &dateTime )
93111
lineEdit()->setVisible( !mAllowNull || !mIsNull );
94112
}
95113

114+
void QgsDateTimeEdit::calendarSelectionChanged()
115+
{
116+
if ( mAllowNull && calendarWidget() && calendarWidget()->selectedDate() == minimumDate() )
117+
{
118+
calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
119+
}
120+
}
121+
96122
int QgsDateTimeEdit::spinButtonWidth() const
97123
{
98124
return calendarPopup() ? 25 : 18;

‎src/gui/editorwidgets/qgsdatetimeedit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
7474
private slots:
7575
void changed( const QDateTime &dateTime );
7676

77+
void calendarSelectionChanged();
78+
7779

7880
private:
7981
int spinButtonWidth() const;
@@ -101,7 +103,6 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
101103
{
102104
setMinimumDateTime( QDateTime::fromString( QStringLiteral( "0100-01-01" ), Qt::ISODate ) );
103105
}
104-
105106
};
106107

107108
#endif // QGSDATETIMEEDIT_H

‎src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
6767
mQDateTimeEdit->setDisplayFormat( displayFormat );
6868

6969
const bool calendar = config( QStringLiteral( "calendar_popup" ), true ).toBool();
70-
mQDateTimeEdit->setCalendarPopup( calendar );
70+
if ( calendar != mQDateTimeEdit->calendarPopup() )
71+
{
72+
mQDateTimeEdit->setCalendarPopup( calendar );
73+
}
7174
if ( calendar && mQDateTimeEdit->calendarWidget() )
7275
{
7376
// highlight today's date

0 commit comments

Comments
 (0)
Please sign in to comment.