QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsdatetimeeditwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatetimeeditwrapper.cpp
3  --------------------------------------
4  Date : 03.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
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 
16 #include "qgsdatetimeeditwrapper.h"
17 #include "qgsdatetimeeditfactory.h"
18 #include "qgsmessagelog.h"
19 #include "qgslogger.h"
20 #include "qgsdatetimeedit.h"
21 #include "qgsdatetimeeditconfig.h"
23 #include "qgsapplication.h"
24 
25 #include <QDateTimeEdit>
26 #include <QDateEdit>
27 #include <QTimeEdit>
28 #include <QTextCharFormat>
29 #include <QCalendarWidget>
30 
31 QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
32  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
33 
34 {
35 }
36 
37 QWidget *QgsDateTimeEditWrapper::createWidget( QWidget *parent )
38 {
39  QgsDateTimeEdit *widget = new QgsDateTimeEdit( parent );
40  widget->setDateTime( QDateTime::currentDateTime() );
41  return widget;
42 }
43 
44 void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
45 {
46  QgsDateTimeEdit *qgsEditor = dynamic_cast<QgsDateTimeEdit *>( editor );
47  if ( qgsEditor )
48  {
49  mQgsDateTimeEdit = qgsEditor;
50  }
51  // assign the Qt editor also if the QGIS editor has been previously assigned
52  // this avoids testing each time which widget to use
53  // the QGIS editor must be used for non-virtual methods (dateTime, setDateTime)
54  QDateTimeEdit *qtEditor = dynamic_cast<QDateTimeEdit *>( editor );
55  if ( qtEditor )
56  {
57  mQDateTimeEdit = qtEditor;
58  }
59 
60  if ( !mQDateTimeEdit )
61  {
62  QgsDebugMsg( QStringLiteral( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ) );
63  QgsMessageLog::logMessage( tr( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ), tr( "UI forms" ), Qgis::Warning );
64  return;
65  }
66 
67  const QString displayFormat = config( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
68  mQDateTimeEdit->setDisplayFormat( displayFormat );
69 
70  const bool calendar = config( QStringLiteral( "calendar_popup" ), true ).toBool();
71  if ( calendar != mQDateTimeEdit->calendarPopup() )
72  {
73  mQDateTimeEdit->setCalendarPopup( calendar );
74  }
75  if ( calendar && mQDateTimeEdit->calendarWidget() )
76  {
77  // highlight today's date
78  QTextCharFormat todayFormat;
79  todayFormat.setBackground( QColor( 160, 180, 200 ) );
80  mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
81  }
82 
83  const bool allowNull = config( QStringLiteral( "allow_null" ), true ).toBool();
84  if ( mQgsDateTimeEdit )
85  {
86  mQgsDateTimeEdit->setAllowNull( allowNull );
87  }
88  else
89  {
90  QgsApplication::messageLog()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
91  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
92  tr( "field widgets" ) );
93  }
94 
95  if ( mQgsDateTimeEdit )
96  {
97  connect( mQgsDateTimeEdit, &QgsDateTimeEdit::valueChanged, this, &QgsDateTimeEditWrapper::dateTimeChanged );
98  }
99  else
100  {
101  connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeEditWrapper::dateTimeChanged );
102  }
103 }
104 
106 {
107  return mQgsDateTimeEdit || mQDateTimeEdit;
108 }
109 
111 {
112  if ( mQgsDateTimeEdit )
113  mQgsDateTimeEdit->setEmpty();
114 }
115 
116 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime &dateTime )
117 {
118  switch ( field().type() )
119  {
120  case QVariant::DateTime:
122  emit valueChanged( dateTime );
124  emit valuesChanged( dateTime );
125  break;
126  case QVariant::Date:
128  emit valueChanged( dateTime.date() );
130  emit valuesChanged( dateTime.date() );
131  break;
132  case QVariant::Time:
134  emit valueChanged( dateTime.time() );
136  emit valuesChanged( dateTime.time() );
137  break;
138  default:
139  if ( !dateTime.isValid() || dateTime.isNull() )
140  {
142  emit valueChanged( QVariant( field().type() ) );
144  emit valuesChanged( QVariant( field().type() ) );
145  }
146  else
147  {
148  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
149  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
150  if ( fieldIsoFormat )
151  {
153  emit valueChanged( dateTime.toString( Qt::ISODate ) );
155  emit valuesChanged( dateTime.toString( Qt::ISODate ) );
156  }
157  else
158  {
160  emit valueChanged( dateTime.toString( fieldFormat ) );
162  emit valuesChanged( dateTime.toString( fieldFormat ) );
163  }
164  }
165  break;
166  }
167 }
168 
170 {
171  if ( !mQDateTimeEdit )
172  return QVariant( field().type() );
173 
174  QDateTime dateTime;
175  if ( mQgsDateTimeEdit )
176  {
177  dateTime = mQgsDateTimeEdit->dateTime();
178  }
179  else
180  {
181  dateTime = mQDateTimeEdit->dateTime();
182  }
183 
184  switch ( field().type() )
185  {
186  case QVariant::DateTime:
187  return dateTime;
188  case QVariant::Date:
189  return dateTime.date();
190  case QVariant::Time:
191  return dateTime.time();
192  default:
193  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
194  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
195  if ( fieldIsoFormat )
196  {
197  return dateTime.toString( Qt::ISODate );
198  }
199  else
200  {
201  return dateTime.toString( fieldFormat );
202  }
203  }
204 #ifndef _MSC_VER // avoid warnings
205  return QVariant(); // avoid warnings
206 #endif
207 }
208 
209 void QgsDateTimeEditWrapper::updateValues( const QVariant &value, const QVariantList & )
210 {
211  if ( !mQDateTimeEdit )
212  return;
213 
214  QDateTime dateTime;
215  switch ( field().type() )
216  {
217  case QVariant::DateTime:
218  case QVariant::Date:
219  case QVariant::Time:
220  dateTime = value.toDateTime();
221  break;
222  default:
223  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
224  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
225  if ( fieldIsoFormat )
226  {
227  dateTime = QDateTime::fromString( value.toString(), Qt::ISODate );
228  }
229  else
230  {
231  dateTime = QDateTime::fromString( value.toString(), fieldFormat );
232  }
233  break;
234  }
235 
236  if ( mQgsDateTimeEdit )
237  {
238  mQgsDateTimeEdit->setDateTime( dateTime );
239  }
240  else
241  {
242  mQDateTimeEdit->setDateTime( dateTime );
243  }
244 }
245 
247 {
248  if ( !mQDateTimeEdit )
249  return;
250 
251  mQDateTimeEdit->setEnabled( enabled );
252 }
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsField field() const
Access the field.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
Manages an editor widget Widget and wrapper share the same parent.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
QVariantMap config() const
Returns the whole config.
QVariant value() const override
Will be used to access the widget&#39;s value.
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
void setEnabled(bool enabled) override
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
bool valid() const override
Returns true if the widget has been properly initialized.
void valueChanged(const QDateTime &date)
Signal emitted whenever the value changes.
QgsDateTimeEditWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
Constructor for QgsDateTimeEditWrapper.
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.
QWidget * widget()
Access the widget managed by this wrapper.
void setDateTime(const QDateTime &dateTime)
Set the date time in the widget and handles null date times.
Represents a vector layer which manages a vector based data sets.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
QDateTime dateTime() const
Returns the date time which can be a null date/time.
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times...