Skip to content
Permalink
Browse files

[date widget] fix current date can't be picked

fix #16579
  • Loading branch information
3nids committed Dec 27, 2017
1 parent 6508543 commit 577c667413ff1739f47d0e2641ac6c928db1b0e5
@@ -13,12 +13,14 @@
* *
***************************************************************************/

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


#include "qgsdatetimeedit.h"

#include "qgsapplication.h"
@@ -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();
}

@@ -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() );
}
@@ -73,6 +89,7 @@ void QgsDateTimeEdit::setEmpty()
mNullLabel->setVisible( false );
lineEdit()->setVisible( false );
mClearButton->setVisible( mAllowNull );
mIsEmpty = true;
}

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


QDateTimeEdit::mousePressEvent( event );
}

@@ -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;
@@ -74,6 +74,8 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
private slots:
void changed( const QDateTime &dateTime );

void calendarSelectionChanged();


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

};

#endif // QGSDATETIMEEDIT_H
@@ -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

0 comments on commit 577c667

Please sign in to comment.
You can’t perform that action at this time.