QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsdatetimeeditconfig.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatetimeeditconfig.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 "qgsdatetimeeditconfig.h"
17 #include "qgsdatetimeeditfactory.h"
18 
20  : QgsEditorConfigWidget( vl, fieldIdx, parent )
21 {
22  setupUi( this );
23 
24  mFieldFormatComboBox->clear();
25  mFieldFormatComboBox->addItem( tr( "Date" ), QGSDATETIMEEDIT_DATEFORMAT );
26  mFieldFormatComboBox->addItem( tr( "Time" ), QGSDATETIMEEDIT_TIMEFORMAT );
27  mFieldFormatComboBox->addItem( tr( "Date time" ), QGSDATETIMEEDIT_DATETIMEFORMAT );
28  mFieldFormatComboBox->addItem( tr( "ISO date time" ), QGSDATETIMEEDIT_ISODATETIMEFORMAT );
29  mFieldFormatComboBox->addItem( tr( "Custom" ), QString() );
30 
31  mDemoDateTimeEdit->setDateTime( QDateTime::currentDateTime() );
32 
33  connect( mDisplayFormatEdit, SIGNAL( textChanged( QString ) ), this, SLOT( updateDemoWidget() ) );
34  connect( mCalendarPopupCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( updateDemoWidget() ) );
35 
36  connect( mFieldFormatComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updateFieldFormat( int ) ) );
37  connect( mFieldFormatEdit, SIGNAL( textChanged( QString ) ), this, SLOT( updateDisplayFormat( QString ) ) );
38  connect( mDisplayFormatComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( displayFormatChanged( int ) ) );
39 
40  connect( mFieldHelpToolButton, SIGNAL( clicked( bool ) ), this, SLOT( showHelp( bool ) ) );
41  connect( mDisplayHelpToolButton, SIGNAL( clicked( bool ) ), this, SLOT( showHelp( bool ) ) );
42 
43  connect( mFieldFormatEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
44  connect( mDisplayFormatEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
45  connect( mCalendarPopupCheckBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
46  connect( mAllowNullCheckBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
47 
48  // initialize
49  updateFieldFormat( mFieldFormatComboBox->currentIndex() );
50  displayFormatChanged( mDisplayFormatComboBox->currentIndex() );
51 }
52 
53 
54 void QgsDateTimeEditConfig::updateDemoWidget()
55 {
56  mDemoDateTimeEdit->setDisplayFormat( mDisplayFormatEdit->text() );
57  mDemoDateTimeEdit->setCalendarPopup( mCalendarPopupCheckBox->isChecked() );
58 }
59 
60 
61 void QgsDateTimeEditConfig::updateFieldFormat( int idx )
62 {
63  const QString format = mFieldFormatComboBox->itemData( idx ).toString();
64  bool custom = format.isEmpty();
65  if ( !custom )
66  {
67  mFieldFormatEdit->setText( format );
68  }
69 
70  mFieldFormatEdit->setEnabled( custom );
71  mFieldHelpToolButton->setVisible( custom );
72  if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
73  {
74  mHelpScrollArea->setVisible( false );
75  }
76 }
77 
78 
79 
80 void QgsDateTimeEditConfig::updateDisplayFormat( const QString& fieldFormat )
81 {
82  if ( mDisplayFormatComboBox->currentIndex() == 0 )
83  {
84  // i.e. display format is default
85  if ( mFieldFormatComboBox->itemData( mFieldFormatComboBox->currentIndex() ) == QGSDATETIMEEDIT_ISODATETIMEFORMAT )
86  {
87  mDisplayFormatEdit->setText( QGSDATETIMEEDIT_ISODISPLAYFORMAT );
88  }
89  else
90  {
91  mDisplayFormatEdit->setText( fieldFormat );
92  }
93  }
94 }
95 
96 
97 void QgsDateTimeEditConfig::displayFormatChanged( int idx )
98 {
99  const bool custom = idx == 1;
100  mDisplayFormatEdit->setEnabled( custom );
101  mDisplayHelpToolButton->setVisible( custom );
102  if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
103  {
104  mHelpScrollArea->setVisible( false );
105  }
106  if ( !custom )
107  {
108  mDisplayFormatEdit->setText( mFieldFormatEdit->text() );
109  }
110 }
111 
112 void QgsDateTimeEditConfig::showHelp( bool buttonChecked )
113 {
114  mFieldHelpToolButton->setChecked( buttonChecked );
115  mDisplayHelpToolButton->setChecked( buttonChecked );
116  mHelpScrollArea->setVisible( buttonChecked );
117 }
118 
119 
121 {
122  QgsEditorWidgetConfig myConfig;
123 
124  myConfig.insert( "field_iso_format", mFieldFormatEdit->text() == QGSDATETIMEEDIT_ISODATETIMEFORMAT );
125  myConfig.insert( "field_format", mFieldFormatEdit->text() );
126  myConfig.insert( "display_format", mDisplayFormatEdit->text() );
127  myConfig.insert( "calendar_popup", mCalendarPopupCheckBox->isChecked() );
128  myConfig.insert( "allow_null", mAllowNullCheckBox->isChecked() );
129 
130  return myConfig;
131 }
132 
133 
135 {
136  if ( config.contains( "field_format" ) )
137  {
138  const QString fieldFormat = config[ "field_format" ].toString();
139  mFieldFormatEdit->setText( fieldFormat );
140 
141  const int idx = mFieldFormatComboBox->findData( fieldFormat );
142  if ( idx >= 0 )
143  {
144  mFieldFormatComboBox->setCurrentIndex( idx );
145  }
146  else
147  {
148  mFieldFormatComboBox->setCurrentIndex( 4 );
149  }
150  }
151 
152  if ( config.contains( "display_format" ) )
153  {
154  const QString displayFormat = config[ "display_format" ].toString();
155  mDisplayFormatEdit->setText( displayFormat );
156  if ( displayFormat == mFieldFormatEdit->text() )
157  {
158  mDisplayFormatComboBox->setCurrentIndex( 0 );
159  }
160  else
161  {
162  mDisplayFormatComboBox->setCurrentIndex( 1 );
163  }
164  }
165 
166  if ( config.contains( "calendar_popup" ) )
167  {
168  mCalendarPopupCheckBox->setChecked( config[ "calendar_popup" ].toBool() );
169  }
170 
171  if ( config.contains( "allow_null" ) )
172  {
173  mAllowNullCheckBox->setChecked( config[ "allow_null" ].toBool() );
174  }
175 
176 }
void setupUi(QWidget *widget)
This class should be subclassed for every configurable editor widget type.
#define QGSDATETIMEEDIT_DATEFORMAT
#define QGSDATETIMEEDIT_ISODATETIMEFORMAT
QgsDateTimeEditConfig(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
void setConfig(const QgsEditorWidgetConfig &config) override
Update the configuration widget to represent the given configuration.
QString tr(const char *sourceText, const char *disambiguation, int n)
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
#define QGSDATETIMEEDIT_TIMEFORMAT
bool isEmpty() const
void changed()
Emitted when the configuration of the widget is changed.
QDateTime currentDateTime()
QgsEditorWidgetConfig config() override
Create a configuration from the current GUI state.
#define QGSDATETIMEEDIT_ISODISPLAYFORMAT
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.
#define QGSDATETIMEEDIT_DATETIMEFORMAT