QGIS API Documentation
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  mQDateTimeEdit->setCalendarPopup( calendar );
69  if ( calendar )
70  {
71  // highlight today's date
72  QTextCharFormat todayFormat;
73  todayFormat.setBackground( QColor( 160, 180, 200 ) );
74  mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
75  }
76 
77  const bool allowNull = config( "allow_null", true ).toBool();
78  if ( mQgsDateTimeEdit )
79  {
80  mQgsDateTimeEdit->setAllowNull( allowNull );
81  }
82  else
83  {
84  QgsMessageLog::instance()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
85  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
86  "field widgets" );
87  }
88 
89  if ( mQgsDateTimeEdit )
90  {
91  connect( mQgsDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
92  }
93  else
94  {
95  connect( mQDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
96  }
97 }
98 
100 {
101  return mQgsDateTimeEdit || mQDateTimeEdit;
102 }
103 
104 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
105 {
106  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
107  emit valueChanged( dateTime.toString( fieldFormat ) );
108 }
109 
111 {
112  if ( !mQDateTimeEdit )
113  return QVariant( field().type() );
114 
115  if ( field().type() == QVariant::DateTime )
116  {
117  if ( mQgsDateTimeEdit )
118  {
119  return mQgsDateTimeEdit->dateTime();
120  }
121  else
122  {
123  return mQDateTimeEdit->dateTime();
124  }
125  }
126 
127  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
128 
129  if ( mQgsDateTimeEdit )
130  {
131  return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
132  }
133  else
134  {
135  return mQDateTimeEdit->dateTime().toString( fieldFormat );
136  }
137 }
138 
140 {
141  if ( !mQDateTimeEdit )
142  return;
143 
144  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
145  const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );
146 
147  if ( mQgsDateTimeEdit )
148  {
149  mQgsDateTimeEdit->setDateTime( date );
150  }
151  else
152  {
153  mQDateTimeEdit->setDateTime( date );
154  }
155 }
156 
158 {
159  if ( !mQDateTimeEdit )
160  return;
161 
162  mQDateTimeEdit->setEnabled( enabled );
163 }
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
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.
void setCalendarPopup(bool enable)
QCalendarWidget * calendarWidget() const
QDateTime currentDateTime()
static QgsMessageLog * instance()
QDate currentDate()
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