22#include <QCalendarWidget>
27#include <QStyleOptionSpinBox>
29#include "moc_qgsdatetimeedit.cpp"
31using namespace Qt::StringLiterals;
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( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
77 changed( QVariant() );
85 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
86 emit dateTimeChanged( QDateTime() );
87 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
93 mClearAction->setVisible( mAllowNull );
99 if (
event->type() == QEvent::ReadOnlyChange ||
event->type() == QEvent::EnabledChange )
101 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
108 if (
event->type() == QEvent::KeyPress )
110 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event );
111 if ( keyEvent->matches( QKeySequence::Paste ) && lineEdit()->hasSelectedText() )
113 const int selectionStart { lineEdit()->selectionStart() };
114 const int selectionEnd { lineEdit()->selectionEnd() };
115 lineEdit()->setCursorPosition( selectionStart );
116 lineEdit()->setSelection( selectionStart, selectionEnd - selectionStart );
120 return QDateTimeEdit::event(
event );
129 bool updateCalendar =
false;
133 QStyle::SubControl control;
134 if ( calendarPopup() )
136 QStyleOptionComboBox optCombo;
137 optCombo.initFrom(
this );
138 optCombo.editable =
true;
139 optCombo.subControls = QStyle::SC_All;
140 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo,
event->pos(),
this );
142 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
144 mCurrentPressEvent =
true;
146 updateCalendar =
true;
148 mCurrentPressEvent =
false;
153 QStyleOptionSpinBox opt;
154 this->initStyleOption( &opt );
155 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt,
event->pos(),
this );
157 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
159 mCurrentPressEvent =
true;
160 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
161 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
162 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
163 mCurrentPressEvent =
false;
168 QDateTimeEdit::mousePressEvent(
event );
170 if ( updateCalendar )
173 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
179 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
183 QAbstractSpinBox::focusOutEvent(
event );
184 if ( lineEdit()->text() != mNullRepresentation )
188 emit editingFinished();
192 QDateTimeEdit::focusOutEvent(
event );
198 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
202 QAbstractSpinBox::focusInEvent(
event );
204 displayCurrentDate();
205 setSelectedSection( sectionAt( 0 ) );
209 QDateTimeEdit::focusInEvent(
event );
210 setSelectedSection( sectionAt( 0 ) );
217 if ( mAllowNull && mIsNull )
221 resetBeforeChange( -
event->angleDelta().y() / ( 15 * 8 ) );
223 QDateTimeEdit::wheelEvent(
event );
228 QDateTimeEdit::showEvent(
event );
229 if ( mAllowNull && mIsNull && lineEdit()->text() != mNullRepresentation )
236void QgsDateTimeEdit::changed(
const QVariant &dateTime )
245 if ( mOriginalStyleSheet.isNull() )
247 mOriginalStyleSheet = lineEdit()->styleSheet();
249 lineEdit()->setStyleSheet( u
"QLineEdit { font-style: italic; color: grey; }"_s );
253 lineEdit()->setStyleSheet( mOriginalStyleSheet );
257 mClearAction->setVisible( mAllowNull && !mIsNull );
265 return mNullRepresentation;
273 lineEdit()->setText( mNullRepresentation );
279 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
280 if ( updateCalendar )
284 QDateTimeEdit::setDateTime( minimumDateTime() );
286 lineEdit()->setCursorPosition( lineEdit()->text().length() );
287 lineEdit()->setText( mNullRepresentation );
288 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
298 return mAllowNull && mIsNull;
301void QgsDateTimeEdit::displayCurrentDate()
303 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
304 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
305 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
308void QgsDateTimeEdit::resetBeforeChange(
int delta )
310 QDateTime dt = QDateTime::currentDateTime();
311 switch ( currentSection() )
313 case QDateTimeEdit::DaySection:
314 dt = dt.addDays( delta );
316 case QDateTimeEdit::MonthSection:
317 dt = dt.addMonths( delta );
319 case QDateTimeEdit::YearSection:
320 dt = dt.addYears( delta );
325 if ( dt < minimumDateTime() )
327 dt = minimumDateTime();
329 else if ( dt > maximumDateTime() )
331 dt = maximumDateTime();
333 QDateTimeEdit::setDateTime( dt );
336void QgsDateTimeEdit::setMinimumEditDateTime()
338 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
339 setMinimumTime( QTime( 0, 0, 0 ) );
340 setMaximumTime( QTime( 23, 59, 59, 999 ) );
363#if QT_VERSION >= QT_VERSION_CHECK( 6, 7, 0 )
364 QDateTimeEdit::setTimeZone(
dateTime.timeZone() );
366 QDateTimeEdit::setTimeSpec(
dateTime.timeSpec() );
369 QDateTimeEdit::setDateTime(
dateTime );
383 return QDateTimeEdit::dateTime();
395 return QDateTimeEdit::time();
407 return QDateTimeEdit::date();
425 if ( !
time.isValid() ||
time.isNull() )
435 QDateTimeEdit::setTime(
time );
460 if ( !
date.isValid() ||
date.isNull() )
470 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.