26
26
#include " qgsapplication.h"
27
27
#include " qgslogger.h"
28
28
29
+
30
+
29
31
QgsDateTimeEdit::QgsDateTimeEdit ( QWidget *parent )
30
32
: QDateTimeEdit( parent )
31
33
{
@@ -70,6 +72,10 @@ void QgsDateTimeEdit::clear()
70
72
disconnect ( this , &QDateTimeEdit::dateTimeChanged, this , &QgsDateTimeEdit::changed );
71
73
emit dateTimeChanged ( QDateTime () );
72
74
connect ( this , &QDateTimeEdit::dateTimeChanged, this , &QgsDateTimeEdit::changed );
75
+
76
+ // otherwise, NULL is not displayed in the line edit
77
+ // this might not be the right way to do it
78
+ clearFocus ();
73
79
}
74
80
}
75
81
@@ -81,31 +87,45 @@ void QgsDateTimeEdit::setEmpty()
81
87
82
88
void QgsDateTimeEdit::mousePressEvent ( QMouseEvent *event )
83
89
{
84
- // catch mouse press on the button
90
+ // catch mouse press on the button (when the current value is null)
85
91
// in non-calendar mode: modifiy the date so it leads to showing current date (don't bother about time)
86
92
// in calendar mode: be sure NULL is displayed when needed and show page of current date in calendar widget
87
93
88
94
bool updateCalendar = false ;
89
95
90
96
if ( mIsNull )
91
97
{
92
- QStyleOptionSpinBox opt;
93
- this ->initStyleOption ( &opt );
94
- const QRect buttonUpRect = style ()->subControlRect ( QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxUp );
95
- const QRect buttonDownRect = style ()->subControlRect ( QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxDown );
96
- if ( buttonUpRect.contains ( event->pos () ) || buttonDownRect.contains ( event->pos () ) )
98
+ QStyle::SubControl control;
99
+ if ( calendarPopup () )
97
100
{
98
- if ( calendarPopup () && calendarWidget () )
101
+ QStyleOptionComboBox optCombo;
102
+ optCombo.init ( this );
103
+ optCombo.editable = true ;
104
+ optCombo.subControls = QStyle::SC_All;
105
+ control = style ()->hitTestComplexControl ( QStyle::CC_ComboBox, &optCombo, event->pos (), this );
106
+
107
+ if ( control == QStyle::SC_ComboBoxArrow && calendarWidget () )
99
108
{
109
+ mCurrentPressEvent = true ;
100
110
// ensure the line edit still displays NULL
101
- displayNull ( true );
102
111
updateCalendar = true ;
112
+ displayNull ( updateCalendar );
113
+ mCurrentPressEvent = false ;
103
114
}
104
- else
115
+ }
116
+ else
117
+ {
118
+ QStyleOptionSpinBox opt;
119
+ this ->initStyleOption ( &opt );
120
+ control = style ()->hitTestComplexControl ( QStyle::CC_SpinBox, &opt, event->pos (), this );
121
+
122
+ if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
105
123
{
106
- blockSignals ( true );
107
- resetBeforeChange ( buttonUpRect.contains ( event->pos () ) ? -1 : 1 );
108
- blockSignals ( false );
124
+ mCurrentPressEvent = true ;
125
+ disconnect ( this , &QDateTimeEdit::dateTimeChanged, this , &QgsDateTimeEdit::changed );
126
+ resetBeforeChange ( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
127
+ connect ( this , &QDateTimeEdit::dateTimeChanged, this , &QgsDateTimeEdit::changed );
128
+ mCurrentPressEvent = false ;
109
129
}
110
130
}
111
131
}
@@ -121,13 +141,13 @@ void QgsDateTimeEdit::mousePressEvent( QMouseEvent *event )
121
141
122
142
void QgsDateTimeEdit::focusOutEvent ( QFocusEvent *event )
123
143
{
124
- if ( mAllowNull && mIsNull )
144
+ if ( mAllowNull && mIsNull && ! mCurrentPressEvent )
125
145
{
146
+ QAbstractSpinBox::focusOutEvent ( event );
126
147
if ( lineEdit ()->text () != QgsApplication::nullRepresentation () )
127
148
{
128
149
displayNull ();
129
150
}
130
- QAbstractSpinBox::focusOutEvent ( event );
131
151
emit editingFinished ();
132
152
}
133
153
else
@@ -184,15 +204,15 @@ void QgsDateTimeEdit::changed( const QDateTime &dateTime )
184
204
185
205
void QgsDateTimeEdit::displayNull ( bool updateCalendar )
186
206
{
187
- blockSignals ( true );
207
+ disconnect ( this , &QDateTimeEdit::dateTimeChanged, this , &QgsDateTimeEdit::changed );
188
208
if ( updateCalendar )
189
209
{
190
210
// set current time to minimum date time to avoid having
191
211
// a date selected in calendar widget
192
212
QDateTimeEdit::setDateTime ( minimumDateTime () );
193
213
}
194
214
lineEdit ()->setText ( QgsApplication::nullRepresentation () );
195
- blockSignals ( false );
215
+ connect ( this , &QDateTimeEdit::dateTimeChanged, this , &QgsDateTimeEdit::changed );
196
216
}
197
217
198
218
void QgsDateTimeEdit::resetBeforeChange ( int delta )
0 commit comments