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( 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 );
111 bool updateCalendar =
false;
115 QStyle::SubControl control;
116 if ( calendarPopup() )
118 QStyleOptionComboBox optCombo;
119#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
120 optCombo.init(
this );
122 optCombo.initFrom(
this );
124 optCombo.editable =
true;
125 optCombo.subControls = QStyle::SC_All;
126 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo, event->pos(),
this );
128 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
130 mCurrentPressEvent =
true;
132 updateCalendar =
true;
134 mCurrentPressEvent =
false;
139 QStyleOptionSpinBox opt;
140 this->initStyleOption( &opt );
141 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(),
this );
143 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
145 mCurrentPressEvent =
true;
146 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
147 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
148 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
149 mCurrentPressEvent =
false;
154 QDateTimeEdit::mousePressEvent( event );
156 if ( updateCalendar )
159 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
165 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
167 QAbstractSpinBox::focusOutEvent( event );
168 if ( lineEdit()->text() != mNullRepresentation )
172 emit editingFinished();
176 QDateTimeEdit::focusOutEvent( event );
182 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
184 QAbstractSpinBox::focusInEvent( event );
186 displayCurrentDate();
190 QDateTimeEdit::focusInEvent( event );
197 if ( mAllowNull && mIsNull )
201 resetBeforeChange( -event->angleDelta().y() / ( 15 * 8 ) );
203 QDateTimeEdit::wheelEvent( event );
208 QDateTimeEdit::showEvent( event );
209 if ( mAllowNull && mIsNull &&
210 lineEdit()->text() != mNullRepresentation )
217void QgsDateTimeEdit::changed(
const QVariant &dateTime )
226 if ( mOriginalStyleSheet.isNull() )
228 mOriginalStyleSheet = lineEdit()->styleSheet();
230 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
234 lineEdit()->setStyleSheet( mOriginalStyleSheet );
238 mClearAction->setVisible( mAllowNull && !mIsNull );
246 return mNullRepresentation;
254 lineEdit()->setText( mNullRepresentation );
260 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
261 if ( updateCalendar )
265 QDateTimeEdit::setDateTime( minimumDateTime() );
267 lineEdit()->setCursorPosition( lineEdit()->text().length() );
268 lineEdit()->setText( mNullRepresentation );
269 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
279 return mAllowNull && mIsNull;
282void QgsDateTimeEdit::displayCurrentDate()
284 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
285 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
286 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
289void QgsDateTimeEdit::resetBeforeChange(
int delta )
291 QDateTime dt = QDateTime::currentDateTime();
292 switch ( currentSection() )
294 case QDateTimeEdit::DaySection:
295 dt = dt.addDays( delta );
297 case QDateTimeEdit::MonthSection:
298 dt = dt.addMonths( delta );
300 case QDateTimeEdit::YearSection:
301 dt = dt.addYears( delta );
306 if ( dt < minimumDateTime() )
308 dt = minimumDateTime();
310 else if ( dt > maximumDateTime() )
312 dt = maximumDateTime();
314 QDateTimeEdit::setDateTime( dt );
317void QgsDateTimeEdit::setMinimumEditDateTime()
319 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
320 setMinimumTime( QTime( 0, 0, 0 ) );
321 setMaximumTime( QTime( 23, 59, 59, 999 ) );
339 QDateTimeEdit::setDateTime(
dateTime );
353 return QDateTimeEdit::dateTime();
365 return QDateTimeEdit::time();
377 return QDateTimeEdit::date();
387#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
401 if ( !
time.isValid() ||
time.isNull() )
411 QDateTimeEdit::setTime(
time );
428#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
442 if ( !
date.isValid() ||
date.isNull() )
452 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.
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)
Returns true if the specified variant should be considered a NULL value.