QGIS API Documentation 3.29.0-Master (8c80f25a4f)
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
19#include "qgsvectorlayer.h"
20#include "qgsfieldcombobox.h"
21
22#include <QStandardItemModel>
23#include <QPushButton>
24
25QgsWmsDimensionDialog::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 const QString nameToolTip = tr( "OAPIF supports \"Name\" and \"Date\" only. For \"WMS\", \"Time\" and \"Elevation\" are predefined values according to OGC specifications but it can be user-defined." );
37 mNameLabel->setToolTip( nameToolTip );
38 mNameComboBox->setToolTip( nameToolTip );
39
40 // Set field combobox
41 mFieldComboBox->setLayer( mLayer );
42 mEndFieldComboBox->setLayer( mLayer );
43 mEndFieldComboBox->setAllowEmptyFieldName( true );
44
45 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsWmsDimensionDialog::accept );
46 connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsWmsDimensionDialog::reject );
47 connect( mFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsWmsDimensionDialog::fieldChanged );
48 connect( mEndFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsWmsDimensionDialog::fieldChanged );
49 connect( mNameComboBox, &QComboBox::editTextChanged, this, &QgsWmsDimensionDialog::nameChanged );
50 connect( mDefaultDisplayComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsWmsDimensionDialog::defaultDisplayChanged );
51
52 // Set available names
53 const QMetaEnum pnMetaEnum( QMetaEnum::fromType<QgsMapLayerServerProperties::PredefinedWmsDimensionName>() );
54 for ( int i = 0; i < pnMetaEnum.keyCount(); i++ )
55 {
56 const QString name( pnMetaEnum.key( i ) );
57 if ( !alreadyDefinedDimensions.contains( name.toLower() ) )
58 {
59 mNameComboBox->addItem( QStringLiteral( "%1%2" ).arg( !name.isEmpty() ? name.at( 0 ) : QString(), name.mid( 1 ).toLower() ), QVariant( pnMetaEnum.value( i ) ) );
60 }
61 }
62
63 // Set default display combobox
64 mDefaultDisplayComboBox->clear();
65 QMap<int, QString> defaultDisplayLabels = QgsMapLayerServerProperties::wmsDimensionDefaultDisplayLabels();
66 for ( auto it = defaultDisplayLabels.constBegin(); it != defaultDisplayLabels.constEnd(); it++ )
67 {
68 mDefaultDisplayComboBox->addItem( it.value(), QVariant( it.key() ) );
69 }
70 // Set default display to All values
71 mDefaultDisplayComboBox->setCurrentIndex( mDefaultDisplayComboBox->findData( QVariant( QgsMapLayerServerProperties::WmsDimensionInfo::AllValues ) ) );
72
73 mReferenceValueLabel->setEnabled( false );
74 mReferenceValueComboBox->setEnabled( false );
75
76 fieldChanged();
77}
78
80{
81 const QMetaEnum pnMetaEnum( QMetaEnum::fromType<QgsMapLayerServerProperties::PredefinedWmsDimensionName>() );
82 const int predefinedNameValue = pnMetaEnum.keyToValue( info.name.toUpper().toStdString().c_str() );
83 if ( predefinedNameValue == -1 )
84 {
85 mNameComboBox->setEditText( info.name );
86 }
87 else
88 {
89 mNameComboBox->setCurrentIndex( mNameComboBox->findData( QVariant( predefinedNameValue ) ) );
90 }
91 mNameComboBox->setEnabled( false );
92
93 mFieldComboBox->setField( info.fieldName );
94 mEndFieldComboBox->setField( info.endFieldName );
95
96 mUnitsLineEdit->setText( info.units );
97 mUnitSymbolLineEdit->setText( info.unitSymbol );
98
99 mDefaultDisplayComboBox->setCurrentIndex( mDefaultDisplayComboBox->findData( QVariant( info.defaultDisplayType ) ) );
101 {
102 const int referenceValueIndex = mReferenceValueComboBox->findData( info.referenceValue );
103 if ( referenceValueIndex == -1 )
104 {
105 mReferenceValueComboBox->setEditText( info.referenceValue.toString() );
106 }
107 else
108 {
109 mReferenceValueComboBox->setCurrentIndex( referenceValueIndex );
110 }
111 }
112 else
113 {
114 mReferenceValueComboBox->setCurrentIndex( 0 );
115 }
116}
117
119{
120 // Is the name a predefined value?
121 QString name = mNameComboBox->currentText();
122 if ( mNameComboBox->findText( name ) != -1 )
123 {
124 name = name.toLower();
125 }
126
127 // Gets the reference value
128 const QString refText = mReferenceValueComboBox->currentText();
129 QVariant refValue;
130 if ( mReferenceValueComboBox->findText( refText ) != -1 )
131 {
132 refValue = mReferenceValueComboBox->currentData();
133 }
134 return QgsMapLayerServerProperties::WmsDimensionInfo( name, mFieldComboBox->currentField(),
135 mEndFieldComboBox->currentField(),
136 mUnitsLineEdit->text(), mUnitSymbolLineEdit->text(),
137 mDefaultDisplayComboBox->currentData().toInt(), refValue );
138}
139
140void QgsWmsDimensionDialog::nameChanged( const QString &name )
141{
142 // reset input
143 mUnitsLabel->setEnabled( true );
144 mUnitsLineEdit->setEnabled( true );
145 mUnitsLineEdit->clear();
146 mUnitSymbolLabel->setEnabled( true );
147 mUnitSymbolLineEdit->setEnabled( true );
148 mUnitSymbolLineEdit->clear();
149 mFieldLabel->setToolTip( QString() );
150 mEndFieldLabel->setToolTip( QString() );
151 mFieldComboBox->setToolTip( QString() );
152 mEndFieldComboBox->setToolTip( QString() );
153
154 // Is the name a predefined value?
155 if ( mNameComboBox->findText( name ) != -1 )
156 {
157 const int data = mNameComboBox->currentData().toInt();
159 {
160 const QgsFieldProxyModel::Filters filters = QgsFieldProxyModel::String |
165 mFieldComboBox->setFilters( filters );
166 mEndFieldComboBox->setFilters( filters );
167 mUnitsLineEdit->setText( QStringLiteral( "ISO8601" ) );
168 mUnitsLabel->setEnabled( false );
169 mUnitsLineEdit->setEnabled( false );
170 mUnitSymbolLabel->setEnabled( false );
171 mUnitSymbolLineEdit->setEnabled( false );
172
173 const QString fieldToolTip = tr( "If a string is used, it must be formatted according to <a href=\"https://www.iso.org/iso-8601-date-and-time-format.html\">ISO8601</a>." );
174 mFieldLabel->setToolTip( fieldToolTip );
175 mEndFieldLabel->setToolTip( fieldToolTip );
176 mFieldComboBox->setToolTip( fieldToolTip );
177 mEndFieldComboBox->setToolTip( fieldToolTip );
178 }
180 {
181 mFieldComboBox->setFilters( QgsFieldProxyModel::String | QgsFieldProxyModel::Date );
182 mEndFieldComboBox->setFilters( QgsFieldProxyModel::String | QgsFieldProxyModel::Date );
183 mUnitsLineEdit->setText( QStringLiteral( "ISO8601" ) );
184 mUnitsLabel->setEnabled( false );
185 mUnitsLineEdit->setEnabled( false );
186 mUnitSymbolLabel->setEnabled( false );
187 mUnitSymbolLineEdit->setEnabled( false );
188 }
190 {
191 mFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
192 mEndFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
193 }
194 }
195 else
196 {
197 mFieldComboBox->setFilters( QgsFieldProxyModel::AllTypes );
198 mEndFieldComboBox->setFilters( QgsFieldProxyModel::AllTypes );
199 }
200
201 fieldChanged();
202}
203
204void QgsWmsDimensionDialog::fieldChanged()
205{
206 const QString currentFieldName = mFieldComboBox->currentField();
207 const int currentFieldIndexOf = mLayer->fields().indexOf( currentFieldName );
208 QSet<QVariant> uniqueValues = mLayer->uniqueValues( currentFieldIndexOf );
209
210 const QString currentEndFieldName = mEndFieldComboBox->currentField();
211 if ( !currentEndFieldName.isEmpty() )
212 {
213 const int currentEndFieldIndexOf = mLayer->fields().indexOf( currentEndFieldName );
214 uniqueValues.unite( mLayer->uniqueValues( currentEndFieldIndexOf ) );
215 }
216 QList<QVariant> values = qgis::setToList( uniqueValues );
217 std::sort( values.begin(), values.end() );
218
219 mReferenceValueComboBox->clear();
220 mReferenceValueComboBox->addItem( QString(), QVariant() ); // Empty value
221 const auto constValues = values;
222 for ( const QVariant &v : constValues )
223 {
224 mReferenceValueComboBox->addItem( v.toString(), v );
225 }
226}
227
228void QgsWmsDimensionDialog::defaultDisplayChanged( int index )
229{
230 if ( index == 3 )
231 {
232 mReferenceValueLabel->setEnabled( true );
233 mReferenceValueComboBox->setEnabled( true );
234 }
235 else
236 {
237 mReferenceValueLabel->setEnabled( false );
238 mReferenceValueComboBox->setEnabled( false );
239 }
240}
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.