Add QgsAbstractProviderConnection subclass + provider metadata code

This commit is contained in:
Martin Dobias 2020-04-03 20:11:58 +02:00
parent 205b8aec30
commit ba4ceb4aed
8 changed files with 145 additions and 48 deletions

View File

@ -21,19 +21,44 @@
///@cond PRIVATE ///@cond PRIVATE
QString QgsVectorTileConnection::encodedUri() const QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
{ {
QgsDataSourceUri uri; QgsDataSourceUri uri;
uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) ); uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
uri.setParam( QStringLiteral( "url" ), url ); uri.setParam( QStringLiteral( "url" ), conn.url );
if ( zMin != -1 ) if ( conn.zMin != -1 )
uri.setParam( QStringLiteral( "zmin" ), QString::number( zMin ) ); uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
if ( zMax != -1 ) if ( conn.zMax != -1 )
uri.setParam( QStringLiteral( "zmax" ), QString::number( zMax ) ); uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
return uri.encodedUri(); return uri.encodedUri();
} }
QStringList QgsVectorTileConnectionUtils::connectionList() QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
{
QgsDataSourceUri dsUri;
dsUri.setEncodedUri( uri );
QgsVectorTileProviderConnection::Data conn;
conn.url = dsUri.param( QStringLiteral( "url" ) );
conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
return conn;
}
QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
{
// compared to encodedUri() this one also adds type=xyz to the URI
QgsDataSourceUri uri;
uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
uri.setParam( QStringLiteral( "url" ), conn.url );
if ( conn.zMin != -1 )
uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
if ( conn.zMax != -1 )
uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
return uri.encodedUri();
}
QStringList QgsVectorTileProviderConnection::connectionList()
{ {
QgsSettings settings; QgsSettings settings;
settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) ); settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) );
@ -42,33 +67,58 @@ QStringList QgsVectorTileConnectionUtils::connectionList()
return connList; return connList;
} }
QgsVectorTileConnection QgsVectorTileConnectionUtils::connection( const QString &name ) QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
{ {
QgsSettings settings; QgsSettings settings;
settings.beginGroup( "qgis/connections-vector-tile/" + name ); settings.beginGroup( "qgis/connections-vector-tile/" + name );
QgsVectorTileConnection conn; if ( settings.value( "url" ).toString().isEmpty() )
conn.name = name; return QgsVectorTileProviderConnection::Data();
QgsVectorTileProviderConnection::Data conn;
conn.url = settings.value( QStringLiteral( "url" ) ).toString(); conn.url = settings.value( QStringLiteral( "url" ) ).toString();
conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt(); conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt(); conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
return conn; return conn;
} }
void QgsVectorTileConnectionUtils::deleteConnection( const QString &name ) void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
{ {
QgsSettings settings; QgsSettings settings;
settings.remove( "qgis/connections-vector-tile/" + name ); settings.remove( "qgis/connections-vector-tile/" + name );
} }
void QgsVectorTileConnectionUtils::addConnection( const QgsVectorTileConnection &conn ) void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
{ {
QgsSettings settings; QgsSettings settings;
settings.beginGroup( "qgis/connections-vector-tile/" + conn.name ); settings.beginGroup( "qgis/connections-vector-tile/" + name );
settings.setValue( QStringLiteral( "url" ), conn.url ); settings.setValue( QStringLiteral( "url" ), conn.url );
settings.setValue( QStringLiteral( "zmin" ), conn.zMin ); settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
settings.setValue( QStringLiteral( "zmax" ), conn.zMax ); settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
} }
//
QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
: QgsAbstractProviderConnection( name )
{
setUri( encodedUri( connection( name ) ) );
}
QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
: QgsAbstractProviderConnection( uri, configuration )
{
}
void QgsVectorTileProviderConnection::store( const QString &name ) const
{
addConnection( name, decodedUri( uri() ) );
}
void QgsVectorTileProviderConnection::remove( const QString &name ) const
{
deleteConnection( name );
}
///@endcond ///@endcond

View File

