QGIS API Documentation  2.99.0-Master (585a4d3)
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 
24 #include <QDateTimeEdit>
25 #include <QDateEdit>
26 #include <QTimeEdit>
27 #include <QTextCharFormat>
28 #include <QCalendarWidget>
29 
30 QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
31  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
32 
33 {
34 }
35 
36 QWidget *QgsDateTimeEditWrapper::createWidget( QWidget *parent )
37 {
38  QgsDateTimeEdit *widget = new QgsDateTimeEdit( parent );
39  widget->setDateTime( QDateTime::currentDateTime() );
40  return widget;
41 }
42 
43 void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
44 {
45  QgsDateTimeEdit *qgsEditor = dynamic_cast<QgsDateTimeEdit *>( editor );
46  if ( qgsEditor )
47  {
48  mQgsDateTimeEdit = qgsEditor;
49  }
50  // assign the Qt editor also if the QGIS editor has been previously assigned
51  // this avoids testing each time which widget to use
52  // the QGIS editor must be used for non-virtual methods (dateTime, setDateTime)
53  QDateTimeEdit *qtEditor = dynamic_cast<QDateTimeEdit *>( editor );
54  if ( qtEditor )
55  {
56  mQDateTimeEdit = qtEditor;
57  }
58 
59  if ( !mQDateTimeEdit )
60  {
61  QgsDebugMsg( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." );
62  QgsMessageLog::logMessage( tr( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ), tr( "UI forms" ), Qgis::Warning );
63  return;
64  }
65 
66  const QString displayFormat = config( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
67  mQDateTimeEdit->setDisplayFormat( displayFormat );
68 
69  const bool calendar = config( QStringLiteral( "calendar_popup" ), true ).toBool();
70  if ( calendar != mQDateTimeEdit->calendarPopup() )
71  {
72  mQDateTimeEdit->setCalendarPopup( calendar );
73  }
74  if ( calendar && mQDateTimeEdit->calendarWidget() )
75  {
76  // highlight today's date
77  QTextCharFormat todayFormat;
78  todayFormat.setBackground( QColor( 160, 180, 200 ) );
79  mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
80  }
81 
82  const bool allowNull = config( QStringLiteral( "allow_null" ), true ).toBool();
83  if ( mQgsDateTimeEdit )
84  {
85  mQgsDateTimeEdit->setAllowNull( allowNull );
86  }
87  else
88  {
89  QgsApplication::messageLog()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
90  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
91  tr( "field widgets" ) );
92  }
93 
94  if ( mQgsDateTimeEdit )
95  {
96  connect( mQgsDateTimeEdit, &QgsDateTimeEdit::valueChanged, this, &QgsDateTimeEditWrapper::dateTimeChanged );
97  }
98  else
99  {
100  connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeEditWrapper::dateTimeChanged );
101  }
102 }
103 
105 {
106  return mQgsDateTimeEdit || mQDateTimeEdit;
107 }
108 
110 {
111  if ( mQgsDateTimeEdit )
112  mQgsDateTimeEdit->setEmpty();
113 }
114 
115 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime &dateTime )
116 {
117  switch ( field().type() )
118  {
119  case QVariant::DateTime:
120  emit valueChanged( dateTime );
121  break;
122  case QVariant::Date:
123  emit valueChanged( dateTime.date() );
124  break;
125  case QVariant::Time:
126  emit valueChanged( dateTime.time() );
127  break;
128  default:
129  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
130  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
131  if ( fieldIsoFormat )
132  {
133  emit valueChanged( dateTime.toString( Qt::ISODate ) );
134  }
135  else
136  {
137  emit valueChanged( dateTime.toString( fieldFormat ) );
138  }
139  break;
140  }
141 }
142 
144 {
145  if ( !mQDateTimeEdit )
146  return QVariant( field().type() );
147 
148  QDateTime dateTime;
149  if ( mQgsDateTimeEdit )
150  {
151  dateTime = mQgsDateTimeEdit->dateTime();
152  }
153  else
154  {
155  dateTime = mQDateTimeEdit->dateTime();
156  }
157 
158  switch ( field().type() )
159  {
160  case QVariant::DateTime:
161  return dateTime;
162  break;
163  case QVariant::Date:
164  return dateTime.date();
165  break;
166  case QVariant::Time:
167  return dateTime.time();
168  break;
169  default:
170  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
171  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
172  if ( fieldIsoFormat )
173  {
174  return dateTime.toString( Qt::ISODate );
175  }
176  else
177  {
178  return dateTime.toString( fieldFormat );
179  }
180  break;
181  }
182  return QVariant(); // avoid warnings
183 }
184 
186 {
187  if ( !mQDateTimeEdit )
188  return;
189 
190  QDateTime dateTime;
191  switch ( field().type() )
192  {
193  case QVariant::DateTime:
194  case QVariant::Date:
195  case QVariant::Time:
196  dateTime = value.toDateTime();
197  break;
198  default:
199  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
200  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
201  if ( fieldIsoFormat )
202  {
203  dateTime = QDateTime::fromString( value.toString(), Qt::ISODate );
204  }
205  else
206  {
207  dateTime = QDateTime::fromString( value.toString(), fieldFormat );
208  }
209  break;
210  }
211 
212  if ( mQgsDateTimeEdit )
213  {
214  mQgsDateTimeEdit->setDateTime( dateTime );
215  }
216  else
217  {
218  mQDateTimeEdit->setDateTime( dateTime );
219  }
220 }
221 
223 {
224  if ( !mQDateTimeEdit )
225  return;
226 
227  mQDateTimeEdit->setEnabled( enabled );
228 }
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.
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
QVariantMap config() const
Returns the whole config.
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&#39;s value.
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
static QString defaultFormat(QVariant::Type type)
Get the default format in function of the type.
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 valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
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)
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...