17#include "moc_qgsdatetimeeditwrapper.cpp"
26#include <QDateTimeEdit>
29#include <QTextCharFormat>
30#include <QCalendarWidget>
41 widget->setDateTime( QDateTime::currentDateTime() );
50 mQgsDateTimeEdit = qgsEditor;
55 QDateTimeEdit *qtEditor =
dynamic_cast<QDateTimeEdit *
>( editor );
58 mQDateTimeEdit = qtEditor;
61 if ( !mQDateTimeEdit )
63 QgsDebugError( QStringLiteral(
"Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ) );
69 mQDateTimeEdit->setDisplayFormat( displayFormat );
71 const bool calendar =
config( QStringLiteral(
"calendar_popup" ),
true ).toBool();
72 if ( calendar != mQDateTimeEdit->calendarPopup() )
74 mQDateTimeEdit->setCalendarPopup( calendar );
76 if ( calendar && mQDateTimeEdit->calendarWidget() )
79 QTextCharFormat todayFormat;
80 todayFormat.setBackground( QColor( 160, 180, 200 ) );
81 mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
84 const bool allowNull =
config( QStringLiteral(
"allow_null" ),
true ).toBool();
85 if ( mQgsDateTimeEdit )
92 "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
93 tr(
"field widgets" ) );
96 if ( mQgsDateTimeEdit )
102 connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEditWrapper::dateTimeChanged );
108 return mQgsDateTimeEdit || mQDateTimeEdit;
113 if ( mQgsDateTimeEdit )
117void QgsDateTimeEditWrapper::dateTimeChanged(
const QDateTime &dateTime )
119 switch (
field().type() )
121 case QMetaType::Type::QDateTime:
127 case QMetaType::Type::QDate:
133 case QMetaType::Type::QTime:
140 if ( !dateTime.isValid() || dateTime.isNull() )
149 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
151 if ( fieldIsoFormat )
172 if ( !mQDateTimeEdit )
176 if ( mQgsDateTimeEdit )
178 dateTime = mQgsDateTimeEdit->
dateTime();
182 dateTime = mQDateTimeEdit->dateTime();
185 if ( dateTime.isNull() )
188 switch (
field().type() )
190 case QMetaType::Type::QDateTime:
192 case QMetaType::Type::QDate:
193 return dateTime.date();
194 case QMetaType::Type::QTime:
195 return dateTime.time();
197 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
199 if ( fieldIsoFormat )
201 return dateTime.toString( Qt::ISODate );
205 return dateTime.toString( fieldFormat );
213void QgsDateTimeEditWrapper::updateValues(
const QVariant &value,
const QVariantList & )
215 if ( !mQDateTimeEdit )
220 switch (
field().type() )
222 case QMetaType::Type::QDateTime:
223 dateTime =
value.toDateTime();
225 case QMetaType::Type::QDate:
226 dateTime.setDate(
value.toDate() );
227 dateTime.setTime( QTime( 0, 0, 0 ) );
229 case QMetaType::Type::QTime:
230 dateTime.setDate( QDate::currentDate() );
231 dateTime.setTime(
value.toTime() );
236 switch (
value.userType() )
238 case QMetaType::Type::QDateTime:
240 dateTime =
value.toDateTime();
243 case QMetaType::Type::QDate:
245 dateTime.setDate(
value.toDate() );
246 dateTime.setTime( QTime( 0, 0, 0 ) );
249 case QMetaType::Type::QTime:
251 dateTime.setDate( QDate::currentDate() );
252 dateTime.setTime(
value.toTime() );
257 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
259 if ( fieldIsoFormat )
261 dateTime = QDateTime::fromString(
value.toString(), Qt::ISODate );
265 dateTime = QDateTime::fromString(
value.toString(), fieldFormat );
273 if ( mQgsDateTimeEdit )
276 if ( mQgsDateTimeEdit->displayFormat().indexOf(
"Z" ) > 0 )
278 dateTime = dateTime.toUTC();
285 mQDateTimeEdit->setDateTime( dateTime );
291 if ( !mQDateTimeEdit )
294 mQDateTimeEdit->setReadOnly( !enabled );
295 mQDateTimeEdit->setFrame( enabled );
@ Warning
Warning message.
bool valid() const override
Returns true if the widget has been properly initialized.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsDateTimeEditWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
Constructor for QgsDateTimeEditWrapper.
QVariant value() const override
Will be used to access the widget's value.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void setEnabled(bool enabled) override
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times.
void setAllowNull(bool allowNull)
Determines if the widget allows setting null date/time.
QDateTime dateTime() const
Returns the date time which can be a null date/time.
void setDateTime(const QDateTime &dateTime)
Set the date time in the widget and handles null date times.
void setEmpty()
Resets the widget to show no value (ie, an "unknown" state).
void valueChanged(const QDateTime &date)
Signal emitted whenever the value changes.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based data sets.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugError(str)