QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmaplayerserverproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerserverproperties.cpp
3  ------------------
4  begin : June 21, 2021
5  copyright : (C) 2021 by Etienne Trimaille
6  email : etrimaille 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 "qgsmaplayer.h"
20 #include "vector/qgsvectorlayer.h"
21 
22 #include <QDomNode>
23 
24 // QgsServerMetadataUrlProperties
25 
27 {
28  return url == other.url &&
29  type == other.type &&
30  format == other.format;
31 }
32 
34 {
35  properties->setMetadataUrls( metadataUrls() );
36 }
37 
39 {
40  mMetadataUrls.clear();
41 }
42 
43 void QgsServerMetadataUrlProperties::readXml( const QDomNode &layer_node )
44 {
45  QDomElement element = layer_node.namedItem( QStringLiteral( "metadataUrls" ) ).toElement();
46  mMetadataUrls.clear();
47  const QDomNodeList el = element.elementsByTagName( QStringLiteral( "metadataUrl" ) );
48  for ( int i = 0; i < el.size(); i++ )
49  {
50  element = el.at( i ).toElement();
52  oneUrl.type = element.attribute( QStringLiteral( "type" ) );
53  oneUrl.format = element.attribute( QStringLiteral( "format" ) );
54  oneUrl.url = element.text();
55  addMetadataUrl( oneUrl );
56  }
57 }
58 
59 void QgsServerMetadataUrlProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
60 {
61  QDomElement urls = document.createElement( QStringLiteral( "metadataUrls" ) );
62  for ( const QgsMapLayerServerProperties::MetadataUrl &url : mMetadataUrls )
63  {
64  QDomElement urlElement = document.createElement( QStringLiteral( "metadataUrl" ) );
65  urlElement.setAttribute( QStringLiteral( "type" ), url.type );
66  urlElement.setAttribute( QStringLiteral( "format" ), url.format );
67  urlElement.appendChild( document.createTextNode( url.url ) );
68  urls.appendChild( urlElement );
69  }
70  layer_node.appendChild( urls );
71 }
72 
73 // QgsServerWmsDimensionProperties
74 
76 {
77  properties->setWmsDimensions( wmsDimensions() );
78 }
79 
81 {
82  mWmsDimensions.clear();
83 }
84 
85 void QgsServerWmsDimensionProperties::setWmsDimensions( const QList<QgsServerWmsDimensionProperties::WmsDimensionInfo> &dimensions )
86 {
87  mWmsDimensions = dimensions;
88 }
89 
91 {
92  QMap<int, QString> labels;
93  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::AllValues] = QObject::tr( "All values" );
94  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::MinValue] = QObject::tr( "Min value" );
95  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::MaxValue] = QObject::tr( "Max value" );
96  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::ReferenceValue] = QObject::tr( "Reference value" );
97  return labels;
98 }
99 
101 {
102  for ( const QgsServerWmsDimensionProperties::WmsDimensionInfo &dim : mWmsDimensions )
103  {
104  if ( dim.name == wmsDimInfo.name )
105  {
106  return false;
107  }
108  }
109  mWmsDimensions.append( wmsDimInfo );
110  return true;
111 }
112 
114 {
115  for ( int i = 0; i < mWmsDimensions.size(); ++i )
116  {
117  if ( mWmsDimensions[ i ].name == wmsDimName )
118  {
119  mWmsDimensions.removeAt( i );
120  return true;
121  }
122  }
123  return false;
124 }
125 
126 const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > QgsServerWmsDimensionProperties::wmsDimensions() const
127 {
128  return mWmsDimensions;
129 }
130 
131 void QgsServerWmsDimensionProperties::readXml( const QDomNode &layer_node )
132 {
133  reset();
134 
135  // Apply only for vector layers
136  if ( layer()->type() != QgsMapLayerType::VectorLayer )
137  return;
138 
139  const QgsFields fields = static_cast<const QgsVectorLayer *>( layer() )->fields();
140  // QGIS Server WMS Dimensions
141  const QDomNode wmsDimsNode = layer_node.namedItem( QStringLiteral( "wmsDimensions" ) );
142  if ( wmsDimsNode.isNull() )
143  {
144  return;
145  }
146  const QDomElement wmsDimsElem = wmsDimsNode.toElement();
147  const QDomNodeList wmsDimsList = wmsDimsElem.elementsByTagName( QStringLiteral( "dimension" ) );
148  for ( int i = 0; i < wmsDimsList.size(); ++i )
149  {
150  const QDomElement dimElem = wmsDimsList.at( i ).toElement();
151  const QString dimName = dimElem.attribute( QStringLiteral( "name" ) );
152  const QString dimFieldName = dimElem.attribute( QStringLiteral( "fieldName" ) );
153  // check field name
154  const int dimFieldNameIndex = fields.indexOf( dimFieldName );
155  if ( dimFieldNameIndex == -1 )
156  {
157  continue;
158  }
159  QVariant dimRefValue;
160  const int dimDefaultDisplayType = dimElem.attribute( QStringLiteral( "defaultDisplayType" ) ).toInt();
162  {
163  const QString dimRefValueStr = dimElem.attribute( QStringLiteral( "referenceValue" ) );
164  if ( !dimRefValueStr.isEmpty() )
165  {
166  const QgsField dimField = fields.at( dimFieldNameIndex );
167  dimRefValue = QVariant( dimRefValueStr );
168  if ( !dimField.convertCompatible( dimRefValue ) )
169  {
170  continue;
171  }
172  }
173  }
174  QgsServerWmsDimensionProperties::WmsDimensionInfo dim( dimName, dimFieldName,
175  dimElem.attribute( QStringLiteral( "endFieldName" ) ),
176  dimElem.attribute( QStringLiteral( "units" ) ),
177  dimElem.attribute( QStringLiteral( "unitSymbol" ) ),
178  dimDefaultDisplayType, dimRefValue );
179  //XXX This add O(n^2) complexity !!!!
180  // addWmsDimension( dim );
181  // Better to trust the XML:
182  mWmsDimensions.append( dim );
183  }
184 }
185 
186 void QgsServerWmsDimensionProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
187 {
188  // save QGIS Server WMS Dimension definitions
189  if ( ! mWmsDimensions.isEmpty() )
190  {
191  QDomElement wmsDimsElem = document.createElement( QStringLiteral( "wmsDimensions" ) );
192  for ( const QgsServerWmsDimensionProperties::WmsDimensionInfo &dim : mWmsDimensions )
193  {
194  QDomElement dimElem = document.createElement( QStringLiteral( "dimension" ) );
195  dimElem.setAttribute( QStringLiteral( "name" ), dim.name );
196  dimElem.setAttribute( QStringLiteral( "fieldName" ), dim.fieldName );
197  dimElem.setAttribute( QStringLiteral( "endFieldName" ), dim.endFieldName );
198  dimElem.setAttribute( QStringLiteral( "units" ), dim.units );
199  dimElem.setAttribute( QStringLiteral( "unitSymbol" ), dim.unitSymbol );
200  dimElem.setAttribute( QStringLiteral( "defaultDisplayType" ), dim.defaultDisplayType );
201  dimElem.setAttribute( QStringLiteral( "referenceValue" ), dim.referenceValue.toString() );
202  wmsDimsElem.appendChild( dimElem );
203  }
204  layer_node.appendChild( wmsDimsElem );
205  }
206 }
207 
208 
209 // QgsMapLayerServerProperties
210 
212  : mLayer( layer )
213 {
214 }
215 
217 {
220 }
221 
223 {
226 }
227 
228 void QgsMapLayerServerProperties::readXml( const QDomNode &layer_node )
229 {
232 }
233 
234 void QgsMapLayerServerProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
235 {
236  QgsServerMetadataUrlProperties::writeXml( layer_node, document );
237  QgsServerWmsDimensionProperties::writeXml( layer_node, document );
238 }
239 
qgsmaplayerserverproperties.h
QgsServerWmsDimensionProperties::WmsDimensionInfo::ReferenceValue
@ ReferenceValue
Remove from current selection.
Definition: qgsmaplayerserverproperties.h:168
QgsServerWmsDimensionProperties::WmsDimensionInfo
Setting to define QGIS Server WMS Dimension.
Definition: qgsmaplayerserverproperties.h:156
QgsMapLayerType::VectorLayer
@ VectorLayer
Vector layer.
QgsServerMetadataUrlProperties
Manages QGIS Server properties for a map layer.
Definition: qgsmaplayerserverproperties.h:38
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsMapLayerServerProperties::writeXml
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
Definition: qgsmaplayerserverproperties.cpp:234
QgsServerMetadataUrlProperties::MetadataUrl::url
QString url
URL of the link.
Definition: qgsmaplayerserverproperties.h:64
QgsServerWmsDimensionProperties::wmsDimensionDefaultDisplayLabels
static QMap< int, QString > wmsDimensionDefaultDisplayLabels()
Returns WMS Dimension default display labels.
Definition: qgsmaplayerserverproperties.cpp:90
QgsServerMetadataUrlProperties::MetadataUrl::type
QString type
Link type.
Definition: qgsmaplayerserverproperties.h:69
QgsServerMetadataUrlProperties::setMetadataUrls
void setMetadataUrls(const QList< QgsServerMetadataUrlProperties::MetadataUrl > &metaUrls)
Sets a the list of metadata URL for the layer.
Definition: qgsmaplayerserverproperties.h:96
QgsServerWmsDimensionProperties::WmsDimensionInfo::MaxValue
@ MaxValue
Modify current selection to include only select features which match.
Definition: qgsmaplayerserverproperties.h:167
QgsMapLayerServerProperties::reset
void reset()
Reset properties to default.
Definition: qgsmaplayerserverproperties.cpp:222
QgsServerMetadataUrlProperties::addMetadataUrl
void addMetadataUrl(const QgsServerMetadataUrlProperties::MetadataUrl &metaUrl)
Add a metadataUrl for the layer.
Definition: qgsmaplayerserverproperties.h:102
QgsServerWmsDimensionProperties::setWmsDimensions
void setWmsDimensions(const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > &dimensions)
Set WMS Dimensions.
Definition: qgsmaplayerserverproperties.cpp:85
QgsServerMetadataUrlProperties::copyTo
void copyTo(QgsServerMetadataUrlProperties *properties) const
Copy properties to another instance.
Definition: qgsmaplayerserverproperties.cpp:33
QgsServerMetadataUrlProperties::MetadataUrl::format
QString format
Format specification of online resource.
Definition: qgsmaplayerserverproperties.h:74
qgsmaplayer.h
QgsMapLayerServerProperties::QgsMapLayerServerProperties
QgsMapLayerServerProperties(QgsMapLayer *layer=nullptr)
Constructor - Creates a Map Layer QGIS Server Properties.
Definition: qgsmaplayerserverproperties.cpp:211
QgsServerMetadataUrlProperties::layer
virtual const QgsMapLayer * layer() const =0
Gets the parent layer.
QgsMapLayerServerProperties
Manages QGIS Server properties for a map layer.
Definition: qgsmaplayerserverproperties.h:272
QgsServerWmsDimensionProperties::writeXml
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
Definition: qgsmaplayerserverproperties.cpp:186
QgsServerMetadataUrlProperties::metadataUrls
QList< QgsServerMetadataUrlProperties::MetadataUrl > metadataUrls() const
Returns a list of metadataUrl resources associated for the layer.
Definition: qgsmaplayerserverproperties.h:89
QgsServerWmsDimensionProperties::reset
void reset()
Reset properties to default.
Definition: qgsmaplayerserverproperties.cpp:80
QgsServerWmsDimensionProperties::addWmsDimension
bool addWmsDimension(const QgsServerWmsDimensionProperties::WmsDimensionInfo &wmsDimInfo)
Adds a QGIS Server WMS Dimension.
Definition: qgsmaplayerserverproperties.cpp:100
QgsServerWmsDimensionProperties::removeWmsDimension
bool removeWmsDimension(const QString &wmsDimName)
Removes a QGIS Server WMS Dimension.
Definition: qgsmaplayerserverproperties.cpp:113
QgsServerMetadataUrlProperties::MetadataUrl
MetadataUrl structure.
Definition: qgsmaplayerserverproperties.h:49
qgsvectorlayer.h
QgsServerMetadataUrlProperties::MetadataUrl::operator==
bool operator==(const QgsServerMetadataUrlProperties::MetadataUrl &other) const
Compare two MetadataUrl structure.
Definition: qgsmaplayerserverproperties.cpp:26
QgsMapLayerServerProperties::copyTo
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
Definition: qgsmaplayerserverproperties.cpp:216
QgsServerWmsDimensionProperties::readXml
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
Definition: qgsmaplayerserverproperties.cpp:131
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsServerWmsDimensionProperties::copyTo
void copyTo(QgsServerWmsDimensionProperties *properties) const
Copy properties to another instance.
Definition: qgsmaplayerserverproperties.cpp:75
QgsServerWmsDimensionProperties::WmsDimensionInfo::name
QString name
Definition: qgsmaplayerserverproperties.h:189
QgsServerWmsDimensionProperties::WmsDimensionInfo::AllValues
@ AllValues
Display all values of the dimension.
Definition: qgsmaplayerserverproperties.h:165
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
QgsServerMetadataUrlProperties::reset
void reset()
Reset properties to default.
Definition: qgsmaplayerserverproperties.cpp:38
QgsServerMetadataUrlProperties::readXml
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
Definition: qgsmaplayerserverproperties.cpp:43
QgsMapLayerServerProperties::readXml
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
Definition: qgsmaplayerserverproperties.cpp:228
QgsServerWmsDimensionProperties::wmsDimensions
const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > wmsDimensions() const
Returns the QGIS Server WMS Dimension list.
Definition: qgsmaplayerserverproperties.cpp:126
QgsField::convertCompatible
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:402
QgsServerWmsDimensionProperties::WmsDimensionInfo::MinValue
@ MinValue
Add selection to current selection.
Definition: qgsmaplayerserverproperties.h:166
QgsServerMetadataUrlProperties::writeXml
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
Definition: qgsmaplayerserverproperties.cpp:59
QgsFields::indexOf
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:207
QgsServerWmsDimensionProperties
Manages QGIS Server properties for Wms dimensions.
Definition: qgsmaplayerserverproperties.h:135
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50