Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
GUI for XYZ tile layers: browser items + actions to add/remove them
  • Loading branch information
wonder-sk committed Aug 31, 2016
1 parent c4181fa commit 4a1b4fd
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/providers/wms/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ SET (WMS_SRCS
qgswmsdataitems.cpp
qgstilescalewidget.cpp
qgswmtsdimensions.cpp
qgsxyzconnection.cpp
)
SET (WMS_MOC_HDRS
qgswmscapabilities.h
Expand Down
86 changes: 86 additions & 0 deletions src/providers/wms/qgswmsdataitems.cpp
Expand Up @@ -16,12 +16,16 @@

#include "qgslogger.h"

#include "qgsdataitemproviderregistry.h"
#include "qgsdatasourceuri.h"
#include "qgswmscapabilities.h"
#include "qgswmsconnection.h"
#include "qgswmssourceselect.h"
#include "qgsnewhttpconnection.h"
#include "qgstilescalewidget.h"
#include "qgsxyzconnection.h"

#include <QInputDialog>

// ---------------------------------------------------------------------------
QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem* parent, QString name, QString path, QString uri )
Expand Down Expand Up @@ -416,6 +420,10 @@ void QgsWMSRootItem::newConnection()
QGISEXTERN void registerGui( QMainWindow *mainWindow )
{
QgsTileScaleWidget::showTileScale( mainWindow );

// with dataItem(...) at provider level we can only have one root item,
// so we have a data item provider for XYZ tile layers
QgsDataItemProviderRegistry::instance()->addProvider( new QgsXyzTileDataItemProvider );
}

QGISEXTERN QgsWMSSourceSelect * selectWidget( QWidget * parent, Qt::WindowFlags fl )
Expand Down Expand Up @@ -450,3 +458,81 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return nullptr;
}


// ---------------------------------------------------------------------------


QgsXyzTileRootItem::QgsXyzTileRootItem( QgsDataItem *parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mCapabilities |= Fast;
mIconName = "mIconWms.svg";
populate();
}

QVector<QgsDataItem *> QgsXyzTileRootItem::createChildren()
{
QVector<QgsDataItem*> connections;
Q_FOREACH ( const QString& connName, QgsXyzConnectionUtils::connectionList() )
{
QgsXyzConnection connection( QgsXyzConnectionUtils::connection( connName ) );
QgsDataItem * conn = new QgsXyzLayerItem( this, connName, mPath + '/' + connName, connection.encodedUri() );
connections.append( conn );
}
return connections;
}

QList<QAction *> QgsXyzTileRootItem::actions()
{
QAction* actionNew = new QAction( tr( "New Connection..." ), this );
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
return QList<QAction*>() << actionNew;
}

void QgsXyzTileRootItem::newConnection()
{
QString url = QInputDialog::getText( nullptr, tr( "New XYZ tile layer" ),
tr( "Please enter XYZ tile layer URL. {x}, {y}, {z} will be replaced by actual tile coordinates." ) );
if ( url.isEmpty() )
return;

QString name = QInputDialog::getText( nullptr, tr( "New XYZ tile layer" ),
tr( "Please enter name of the tile layer:" ) );
if ( name.isEmpty() )
return;

QgsXyzConnection conn;
conn.name = name;
conn.url = url;
QgsXyzConnectionUtils::addConnection( conn );

refresh();
}


// ---------------------------------------------------------------------------


QgsXyzLayerItem::QgsXyzLayerItem( QgsDataItem *parent, QString name, QString path, const QString &encodedUri )
: QgsLayerItem( parent, name, path, encodedUri, QgsLayerItem::Raster, "wms" )
{
setState( Populated );
}

