QGIS API Documentation  2.11.0-Master
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 : denis.rouzaud@gmail.com
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( NULL )
31  , mQgsDateTimeEdit( NULL )
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  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
110 
111  if ( mQgsDateTimeEdit )
112  {
113  return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
114  }
115  else
116  {
117  return mQDateTimeEdit->dateTime().toString( fieldFormat );
118  }
119 }
120 
122 {
123  if ( !mQDateTimeEdit )
124  return;
125 
126  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
127  const QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );
128 
129  if ( mQgsDateTimeEdit )
130  {
131  mQgsDateTimeEdit->setDateTime( date );
132  }
133  else
134  {
135  mQDateTimeEdit->setDateTime( date );
136  }
137 }
138 
140 {
141  if ( !mQDateTimeEdit )
142  return;
143 
144  mQDateTimeEdit->setEnabled( enabled );
145 }
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.
QgsDateTimeEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=0)
void setDisplayFormat(const QString &format)
QgsField field()
Access the field.
QString tr(const char *sourceText, const char *disambiguation, int n)
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void setEnabled(bool)
void setValue(const QVariant &value) override
const QgsEditorWidgetConfig config()
Returns the whole config.
void setEnabled(bool enabled) override
QVariant value() override
Will be used to access the widget's value.
bool valid() override
Return true if the widget has been properly initialized.
QDateTime fromString(const QString &string, Qt::DateFormat format)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setCalendarPopup(bool enable)
QDateTime currentDateTime()
static QgsMessageLog * instance()
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.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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...