QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgswmsdimensiondialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswmsdimensiondialog.cpp
3  ------------------
4  begin : August 20, 2019
5  copyright : (C) 2019 by René-Luc D'Hont
6  email : rldhont at 3liz dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgswmsdimensiondialog.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsfieldcombobox.h"
21 
22 #include <QStandardItemModel>
23 #include <QPushButton>
24 
25 QgsWmsDimensionDialog::QgsWmsDimensionDialog( QgsVectorLayer *layer, QStringList alreadyDefinedDimensions, QWidget *parent, Qt::WindowFlags f )
26  : QDialog( parent, f )
27  , mLayer( layer )
28 {
29  setupUi( this );
30 
31  if ( !mLayer )
32  {
33  return;
34  }
35 
36  // Set field combobox
37  mFieldComboBox->setLayer( mLayer );
38  mEndFieldComboBox->setLayer( mLayer );
39  mEndFieldComboBox->setAllowEmptyFieldName( true );
40 
41  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsWmsDimensionDialog::accept );
42  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsWmsDimensionDialog::reject );
43  connect( mFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsWmsDimensionDialog::fieldChanged );
44  connect( mEndFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsWmsDimensionDialog::fieldChanged );
45  connect( mNameComboBox, &QComboBox::editTextChanged, this, &QgsWmsDimensionDialog::nameChanged );
46  connect( mDefaultDisplayComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsWmsDimensionDialog::defaultDisplayChanged );
47 
48  // Set available names
49  const QMetaEnum pnMetaEnum( QMetaEnum::fromType<QgsMapLayerServerProperties::PredefinedWmsDimensionName>() );
50  for ( int i = 0; i < pnMetaEnum.keyCount(); i++ )
51  {
52  const QString name( pnMetaEnum.key( i ) );
53  if ( !alreadyDefinedDimensions.contains( name.toLower() ) )
54  {
55  mNameComboBox->addItem( QStringLiteral( "%1%2" ).arg( !name.isEmpty() ? name.at( 0 ) : QString(), name.mid( 1 ).toLower() ), QVariant( pnMetaEnum.value( i ) ) );
56  }
57  }
58 
59  // Set default display combobox
60  mDefaultDisplayComboBox->clear();
61  QMap<int, QString> defaultDisplayLabels = QgsMapLayerServerProperties::wmsDimensionDefaultDisplayLabels();
62  for ( const int &k : defaultDisplayLabels.keys() )
63  {
64  mDefaultDisplayComboBox->addItem( defaultDisplayLabels[k], QVariant( k ) );
65  }
66  // Set default display to All values
67  mDefaultDisplayComboBox->setCurrentIndex( mDefaultDisplayComboBox->findData( QVariant( QgsMapLayerServerProperties::WmsDimensionInfo::AllValues ) ) );
68 
69  mReferenceValueLabel->setEnabled( false );
70  mReferenceValueComboBox->setEnabled( false );
71 }
72 
74 {
75  const QMetaEnum pnMetaEnum( QMetaEnum::fromType<QgsMapLayerServerProperties::PredefinedWmsDimensionName>() );
76  const int predefinedNameValue = pnMetaEnum.keyToValue( info.name.toUpper().toStdString().c_str() );
77  if ( predefinedNameValue == -1 )
78  {
79  mNameComboBox->setEditText( info.name );
80  }
81  else
82  {
83  mNameComboBox->setCurrentIndex( mNameComboBox->findData( QVariant( predefinedNameValue ) ) );
84  }
85  mNameComboBox->setEnabled( false );
86 
87  mFieldComboBox->setField( info.fieldName );
88  mEndFieldComboBox->setField( info.endFieldName );
89 
90  mUnitsLineEdit->setText( info.units );
91  mUnitSymbolLineEdit->setText( info.unitSymbol );
92 
93  mDefaultDisplayComboBox->setCurrentIndex( mDefaultDisplayComboBox->findData( QVariant( info.defaultDisplayType ) ) );
95  {
96  const int referenceValueIndex = mReferenceValueComboBox->findData( info.referenceValue );
97  if ( referenceValueIndex == -1 )
98  {
99  mReferenceValueComboBox->setEditText( info.referenceValue.toString() );
100  }
101  else
102  {
103  mReferenceValueComboBox->setCurrentIndex( referenceValueIndex );
104  }
105  }
106  else
107  {
108  mReferenceValueComboBox->setCurrentIndex( 0 );
109  }
110 }
111 
113 {
114  // Is the name a predefined value?
115  QString name = mNameComboBox->currentText();
116  if ( mNameComboBox->findText( name ) != -1 )
117  {
118  name = name.toLower();
119  }
120 
121  // Gets the reference value
122  const QString refText = mReferenceValueComboBox->currentText();
123  QVariant refValue;
124  if ( mReferenceValueComboBox->findText( refText ) != -1 )
125  {
126  refValue = mReferenceValueComboBox->currentData();
127  }
128  return QgsMapLayerServerProperties::WmsDimensionInfo( name, mFieldComboBox->currentField(),
129  mEndFieldComboBox->currentField(),
130  mUnitsLineEdit->text(), mUnitSymbolLineEdit->text(),
131  mDefaultDisplayComboBox->currentData().toInt(), refValue );
132 }
133 
134 void QgsWmsDimensionDialog::nameChanged( const QString &name )
135 {
136  // reset input
137  mUnitsLabel->setEnabled( true );
138  mUnitsLineEdit->setEnabled( true );
139  mUnitsLineEdit->clear();
140  mUnitSymbolLabel->setEnabled( true );
141  mUnitSymbolLineEdit->setEnabled( true );
142  mUnitSymbolLineEdit->clear();
143 
144  // Is the name a predefined value?
145  if ( mNameComboBox->findText( name ) != -1 )
146  {
147  const int data = mNameComboBox->currentData().toInt();
148  if ( data == QgsMapLayerServerProperties::TIME )
149  {
150  const QgsFieldProxyModel::Filters filters = QgsFieldProxyModel::String |
155  mFieldComboBox->setFilters( filters );
156  mEndFieldComboBox->setFilters( filters );
157  mUnitsLineEdit->setText( QStringLiteral( "ISO8601" ) );
158  mUnitsLabel->setEnabled( false );
159  mUnitsLineEdit->setEnabled( false );
160  mUnitSymbolLabel->setEnabled( false );
161  mUnitSymbolLineEdit->setEnabled( false );
162  }
163  if ( data == QgsMapLayerServerProperties::DATE )
164  {
165  mFieldComboBox->setFilters( QgsFieldProxyModel::String | QgsFieldProxyModel::Date );
166  mEndFieldComboBox->setFilters( QgsFieldProxyModel::String | QgsFieldProxyModel::Date );
167  mUnitsLineEdit->setText( QStringLiteral( "ISO8601" ) );
168  mUnitsLabel->setEnabled( false );
169  mUnitsLineEdit->setEnabled( false );
170  mUnitSymbolLabel->setEnabled( false );
171  mUnitSymbolLineEdit->setEnabled( false );
172  }
173  else if ( data == QgsMapLayerServerProperties::ELEVATION )
174  {
175  mFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
176  mEndFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
177  }
178  }
179  else
180  {
181  mFieldComboBox->setFilters( QgsFieldProxyModel::AllTypes );
182  mEndFieldComboBox->setFilters( QgsFieldProxyModel::AllTypes );
183  }
184 
185  fieldChanged();
186 }
187 
188 void QgsWmsDimensionDialog::fieldChanged()
189 {
190  const QString currentFieldName = mFieldComboBox->currentField();
191  const int currentFieldIndexOf = mLayer->fields().indexOf( currentFieldName );
192  QSet<QVariant> uniqueValues = mLayer->uniqueValues( currentFieldIndexOf );
193 
194  const QString currentEndFieldName = mEndFieldComboBox->currentField();
195  if ( !currentEndFieldName.isEmpty() )
196  {
197  const int currentEndFieldIndexOf = mLayer->fields().indexOf( currentEndFieldName );
198  uniqueValues.unite( mLayer->uniqueValues( currentEndFieldIndexOf ) );
199  }
200  QList<QVariant> values = qgis::setToList( uniqueValues );
201  std::sort( values.begin(), values.end() );
202 
203  mReferenceValueComboBox->clear();
204  mReferenceValueComboBox->addItem( QString(), QVariant() ); // Empty value
205  const auto constValues = values;
206  for ( const QVariant &v : constValues )
207  {
208  mReferenceValueComboBox->addItem( v.toString(), v );
209  }
210 }
211 
212 void QgsWmsDimensionDialog::defaultDisplayChanged( int index )
213 {
214  if ( index == 3 )
215  {
216  mReferenceValueLabel->setEnabled( true );
217  mReferenceValueComboBox->setEnabled( true );
218  }
219  else
220  {
221  mReferenceValueLabel->setEnabled( false );
222  mReferenceValueComboBox->setEnabled( false );
223  }
224 }
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
@ DateTime
Datetime fieldss.
@ LongLong
Longlong fields.
@ AllTypes
All field types.
@ Date
Date or datetime fields.
@ Int
Integer fields.
@ String
String fields.
@ Numeric
All numeric fields.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:207
static QMap< int, QString > wmsDimensionDefaultDisplayLabels()
Returns WMS Dimension default display labels.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const FINAL
Calculates a list of unique values contained within an attribute in the layer.
void setInfo(const QgsMapLayerServerProperties::WmsDimensionInfo &info)
QgsWmsDimensionDialog(QgsVectorLayer *layer, QStringList alreadyDefinedDimensions, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
QgsMapLayerServerProperties::WmsDimensionInfo info() const
Setting to define QGIS Server WMS Dimension.