26#include <QCalendarWidget>
28#include <QDateTimeEdit>
30#include <QTextCharFormat>
33#include "moc_qgsdatetimeeditwrapper.cpp"
35using namespace Qt::StringLiterals;
45 widget->setDateTime( QDateTime::currentDateTime() );
54 mQgsDateTimeEdit = qgsEditor;
59 QDateTimeEdit *qtEditor =
dynamic_cast<QDateTimeEdit *
>( editor );
62 mQDateTimeEdit = qtEditor;
65 if ( !mQDateTimeEdit )
67 QgsDebugError( u
"Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit."_s );
73 mQDateTimeEdit->setDisplayFormat( displayFormat );
75 const bool calendar =
config( u
"calendar_popup"_s,
true ).toBool();
76 if ( calendar != mQDateTimeEdit->calendarPopup() )
78 mQDateTimeEdit->setCalendarPopup( calendar );
80 if ( calendar && mQDateTimeEdit->calendarWidget() )
83 QTextCharFormat todayFormat;
84 todayFormat.setBackground( QColor( 160, 180, 200 ) );
85 mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
88 const bool allowNull =
config( u
"allow_null"_s,
true ).toBool();
89 if ( mQgsDateTimeEdit )
91 mQgsDateTimeEdit->setAllowNull( allowNull );
97 "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
98 "For that the QGIS custom widget QgsDateTimeEdit needs to be used."
100 tr(
"field widgets" )
104 if ( mQgsDateTimeEdit )
110 connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEditWrapper::dateTimeChanged );
116 return mQgsDateTimeEdit || mQDateTimeEdit;
121 if ( mQgsDateTimeEdit )
125void QgsDateTimeEditWrapper::dateTimeChanged(
const QDateTime &dateTime )
127 switch (
field().type() )
129 case QMetaType::Type::QDateTime:
135 case QMetaType::Type::QDate:
141 case QMetaType::Type::QTime:
148 if ( !dateTime.isValid() || dateTime.isNull() )
157 const bool fieldIsoFormat =
config( u
"field_iso_format"_s,
false ).toBool();
159 if ( fieldIsoFormat )
180 if ( !mQDateTimeEdit )
184 if ( mQgsDateTimeEdit )
186 dateTime = mQgsDateTimeEdit->dateTime();
190 dateTime = mQDateTimeEdit->dateTime();
193 if ( dateTime.isNull() )
196 switch (
field().type() )
198 case QMetaType::Type::QDateTime:
200 case QMetaType::Type::QDate:
201 return dateTime.date();
202 case QMetaType::Type::QTime:
203 return dateTime.time();
205 const bool fieldIsoFormat =
config( u
"field_iso_format"_s,
false ).toBool();
207 if ( fieldIsoFormat )
209 return dateTime.toString( Qt::ISODate );
213 return dateTime.toString( fieldFormat );
221void QgsDateTimeEditWrapper::updateValues(
const QVariant &value,
const QVariantList & )
223 if ( !mQDateTimeEdit )
228 switch (
field().type() )
230 case QMetaType::Type::QDateTime:
231 dateTime =
value.toDateTime();
233 case QMetaType::Type::QDate:
234 dateTime.setDate(
value.toDate() );
235 dateTime.setTime( QTime( 0, 0, 0 ) );
237 case QMetaType::Type::QTime:
238 dateTime.setDate( QDate::currentDate() );
239 dateTime.setTime(
value.toTime() );
244 switch (
value.userType() )
246 case QMetaType::Type::QDateTime:
248 dateTime =
value.toDateTime();
251 case QMetaType::Type::QDate:
253 dateTime.setDate(
value.toDate() );
254 dateTime.setTime( QTime( 0, 0, 0 ) );
257 case QMetaType::Type::QTime:
259 dateTime.setDate( QDate::currentDate() );
260 dateTime.setTime(
value.toTime() );
265 const bool fieldIsoFormat =
config( u
"field_iso_format"_s,
false ).toBool();
267 if ( fieldIsoFormat )
269 dateTime = QDateTime::fromString(
value.toString(), Qt::ISODate );
273 dateTime = QDateTime::fromString(
value.toString(), fieldFormat );
281 if ( mQgsDateTimeEdit )
284 if ( mQgsDateTimeEdit->displayFormat().indexOf(
"Z" ) > 0 )
286 dateTime = dateTime.toUTC();
289 mQgsDateTimeEdit->setDateTime( dateTime );
293 mQDateTimeEdit->setDateTime( dateTime );
299 if ( !mQDateTimeEdit )
302 mQDateTimeEdit->setReadOnly( !enabled );
303 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.
A QDateTimeEdit with the capability of setting/reading 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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
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 dataset.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugError(str)