QGIS API Documentation  2.99.0-Master (6a61179)
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"
22 
23 #include <QDateTimeEdit>
24 #include <QDateEdit>
25 #include <QTimeEdit>
26 #include <QTextCharFormat>
27 #include <QCalendarWidget>
28 
29 QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
30  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
31  , mQDateTimeEdit( nullptr )
32  , mQgsDateTimeEdit( nullptr )
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( QStringLiteral( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ), QStringLiteral( "UI forms" ), QgsMessageLog::WARNING );
63  return;
64  }
65 
66  const QString displayFormat = config( QStringLiteral( "display_format" ), QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
67  mQDateTimeEdit->setDisplayFormat( displayFormat );
68 
69  const bool calendar = config( QStringLiteral( "calendar_popup" ), false ).toBool();
70  mQDateTimeEdit->setCalendarPopup( calendar );
71  if ( calendar && mQDateTimeEdit->calendarWidget() )
72  {
73  // highlight today's date
74  QTextCharFormat todayFormat;
75  todayFormat.setBackground( QColor( 160, 180, 200 ) );
76  mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
77  }
78 
79  const bool allowNull = config( QStringLiteral( "allow_null" ), true ).toBool();
80  if ( mQgsDateTimeEdit )
81  {
82  mQgsDateTimeEdit->setAllowNull( allowNull );
83  }
84  else
85  {
86  QgsMessageLog::instance()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
87  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
88  QStringLiteral( "field widgets" ) );
89  }
90 
91  if ( mQgsDateTimeEdit )
92  {
93  connect( mQgsDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
94  }
95  else
96  {
97  connect( mQDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
98  }
99 }
100 
102 {
103  return mQgsDateTimeEdit || mQDateTimeEdit;
104 }
105 
107 {
108  if ( mQgsDateTimeEdit )
109  mQgsDateTimeEdit->setEmpty();
110 }
111 
112 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
113 {
114  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
115  emit valueChanged( dateTime.toString( fieldFormat ) );
116 }
117 
119 {
120  if ( !mQDateTimeEdit )
121  return QVariant( field().type() );
122 
123  if ( field().type() == QVariant::DateTime )
124  {
125  if ( mQgsDateTimeEdit )
126  {
127  return mQgsDateTimeEdit->dateTime();
128  }
129  else
130  {
131  return mQDateTimeEdit->dateTime();
132  }
133  }
134 
135  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
136 
137  if ( mQgsDateTimeEdit )
138  {
139  return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
140  }
141  else
142  {
143  return mQDateTimeEdit->dateTime().toString( fieldFormat );
144  }
145 }
146 
148 {
149  if ( !mQDateTimeEdit )
150  return;
151 
152  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
153  const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );
154 
155  if ( mQgsDateTimeEdit )
156  {
157  mQgsDateTimeEdit->setDateTime( date );
158  }
159  else
160  {
161  mQDateTimeEdit->setDateTime( date );
162  }
163 }
164 
166 {
167  if ( !mQDateTimeEdit )
168  return;
169 
170  mQDateTimeEdit->setEnabled( enabled );
171 }
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
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.
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
QVariant value() const override
Will be used to access the widget&#39;s value.
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)
static QString defaultFormat(const QVariant::Type type)
Get the default format in fonction of the type.
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.
static QgsMessageLog * instance()
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.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:98
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...