17#include <QCalendarWidget>
21#include <QStyleOptionSpinBox>
25#include "moc_qgsdatetimeedit.cpp"
32#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 && lineEdit()->text() != mNullRepresentation )
226void QgsDateTimeEdit::changed(
const QVariant &dateTime )
235 if ( mOriginalStyleSheet.isNull() )
237 mOriginalStyleSheet = lineEdit()->styleSheet();
239 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
243 lineEdit()->setStyleSheet( mOriginalStyleSheet );
247 mClearAction->setVisible( mAllowNull && !mIsNull );
255 return mNullRepresentation;
263 lineEdit()->setText( mNullRepresentation );
269 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
270 if ( updateCalendar )
274 QDateTimeEdit::setDateTime( minimumDateTime() );
276 lineEdit()->setCursorPosition( lineEdit()->text().length() );
277 lineEdit()->setText( mNullRepresentation );
278 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
288 return mAllowNull && mIsNull;
291void QgsDateTimeEdit::displayCurrentDate()
293 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
294 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
295 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
298void QgsDateTimeEdit::resetBeforeChange(
int delta )
300 QDateTime dt = QDateTime::currentDateTime();
301 switch ( currentSection() )
303 case QDateTimeEdit::DaySection:
304 dt = dt.addDays( delta );
306 case QDateTimeEdit::MonthSection:
307 dt = dt.addMonths( delta );
309 case QDateTimeEdit::YearSection:
310 dt = dt.addYears( delta );
315 if ( dt < minimumDateTime() )
317 dt = minimumDateTime();
319 else if ( dt > maximumDateTime() )
321 dt = maximumDateTime();
323 QDateTimeEdit::setDateTime( dt );
326void QgsDateTimeEdit::setMinimumEditDateTime()
328 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
329 setMinimumTime( QTime( 0, 0, 0 ) );
330 setMaximumTime( QTime( 23, 59, 59, 999 ) );
350 QDateTimeEdit::setTimeSpec(
dateTime.timeSpec() );
351 QDateTimeEdit::setDateTime(
dateTime );
365 return QDateTimeEdit::dateTime();
377 return QDateTimeEdit::time();
389 return QDateTimeEdit::date();
399#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
412 if ( !
time.isValid() ||
time.isNull() )
422 QDateTimeEdit::setTime(
time );
439#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.