17 #include <QCalendarWidget>
19 #include <QMouseEvent>
21 #include <QStyleOptionSpinBox>
39 : QDateTimeEdit( var, parserType, parent )
43 mClearAction =
new QAction( clearIcon, tr(
"clear" ),
this );
44 mClearAction->setCheckable(
false );
45 lineEdit()->addAction( mClearAction, QLineEdit::TrailingPosition );
46 mClearAction->setVisible( mAllowNull );
49 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
52 setCalendarPopup(
true );
54 setMinimumEditDateTime();
57 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
64 mClearAction->setVisible( mAllowNull && ( !mIsNull ||
mIsEmpty ) );
77 changed( QDateTime() );
85 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
86 emit dateTimeChanged( QDateTime() );
87 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
93 mClearAction->setVisible( mAllowNull );
103 bool updateCalendar =
false;
107 QStyle::SubControl control;
108 if ( calendarPopup() )
110 QStyleOptionComboBox optCombo;
111 optCombo.init(
this );
112 optCombo.editable =
true;
113 optCombo.subControls = QStyle::SC_All;
114 control = style()->hitTestComplexControl( QStyle::CC_ComboBox, &optCombo, event->pos(),
this );
116 if ( control == QStyle::SC_ComboBoxArrow && calendarWidget() )
118 mCurrentPressEvent =
true;
120 updateCalendar =
true;
122 mCurrentPressEvent =
false;
127 QStyleOptionSpinBox opt;
128 this->initStyleOption( &opt );
129 control = style()->hitTestComplexControl( QStyle::CC_SpinBox, &opt, event->pos(),
this );
131 if ( control == QStyle::SC_SpinBoxDown || control == QStyle::SC_SpinBoxUp )
133 mCurrentPressEvent =
true;
134 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
135 resetBeforeChange( control == QStyle::SC_SpinBoxDown ? -1 : 1 );
136 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
137 mCurrentPressEvent =
false;
142 QDateTimeEdit::mousePressEvent( event );
144 if ( updateCalendar )
147 calendarWidget()->setCurrentPage( QDate::currentDate().year(), QDate::currentDate().month() );
153 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
155 QAbstractSpinBox::focusOutEvent( event );
156 if ( lineEdit()->text() != mNullRepresentation )
160 emit editingFinished();
164 QDateTimeEdit::focusOutEvent( event );
170 if ( mAllowNull && mIsNull && !mCurrentPressEvent )
172 QAbstractSpinBox::focusInEvent( event );
174 displayCurrentDate();
178 QDateTimeEdit::focusInEvent( event );
185 if ( mAllowNull && mIsNull )
187 resetBeforeChange( -event->delta() );
189 QDateTimeEdit::wheelEvent( event );
194 QDateTimeEdit::showEvent( event );
195 if ( mAllowNull && mIsNull &&
196 lineEdit()->text() != mNullRepresentation )
203 void QgsDateTimeEdit::changed(
const QVariant &dateTime )
212 if ( mOriginalStyleSheet.isNull() )
214 mOriginalStyleSheet = lineEdit()->styleSheet();
216 lineEdit()->setStyleSheet( QStringLiteral(
"QLineEdit { font-style: italic; color: grey; }" ) );
220 lineEdit()->setStyleSheet( mOriginalStyleSheet );
224 mClearAction->setVisible( mAllowNull && !mIsNull );
232 return mNullRepresentation;
240 lineEdit()->setText( mNullRepresentation );
246 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
247 if ( updateCalendar )
251 QDateTimeEdit::setDateTime( minimumDateTime() );
253 lineEdit()->setCursorPosition( lineEdit()->text().length() );
254 lineEdit()->setText( mNullRepresentation );
255 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
265 return mAllowNull && mIsNull;
268 void QgsDateTimeEdit::displayCurrentDate()
270 disconnect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
271 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
272 connect(
this, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEdit::changed );
275 void QgsDateTimeEdit::resetBeforeChange(
int delta )
277 QDateTime dt = QDateTime::currentDateTime();
278 switch ( currentSection() )
280 case QDateTimeEdit::DaySection:
281 dt = dt.addDays( delta );
283 case QDateTimeEdit::MonthSection:
284 dt = dt.addMonths( delta );
286 case QDateTimeEdit::YearSection:
287 dt = dt.addYears( delta );
292 if ( dt < minimumDateTime() )
294 dt = minimumDateTime();
296 else if ( dt > maximumDateTime() )
298 dt = maximumDateTime();
300 QDateTimeEdit::setDateTime( dt );
318 QDateTimeEdit::setDateTime(
dateTime );
332 return QDateTimeEdit::dateTime();
344 return QDateTimeEdit::time();
356 return QDateTimeEdit::date();
376 if ( !
time.isValid() ||
time.isNull() )
386 QDateTimeEdit::setTime(
time );
413 if ( !
date.isValid() ||
date.isNull() )
423 QDateTimeEdit::setDate(
date );
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
static QIcon getThemeIcon(const QString &name)
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.