QGIS API Documentation  3.2.0-Bonn (bc43194)
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  if ( !dateTime.isValid() || dateTime.isNull() )
130  {
131  emit valueChanged( QVariant( field().type() ) );
132  }
133  else
134  {
135  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
136  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
137  if ( fieldIsoFormat )
138  {
139  emit valueChanged( dateTime.toString( Qt::ISODate ) );
140  }
141  else
142  {
143  emit valueChanged( dateTime.toString( fieldFormat ) );
144  }
145  }
146  break;
147  }
148 }
149 
151 {
152  if ( !mQDateTimeEdit )
153  return QVariant( field().type() );
154 
155  QDateTime dateTime;
156  if ( mQgsDateTimeEdit )
157  {
158  dateTime = mQgsDateTimeEdit->dateTime();
159  }
160  else
161  {
162  dateTime = mQDateTimeEdit->dateTime();
163  }
164 
165  switch ( field().type() )
166  {
167  case QVariant::DateTime:
168  return dateTime;
169  break;
170  case QVariant::Date:
171  return dateTime.date();
172  break;
173  case QVariant::Time:
174  return dateTime.time();
175  break;
176  default:
177  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
178  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
179  if ( fieldIsoFormat )
180  {
181  return dateTime.toString( Qt::ISODate );
182  }
183  else
184  {
185  return dateTime.toString( fieldFormat );
186  }
187  break;
188  }
189  return QVariant(); // avoid warnings
190 }
191 
193 {
194  if ( !mQDateTimeEdit )
195  return;
196 
197  QDateTime dateTime;
198  switch ( field().type() )
199  {
200  case QVariant::DateTime:
201  case QVariant::Date:
202  case QVariant::Time:
203  dateTime = value.toDateTime();
204  break;
205  default:
206  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
207  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
208  if ( fieldIsoFormat )
209  {
210  dateTime = QDateTime::fromString( value.toString(), Qt::ISODate );
211  }
212  else
213  {
214  dateTime = QDateTime::fromString( value.toString(), fieldFormat );
215  }
216  break;
217  }
218 
219  if ( mQgsDateTimeEdit )
220  {
221  mQgsDateTimeEdit->setDateTime( dateTime );
222  }
223  else
224  {
225  mQDateTimeEdit->setDateTime( dateTime );
226  }
227 }
228 
230 {
231  if ( !mQDateTimeEdit )
232  return;
233 
234  mQDateTimeEdit->setEnabled( enabled );
235 }
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.
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 setValue(const QVariant &value) override
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.
bool valid() const override
Returns 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...