@ -23,31 +23,42 @@
#include <QStringList> #include <QStringList>
struct QgsVectorTileConnection #include "qgsabstractproviderconnection.h"
{
QString name;
QString url;
int zMin = -1;
int zMax = -1;
QString encodedUri() const; class CORE_EXPORT QgsVectorTileProviderConnection : public QgsAbstractProviderConnection
};
//! Utility class for handling list of connections to vector tile layers
class CORE_EXPORT QgsVectorTileConnectionUtils
{ {
public: public:
QgsVectorTileProviderConnection( const QString &name );
QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration );
virtual void store( const QString &name ) const override;
virtual void remove( const QString &name ) const override;
//! Represents decoded data of a connection
struct Data
{
QString url;
int zMin = -1;
int zMax = -1;
};
//! Returns connection data encoded as a string
static QString encodedUri( const Data &conn );
//! Decodes connection string to a data structure
static Data decodedUri( const QString &uri );
//! Returns connection data encoded as a string containg URI for QgsVectorTileLayer
static QString encodedLayerUri( const Data &conn );
//! Returns list of existing connections, unless the hidden ones //! Returns list of existing connections, unless the hidden ones
static QStringList connectionList(); static QStringList connectionList();
//! Returns connection details //! Returns connection details
static QgsVectorTileConnection connection( const QString &name ); static Data connection( const QString &name );
//! Removes a connection from the list //! Removes a connection from the list
static void deleteConnection( const QString &name ); static void deleteConnection( const QString &name );
//! Adds a new connection to the list //! Adds a new connection to the list
static void addConnection( const QgsVectorTileConnection &conn ); static void addConnection( const QString &name, Data conn );
}; };
///@endcond ///@endcond

View File

@ -30,11 +30,11 @@ QgsVectorTileRootItem::QgsVectorTileRootItem( QgsDataItem *parent, QString name,
QVector<QgsDataItem *> QgsVectorTileRootItem::createChildren() QVector<QgsDataItem *> QgsVectorTileRootItem::createChildren()
{ {
QVector<QgsDataItem *> connections; QVector<QgsDataItem *> connections;
const auto connectionList = QgsVectorTileConnectionUtils::connectionList(); const auto connectionList = QgsVectorTileProviderConnection::connectionList();
for ( const QString &connName : connectionList ) for ( const QString &connName : connectionList )
{ {
QgsVectorTileConnection connection( QgsVectorTileConnectionUtils::connection( connName ) ); QString uri = QgsVectorTileProviderConnection::encodedLayerUri( QgsVectorTileProviderConnection::connection( connName ) );
QgsDataItem *conn = new QgsVectorTileLayerItem( this, connName, mPath + '/' + connName, connection.encodedUri() ); QgsDataItem *conn = new QgsVectorTileLayerItem( this, connName, mPath + '/' + connName, uri );
connections.append( conn ); connections.append( conn );
} }
return connections; return connections;

View File

@ -15,6 +15,7 @@
#include "qgsvectortileprovidermetadata.h" #include "qgsvectortileprovidermetadata.h"
#include "qgsvectortileconnection.h"
#include "qgsvectortiledataitems.h" #include "qgsvectortiledataitems.h"
///@cond PRIVATE ///@cond PRIVATE
@ -34,4 +35,24 @@ QList<QgsDataItemProvider *> QgsVectorTileProviderMetadata::dataItemProviders()
return providers; return providers;
} }
QMap<QString, QgsAbstractProviderConnection *> QgsVectorTileProviderMetadata::connections( bool cached )
{
return connectionsProtected<QgsVectorTileProviderConnection, QgsVectorTileProviderConnection>( cached );
}
QgsAbstractProviderConnection *QgsVectorTileProviderMetadata::createConnection( const QString &name )
{
return new QgsVectorTileProviderConnection( name );
}
void QgsVectorTileProviderMetadata::deleteConnection( const QString &name )
{
deleteConnectionProtected<QgsVectorTileProviderConnection>( name );
}
void QgsVectorTileProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
{
saveConnectionProtected( connection, name );
}
///@endcond ///@endcond

View File

@ -33,6 +33,13 @@ class QgsVectorTileProviderMetadata : public QgsProviderMetadata
QgsVectorTileProviderMetadata(); QgsVectorTileProviderMetadata();
QList< QgsDataItemProvider * > dataItemProviders() const override; QList< QgsDataItemProvider * > dataItemProviders() const override;
// handling of stored connections
QMap<QString, QgsAbstractProviderConnection *> connections( bool cached ) override;
QgsAbstractProviderConnection *createConnection( const QString &name ) override;
void deleteConnection( const QString &name ) override;
void saveConnection( const QgsAbstractProviderConnection *connection, const QString &name ) override;
}; };
///@endcond ///@endcond

View File

