QGIS API Documentation  2.99.0-Master (a18066b)
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 
24 #include <QDateTimeEdit>
25 #include <QDateEdit>
26 #include <QTimeEdit>
27 #include <QTextCharFormat>
28 #include <QCalendarWidget>
29 
30 QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
31  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
32  , mQDateTimeEdit( nullptr )
33  , mQgsDateTimeEdit( nullptr )
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( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." );
63  QgsMessageLog::logMessage( QStringLiteral( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ), QStringLiteral( "UI forms" ), QgsMessageLog::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" ), false ).toBool();
71  mQDateTimeEdit->setCalendarPopup( calendar );
72  if ( calendar && mQDateTimeEdit->calendarWidget() )
73  {
74  // highlight today's date
75  QTextCharFormat todayFormat;
76  todayFormat.setBackground( QColor( 160, 180, 200 ) );
77  mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
78  }
79 
80  const bool allowNull = config( QStringLiteral( "allow_null" ), true ).toBool();
81  if ( mQgsDateTimeEdit )
82  {
83  mQgsDateTimeEdit->setAllowNull( allowNull );
84  }
85  else
86  {
87  QgsApplication::messageLog()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
88  "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
89  QStringLiteral( "field widgets" ) );
90  }
91 
92  if ( mQgsDateTimeEdit )
93  {
94  connect( mQgsDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
95  }
96  else
97  {
98  connect( mQDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
99  }
100 }
101 
103 {
104  return mQgsDateTimeEdit || mQDateTimeEdit;
105 }
106 
108 {
109  if ( mQgsDateTimeEdit )
110  mQgsDateTimeEdit->setEmpty();
111 }
112 
113 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
114 {
115  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
116  emit valueChanged( dateTime.toString( fieldFormat ) );
117 }
118 
120 {
121  if ( !mQDateTimeEdit )
122  return QVariant( field().type() );
123 
124  if ( field().type() == QVariant::DateTime )
125  {
126  if ( mQgsDateTimeEdit )
127  {
128  return mQgsDateTimeEdit->dateTime();
129  }
130  else
131  {
132  return mQDateTimeEdit->dateTime();
133  }
134  }
135 
136  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
137 
138  if ( mQgsDateTimeEdit )
139  {
140  return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
141  }
142  else
143  {
144  return mQDateTimeEdit->dateTime().toString( fieldFormat );
145  }
146 }
147 
149 {
150  if ( !mQDateTimeEdit )
151  return;
152 
153  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
154  const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );
155 
156  if ( mQgsDateTimeEdit )
157  {
158  mQgsDateTimeEdit->setDateTime( date );
159  }
160  else
161  {
162  mQDateTimeEdit->setDateTime( date );
163  }
164 }
165 
167 {
168  if ( !mQDateTimeEdit )
169  return;
170 
171  mQDateTimeEdit->setEnabled( enabled );
172 }
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:36
Manages an editor widget Widget and wrapper share the same parent.
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=nullptr)
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)
Get 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::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
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.
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.
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:94
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...