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