17#include <QCalendarWidget>
21#include <QStyleOptionSpinBox>
25#include "moc_qgsdatetimeedit.cpp"
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( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
86 changed( QVariant() );
94 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
95 emit dateTimeChanged( QDateTime() );
96 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
102 mClearAction->setVisible( mAllowNull );
108 if (
event->type() == QEvent::ReadOnlyChange ||
event->type() == QEvent::EnabledChange )
110 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
113 return QDateTimeEdit::event(
event );
122 bool updateCalendar =
false;
126 QStyle::SubControl control;
127 if ( calendarPopup() )
129 QStyleOptionComboBox optCombo;
130#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
131 optCombo.init(
this );
133 optCombo.initFrom(
this );
135 optCombo.editable =
true;
136 optCombo.subControls = QStyle::SC_All;
137 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo,
event->pos(),
this );
139 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
141 mCurrentPressEvent =
true;
143 updateCalendar =
true;
145 mCurrentPressEvent =
false;
150 QStyleOptionSpinBox opt;
151 this->initStyleOption( &opt );
152 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt,
event->pos(),
this );
154 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
156 mCurrentPressEvent =
true;
157 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
158 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
159 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
160 mCurrentPressEvent =
false;
165 QDateTimeEdit::mousePressEvent(
event );
167 if ( updateCalendar )
170 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
176 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
178 QAbstractSpinBox::focusOutEvent(
event );
179 if ( lineEdit()->text() != mNullRepresentation )
183 emit editingFinished();
187 QDateTimeEdit::focusOutEvent(
event );
193 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
195 QAbstractSpinBox::focusInEvent(
event );
197 displayCurrentDate();
201 QDateTimeEdit::focusInEvent(
event );
208 if ( mAllowNull && mIsNull )
212 resetBeforeChange( -
event->angleDelta().y() / ( 15 * 8 ) );
214 QDateTimeEdit::wheelEvent(
event );
219 QDateTimeEdit::showEvent(
event );
220 if ( mAllowNull && mIsNull &&
221 lineEdit()->text() != mNullRepresentation )
228void QgsDateTimeEdit::changed(
const QVariant &dateTime )
237 if ( mOriginalStyleSheet.isNull() )
239 mOriginalStyleSheet = lineEdit()->styleSheet();
241 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
245 lineEdit()->setStyleSheet( mOriginalStyleSheet );
249 mClearAction->setVisible( mAllowNull && !mIsNull );
257 return mNullRepresentation;
265 lineEdit()->setText( mNullRepresentation );
271 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
272 if ( updateCalendar )
276 QDateTimeEdit::setDateTime( minimumDateTime() );
278 lineEdit()->setCursorPosition( lineEdit()->text().length() );
279 lineEdit()->setText( mNullRepresentation );
280 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
290 return mAllowNull && mIsNull;
293void QgsDateTimeEdit::displayCurrentDate()
295 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
296 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
297 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
300void QgsDateTimeEdit::resetBeforeChange(
int delta )
302 QDateTime dt = QDateTime::currentDateTime();
303 switch ( currentSection() )
305 case QDateTimeEdit::DaySection:
306 dt = dt.addDays( delta );
308 case QDateTimeEdit::MonthSection:
309 dt = dt.addMonths( delta );
311 case QDateTimeEdit::YearSection:
312 dt = dt.addYears( delta );
317 if ( dt < minimumDateTime() )
319 dt = minimumDateTime();
321 else if ( dt > maximumDateTime() )
323 dt = maximumDateTime();
325 QDateTimeEdit::setDateTime( dt );
328void QgsDateTimeEdit::setMinimumEditDateTime()
330 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
331 setMinimumTime( QTime( 0, 0, 0 ) );
332 setMaximumTime( QTime( 23, 59, 59, 999 ) );
352 QDateTimeEdit::setTimeSpec(
dateTime.timeSpec() );
353 QDateTimeEdit::setDateTime(
dateTime );
367 return QDateTimeEdit::dateTime();
379 return QDateTimeEdit::time();
391 return QDateTimeEdit::date();
401#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
415 if ( !
time.isValid() ||
time.isNull() )
425 QDateTimeEdit::setTime(
time );
442#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
456 if ( !
date.isValid() ||
date.isNull() )
466 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.
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times.
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.