13
13
* *
14
14
***************************************************************************/
15
15
16
+ #include < QCalendarWidget>
16
17
#include < QLineEdit>
17
18
#include < QMouseEvent>
18
19
#include < QSettings>
19
20
#include < QStyle>
21
+ #include < QStyleOptionSpinBox>
20
22
#include < QToolButton>
21
23
22
24
#include " qgsdatetimeedit.h"
@@ -37,11 +39,6 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
37
39
mClearButton ->hide ();
38
40
connect ( mClearButton , SIGNAL ( clicked () ), this , SLOT ( clear () ) );
39
41
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
-
45
42
setStyleSheet ( QString ( " .QWidget, QLineEdit, QToolButton { padding-right: %1px; }" ).arg ( mClearButton ->sizeHint ().width () + spinButtonWidth () + frameWidth () + 1 ) );
46
43
47
44
QSize msz = minimumSizeHint ();
@@ -50,6 +47,13 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
50
47
51
48
connect ( this , SIGNAL ( dateTimeChanged ( QDateTime ) ), this , SLOT ( changed ( QDateTime ) ) );
52
49
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
+
53
57
// init with current time so mIsNull is properly initialized
54
58
QDateTimeEdit::setDateTime ( QDateTime::currentDateTime () );
55
59
}
@@ -58,41 +62,78 @@ void QgsDateTimeEdit::setAllowNull( bool allowNull )
58
62
{
59
63
mAllowNull = allowNull;
60
64
61
- mNullLabel ->setVisible (( mAllowNull && mIsNull ) && !mIsEmpty );
62
65
mClearButton ->setVisible ( mAllowNull && ( !mIsNull || mIsEmpty ) );
63
- lineEdit ()->setVisible (( !mAllowNull || !mIsNull ) && !mIsEmpty );
64
66
}
65
67
66
68
67
69
void QgsDateTimeEdit::clear ()
68
70
{
71
+ QDateTimeEdit::blockSignals ( true );
72
+ setSpecialValueText ( QSettings ().value ( " qgis/nullValue" , " NULL" ).toString () );
73
+ QDateTimeEdit::setDateTime ( minimumDateTime () );
74
+ QDateTimeEdit::blockSignals ( false );
69
75
changed ( QDateTime () );
70
76
emit dateTimeChanged ( QDateTime () );
71
77
}
72
78
73
79
void QgsDateTimeEdit::setEmpty ()
74
80
{
75
- mNullLabel ->setVisible ( false );
76
- lineEdit ()->setVisible ( false );
77
81
mClearButton ->setVisible ( mAllowNull );
82
+ mIsEmpty = true ;
78
83
}
79
84
80
85
void QgsDateTimeEdit::mousePressEvent ( QMouseEvent* event )
81
86
{
82
- QRect lerect = rect ().adjusted ( 0 , 0 , -spinButtonWidth (), 0 );
87
+ const QRect lerect = rect ().adjusted ( 0 , 0 , -spinButtonWidth (), 0 );
83
88
if ( mAllowNull && mIsNull && lerect.contains ( event->pos () ) )
84
89
return ;
85
90
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
+
86
105
QDateTimeEdit::mousePressEvent ( event );
87
106
}
88
107
89
108
void QgsDateTimeEdit::changed ( const QDateTime & dateTime )
90
109
{
91
110
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
+ }
94
128
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
+ }
96
137
}
97
138
98
139
int QgsDateTimeEdit::spinButtonWidth () const
@@ -143,8 +184,4 @@ void QgsDateTimeEdit::resizeEvent( QResizeEvent * event )
143
184
144
185
mClearButton ->move ( rect ().right () - frameWidth () - spinButtonWidth () - sz.width (),
145
186
( 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 () );
150
187
}
0 commit comments