QList<QAction *> QgsXyzLayerItem::actions()
{
QList<QAction*> lst = QgsLayerItem::actions();

QAction* actionDelete = new QAction( tr( "Delete" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst << actionDelete;

return lst;
}

void QgsXyzLayerItem::deleteConnection()
{
QgsXyzConnectionUtils::deleteConnection( mName );

mParent->refresh();
}
47 changes: 47 additions & 0 deletions src/providers/wms/qgswmsdataitems.h
Expand Up @@ -16,6 +16,7 @@
#define QGSWMSDATAITEMS_H

#include "qgsdataitem.h"
#include "qgsdataitemprovider.h"
#include "qgsdatasourceuri.h"
#include "qgswmsprovider.h"

Expand Down Expand Up @@ -105,4 +106,50 @@ class QgsWMSRootItem : public QgsDataCollectionItem
void newConnection();
};

//! Root item for XYZ tile layers
class QgsXyzTileRootItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsXyzTileRootItem( QgsDataItem* parent, QString name, QString path );

QVector<QgsDataItem*> createChildren() override;

virtual QList<QAction*> actions() override;

private slots:
void newConnection();
};

//! Item implementation for XYZ tile layers
class QgsXyzLayerItem : public QgsLayerItem
{
Q_OBJECT
public:
QgsXyzLayerItem( QgsDataItem* parent, QString name, QString path, const QString& encodedUri );

virtual QList<QAction*> actions() override;

public slots:
void deleteConnection();
};


//! Provider for XYZ root data item
class QgsXyzTileDataItemProvider : public QgsDataItemProvider
{
public:
virtual QString name() override { return "XYZ Tiles"; }

virtual int capabilities() override { return QgsDataProvider::Net; }

virtual QgsDataItem* createDataItem( const QString& path, QgsDataItem* parentItem ) override
{
if ( path.isEmpty() )
return new QgsXyzTileRootItem( parentItem, "Tile Server (XYZ)", "xyz:" );
return nullptr;
}
};


#endif // QGSWMSDATAITEMS_H
59 changes: 59 additions & 0 deletions src/providers/wms/qgsxyzconnection.cpp
@@ -0,0 +1,59 @@
/***************************************************************************
qgsxyzconnection.h
---------------------
begin : August 2016
copyright : (C) 2016 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsxyzconnection.h"

#include "qgsdatasourceuri.h"

#include <QSettings>

QString QgsXyzConnection::encodedUri() const
{
QgsDataSourceUri uri;
uri.setParam( "type", "xyz" );
uri.setParam( "url", url );
return uri.encodedUri();
}

QStringList QgsXyzConnectionUtils::connectionList()
{
QSettings settings;
settings.beginGroup( "/Qgis/connections-xyz" );
return settings.childGroups();
}

QgsXyzConnection QgsXyzConnectionUtils::connection( const QString &name )
{
QSettings settings;
settings.beginGroup( "/Qgis/connections-xyz/" + name );

QgsXyzConnection conn;
conn.name = name;
conn.url = settings.value( "url" ).toString();
return conn;
}

void QgsXyzConnectionUtils::deleteConnection( const QString& name )
{
QSettings settings;
settings.remove( "/Qgis/connections-xyz/" + name );
}

void QgsXyzConnectionUtils::addConnection( const QgsXyzConnection &conn )
{
QSettings settings;
settings.beginGroup( "/Qgis/connections-xyz/" + conn.name );
settings.setValue( "url", conn.url );
}
47 changes: 47 additions & 0 deletions src/providers/wms/qgsxyzconnection.h
@@ -0,0 +1,47 @@
/***************************************************************************
qgsxyzconnection.h
---------------------
begin : August 2016
copyright : (C) 2016 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSXYZCONNECTION_H
#define QGSXYZCONNECTION_H

#include <QStringList>

struct QgsXyzConnection
{
QString name;
QString url;

QString encodedUri() const;
};

/** Utility class for handling list of connections to XYZ tile layers */
class QgsXyzConnectionUtils
{
public:
//! Returns list of existing connections
static QStringList connectionList();

//! Returns connection details
static QgsXyzConnection connection( const QString& name );

//! Removes a connection from the list
static void deleteConnection( const QString& name );

//! Adds a new connection to the list
static void addConnection( const QgsXyzConnection& conn );
};


#endif // QGSXYZCONNECTION_H

0 comments on commit 4a1b4fd

Please sign in to comment.