22#include <QCalendarWidget>
27#include <QStyleOptionSpinBox>
29#include "moc_qgsdatetimeedit.cpp"
31using namespace Qt::StringLiterals;
40 : QDateTimeEdit( var, parserType, parent )
44 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
45 mClearAction->setCheckable(
false );
46 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
47 mClearAction->setVisible( mAllowNull );
50 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
53 setCalendarPopup(
true );
55 setMinimumEditDateTime();
58 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
65 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
78 changed( QVariant() );
86 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
87 emit dateTimeChanged( QDateTime() );
88 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
94 mClearAction->setVisible( mAllowNull );
100 if (
event->type() == QEvent::ReadOnlyChange ||
event->type() == QEvent::EnabledChange )
102 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
109 if (
event->type() == QEvent::KeyPress )
111 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event );
112 if ( keyEvent->matches( QKeySequence::Paste ) && lineEdit()->hasSelectedText() )
114 const int selectionStart { lineEdit()->selectionStart() };
115 const int selectionEnd { lineEdit()->selectionEnd() };
116 lineEdit()->setCursorPosition( selectionStart );
117 lineEdit()->setSelection( selectionStart, selectionEnd - selectionStart );
121 return QDateTimeEdit::event(
event );
130 bool updateCalendar =
false;
134 QStyle::SubControl control;
135 if ( calendarPopup() )
137 QStyleOptionComboBox optCombo;
138 optCombo.initFrom(
this );
139 optCombo.editable =
true;
140 optCombo.subControls = QStyle::SC_All;
141 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo,
event->pos(),
this );
143 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
145 mCurrentPressEvent =
true;
147 updateCalendar =
true;
149 mCurrentPressEvent =
false;
154 QStyleOptionSpinBox opt;
155 this->initStyleOption( &opt );
156 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt,
event->pos(),
this );
158 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
160 mCurrentPressEvent =
true;
161 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
162 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
163 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
164 mCurrentPressEvent =
false;
169 QDateTimeEdit::mousePressEvent(
event );
171 if ( updateCalendar )
174 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
180 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
184 QAbstractSpinBox::focusOutEvent(
event );
185 if ( lineEdit()->text() != mNullRepresentation )
189 emit editingFinished();
193 QDateTimeEdit::focusOutEvent(
event );
199 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
203 QAbstractSpinBox::focusInEvent(
event );
205 displayCurrentDate();
209 QDateTimeEdit::focusInEvent(
event );
216 if ( mAllowNull && mIsNull )
220 resetBeforeChange( -
event->angleDelta().y() / ( 15 * 8 ) );
222 QDateTimeEdit::wheelEvent(
event );
227 QDateTimeEdit::showEvent(
event );
228 if ( mAllowNull && mIsNull && lineEdit()->text() != mNullRepresentation )
235void QgsDateTimeEdit::changed(
const QVariant &dateTime )
244 if ( mOriginalStyleSheet.isNull() )
246 mOriginalStyleSheet = lineEdit()->styleSheet();
248 lineEdit()->setStyleSheet( u
"QLineEdit { font-style: italic; color: grey; }"_s );
252 lineEdit()->setStyleSheet( mOriginalStyleSheet );
256 mClearAction->setVisible( mAllowNull && !mIsNull );
264 return mNullRepresentation;
272 lineEdit()->setText( mNullRepresentation );
278 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
279 if ( updateCalendar )
283 QDateTimeEdit::setDateTime( minimumDateTime() );
285 lineEdit()->setCursorPosition( lineEdit()->text().length() );
286 lineEdit()->setText( mNullRepresentation );
287 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
297 return mAllowNull && mIsNull;
300void QgsDateTimeEdit::displayCurrentDate()
302 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
303 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
304 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
307void QgsDateTimeEdit::resetBeforeChange(
int delta )
309 QDateTime dt = QDateTime::currentDateTime();
310 switch ( currentSection() )
312 case QDateTimeEdit::DaySection:
313 dt = dt.addDays( delta );
315 case QDateTimeEdit::MonthSection:
316 dt = dt.addMonths( delta );
318 case QDateTimeEdit::YearSection:
319 dt = dt.addYears( delta );
324 if ( dt < minimumDateTime() )
326 dt = minimumDateTime();
328 else if ( dt > maximumDateTime() )
330 dt = maximumDateTime();
332 QDateTimeEdit::setDateTime( dt );
335void QgsDateTimeEdit::setMinimumEditDateTime()
337 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
338 setMinimumTime( QTime( 0, 0, 0 ) );
339 setMaximumTime( QTime( 23, 59, 59, 999 ) );
362#if QT_VERSION >= QT_VERSION_CHECK( 6, 7, 0 )
363 QDateTimeEdit::setTimeZone(
dateTime.timeZone() );
365 QDateTimeEdit::setTimeSpec(
dateTime.timeSpec() );
368 QDateTimeEdit::setDateTime(
dateTime );
382 return QDateTimeEdit::dateTime();
394 return QDateTimeEdit::time();
406 return QDateTimeEdit::date();
425 if ( !
time.isValid() ||
time.isNull() )
435 QDateTimeEdit::setTime(
time );
461 if ( !
date.isValid() ||
date.isNull() )
471 QDateTimeEdit::setDate(
date );
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setDate(const QDate &date)
Sets the date for the widget and handles null dates.
void dateValueChanged(const QDate &date)
Signal emitted whenever the date changes.
QgsDateEdit(QWidget *parent=nullptr)
Constructor for QgsDateEdit.
void emitValueChanged(const QVariant &value) override
Emits the widget's correct value changed signal.
void wheelEvent(QWheelEvent *event) override
void setAllowNull(bool allowNull)
Determines if the widget allows setting null date/time.
void setNullRepresentation(const QString &null)
Sets the widget's null representation, which defaults to QgsApplication::nullRepresentation().
int mBlockChangedSignal
Block change signals if true.
void showEvent(QShowEvent *event) override
QDateTime dateTime() const
Returns the date time which can be a null date/time.
void focusInEvent(QFocusEvent *event) override
bool isNull() const
Returns true if the widget is currently set to a null value.
virtual void emitValueChanged(const QVariant &value)
Emits the widget's correct value changed signal.
void mousePressEvent(QMouseEvent *event) override
void setDateTime(const QDateTime &dateTime)
Set the date time in the widget and handles null date times.
QTime time() const
Returns the time which can be a null time.
void setEmpty()
Resets the widget to show no value (ie, an "unknown" state).
QString nullRepresentation() const
Returns the widget's NULL representation, which defaults to QgsApplication::nullRepresentation().
void focusOutEvent(QFocusEvent *event) override
bool mIsEmpty
true if the widget is empty
void displayNull(bool updateCalendar=false)
write the null value representation to the line edit without changing the value
void clear() override
Set the current date as NULL.
bool event(QEvent *event) override
Reimplemented to enable/disable the clear action depending on read-only status.
QgsDateTimeEdit(QWidget *parent=nullptr)
Constructor for QgsDateTimeEdit.
QDate date() const
Returns the date which can be a null date.
void valueChanged(const QDateTime &date)
Signal emitted whenever the value changes.
QgsTimeEdit(QWidget *parent=nullptr)
Constructor for QgsTimeEdit.
void emitValueChanged(const QVariant &value) override
Emits the widget's correct value changed signal.
void timeValueChanged(const QTime &time)
Signal emitted whenever the time changes.
void setTime(const QTime &time)
Sets the time for the widget and handles null times.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.