QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
21 #include <QDateTimeEdit>
22 #include <QDateEdit>
23 #include <QTimeEdit>
24 #include <QTextCharFormat>
25 #include <QCalendarWidget>
26 
28  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
29  , mQDateTimeEdit( nullptr )
30  , mQgsDateTimeEdit( nullptr )
31 {
32 }
33 
35 {
36  QgsDateTimeEdit* widget = new QgsDateTimeEdit( parent );
38  return widget;
39 }
40 
42 {
43  QgsDateTimeEdit* qgsEditor = dynamic_cast<QgsDateTimeEdit*>( editor );
44  if ( qgsEditor )
45  {
46  mQgsDateTimeEdit = qgsEditor;
47  }
48  // assign the Qt editor also if the QGIS editor has been previously assigned
49  // this avoids testing each time which widget to use
50  // the QGIS editor must be used for non-virtual methods (dateTime, setDateTime)
51  QDateTimeEdit* qtEditor = dynamic_cast<QDateTimeEdit*>( editor );
52  if ( qtEditor )
53  {
54  mQDateTimeEdit = qtEditor;
55  }
56 
57  if ( !mQDateTimeEdit )
58  {
59  QgsDebugMsg( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." );
60  QgsMessageLog::logMessage( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit.", "UI forms", QgsMessageLog::WARNING );
61  return;
62  }
63 
64  const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
65  mQDateTimeEdit->setDisplayFormat( displayFormat );
66 
67  const bool calendar = config( "calendar_popup", false ).toBool();
68 
69  if ( calendar != mQDateTimeEdit->calendarPopup() )
70  {
71  mQDateTimeEdit->setCalendarPopup( calendar );
72  }
73  if ( calendar && mQDateTimeEdit->calendarWidget() )
74  {
75  // highlight today's date
76  QTextCharFormat todayFormat;
77  todayFormat.setBackground( QColor( 160, 180, 200 ) );
78  mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
79  }
80 
81  const bool allowNull = config( "allow_null", true ).toBool();
82  if ( mQgsDateTimeEdit )
83  {
84  mQgsDateTimeEdit->setAllowNull( allowNull );
85  }
86  else
87  {
88  QgsMessageLog::instance()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
89  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
90  "field widgets" );
91  }
92 
93  if ( mQgsDateTimeEdit )
94  {
95  connect( mQgsDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
96  }
97  else
98  {
99  connect( mQDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
100  }
101 }
102 
104 {
105  return mQgsDateTimeEdit || mQDateTimeEdit;
106 }
107 
109 {
110  if ( mQgsDateTimeEdit )
111  mQgsDateTimeEdit->setEmpty();
112 }
113 
114 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
115 {
116  switch ( field().type() )
117  {
118  case QVariant::DateTime:
119  emit valueChanged( dateTime );
120  break;
121  case QVariant::Date:
122  emit valueChanged( dateTime.date() );
123  break;
124  case QVariant::Time:
125  emit valueChanged( dateTime.time() );
126  break;
127  default:
128  const bool fieldIsoFormat = config( "field_iso_format" , false ).toBool();
129  const QString fieldFormat = config( "field_format" , QGSDATETIMEEDIT_DATEFORMAT ).toString();
130  if ( fieldIsoFormat )
131  {
132  emit valueChanged( dateTime.toString( Qt::ISODate ) );
133  }
134  else
135  {
136  emit valueChanged( dateTime.toString( fieldFormat ) );
137  }
138  break;
139  }
140 }
141 
143 {
144  if ( !mQDateTimeEdit )
145  return QVariant( field().type() );
146 
147  QDateTime dateTime;
148  if ( mQgsDateTimeEdit )
149  {
150  dateTime = mQgsDateTimeEdit->dateTime();
151  }
152  else
153  {
154  dateTime = mQDateTimeEdit->dateTime();
155  }
156 
157  switch ( field().type() )
158  {
159  case QVariant::DateTime:
160  return dateTime;
161  break;
162  case QVariant::Date:
163  return dateTime.date();
164  break;
165  case QVariant::Time:
166  return dateTime.time();
167  break;
168  default:
169  const bool fieldIsoFormat = config( "field_iso_format", false ).toBool();
170  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
171  if ( fieldIsoFormat )
172  {
173  return dateTime.toString( Qt::ISODate );
174  }
175  else
176  {
177  return dateTime.toString( fieldFormat );
178  }
179  break;
180  }
181  return QVariant();
182 }
183 
185 {
186  if ( !mQDateTimeEdit )
187  return;
188 
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( "field_iso_format", false ).toBool();
200  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).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.
QString toString(Qt::DateFormat format) const
#define QGSDATETIMEEDIT_DATEFORMAT
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
Manages an editor widget Widget and wrapper share the same parent.
QDateTime toDateTime() const
void setDisplayFormat(const QString &format)
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
QTime time() const
QString tr(const char *sourceText, const char *disambiguation, int n)
QVariant value() const override
Will be used to access the widget&#39;s value.
void setEnabled(bool)
void setValue(const QVariant &value) override
void setEnabled(bool enabled) override
void setBackground(const QBrush &brush)
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void setDateTextFormat(const QDate &date, const QTextCharFormat &format)
QDateTime fromString(const QString &string, Qt::DateFormat format)
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.
QCalendarWidget * calendarWidget() const
QDateTime currentDateTime()
QDate date() const
static QgsMessageLog * instance()
QDate currentDate()
virtual 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.
QgsEditorWidgetConfig config() const
Returns the whole config.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString toString() const
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...