QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsvectortileconnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectortileconnection.cpp
3  ---------------------
4  begin : March 2020
5  copyright : (C) 2020 by Martin Dobias
6  email : wonder dot sk at gmail dot com
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 
17 
18 #include "qgslogger.h"
19 #include "qgsdatasourceuri.h"
20 #include "qgssettings.h"
21 
23 
24 QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
25 {
26  QgsDataSourceUri uri;
27  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
28  uri.setParam( QStringLiteral( "url" ), conn.url );
29  if ( conn.zMin != -1 )
30  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
31  if ( conn.zMax != -1 )
32  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
33  if ( !conn.authCfg.isEmpty() )
34  uri.setAuthConfigId( conn.authCfg );
35  if ( !conn.username.isEmpty() )
36  uri.setUsername( conn.username );
37  if ( !conn.password.isEmpty() )
38  uri.setPassword( conn.password );
39  if ( !conn.referer.isEmpty() )
40  uri.setParam( QStringLiteral( "referer" ), conn.referer );
41  if ( !conn.styleUrl.isEmpty() )
42  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
43 
44  switch ( conn.serviceType )
45  {
46  case Generic:
47  break;
48 
49  case ArcgisVectorTileService:
50  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
51  break;
52  }
53 
54  return uri.encodedUri();
55 }
56 
57 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
58 {
59  QgsDataSourceUri dsUri;
60  dsUri.setEncodedUri( uri );
61 
62  QgsVectorTileProviderConnection::Data conn;
63  conn.url = dsUri.param( QStringLiteral( "url" ) );
64  conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
65  conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
66  conn.authCfg = dsUri.authConfigId();
67  conn.username = dsUri.username();
68  conn.password = dsUri.password();
69  conn.referer = dsUri.param( QStringLiteral( "referer" ) );
70  conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
71 
72  if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
73  {
74  if ( dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
75  conn.serviceType = ArcgisVectorTileService;
76  }
77  return conn;
78 }
79 
80 QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
81 {
82  // compared to encodedUri() this one also adds type=xyz to the URI
83  QgsDataSourceUri uri;
84  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
85  uri.setParam( QStringLiteral( "url" ), conn.url );
86  if ( conn.zMin != -1 )
87  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
88  if ( conn.zMax != -1 )
89  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
90  if ( !conn.authCfg.isEmpty() )
91  uri.setAuthConfigId( conn.authCfg );
92  if ( !conn.username.isEmpty() )
93  uri.setUsername( conn.username );
94  if ( !conn.password.isEmpty() )
95  uri.setPassword( conn.password );
96  if ( !conn.referer.isEmpty() )
97  uri.setParam( QStringLiteral( "referer" ), conn.referer );
98  if ( !conn.styleUrl.isEmpty() )
99  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
100 
101  switch ( conn.serviceType )
102  {
103  case Generic:
104  break;
105 
106  case ArcgisVectorTileService:
107  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
108  break;
109  }
110 
111  return uri.encodedUri();
112 }
113 
114 QStringList QgsVectorTileProviderConnection::connectionList()
115 {
116  QgsSettings settings;
117  settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) );
118  QStringList connList = settings.childGroups();
119 
120  return connList;
121 }
122 
123 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
124 {
125  QgsSettings settings;
126  settings.beginGroup( "qgis/connections-vector-tile/" + name );
127 
128  if ( settings.value( "url" ).toString().isEmpty() )
129  return QgsVectorTileProviderConnection::Data();
130 
131  QgsVectorTileProviderConnection::Data conn;
132  conn.url = settings.value( QStringLiteral( "url" ) ).toString();
133  conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
134  conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
135  conn.authCfg = settings.value( QStringLiteral( "authcfg" ) ).toString();
136  conn.username = settings.value( QStringLiteral( "username" ) ).toString();
137  conn.password = settings.value( QStringLiteral( "password" ) ).toString();
138  conn.referer = settings.value( QStringLiteral( "referer" ) ).toString();
139  conn.styleUrl = settings.value( QStringLiteral( "styleUrl" ) ).toString();
140 
141  if ( settings.contains( QStringLiteral( "serviceType" ) ) )
142  {
143  if ( settings.value( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
144  conn.serviceType = ArcgisVectorTileService;
145  }
146 
147  return conn;
148 }
149 
150 void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
151 {
152  QgsSettings settings;
153  settings.remove( "qgis/connections-vector-tile/" + name );
154 }
155 
156 void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
157 {
158  QgsSettings settings;
159 
160  settings.beginGroup( "qgis/connections-vector-tile/" + name );
161  settings.setValue( QStringLiteral( "url" ), conn.url );
162  settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
163  settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
164  settings.setValue( QStringLiteral( "authcfg" ), conn.authCfg );
165  settings.setValue( QStringLiteral( "username" ), conn.username );
166  settings.setValue( QStringLiteral( "password" ), conn.password );
167  settings.setValue( QStringLiteral( "referer" ), conn.referer );
168  settings.setValue( QStringLiteral( "styleUrl" ), conn.styleUrl );
169 
170  switch ( conn.serviceType )
171  {
172  case Generic:
173  break;
174 
175  case ArcgisVectorTileService:
176  settings.setValue( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
177  break;
178  }
179 }
180 
181 QString QgsVectorTileProviderConnection::selectedConnection()
182 {
183  QgsSettings settings;
184  return settings.value( QStringLiteral( "qgis/connections-vector-tile/selected" ) ).toString();
185 }
186 
187 void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
188 {
189  QgsSettings settings;
190  return settings.setValue( QStringLiteral( "qgis/connections-vector-tile/selected" ), name );
191 }
192 
193 //
194 
195 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
197 {
198  setUri( encodedUri( connection( name ) ) );
199 }
200 
201 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
202  : QgsAbstractProviderConnection( uri, configuration )
203 {
204 }
205 
206 void QgsVectorTileProviderConnection::store( const QString &name ) const
207 {
208  addConnection( name, decodedUri( uri() ) );
209 }
210 
211 void QgsVectorTileProviderConnection::remove( const QString &name ) const
212 {
213  deleteConnection( name );
214 }
215 
QgsSettings::remove
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
Definition: qgssettings.cpp:205
QgsDataSourceUri
Class for storing the component parts of a RDBMS data source URI (e.g.
Definition: qgsdatasourceuri.h:36
QgsDataSourceUri::password
QString password() const
Returns the password stored in the URI.
Definition: qgsdatasourceuri.cpp:284
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsDataSourceUri::param
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
Definition: qgsdatasourceuri.cpp:823
QgsDataSourceUri::setUsername
void setUsername(const QString &username)
Sets the username for the URI.
Definition: qgsdatasourceuri.cpp:264
QgsDataSourceUri::username
QString username() const
Returns the username stored in the URI.
Definition: qgsdatasourceuri.cpp:259
qgsdatasourceuri.h
QgsDataSourceUri::setAuthConfigId
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
Definition: qgsdatasourceuri.cpp:716
QgsDataSourceUri::setParam
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
Definition: qgsdatasourceuri.cpp:778
qgsvectortileconnection.h
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsDataSourceUri::hasParam
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
Definition: qgsdatasourceuri.cpp:849
QgsDataSourceUri::encodedUri
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
Definition: qgsdatasourceuri.cpp:610
QgsSettings::beginGroup
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:87
QgsDataSourceUri::setEncodedUri
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
Definition: qgsdatasourceuri.cpp:630
qgssettings.h
QgsSettings::contains
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
Definition: qgssettings.cpp:188
QgsAbstractProviderConnection
The QgsAbstractProviderConnection provides an interface for data provider connections.
Definition: qgsabstractproviderconnection.h:45
QgsDataSourceUri::authConfigId
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
Definition: qgsdatasourceuri.cpp:254
qgslogger.h
QgsDataSourceUri::setPassword
void setPassword(const QString &password)
Sets the password for the URI.
Definition: qgsdatasourceuri.cpp:289
QgsSettings::childGroups
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
Definition: qgssettings.cpp:144