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 ) );
65 changed( QDateTime() );
72 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
73 emit dateTimeChanged( QDateTime() );
74 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
84 mClearAction->setVisible( mAllowNull );
94 bool updateCalendar =
false;
98 QStyle::SubControl control;
99 if ( calendarPopup() )
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 );
107 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
109 mCurrentPressEvent =
true;
111 updateCalendar =
true;
112 displayNull( updateCalendar );
113 mCurrentPressEvent =
false;
118 QStyleOptionSpinBox opt;
119 this->initStyleOption( &opt );
120 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(), this );
122 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
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;
133 QDateTimeEdit::mousePressEvent( event );
135 if ( updateCalendar )
138 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
144 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
146 QAbstractSpinBox::focusOutEvent( event );
151 emit editingFinished();
155 QDateTimeEdit::focusOutEvent( event );
162 if ( mAllowNull && mIsNull )
164 resetBeforeChange( -event->delta() );
166 QDateTimeEdit::wheelEvent( event );
171 QDateTimeEdit::showEvent( event );
172 if ( mAllowNull && mIsNull &&
179 void QgsDateTimeEdit::changed(
const QDateTime &
dateTime )
182 bool isNull = dateTime.isNull();
183 if ( isNull != mIsNull )
188 if ( mOriginalStyleSheet.isNull() )
190 mOriginalStyleSheet = lineEdit()->styleSheet();
192 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
196 lineEdit()->setStyleSheet( mOriginalStyleSheet );
200 mClearAction->setVisible( mAllowNull && !mIsNull );
205 void QgsDateTimeEdit::displayNull(
bool updateCalendar )
207 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
208 if ( updateCalendar )
212 QDateTimeEdit::setDateTime( minimumDateTime() );
215 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
218 void QgsDateTimeEdit::resetBeforeChange(
int delta )
220 QDateTime dt = QDateTime::currentDateTime();
221 switch ( currentSection() )
223 case QDateTimeEdit::DaySection:
224 dt = dt.addDays( delta );
226 case QDateTimeEdit::MonthSection:
227 dt = dt.addMonths( delta );
229 case QDateTimeEdit::YearSection:
230 dt = dt.addYears( delta );
235 if ( dt < minimumDateTime() )
237 dt = minimumDateTime();
239 else if ( dt > maximumDateTime() )
241 dt = maximumDateTime();
243 QDateTimeEdit::setDateTime( dt );
251 if ( !dateTime.isValid() || dateTime.isNull() )
257 QDateTimeEdit::setDateTime( dateTime );
264 if ( mAllowNull && mIsNull )
270 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