QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
28 QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
29  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
30  , mQDateTimeEdit( NULL )
31  , mQgsDateTimeEdit( NULL )
32 {
33 }
34 
35 QWidget *QgsDateTimeEditWrapper::createWidget( QWidget *parent )
36 {
37  QgsDateTimeEdit* widget = new QgsDateTimeEdit( parent );
38  widget->setDateTime( QDateTime::currentDateTime() );
39  return widget;
40 }
41 
42 void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
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 
93 void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
94 {
95  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
96  emit valueChanged( dateTime.toString( fieldFormat ) );
97 }
98 
100 {
101  if ( !mQDateTimeEdit )
102  return QVariant( field().type() );
103 
104  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
105 
106  if ( mQgsDateTimeEdit )
107  {
108  return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
109  }
110  else
111  {
112  return mQDateTimeEdit->dateTime().toString( fieldFormat );
113  }
114 }
115 
116 void QgsDateTimeEditWrapper::setValue( const QVariant &value )
117 {
118  if ( !mQDateTimeEdit )
119  return;
120 
121  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
122  const QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );
123 
124  if ( mQgsDateTimeEdit )
125  {
126  mQgsDateTimeEdit->setDateTime( date );
127  }
128  else
129  {
130  mQDateTimeEdit->setDateTime( date );
131  }
132 }
133 
135 {
136  if ( !mQDateTimeEdit )
137  return;
138 
139  mQDateTimeEdit->setEnabled( enabled );
140 }