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 ) );
116 if (
event->type() == QEvent::KeyPress )
118 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event );
119 if ( keyEvent->matches( QKeySequence::Paste ) && lineEdit()->hasSelectedText() )
121 const int selectionStart { lineEdit()->selectionStart() };
122 const int selectionEnd { lineEdit()->selectionEnd() };
123 lineEdit()->setCursorPosition( selectionStart );
124 lineEdit()->setSelection( selectionStart, selectionEnd - selectionStart );
128 return QDateTimeEdit::event(
event );
137 bool updateCalendar =
false;
141 QStyle::SubControl control;
142 if ( calendarPopup() )
144 QStyleOptionComboBox optCombo;
145#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
146 optCombo.init(
this );
148 optCombo.initFrom(
this );
150 optCombo.editable =
true;
151 optCombo.subControls = QStyle::SC_All;
152 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo,
event->pos(),
this );
154 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
156 mCurrentPressEvent =
true;
158 updateCalendar =
true;
160 mCurrentPressEvent =
false;
165 QStyleOptionSpinBox opt;
166 this->initStyleOption( &opt );
167 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt,
event->pos(),
this );
169 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
171 mCurrentPressEvent =
true;
172 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
173 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
174 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
175 mCurrentPressEvent =
false;
180 QDateTimeEdit::mousePressEvent(
event );
182 if ( updateCalendar )
185 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
191 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
193 QAbstractSpinBox::focusOutEvent(
event );
194 if ( lineEdit()->text() != mNullRepresentation )
198 emit editingFinished();
202 QDateTimeEdit::focusOutEvent(
event );
208 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
210 QAbstractSpinBox::focusInEvent(
event );
212 displayCurrentDate();
216 QDateTimeEdit::focusInEvent(
event );
223 if ( mAllowNull && mIsNull )
227 resetBeforeChange( -
event->angleDelta().y() / ( 15 * 8 ) );
229 QDateTimeEdit::wheelEvent(
event );
234 QDateTimeEdit::showEvent(
event );
235 if ( mAllowNull && mIsNull && lineEdit()->text() != mNullRepresentation )
242void QgsDateTimeEdit::changed(
const QVariant &dateTime )
251 if ( mOriginalStyleSheet.isNull() )
253 mOriginalStyleSheet = lineEdit()->styleSheet();
255 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
259 lineEdit()->setStyleSheet( mOriginalStyleSheet );
263 mClearAction->setVisible( mAllowNull && !mIsNull );
271 return mNullRepresentation;
279 lineEdit()->setText( mNullRepresentation );
285 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
286 if ( updateCalendar )
290 QDateTimeEdit::setDateTime( minimumDateTime() );
292 lineEdit()->setCursorPosition( lineEdit()->text().length() );
293 lineEdit()->setText( mNullRepresentation );
294 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
304 return mAllowNull && mIsNull;
307void QgsDateTimeEdit::displayCurrentDate()
309 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
310 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
311 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
314void QgsDateTimeEdit::resetBeforeChange(
int delta )
316 QDateTime dt = QDateTime::currentDateTime();
317 switch ( currentSection() )
319 case QDateTimeEdit::DaySection:
320 dt = dt.addDays( delta );
322 case QDateTimeEdit::MonthSection:
323 dt = dt.addMonths( delta );
325 case QDateTimeEdit::YearSection:
326 dt = dt.addYears( delta );
331 if ( dt < minimumDateTime() )
333 dt = minimumDateTime();
335 else if ( dt > maximumDateTime() )
337 dt = maximumDateTime();
339 QDateTimeEdit::setDateTime( dt );
342void QgsDateTimeEdit::setMinimumEditDateTime()
344 setDateRange( QDate( 1, 1, 1 ), maximumDate() );
345 setMinimumTime( QTime( 0, 0, 0 ) );
346 setMaximumTime( QTime( 23, 59, 59, 999 ) );
366 QDateTimeEdit::setTimeSpec(
dateTime.timeSpec() );
367 QDateTimeEdit::setDateTime(
dateTime );
381 return QDateTimeEdit::dateTime();
393 return QDateTimeEdit::time();
405 return QDateTimeEdit::date();
415#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
428 if ( !
time.isValid() ||
time.isNull() )
438 QDateTimeEdit::setTime(
time );
455#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
468 if ( !
date.isValid() ||
date.isNull() )
478 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.