17 #include <QCalendarWidget>
19 #include <QMouseEvent>
21 #include <QStyleOptionSpinBox>
32 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
42 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
44 : QDateTimeEdit( var, parserType, parent )
47 : QDateTimeEdit( var, parserType, parent )
52 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
53 mClearAction->setCheckable(
false );
54 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
55 mClearAction->setVisible( mAllowNull );
58 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
61 setCalendarPopup(
true );
63 setMinimumEditDateTime();
66 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
73 mClearAction->setVisible( mAllowNull && ( !mIsNull ||
mIsEmpty ) );
86 changed( QDateTime() );
94 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
95 emit dateTimeChanged( QDateTime() );
96 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
102 mClearAction->setVisible( mAllowNull );
112 bool updateCalendar =
false;
116 QStyle::SubControl control;
117 if ( calendarPopup() )
119 QStyleOptionComboBox optCombo;
120 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
121 optCombo.init(
this );
123 optCombo.initFrom(
this );
125 optCombo.editable =
true;
126 optCombo.subControls = QStyle::SC_All;
127 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo, event->pos(),
this );
129 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
131 mCurrentPressEvent =
true;
133 updateCalendar =
true;
135 mCurrentPressEvent =
false;
140 QStyleOptionSpinBox opt;
141 this->initStyleOption( &opt );
142 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(),
this );
144 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
146 mCurrentPressEvent =
true;
147 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
148 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
149 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
150 mCurrentPressEvent =
false;
155 QDateTimeEdit::mousePressEvent( event );
157 if ( updateCalendar )
160 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
166 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
168 QAbstractSpinBox::focusOutEvent( event );
169 if ( lineEdit()->text() != mNullRepresentation )
173 emit editingFinished();
177 QDateTimeEdit::focusOutEvent( event );
183 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
185 QAbstractSpinBox::focusInEvent( event );
187 displayCurrentDate();
191 QDateTimeEdit::focusInEvent( event );
198 if ( mAllowNull && mIsNull )
202 resetBeforeChange( -event->angleDelta().y() / ( 15 * 8 ) );
204 QDateTimeEdit::wheelEvent( event );
209 QDateTimeEdit::showEvent( event );
210 if ( mAllowNull && mIsNull &&
211 lineEdit()->text() != mNullRepresentation )
218 void QgsDateTimeEdit::changed(
const QVariant &dateTime )
227 if ( mOriginalStyleSheet.isNull() )
229 mOriginalStyleSheet = lineEdit()->styleSheet();
231 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
235 lineEdit()->setStyleSheet( mOriginalStyleSheet );
239 mClearAction->setVisible( mAllowNull && !mIsNull );
247 return mNullRepresentation;
255 lineEdit()->setText( mNullRepresentation );
261 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
262 if ( updateCalendar )
266 QDateTimeEdit::setDateTime( minimumDateTime() );
268 lineEdit()->setCursorPosition( lineEdit()->text().length() );
269 lineEdit()->setText( mNullRepresentation );
270 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
280 return mAllowNull && mIsNull;
283 void QgsDateTimeEdit::displayCurrentDate()
285 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
286 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
287 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
290 void QgsDateTimeEdit::resetBeforeChange(
int delta )
292 QDateTime dt = QDateTime::currentDateTime();
293 switch ( currentSection() )
295 case QDateTimeEdit::DaySection:
296 dt = dt.addDays( delta );
298 case QDateTimeEdit::MonthSection:
299 dt = dt.addMonths( delta );
301 case QDateTimeEdit::YearSection:
302 dt = dt.addYears( delta );
307 if ( dt < minimumDateTime() )
309 dt = minimumDateTime();
311 else if ( dt > maximumDateTime() )
313 dt = maximumDateTime();
315 QDateTimeEdit::setDateTime( dt );
333 QDateTimeEdit::setDateTime(
dateTime );
347 return QDateTimeEdit::dateTime();
359 return QDateTimeEdit::time();
371 return QDateTimeEdit::date();
381 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
395 if ( !
time.isValid() ||
time.isNull() )
405 QDateTimeEdit::setTime(
time );
422 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
436 if ( !
date.isValid() ||
date.isNull() )
446 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.