17 #include <QCalendarWidget>
19 #include <QMouseEvent>
21 #include <QStyleOptionSpinBox>
39 : QDateTimeEdit( var, parserType, parent )
43 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
44 mClearAction->setCheckable(
false );
45 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
46 mClearAction->setVisible( mAllowNull );
49 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
52 setCalendarPopup(
true );
54 setMinimumEditDateTime();
57 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
64 mClearAction->setVisible( mAllowNull && ( !mIsNull ||
mIsEmpty ) );
77 changed( QDateTime() );
85 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
86 emit dateTimeChanged( QDateTime() );
87 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
93 mClearAction->setVisible( mAllowNull );
103 bool updateCalendar =
false;
107 QStyle::SubControl control;
108 if ( calendarPopup() )
110 QStyleOptionComboBox optCombo;
111 optCombo.init(
this );
112 optCombo.editable =
true;
113 optCombo.subControls = QStyle::SC_All;
114 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo, event->pos(),
this );
116 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
118 mCurrentPressEvent =
true;
120 updateCalendar =
true;
122 mCurrentPressEvent =
false;
127 QStyleOptionSpinBox opt;
128 this->initStyleOption( &opt );
129 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(),
this );
131 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
133 mCurrentPressEvent =
true;
134 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
135 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
136 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
137 mCurrentPressEvent =
false;
142 QDateTimeEdit::mousePressEvent( event );
144 if ( updateCalendar )
147 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
153 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
155 QAbstractSpinBox::focusOutEvent( event );
156 if ( lineEdit()->text() != mNullRepresentation )
160 emit editingFinished();
164 QDateTimeEdit::focusOutEvent( event );
170 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
172 QAbstractSpinBox::focusInEvent( event );
174 displayCurrentDate();
178 QDateTimeEdit::focusInEvent( event );
185 if ( mAllowNull && mIsNull )
187 resetBeforeChange( -event->delta() );
189 QDateTimeEdit::wheelEvent( event );
194 QDateTimeEdit::showEvent( event );
195 if ( mAllowNull && mIsNull &&
196 lineEdit()->text() != mNullRepresentation )
203 void QgsDateTimeEdit::changed(
const QVariant &dateTime )
212 if ( mOriginalStyleSheet.isNull() )
214 mOriginalStyleSheet = lineEdit()->styleSheet();
216 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
220 lineEdit()->setStyleSheet( mOriginalStyleSheet );
224 mClearAction->setVisible( mAllowNull && !mIsNull );
232 return mNullRepresentation;
240 lineEdit()->setText( mNullRepresentation );
246 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
247 if ( updateCalendar )
251 QDateTimeEdit::setDateTime( minimumDateTime() );
253 lineEdit()->setCursorPosition( lineEdit()->text().length() );
254 lineEdit()->setText( mNullRepresentation );
255 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
265 return mAllowNull && mIsNull;
268 void QgsDateTimeEdit::displayCurrentDate()
270 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
271 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
272 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
275 void QgsDateTimeEdit::resetBeforeChange(
int delta )
277 QDateTime dt = QDateTime::currentDateTime();
278 switch ( currentSection() )
280 case QDateTimeEdit::DaySection:
281 dt = dt.addDays( delta );
283 case QDateTimeEdit::MonthSection:
284 dt = dt.addMonths( delta );
286 case QDateTimeEdit::YearSection:
287 dt = dt.addYears( delta );
292 if ( dt < minimumDateTime() )
294 dt = minimumDateTime();
296 else if ( dt > maximumDateTime() )
298 dt = maximumDateTime();
300 QDateTimeEdit::setDateTime( dt );
318 QDateTimeEdit::setDateTime(
dateTime );
332 return QDateTimeEdit::dateTime();
344 return QDateTimeEdit::time();
356 return QDateTimeEdit::date();
376 if ( !
time.isValid() ||
time.isNull() )
386 QDateTimeEdit::setTime(
time );
413 if ( !
date.isValid() ||
date.isNull() )
423 QDateTimeEdit::setDate(
date );