17 #include <QCalendarWidget> 19 #include <QMouseEvent> 21 #include <QStyleOptionSpinBox> 32 : QDateTimeEdit( parent )
35 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
36 mClearAction->setCheckable(
false );
37 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
38 mClearAction->setVisible( mAllowNull );
41 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
44 setCalendarPopup(
true );
46 setMinimumEditDateTime();
49 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
55 mClearAction->setVisible( mAllowNull && ( !mIsNull || mIsEmpty ) );
68 changed( QDateTime() );
76 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
77 emit dateTimeChanged( QDateTime() );
78 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
88 mClearAction->setVisible( mAllowNull );
98 bool updateCalendar =
false;
102 QStyle::SubControl control;
103 if ( calendarPopup() )
105 QStyleOptionComboBox optCombo;
106 optCombo.init(
this );
107 optCombo.editable =
true;
108 optCombo.subControls = QStyle::SC_All;
109 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo, event->pos(), this );
111 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
113 mCurrentPressEvent =
true;
115 updateCalendar =
true;
116 displayNull( updateCalendar );
117 mCurrentPressEvent =
false;
122 QStyleOptionSpinBox opt;
123 this->initStyleOption( &opt );
124 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(), this );
126 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
128 mCurrentPressEvent =
true;
129 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
130 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
131 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
132 mCurrentPressEvent =
false;
137 QDateTimeEdit::mousePressEvent( event );
139 if ( updateCalendar )
142 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
148 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
150 QAbstractSpinBox::focusOutEvent( event );
155 emit editingFinished();
159 QDateTimeEdit::focusOutEvent( event );
166 if ( mAllowNull && mIsNull )
168 resetBeforeChange( -event->delta() );
170 QDateTimeEdit::wheelEvent( event );
175 QDateTimeEdit::showEvent( event );
176 if ( mAllowNull && mIsNull &&
183 void QgsDateTimeEdit::changed(
const QDateTime &
dateTime )
186 bool isNull = dateTime.isNull();
187 if ( isNull != mIsNull )
192 if ( mOriginalStyleSheet.isNull() )
194 mOriginalStyleSheet = lineEdit()->styleSheet();
196 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
200 lineEdit()->setStyleSheet( mOriginalStyleSheet );
204 mClearAction->setVisible( mAllowNull && !mIsNull );
209 void QgsDateTimeEdit::displayNull(
bool updateCalendar )
211 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
212 if ( updateCalendar )
216 QDateTimeEdit::setDateTime( minimumDateTime() );
219 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
222 void QgsDateTimeEdit::resetBeforeChange(
int delta )
224 QDateTime dt = QDateTime::currentDateTime();
225 switch ( currentSection() )
227 case QDateTimeEdit::DaySection:
228 dt = dt.addDays( delta );
230 case QDateTimeEdit::MonthSection:
231 dt = dt.addMonths( delta );
233 case QDateTimeEdit::YearSection:
234 dt = dt.addYears( delta );
239 if ( dt < minimumDateTime() )
241 dt = minimumDateTime();
243 else if ( dt > maximumDateTime() )
245 dt = maximumDateTime();
247 QDateTimeEdit::setDateTime( dt );
255 if ( !dateTime.isValid() || dateTime.isNull() )
262 QDateTimeEdit::setDateTime( dateTime );
269 if ( mAllowNull && mIsNull )
275 return QDateTimeEdit::dateTime();
void showEvent(QShowEvent *event) override
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void focusOutEvent(QFocusEvent *event) override
void wheelEvent(QWheelEvent *event) override
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
void valueChanged(const QDateTime &date)
signal emitted whenever the value changes.
void setEmpty()
Resets the widget to show no value (ie, an "unknown" state).
void setAllowNull(bool allowNull)
Determines if the widget allows setting null date/time.
QgsDateTimeEdit(QWidget *parent=nullptr)
Constructor for QgsDateTimeEdit.
void setDateTime(const QDateTime &dateTime)
setDateTime set the date time in the widget and handles null date times.
void clear() override
Set the current date as NULL.
QDateTime dateTime() const
dateTime returns the date time which can eventually be a null date/time
void mousePressEvent(QMouseEvent *event) override