QGIS API Documentation 3.41.0-Master (25ec5511245)
Loading...
Searching...
No Matches
qgsdatetimeeditwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatetimeeditwrapper.cpp
3 --------------------------------------
4 Date : 03.2014
5 Copyright : (C) 2014 Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsdatetimeeditwrapper.cpp"
19#include "qgsmessagelog.h"
20#include "qgslogger.h"
21#include "qgsdatetimeedit.h"
24#include "qgsapplication.h"
25
26#include <QDateTimeEdit>
27#include <QDateEdit>
28#include <QTimeEdit>
29#include <QTextCharFormat>
30#include <QCalendarWidget>
31
32QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
33 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
34
35{
36}
37
38QWidget *QgsDateTimeEditWrapper::createWidget( QWidget *parent )
39{
40 QgsDateTimeEdit *widget = new QgsDateTimeEdit( parent );
41 widget->setDateTime( QDateTime::currentDateTime() );
42 return widget;
43}
44
46{
47 QgsDateTimeEdit *qgsEditor = dynamic_cast<QgsDateTimeEdit *>( editor );
48 if ( qgsEditor )
49 {
50 mQgsDateTimeEdit = qgsEditor;
51 }
52 // assign the Qt editor also if the QGIS editor has been previously assigned
53 // this avoids testing each time which widget to use
54 // the QGIS editor must be used for non-virtual methods (dateTime, setDateTime)
55 QDateTimeEdit *qtEditor = dynamic_cast<QDateTimeEdit *>( editor );
56 if ( qtEditor )
57 {
58 mQDateTimeEdit = qtEditor;
59 }
60
61 if ( !mQDateTimeEdit )
62 {
63 QgsDebugError( QStringLiteral( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ) );
64 QgsMessageLog::logMessage( tr( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ), tr( "UI forms" ), Qgis::MessageLevel::Warning );
65 return;
66 }
67
68 const QString displayFormat = config( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
69 mQDateTimeEdit->setDisplayFormat( displayFormat );
70
71 const bool calendar = config( QStringLiteral( "calendar_popup" ), true ).toBool();
72 if ( calendar != mQDateTimeEdit->calendarPopup() )
73 {
74 mQDateTimeEdit->setCalendarPopup( calendar );
75 }
76 if ( calendar && mQDateTimeEdit->calendarWidget() )
77 {
78 // highlight today's date
79 QTextCharFormat todayFormat;
80 todayFormat.setBackground( QColor( 160, 180, 200 ) );
81 mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
82 }
83
84 const bool allowNull = config( QStringLiteral( "allow_null" ), true ).toBool();
85 if ( mQgsDateTimeEdit )
86 {
87 mQgsDateTimeEdit->setAllowNull( allowNull );
88 }
89 else
90 {
91 QgsMessageLog::logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
92 "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
93 tr( "field widgets" ) );
94 }
95
96 if ( mQgsDateTimeEdit )
97 {
98 connect( mQgsDateTimeEdit, &QgsDateTimeEdit::valueChanged, this, &QgsDateTimeEditWrapper::dateTimeChanged );
99 }
100 else
101 {
102 connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeEditWrapper::dateTimeChanged );
103 }
104}
105
107{
108 return mQgsDateTimeEdit || mQDateTimeEdit;
109}
110
112{
113 if ( mQgsDateTimeEdit )
114 mQgsDateTimeEdit->setEmpty();
115}
116
117void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime &dateTime )
118{
119 switch ( field().type() )
120 {
121 case QMetaType::Type::QDateTime:
123 emit valueChanged( dateTime );
125 emit valuesChanged( dateTime );
126 break;
127 case QMetaType::Type::QDate:
129 emit valueChanged( dateTime.date() );
131 emit valuesChanged( dateTime.date() );
132 break;
133 case QMetaType::Type::QTime:
135 emit valueChanged( dateTime.time() );
137 emit valuesChanged( dateTime.time() );
138 break;
139 default:
140 if ( !dateTime.isValid() || dateTime.isNull() )
141 {
146 }
147 else
148 {
149 const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
150 const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
151 if ( fieldIsoFormat )
152 {
154 emit valueChanged( dateTime.toString( Qt::ISODate ) );
156 emit valuesChanged( dateTime.toString( Qt::ISODate ) );
157 }
158 else
159 {
161 emit valueChanged( dateTime.toString( fieldFormat ) );
163 emit valuesChanged( dateTime.toString( fieldFormat ) );
164 }
165 }
166 break;
167 }
168}
169
171{
172 if ( !mQDateTimeEdit )
173 return QgsVariantUtils::createNullVariant( field().type() );
174
175 QDateTime dateTime;
176 if ( mQgsDateTimeEdit )
177 {
178 dateTime = mQgsDateTimeEdit->dateTime();
179 }
180 else
181 {
182 dateTime = mQDateTimeEdit->dateTime();
183 }
184
185 if ( dateTime.isNull() )
186 return QgsVariantUtils::createNullVariant( field().type() );
187
188 switch ( field().type() )
189 {
190 case QMetaType::Type::QDateTime:
191 return dateTime;
192 case QMetaType::Type::QDate:
193 return dateTime.date();
194 case QMetaType::Type::QTime:
195 return dateTime.time();
196 default:
197 const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
198 const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
199 if ( fieldIsoFormat )
200 {
201 return dateTime.toString( Qt::ISODate );
202 }
203 else
204 {
205 return dateTime.toString( fieldFormat );
206 }
207 }
208#ifndef _MSC_VER // avoid warnings
209 return QVariant(); // avoid warnings
210#endif
211}
212
213void QgsDateTimeEditWrapper::updateValues( const QVariant &value, const QVariantList & )
214{
215 if ( !mQDateTimeEdit )
216 return;
217
218 QDateTime dateTime;
219
220 switch ( field().type() )
221 {
222 case QMetaType::Type::QDateTime:
223 dateTime = value.toDateTime();
224 break;
225 case QMetaType::Type::QDate:
226 dateTime.setDate( value.toDate() );
227 dateTime.setTime( QTime( 0, 0, 0 ) );
228 break;
229 case QMetaType::Type::QTime:
230 dateTime.setDate( QDate::currentDate() );
231 dateTime.setTime( value.toTime() );
232 break;
233 default:
234 // Field type is not a date/time but we might already have a date/time variant
235 // value coming from a default: no need for string parsing in that case
236 switch ( value.userType() )
237 {
238 case QMetaType::Type::QDateTime:
239 {
240 dateTime = value.toDateTime();
241 break;
242 }
243 case QMetaType::Type::QDate:
244 {
245 dateTime.setDate( value.toDate() );
246 dateTime.setTime( QTime( 0, 0, 0 ) );
247 break;
248 }
249 case QMetaType::Type::QTime:
250 {
251 dateTime.setDate( QDate::currentDate() );
252 dateTime.setTime( value.toTime() );
253 break;
254 }
255 default:
256 {
257 const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
258 const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
259 if ( fieldIsoFormat )
260 {
261 dateTime = QDateTime::fromString( value.toString(), Qt::ISODate );
262 }
263 else
264 {
265 dateTime = QDateTime::fromString( value.toString(), fieldFormat );
266 }
267 break;
268 }
269 }
270 break;
271 }
272
273 if ( mQgsDateTimeEdit )
274 {
275 // Convert to UTC if the format string includes a Z
276 if ( mQgsDateTimeEdit->displayFormat().indexOf( "Z" ) > 0 )
277 {
278 dateTime = dateTime.toUTC();
279 }
280
281 mQgsDateTimeEdit->setDateTime( dateTime );
282 }
283 else
284 {
285 mQDateTimeEdit->setDateTime( dateTime );
286 }
287}
288
290{
291 if ( !mQDateTimeEdit )
292 return;
293
294 mQDateTimeEdit->setReadOnly( !enabled );
295 mQDateTimeEdit->setFrame( enabled );
296}
@ Warning
Warning message.
Definition qgis.h:156
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 QString defaultFormat(QMetaType::Type type)
Gets the default format in function of the type.
Manages an editor widget Widget and wrapper share the same parent.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
QgsField field() const
Access the field.
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.
QWidget * widget()
Access the widget managed by this wrapper.
QVariantMap config() const
Returns the whole config.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6526
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6525
#define QgsDebugError(str)
Definition qgslogger.h:38