Skip to content

Commit 8234060

Browse files
committedJan 2, 2018
[date widget] fix current date can't be picked
better solution has been found for Qt5 but can't be used in Qt4 see #16579
1 parent a947f6d commit 8234060

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed
 

‎src/gui/editorwidgets/qgsdatetimeedit.cpp

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
* *
1414
***************************************************************************/
1515

16+
#include <QCalendarWidget>
1617
#include <QLineEdit>
1718
#include <QMouseEvent>
1819
#include <QSettings>
1920
#include <QStyle>
21+
#include <QStyleOptionSpinBox>
2022
#include <QToolButton>
2123

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

40-
mNullLabel = new QLineEdit( QSettings().value( "qgis/nullValue", "NULL" ).toString(), this );
41-
mNullLabel->setReadOnly( true );
42-
mNullLabel->setStyleSheet( "position: absolute; border: none; font-style: italic; color: grey;" );
43-
mNullLabel->hide();
44-
4542
setStyleSheet( QString( ".QWidget, QLineEdit, QToolButton { padding-right: %1px; }" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );
4643

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

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

50+
// set this by defaut to properly connect the calendar widget
51+
setCalendarPopup( true );
52+
// when clearing the widget, date of the QDateTimeEdit will be set to minimum date
53+
// hence when the calendar popups, on selection changed if it set to the minimum date,
54+
// the page of the current date will be shown
55+
connect( calendarWidget(), SIGNAL( selectionChanged() ), this, SLOT( calendarSelectionChanged() ) );
56+
5357
// init with current time so mIsNull is properly initialized
5458
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
5559
}
@@ -58,41 +62,78 @@ void QgsDateTimeEdit::setAllowNull( bool allowNull )
5862
{
5963
mAllowNull = allowNull;
6064

61-
mNullLabel->setVisible(( mAllowNull && mIsNull ) && !mIsEmpty );
6265
mClearButton->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
63-
lineEdit()->setVisible(( !mAllowNull || !mIsNull ) && !mIsEmpty );
6466
}
6567

6668

6769
void QgsDateTimeEdit::clear()
6870
{
71+
QDateTimeEdit::blockSignals( true );
72+
setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
73+
QDateTimeEdit::setDateTime( minimumDateTime() );
74+
QDateTimeEdit::blockSignals( false );
6975
changed( QDateTime() );
7076
emit dateTimeChanged( QDateTime() );
7177
}
7278

7379
void QgsDateTimeEdit::setEmpty()
7480
{
75-
mNullLabel->setVisible( false );
76-
lineEdit()->setVisible( false );
7781
mClearButton->setVisible( mAllowNull );
82+
mIsEmpty = true;
7883
}
7984

8085
void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
8186
{
82-
QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
87+
const QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
8388
if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
8489
return;
8590

91+
if ( mIsNull && !calendarPopup() )
92+
{
93+
QStyleOptionSpinBox opt;
94+
this->initStyleOption( &opt );
95+
const QRect buttonUpRect = style()->subControlRect( QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxUp );
96+
const QRect buttonDownRect = style()->subControlRect( QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxDown );
97+
if ( buttonUpRect.contains( event->pos() ) || buttonDownRect.contains( event->pos() ) )
98+
{
99+
blockSignals( true );
100+
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
101+
blockSignals( false );
102+
}
103+
}
104+
86105
QDateTimeEdit::mousePressEvent( event );
87106
}
88107

89108
void QgsDateTimeEdit::changed( const QDateTime & dateTime )
90109
{
91110
mIsEmpty = false;
92-
mIsNull = dateTime.isNull();
93-
mNullLabel->setVisible( mAllowNull && mIsNull );
111+
bool isNull = dateTime.isNull() || dateTime == minimumDateTime();
112+
if ( mIsNull != isNull )
113+
{
114+
mIsNull = isNull;
115+
if ( mIsNull )
116+
{
117+
if ( mOriginalStyleSheet.isNull() )
118+
{
119+
mOriginalStyleSheet = lineEdit()->styleSheet();
120+
}
121+
lineEdit()->setStyleSheet( "font-style: italic; color: grey; }" );
122+
}
123+
else
124+
{
125+
lineEdit()->setStyleSheet( mOriginalStyleSheet );
126+
}
127+
}
94128
mClearButton->setVisible( mAllowNull && !mIsNull );
95-
lineEdit()->setVisible( !mAllowNull || !mIsNull );
129+
}
130+
131+
void QgsDateTimeEdit::calendarSelectionChanged()
132+
{
133+
if ( mAllowNull && calendarWidget() && calendarWidget()->selectedDate() == minimumDate() )
134+
{
135+
calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
136+
}
96137
}
97138

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

144185
mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
145186
( rect().bottom() + 1 - sz.height() ) / 2 );
146-
147-
mNullLabel->move( 0, 0 );
148-
mNullLabel->setMinimumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
149-
mNullLabel->setMaximumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
150187
}

‎src/gui/editorwidgets/qgsdatetimeedit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
6666
private slots:
6767
void changed( const QDateTime & dateTime );
6868

69+
void calendarSelectionChanged();
6970

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

7879
QLineEdit* mNullLabel;
7980
QToolButton* mClearButton;
81+
QString mOriginalStyleSheet = QString();
8082

8183
};
8284

‎src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
6565
mQDateTimeEdit->setDisplayFormat( displayFormat );
6666

6767
const bool calendar = config( "calendar_popup", false ).toBool();
68-
mQDateTimeEdit->setCalendarPopup( calendar );
68+
69+
if ( calendar != mQDateTimeEdit->calendarPopup() )
70+
{
71+
mQDateTimeEdit->setCalendarPopup( calendar );
72+
}
6973
if ( calendar && mQDateTimeEdit->calendarWidget() )
7074
{
7175
// highlight today's date

0 commit comments

Comments
 (0)
Please sign in to comment.