QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  return uri.encodedUri();
34 }
35 
36 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
37 {
38  QgsDataSourceUri dsUri;
39  dsUri.setEncodedUri( uri );
40 
41  QgsVectorTileProviderConnection::Data conn;
42  conn.url = dsUri.param( QStringLiteral( "url" ) );
43  conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
44  conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
45  return conn;
46 }
47 
48 QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
49 {
50  // compared to encodedUri() this one also adds type=xyz to the URI
51  QgsDataSourceUri uri;
52  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
53  uri.setParam( QStringLiteral( "url" ), conn.url );
54  if ( conn.zMin != -1 )
55  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
56  if ( conn.zMax != -1 )
57  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
58  return uri.encodedUri();
59 }
60 
61 QStringList QgsVectorTileProviderConnection::connectionList()
62 {
63  QgsSettings settings;
64  settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) );
65  QStringList connList = settings.childGroups();
66 
67  return connList;
68 }
69 
70 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
71 {
72  QgsSettings settings;
73  settings.beginGroup( "qgis/connections-vector-tile/" + name );
74 
75  if ( settings.value( "url" ).toString().isEmpty() )
76  return QgsVectorTileProviderConnection::Data();
77 
78  QgsVectorTileProviderConnection::Data conn;
79  conn.url = settings.value( QStringLiteral( "url" ) ).toString();
80  conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
81  conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
82  return conn;
83 }
84 
85 void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
86 {
87  QgsSettings settings;
88  settings.remove( "qgis/connections-vector-tile/" + name );
89 }
90 
91 void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
92 {
93  QgsSettings settings;
94 
95  settings.beginGroup( "qgis/connections-vector-tile/" + name );
96  settings.setValue( QStringLiteral( "url" ), conn.url );
97  settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
98  settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
99 }
100 
101 QString QgsVectorTileProviderConnection::selectedConnection()
102 {
103  QgsSettings settings;
104  return settings.value( QStringLiteral( "qgis/connections-vector-tile/selected" ) ).toString();
105 }
106 
107 void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
108 {
109  QgsSettings settings;
110  return settings.setValue( QStringLiteral( "qgis/connections-vector-tile/selected" ), name );
111 }
112 
113 //
114 
115 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
117 {
118  setUri( encodedUri( connection( name ) ) );
119 }
120 
121 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
122  : QgsAbstractProviderConnection( uri, configuration )
123 {
124 }
125 
126 void QgsVectorTileProviderConnection::store( const QString &name ) const
127 {
128  addConnection( name, decodedUri( uri() ) );
129 }
130 
131 void QgsVectorTileProviderConnection::remove( const QString &name ) const
132 {
133  deleteConnection( name );
134 }
135 
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
Definition: qgsdatasourceuri.h:35
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
Definition: qgssettings.h:61
QgsDataSourceUri::param
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
Definition: qgsdatasourceuri.cpp:823
qgsdatasourceuri.h
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
QgsAbstractProviderConnection
The QgsAbstractProviderConnection provides an interface for data provider connections.
Definition: qgsabstractproviderconnection.h:44
qgslogger.h
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