@ -31,9 +31,11 @@ QgsVectorTileConnectionDialog::QgsVectorTileConnectionDialog( QWidget *parent )
connect( mCheckBoxZMax, &QCheckBox::toggled, mSpinZMax, &QSpinBox::setEnabled ); connect( mCheckBoxZMax, &QCheckBox::toggled, mSpinZMax, &QSpinBox::setEnabled );
} }
void QgsVectorTileConnectionDialog::setConnection( const QgsVectorTileConnection &conn ) void QgsVectorTileConnectionDialog::setConnection( const QString &name, const QString &uri )
{ {
mEditName->setText( conn.name ); mEditName->setText( name );
QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( uri );
mEditUrl->setText( conn.url ); mEditUrl->setText( conn.url );
mCheckBoxZMin->setChecked( conn.zMin != -1 ); mCheckBoxZMin->setChecked( conn.zMin != -1 );
mSpinZMin->setValue( conn.zMin != -1 ? conn.zMin : 0 ); mSpinZMin->setValue( conn.zMin != -1 ? conn.zMin : 0 );
@ -41,16 +43,20 @@ void QgsVectorTileConnectionDialog::setConnection( const QgsVectorTileConnection
mSpinZMax->setValue( conn.zMax != -1 ? conn.zMax : 14 ); mSpinZMax->setValue( conn.zMax != -1 ? conn.zMax : 14 );
} }
QgsVectorTileConnection QgsVectorTileConnectionDialog::connection() const QString QgsVectorTileConnectionDialog::connectionUri() const
{ {
QgsVectorTileConnection conn; QgsVectorTileProviderConnection::Data conn;
conn.name = mEditName->text();
conn.url = mEditUrl->text(); conn.url = mEditUrl->text();
if ( mCheckBoxZMin->isChecked() ) if ( mCheckBoxZMin->isChecked() )
conn.zMin = mSpinZMin->value(); conn.zMin = mSpinZMin->value();
if ( mCheckBoxZMax->isChecked() ) if ( mCheckBoxZMax->isChecked() )
conn.zMax = mSpinZMax->value(); conn.zMax = mSpinZMax->value();
return conn; return QgsVectorTileProviderConnection::encodedUri( conn );
}
QString QgsVectorTileConnectionDialog::connectionName() const
{
return mEditName->text();
} }
void QgsVectorTileConnectionDialog::accept() void QgsVectorTileConnectionDialog::accept()

View File

@ -24,18 +24,16 @@
#include "ui_qgsvectortileconnectiondialog.h" #include "ui_qgsvectortileconnectiondialog.h"
struct QgsVectorTileConnection;
class QgsVectorTileConnectionDialog : public QDialog, public Ui::QgsVectorTileConnectionDialog class QgsVectorTileConnectionDialog : public QDialog, public Ui::QgsVectorTileConnectionDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QgsVectorTileConnectionDialog( QWidget *parent = nullptr ); explicit QgsVectorTileConnectionDialog( QWidget *parent = nullptr );
void setConnection( const QgsVectorTileConnection &conn ); void setConnection( const QString &name, const QString &uri );
QgsVectorTileConnection connection() const; QString connectionUri() const;
QString connectionName() const;
void accept() override; void accept() override;

View File

@ -57,12 +57,14 @@ void QgsVectorTileDataItemGuiProvider::populateContextMenu( QgsDataItem *item, Q
void QgsVectorTileDataItemGuiProvider::editConnection( QgsDataItem *item ) void QgsVectorTileDataItemGuiProvider::editConnection( QgsDataItem *item )
{ {
QgsVectorTileConnectionDialog dlg; QgsVectorTileConnectionDialog dlg;
dlg.setConnection( QgsVectorTileConnectionUtils::connection( item->name() ) ); QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( item->name() ) );
dlg.setConnection( item->name(), uri );
if ( !dlg.exec() ) if ( !dlg.exec() )
return; return;
QgsVectorTileConnectionUtils::deleteConnection( item->name() ); QgsVectorTileProviderConnection::deleteConnection( item->name() );
QgsVectorTileConnectionUtils::addConnection( dlg.connection() ); QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
item->parent()->refreshConnections(); item->parent()->refreshConnections();
} }
@ -73,7 +75,7 @@ void QgsVectorTileDataItemGuiProvider::deleteConnection( QgsDataItem *item )
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes ) QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return; return;
QgsVectorTileConnectionUtils::deleteConnection( item->name() ); QgsVectorTileProviderConnection::deleteConnection( item->name() );
item->parent()->refreshConnections(); item->parent()->refreshConnections();
} }
@ -84,7 +86,9 @@ void QgsVectorTileDataItemGuiProvider::newConnection( QgsDataItem *item )
if ( !dlg.exec() ) if ( !dlg.exec() )
return; return;
QgsVectorTileConnectionUtils::addConnection( dlg.connection() ); QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
item->refreshConnections(); item->refreshConnections();
} }