17#include <QCalendarWidget>
21#include <QStyleOptionSpinBox>
31#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
41#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
43 : QDateTimeEdit( var, parserType, parent )
46 : QDateTimeEdit( var, parserType, parent )
51 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
52 mClearAction->setCheckable(
false );
53 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
54 mClearAction->setVisible( mAllowNull );
57 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
60 setCalendarPopup(
true );
62 setMinimumEditDateTime();
65 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
72 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
85 changed( QVariant() );
93 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
94 emit dateTimeChanged( QDateTime() );
95 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
101 mClearAction->setVisible( mAllowNull );
107 if (
event->type() == QEvent::ReadOnlyChange ||
event->type() == QEvent::EnabledChange )
109 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
112 return QDateTimeEdit::event(
event );
121 bool updateCalendar =
false;
125 QStyle::SubControl control;
126 if ( calendarPopup() )
128 QStyleOptionComboBox optCombo;
129#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
130 optCombo.init(
this );
132 optCombo.initFrom(
this );
134 optCombo.editable =
true;
135 optCombo.subControls = QStyle::SC_All;
136 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo,
event->pos(),
this );
138 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
140 mCurrentPressEvent =
true;
142 updateCalendar =
true;
144 mCurrentPressEvent =
false;
149 QStyleOptionSpinBox opt;
150 this->initStyleOption( &opt );
151 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt,
event->pos(),
this );
153 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
155 mCurrentPressEvent =
true;
156 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
157 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
158 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
159 mCurrentPressEvent =
false;
164 QDateTimeEdit::mousePressEvent(
event );
166 if ( updateCalendar )
169 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
175 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
177 QAbstractSpinBox::focusOutEvent(
event );
178 if ( lineEdit()->text() != mNullRepresentation )
182 emit editingFinished();
186 QDateTimeEdit::focusOutEvent(
event );
192 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
194 QAbstractSpinBox::focusInEvent(
event );
196 displayCurrentDate();
200 QDateTimeEdit::focusInEvent(
event );
207 if ( mAllowNull && mIsNull )
211 resetBeforeChange( -
event->angleDelta().y() / ( 15 * 8 ) );
213 QDateTimeEdit::wheelEvent(
event );
218 QDateTimeEdit::showEvent(
event );
219 if ( mAllowNull && mIsNull &&
220 lineEdit()->text() != mNullRepresentation )
227void QgsDateTimeEdit::changed(
const QVariant &dateTime )
236 if ( mOriginalStyleSheet.isNull() )
238 mOriginalStyleSheet = lineEdit()->styleSheet();
240 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
244 lineEdit()->setStyleSheet( mOriginalStyleSheet );
248 mClearAction->setVisible( mAllowNull && !mIsNull );
256 return mNullRepresentation;
264 lineEdit()->setText( mNullRepresentation );
270 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
271 if ( updateCalendar )
275 QDateTimeEdit::setDateTime( minimumDateTime() );
277 lineEdit()->setCursorPosition( lineEdit()->text().length() );
278 lineEdit()->setText( mNullRepresentation );
279 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
289 return mAllowNull && mIsNull;
292void QgsDateTimeEdit::displayCurrentDate()
294 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
295 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
296 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
299void QgsDateTimeEdit::resetBeforeChange(
int delta )
301 QDateTime dt = QDateTime::currentDateTime();
302 switch ( currentSection() )
304 case QDateTimeEdit::DaySection:
305 dt = dt.addDays( delta );
307 case QDateTimeEdit::MonthSection:
308 dt = dt.addMonths( delta );
310 case QDateTimeEdit::YearSection:
311 dt = dt.addYears( delta );
316 if ( dt < minimumDateTime() )
318 dt = minimumDateTime();
320 else if ( dt > maximumDateTime() )
322 dt = maximumDateTime();
324 QDateTimeEdit::setDateTime( dt );
327void QgsDateTimeEdit::setMinimumEditDateTime()
329 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
330 setMinimumTime( QTime( 0, 0, 0 ) );
331 setMaximumTime( QTime( 23, 59, 59, 999 ) );
349 QDateTimeEdit::setDateTime(
dateTime );
363 return QDateTimeEdit::dateTime();
375 return QDateTimeEdit::time();
387 return QDateTimeEdit::date();
397#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
411 if ( !
time.isValid() ||
time.isNull() )
421 QDateTimeEdit::setTime(
time );
438#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
452 if ( !
date.isValid() ||
date.isNull() )
462 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.