QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
19 QgsDateTimeEditConfig::QgsDateTimeEditConfig( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
20  : QgsEditorConfigWidget( vl, fieldIdx, parent )
21 {
22  setupUi( this );
23 
24  mDemoDateTimeEdit->setDateTime( QDateTime::currentDateTime() );
25 
26  connect( mDisplayFormatEdit, SIGNAL( textChanged( QString ) ), this, SLOT( updateDemoWidget() ) );
27  connect( mCalendarPopupCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( updateDemoWidget() ) );
28 
29  connect( mFieldFormatComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updateFieldFormat( int ) ) );
30  connect( mFieldFormatEdit, SIGNAL( textChanged( QString ) ), this, SLOT( updateDisplayFormat( QString ) ) );
31  connect( mDisplayFormatComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( displayFormatChanged( int ) ) );
32 
33  connect( mFieldHelpToolButton, SIGNAL( clicked( bool ) ), this, SLOT( showHelp( bool ) ) );
34  connect( mDisplayHelpToolButton, SIGNAL( clicked( bool ) ), this, SLOT( showHelp( bool ) ) );
35 
36  // initialize
37  updateFieldFormat( mFieldFormatComboBox->currentIndex() );
38  displayFormatChanged( mDisplayFormatComboBox->currentIndex() );
39 }
40 
41 
42 void QgsDateTimeEditConfig::updateDemoWidget()
43 {
44  mDemoDateTimeEdit->setDisplayFormat( mDisplayFormatEdit->text() );
45  mDemoDateTimeEdit->setCalendarPopup( mCalendarPopupCheckBox->isChecked() );
46 }
47 
48 
49 void QgsDateTimeEditConfig::updateFieldFormat( int idx )
50 {
51  if ( idx == 0 )
52  {
53  mFieldFormatEdit->setText( QGSDATETIMEEDIT_DATEFORMAT );
54  }
55  else if ( idx == 1 )
56  {
57  mFieldFormatEdit->setText( QGSDATETIMEEDIT_TIMEFORMAT );
58  }
59  else if ( idx == 2 )
60  {
61  mFieldFormatEdit->setText( QGSDATETIMEEDIT_DATETIMEFORMAT );
62  }
63 
64  mFieldFormatEdit->setVisible( idx == 3 );
65  mFieldHelpToolButton->setVisible( idx == 3 );
66  if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
67  {
68  mHelpScrollArea->setVisible( false );
69  }
70 }
71 
72 
73 void QgsDateTimeEditConfig::updateDisplayFormat( QString fieldFormat )
74 {
75  if ( mDisplayFormatComboBox->currentIndex() == 0 )
76  {
77  mDisplayFormatEdit->setText( fieldFormat );
78  }
79 }
80 
81 
82 void QgsDateTimeEditConfig::displayFormatChanged( int idx )
83 {
84  mDisplayFormatEdit->setEnabled( idx == 1 );
85  mDisplayHelpToolButton->setVisible( idx == 1 );
86  if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
87  {
88  mHelpScrollArea->setVisible( false );
89  }
90  if ( idx == 0 )
91  {
92  mDisplayFormatEdit->setText( mFieldFormatEdit->text() );
93  }
94 }
95 
96 void QgsDateTimeEditConfig::showHelp( bool buttonChecked )
97 {
98  mFieldHelpToolButton->setChecked( buttonChecked );
99  mDisplayHelpToolButton->setChecked( buttonChecked );
100  mHelpScrollArea->setVisible( buttonChecked );
101 }
102 
103 
105 {
106  QgsEditorWidgetConfig myConfig;
107 
108  myConfig.insert( "field_format", mFieldFormatEdit->text() );
109  myConfig.insert( "display_format", mDisplayFormatEdit->text() );
110  myConfig.insert( "calendar_popup", mCalendarPopupCheckBox->isChecked() );
111  myConfig.insert( "allow_null", mAllowNullCheckBox->isChecked() );
112 
113  return myConfig;
114 }
115 
116 
118 {
119  if ( config.contains( "field_format" ) )
120  {
121  const QString fieldFormat = config[ "field_format" ].toString();
122  mFieldFormatEdit->setText( fieldFormat );
123 
124  if ( fieldFormat == QGSDATETIMEEDIT_DATEFORMAT )
125  mFieldFormatComboBox->setCurrentIndex( 0 );
126  else if ( fieldFormat == QGSDATETIMEEDIT_TIMEFORMAT )
127  mFieldFormatComboBox->setCurrentIndex( 1 );
128  else if ( fieldFormat == QGSDATETIMEEDIT_DATETIMEFORMAT )
129  mFieldFormatComboBox->setCurrentIndex( 2 );
130  else
131  mFieldFormatComboBox->setCurrentIndex( 3 );
132  }
133 
134  if ( config.contains( "display_format" ) )
135  {
136  const QString displayFormat = config[ "display_format" ].toString();
137  mDisplayFormatEdit->setText( displayFormat );
138  if ( displayFormat == mFieldFormatEdit->text() )
139  {
140  mDisplayFormatComboBox->setCurrentIndex( 0 );
141  }
142  else
143  {
144  mDisplayFormatComboBox->setCurrentIndex( 1 );
145  }
146  }
147 
148  if ( config.contains( "calendar_popup" ) )
149  {
150  mCalendarPopupCheckBox->setChecked( config[ "calendar_popup" ].toBool() );
151  }
152 
153  if ( config.contains( "allow_null" ) )
154  {
155  mAllowNullCheckBox->setChecked( config[ "allow_null" ].toBool() );
156  }
157 
158 }