17 #include <QCalendarWidget>
19 #include <QMouseEvent>
21 #include <QStyleOptionSpinBox>
32 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
42 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
44 : QDateTimeEdit( var, parserType, parent )
47 : QDateTimeEdit( var, parserType, parent )
52 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
53 mClearAction->setCheckable(
false );
54 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
55 mClearAction->setVisible( mAllowNull );
58 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
61 setCalendarPopup(
true );
63 setMinimumEditDateTime();
66 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
73 mClearAction->setVisible( mAllowNull && ( !mIsNull ||
mIsEmpty ) );
86 changed( QDateTime() );
94 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
95 emit dateTimeChanged( QDateTime() );
96 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
102 mClearAction->setVisible( mAllowNull );
112 bool updateCalendar =
false;
116 QStyle::SubControl control;
117 if ( calendarPopup() )
119 QStyleOptionComboBox optCombo;
120 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
121 optCombo.init(
this );
123 optCombo.initFrom(
this );
125 optCombo.editable =
true;
126 optCombo.subControls = QStyle::SC_All;
127 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo, event->pos(),
this );
129 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
131 mCurrentPressEvent =
true;
133 updateCalendar =
true;
135 mCurrentPressEvent =
false;
140 QStyleOptionSpinBox opt;
141 this->initStyleOption( &opt );
142 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(),
this );
144 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
146 mCurrentPressEvent =
true;
147 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
148 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
149 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
150 mCurrentPressEvent =
false;
155 QDateTimeEdit::mousePressEvent( event );
157 if ( updateCalendar )
160 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
166 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
168 QAbstractSpinBox::focusOutEvent( event );
169 if ( lineEdit()->text() != mNullRepresentation )
173 emit editingFinished();
177 QDateTimeEdit::focusOutEvent( event );
183 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
185 QAbstractSpinBox::focusInEvent( event );
187 displayCurrentDate();
191 QDateTimeEdit::focusInEvent( event );
198 if ( mAllowNull && mIsNull )
202 resetBeforeChange( -event->angleDelta().y() / ( 15 * 8 ) );
204 QDateTimeEdit::wheelEvent( event );
209 QDateTimeEdit::showEvent( event );
210 if ( mAllowNull && mIsNull &&
211 lineEdit()->text() != mNullRepresentation )
218 void QgsDateTimeEdit::changed(
const QVariant &dateTime )
227 if ( mOriginalStyleSheet.isNull() )
229 mOriginalStyleSheet = lineEdit()->styleSheet();
231 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
235 lineEdit()->setStyleSheet( mOriginalStyleSheet );
239 mClearAction->setVisible( mAllowNull && !mIsNull );
247 return mNullRepresentation;
255 lineEdit()->setText( mNullRepresentation );
261 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
262 if ( updateCalendar )
266 QDateTimeEdit::setDateTime( minimumDateTime() );
268 lineEdit()->setCursorPosition( lineEdit()->text().length() );
269 lineEdit()->setText( mNullRepresentation );
270 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
280 return mAllowNull && mIsNull;
283 void QgsDateTimeEdit::displayCurrentDate()
285 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
286 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
287 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
290 void QgsDateTimeEdit::resetBeforeChange(
int delta )
292 QDateTime dt = QDateTime::currentDateTime();
293 switch ( currentSection() )
295 case QDateTimeEdit::DaySection:
296 dt = dt.addDays( delta );
298 case QDateTimeEdit::MonthSection:
299 dt = dt.addMonths( delta );
301 case QDateTimeEdit::YearSection:
302 dt = dt.addYears( delta );
307 if ( dt < minimumDateTime() )
309 dt = minimumDateTime();
311 else if ( dt > maximumDateTime() )
313 dt = maximumDateTime();
315 QDateTimeEdit::setDateTime( dt );
333 QDateTimeEdit::setDateTime(
dateTime );
347 return QDateTimeEdit::dateTime();
359 return QDateTimeEdit::time();
371 return QDateTimeEdit::date();
381 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
395 if ( !
time.isValid() ||
time.isNull() )
405 QDateTimeEdit::setTime(
time );
422 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
436 if ( !
date.isValid() ||
date.isNull() )
446 QDateTimeEdit::setDate(
date );