22#include <QCalendarWidget>
26#include <QStyleOptionSpinBox>
28#include "moc_qgsdatetimeedit.cpp"
31#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
40#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
42 : QDateTimeEdit( var, parserType, parent )
45 : QDateTimeEdit( var, parserType, parent )
50 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
51 mClearAction->setCheckable(
false );
52 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
53 mClearAction->setVisible( mAllowNull );
56 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
59 setCalendarPopup(
true );
61 setMinimumEditDateTime();
64 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
71 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
84 changed( QVariant() );
92 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
93 emit dateTimeChanged( QDateTime() );
94 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
100 mClearAction->setVisible( mAllowNull );
106 if (
event->type() == QEvent::ReadOnlyChange ||
event->type() == QEvent::EnabledChange )
108 mClearAction->setVisible( !isReadOnly() && mAllowNull && ( !mIsNull ||
mIsEmpty ) );
115 if (
event->type() == QEvent::KeyPress )
117 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event );
118 if ( keyEvent->matches( QKeySequence::Paste ) && lineEdit()->hasSelectedText() )
120 const int selectionStart { lineEdit()->selectionStart() };
121 const int selectionEnd { lineEdit()->selectionEnd() };
122 lineEdit()->setCursorPosition( selectionStart );
123 lineEdit()->setSelection( selectionStart, selectionEnd - selectionStart );
127 return QDateTimeEdit::event(
event );
136 bool updateCalendar =
false;
140 QStyle::SubControl control;
141 if ( calendarPopup() )
143 QStyleOptionComboBox optCombo;
144#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
145 optCombo.init(
this );
147 optCombo.initFrom(
this );
149 optCombo.editable =
true;
150 optCombo.subControls = QStyle::SC_All;
151 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo,
event->pos(),
this );
153 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
155 mCurrentPressEvent =
true;
157 updateCalendar =
true;
159 mCurrentPressEvent =
false;
164 QStyleOptionSpinBox opt;
165 this->initStyleOption( &opt );
166 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt,
event->pos(),
this );
168 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
170 mCurrentPressEvent =
true;
171 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
172 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
173 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
174 mCurrentPressEvent =
false;
179 QDateTimeEdit::mousePressEvent(
event );
181 if ( updateCalendar )
184 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
190 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
194 QAbstractSpinBox::focusOutEvent(
event );
195 if ( lineEdit()->text() != mNullRepresentation )
199 emit editingFinished();
203 QDateTimeEdit::focusOutEvent(
event );
209 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
213 QAbstractSpinBox::focusInEvent(
event );
215 displayCurrentDate();
219 QDateTimeEdit::focusInEvent(
event );
226 if ( mAllowNull && mIsNull )
230 resetBeforeChange( -
event->angleDelta().y() / ( 15 * 8 ) );
232 QDateTimeEdit::wheelEvent(
event );
237 QDateTimeEdit::showEvent(
event );
238 if ( mAllowNull && mIsNull && lineEdit()->text() != mNullRepresentation )
245void QgsDateTimeEdit::changed(
const QVariant &dateTime )
254 if ( mOriginalStyleSheet.isNull() )
256 mOriginalStyleSheet = lineEdit()->styleSheet();
258 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
262 lineEdit()->setStyleSheet( mOriginalStyleSheet );
266 mClearAction->setVisible( mAllowNull && !mIsNull );
274 return mNullRepresentation;
282 lineEdit()->setText( mNullRepresentation );
288 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
289 if ( updateCalendar )
293 QDateTimeEdit::setDateTime( minimumDateTime() );
295 lineEdit()->setCursorPosition( lineEdit()->text().length() );
296 lineEdit()->setText( mNullRepresentation );
297 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
307 return mAllowNull && mIsNull;
310void QgsDateTimeEdit::displayCurrentDate()
312 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
313 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
314 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
317void QgsDateTimeEdit::resetBeforeChange(
int delta )
319 QDateTime dt = QDateTime::currentDateTime();
320 switch ( currentSection() )
322 case QDateTimeEdit::DaySection:
323 dt = dt.addDays( delta );
325 case QDateTimeEdit::MonthSection:
326 dt = dt.addMonths( delta );
328 case QDateTimeEdit::YearSection:
329 dt = dt.addYears( delta );
334 if ( dt < minimumDateTime() )
336 dt = minimumDateTime();
338 else if ( dt > maximumDateTime() )
340 dt = maximumDateTime();
342 QDateTimeEdit::setDateTime( dt );
345void QgsDateTimeEdit::setMinimumEditDateTime()
347 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
348 setMinimumTime( QTime( 0, 0, 0 ) );
349 setMaximumTime( QTime( 23, 59, 59, 999 ) );
372#if QT_VERSION >= QT_VERSION_CHECK( 6, 7, 0 )
373 QDateTimeEdit::setTimeZone(
dateTime.timeZone() );
375 QDateTimeEdit::setTimeSpec(
dateTime.timeSpec() );
378 QDateTimeEdit::setDateTime(
dateTime );
392 return QDateTimeEdit::dateTime();
404 return QDateTimeEdit::time();
416 return QDateTimeEdit::date();
426#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
439 if ( !
time.isValid() ||
time.isNull() )
449 QDateTimeEdit::setTime(
time );
466#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
479 if ( !
date.isValid() ||
date.isNull() )
489 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.
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.