24 #include <QDateTimeEdit> 27 #include <QTextCharFormat> 28 #include <QCalendarWidget> 39 widget->
setDateTime( QDateTime::currentDateTime() );
48 mQgsDateTimeEdit = qgsEditor;
53 QDateTimeEdit *qtEditor =
dynamic_cast<QDateTimeEdit *
>( editor );
56 mQDateTimeEdit = qtEditor;
59 if ( !mQDateTimeEdit )
61 QgsDebugMsg(
"Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." );
67 mQDateTimeEdit->setDisplayFormat( displayFormat );
69 const bool calendar =
config( QStringLiteral(
"calendar_popup" ),
true ).toBool();
70 if ( calendar != mQDateTimeEdit->calendarPopup() )
72 mQDateTimeEdit->setCalendarPopup( calendar );
74 if ( calendar && mQDateTimeEdit->calendarWidget() )
77 QTextCharFormat todayFormat;
78 todayFormat.setBackground( QColor( 160, 180, 200 ) );
79 mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
82 const bool allowNull =
config( QStringLiteral(
"allow_null" ),
true ).toBool();
83 if ( mQgsDateTimeEdit )
90 "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
91 tr(
"field widgets" ) );
94 if ( mQgsDateTimeEdit )
100 connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged,
this, &QgsDateTimeEditWrapper::dateTimeChanged );
106 return mQgsDateTimeEdit || mQDateTimeEdit;
111 if ( mQgsDateTimeEdit )
115 void QgsDateTimeEditWrapper::dateTimeChanged(
const QDateTime &dateTime )
117 switch (
field().type() )
119 case QVariant::DateTime:
129 if ( !dateTime.isValid() || dateTime.isNull() )
135 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
137 if ( fieldIsoFormat )
152 if ( !mQDateTimeEdit )
153 return QVariant(
field().type() );
156 if ( mQgsDateTimeEdit )
158 dateTime = mQgsDateTimeEdit->
dateTime();
162 dateTime = mQDateTimeEdit->dateTime();
165 switch (
field().type() )
167 case QVariant::DateTime:
171 return dateTime.date();
174 return dateTime.time();
177 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
179 if ( fieldIsoFormat )
181 return dateTime.toString( Qt::ISODate );
185 return dateTime.toString( fieldFormat );
194 if ( !mQDateTimeEdit )
198 switch (
field().type() )
200 case QVariant::DateTime:
203 dateTime = value.toDateTime();
206 const bool fieldIsoFormat =
config( QStringLiteral(
"field_iso_format" ),
false ).toBool();
208 if ( fieldIsoFormat )
210 dateTime = QDateTime::fromString( value.toString(), Qt::ISODate );
214 dateTime = QDateTime::fromString( value.toString(), fieldFormat );
219 if ( mQgsDateTimeEdit )
225 mQDateTimeEdit->setDateTime( dateTime );
231 if ( !mQDateTimeEdit )
234 mQDateTimeEdit->setEnabled( enabled );
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
QVariant value() const override
Will be used to access the widget's value.
static QgsMessageLog * messageLog()
Returns the application's message log.
void setValue(const QVariant &value) override
void setEnabled(bool enabled) override
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
bool valid() const override
Return true if the widget has been properly initialized.
void valueChanged(const QDateTime &date)
signal emitted whenever the value changes.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void setEmpty()
Resets the widget to show no value (ie, an "unknown" state).
void setAllowNull(bool allowNull)
Determines if the widget allows setting null date/time.
void setDateTime(const QDateTime &dateTime)
setDateTime set the date time in the widget and handles null date times.
Represents a vector layer which manages a vector based data sets.
QDateTime dateTime() const
dateTime returns the date time which can eventually be a null date/time
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times...