25#include <QDateTimeEdit>
28#include <QTextCharFormat>
29#include <QCalendarWidget>
40 widget->setDateTime( QDateTime::currentDateTime() );
49 mQgsDateTimeEdit = qgsEditor;
54 QDateTimeEdit *qtEditor =
dynamic_cast<QDateTimeEdit *
>( editor );
57 mQDateTimeEdit = qtEditor;
60 if ( !mQDateTimeEdit )
62 QgsDebugError( QStringLiteral(
"Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ) );
68 mQDateTimeEdit->setDisplayFormat( displayFormat );
70 const bool calendar =
config( QStringLiteral(
"calendar_popup" ),
true ).toBool();
71 if ( calendar != mQDateTimeEdit->calendarPopup() )
73 mQDateTimeEdit->setCalendarPopup( calendar );
75 if ( calendar && mQDateTimeEdit->calendarWidget() )
78 QTextCharFormat todayFormat;
79 todayFormat.setBackground( QColor( 160, 180, 200 ) );
80 mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
83 const bool allowNull =
config( QStringLiteral(
"allow_null" ),
true ).toBool();
84 if ( mQgsDateTimeEdit )
91 "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
92 tr(
"field widgets" ) );
95 if ( mQgsDateTimeEdit )
101 connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEditWrapper::dateTimeChanged );
107 return mQgsDateTimeEdit || mQDateTimeEdit;
112 if ( mQgsDateTimeEdit )
116void QgsDateTimeEditWrapper::dateTimeChanged(
const QDateTime &dateTime )
118 switch (
field().type() )
120 case QMetaType::Type::QDateTime:
126 case QMetaType::Type::QDate:
132 case QMetaType::Type::QTime:
139 if ( !dateTime.isValid() || dateTime.isNull() )
148 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
150 if ( fieldIsoFormat )
171 if ( !mQDateTimeEdit )
175 if ( mQgsDateTimeEdit )
177 dateTime = mQgsDateTimeEdit->
dateTime();
181 dateTime = mQDateTimeEdit->dateTime();
184 if ( dateTime.isNull() )
187 switch (
field().type() )
189 case QMetaType::Type::QDateTime:
191 case QMetaType::Type::QDate:
192 return dateTime.date();
193 case QMetaType::Type::QTime:
194 return dateTime.time();
196 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
198 if ( fieldIsoFormat )
200 return dateTime.toString( Qt::ISODate );
204 return dateTime.toString( fieldFormat );
212void QgsDateTimeEditWrapper::updateValues(
const QVariant &value,
const QVariantList & )
214 if ( !mQDateTimeEdit )
219 switch (
field().type() )
221 case QMetaType::Type::QDateTime:
222 dateTime =
value.toDateTime();
224 case QMetaType::Type::QDate:
225 dateTime.setDate(
value.toDate() );
226 dateTime.setTime( QTime( 0, 0, 0 ) );
228 case QMetaType::Type::QTime:
229 dateTime.setDate( QDate::currentDate() );
230 dateTime.setTime(
value.toTime() );
235 switch (
value.userType() )
237 case QMetaType::Type::QDateTime:
239 dateTime =
value.toDateTime();
242 case QMetaType::Type::QDate:
244 dateTime.setDate(
value.toDate() );
245 dateTime.setTime( QTime( 0, 0, 0 ) );
248 case QMetaType::Type::QTime:
250 dateTime.setDate( QDate::currentDate() );
251 dateTime.setTime(
value.toTime() );
256 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
258 if ( fieldIsoFormat )
260 dateTime = QDateTime::fromString(
value.toString(), Qt::ISODate );
264 dateTime = QDateTime::fromString(
value.toString(), fieldFormat );
272 if ( mQgsDateTimeEdit )
275 if ( mQgsDateTimeEdit->displayFormat().indexOf(
"Z" ) > 0 )
277 dateTime = dateTime.toUTC();
284 mQDateTimeEdit->setDateTime( dateTime );
290 if ( !mQDateTimeEdit )
293 mQDateTimeEdit->setReadOnly( !enabled );
294 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)