QGIS API Documentation  2.14.0-Essen
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 
17 #include <QDateTimeEdit>
18 #include <QDateEdit>
19 #include <QTimeEdit>
20 
21 
22 #include "qgsdatetimeeditwrapper.h"
23 #include "qgsdatetimeeditfactory.h"
24 #include "qgsmessagelog.h"
25 #include "qgslogger.h"
26 
27 
29  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
30  , mQDateTimeEdit( nullptr )
31  , mQgsDateTimeEdit( nullptr )
32 {
33 }
34 
36 {
37  QgsDateTimeEdit* widget = new QgsDateTimeEdit( parent );
39  return widget;
40 }
41 
43 {
44  QgsDateTimeEdit* qgsEditor = dynamic_cast<QgsDateTimeEdit*>( editor );
45  if ( qgsEditor )
46  {
47  mQgsDateTimeEdit = qgsEditor;
48  }
49  // assign the Qt editor also if the QGIS editor has been previously assigned
50  // this avoids testing each time which widget to use
51  // the QGIS editor must be used for non-virtual methods (dateTime, setDateTime)
52  QDateTimeEdit* qtEditor = dynamic_cast<QDateTimeEdit*>( editor );
53  if ( qtEditor )
54  {
55  mQDateTimeEdit = qtEditor;
56  }
57 
58  if ( !mQDateTimeEdit )
59  {
60  QgsDebugMsg( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." );
61  QgsMessageLog::logMessage( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit.", "UI forms", QgsMessageLog::WARNING );
62  return;
63  }
64 
65  const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
66  mQDateTimeEdit->setDisplayFormat( displayFormat );
67 
68  const bool calendar = config( "calendar_popup", false ).toBool();
69  mQDateTimeEdit->setCalendarPopup( calendar );
70 
71  const bool allowNull = config( "allow_null", true ).toBool();
72  if ( mQgsDateTimeEdit )
73  {
74  mQgsDateTimeEdit->setAllowNull( allowNull );
75  }
76  else
77  {
78  QgsMessageLog::instance()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
79  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
80  "field widgets" );
81  }
82 
83  if ( mQgsDateTimeEdit )
84  {
85  connect( mQgsDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
86  }
87  else
88  {
89  connect( mQDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
90  }
91 }
92 
94 {
95  return mQgsDateTimeEdit || mQDateTimeEdit;
96 }
97 
98 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
99 {
100  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
101  emit valueChanged( dateTime.toString( fieldFormat ) );
102 }
103 
105 {
106  if ( !mQDateTimeEdit )
107  return QVariant( field().type() );
108 
109  if ( field().type() == QVariant::DateTime )
110  {
111  if ( mQgsDateTimeEdit )
112  {
113  return mQgsDateTimeEdit->dateTime();
114  }
115  else
116  {
117  return mQDateTimeEdit->dateTime();
118  }
119  }
120 
121  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
122 
123  if ( mQgsDateTimeEdit )
124  {
125  return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
126  }
127  else
128  {
129  return mQDateTimeEdit->dateTime().toString( fieldFormat );
130  }
131 }
132 
134 {
135  if ( !mQDateTimeEdit )
136  return;
137 
138  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
139  const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );
140 
141  if ( mQgsDateTimeEdit )
142  {
143  mQgsDateTimeEdit->setDateTime( date );
144  }
145  else
146  {
147  mQDateTimeEdit->setDateTime( date );
148  }
149 }
150 
152 {
153  if ( !mQDateTimeEdit )
154  return;
155 
156  mQDateTimeEdit->setEnabled( enabled );
157 }
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QString toString(Qt::DateFormat format) const
QDateTime dateTime() const
dateTime returns the date time which can eventually be a null date/time
#define QGSDATETIMEEDIT_DATEFORMAT
void valueChanged()
Will call the value() method to determine the emitted value.
#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)
QgsField field() const
Access the field.
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
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
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)
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.
void setCalendarPopup(bool enable)
QDateTime currentDateTime()
static QgsMessageLog * instance()
void setAllowNull(bool allowNull)
determines if the widget allows setting null date/time.
QgsEditorWidgetConfig config() const
Returns the whole config.
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.
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
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times...
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:89