Skip to content

Commit 4a1b4fd

Browse files
committed
GUI for XYZ tile layers: browser items + actions to add/remove them
1 parent c4181fa commit 4a1b4fd

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed

src/providers/wms/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SET (WMS_SRCS
1111
qgswmsdataitems.cpp
1212
qgstilescalewidget.cpp
1313
qgswmtsdimensions.cpp
14+
qgsxyzconnection.cpp
1415
)
1516
SET (WMS_MOC_HDRS
1617
qgswmscapabilities.h

src/providers/wms/qgswmsdataitems.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
#include "qgslogger.h"
1818

19+
#include "qgsdataitemproviderregistry.h"
1920
#include "qgsdatasourceuri.h"
2021
#include "qgswmscapabilities.h"
2122
#include "qgswmsconnection.h"
2223
#include "qgswmssourceselect.h"
2324
#include "qgsnewhttpconnection.h"
2425
#include "qgstilescalewidget.h"
26+
#include "qgsxyzconnection.h"
27+
28+
#include <QInputDialog>
2529

2630
// ---------------------------------------------------------------------------
2731
QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem* parent, QString name, QString path, QString uri )
@@ -416,6 +420,10 @@ void QgsWMSRootItem::newConnection()
416420
QGISEXTERN void registerGui( QMainWindow *mainWindow )
417421
{
418422
QgsTileScaleWidget::showTileScale( mainWindow );
423+
424+
// with dataItem(...) at provider level we can only have one root item,
425+
// so we have a data item provider for XYZ tile layers
426+
QgsDataItemProviderRegistry::instance()->addProvider( new QgsXyzTileDataItemProvider );
419427
}
420428

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

461+
462+
// ---------------------------------------------------------------------------
463+
464+
465+
QgsXyzTileRootItem::QgsXyzTileRootItem( QgsDataItem *parent, QString name, QString path )
466+
: QgsDataCollectionItem( parent, name, path )
467+
{
468+
mCapabilities |= Fast;
469+
mIconName = "mIconWms.svg";
470+
populate();
471+
}
472+
473+
QVector<QgsDataItem *> QgsXyzTileRootItem::createChildren()
474+
{
475+
QVector<QgsDataItem*> connections;
476+
Q_FOREACH ( const QString& connName, QgsXyzConnectionUtils::connectionList() )
477+
{
478+
QgsXyzConnection connection( QgsXyzConnectionUtils::connection( connName ) );
479+
QgsDataItem * conn = new QgsXyzLayerItem( this, connName, mPath + '/' + connName, connection.encodedUri() );
480+
connections.append( conn );
481+
}
482+
return connections;
483+
}
484+
485+
QList<QAction *> QgsXyzTileRootItem::actions()
486+
{
487+
QAction* actionNew = new QAction( tr( "New Connection..." ), this );
488+
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
489+
return QList<QAction*>() << actionNew;
490+
}
491+
492+
void QgsXyzTileRootItem::newConnection()
493+
{
494+
QString url = QInputDialog::getText( nullptr, tr( "New XYZ tile layer" ),
495+
tr( "Please enter XYZ tile layer URL. {x}, {y}, {z} will be replaced by actual tile coordinates." ) );
496+
if ( url.isEmpty() )
497+
return;
498+
499+
QString name = QInputDialog::getText( nullptr, tr( "New XYZ tile layer" ),
500+
tr( "Please enter name of the tile layer:" ) );
501+
if ( name.isEmpty() )
502+
return;
503+
504+
QgsXyzConnection conn;
505+
conn.name = name;
506+
conn.url = url;
507+
QgsXyzConnectionUtils::addConnection( conn );
508+
509+
refresh();
510+
}
511+
512+
513+
// ---------------------------------------------------------------------------
514+
515+
516+
QgsXyzLayerItem::QgsXyzLayerItem( QgsDataItem *parent, QString name, QString path, const QString &encodedUri )
517+
: QgsLayerItem( parent, name, path, encodedUri, QgsLayerItem::Raster, "wms" )
518+
{
519+
setState( Populated );
520+
}
521+
522+
QList<QAction *> QgsXyzLayerItem::actions()
523+
{
524+
QList<QAction*> lst = QgsLayerItem::actions();
525+
526+
QAction* actionDelete = new QAction( tr( "Delete" ), this );
527+
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
528+
lst << actionDelete;
529+
530+
return lst;
531+
}
532+
533+
void QgsXyzLayerItem::deleteConnection()
534+
{
535+
QgsXyzConnectionUtils::deleteConnection( mName );
536+
537+
mParent->refresh();
538+
}

src/providers/wms/qgswmsdataitems.h

+47
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define QGSWMSDATAITEMS_H
1717

1818
#include "qgsdataitem.h"
19+
#include "qgsdataitemprovider.h"
1920
#include "qgsdatasourceuri.h"
2021
#include "qgswmsprovider.h"
2122

@@ -105,4 +106,50 @@ class QgsWMSRootItem : public QgsDataCollectionItem
105106
void newConnection();
106107
};
107108

109+
//! Root item for XYZ tile layers
110+
class QgsXyzTileRootItem : public QgsDataCollectionItem
111+
{
112+
Q_OBJECT
113+
public:
114+
QgsXyzTileRootItem( QgsDataItem* parent, QString name, QString path );
115+
116+
QVector<QgsDataItem*> createChildren() override;
117+
118+
virtual QList<QAction*> actions() override;
119+
120+
private slots:
121+
void newConnection();
122+
};
123+
124+
//! Item implementation for XYZ tile layers
125+
class QgsXyzLayerItem : public QgsLayerItem
126+
{
127+
Q_OBJECT
128+
public:
129+
QgsXyzLayerItem( QgsDataItem* parent, QString name, QString path, const QString& encodedUri );
130+
131+
virtual QList<QAction*> actions() override;
132+
133+
public slots:
134+
void deleteConnection();
135+
};
136+
137+
138+
//! Provider for XYZ root data item
139+
class QgsXyzTileDataItemProvider : public QgsDataItemProvider
140+
{
141+
public:
142+
virtual QString name() override { return "XYZ Tiles"; }
143+
144+
virtual int capabilities() override { return QgsDataProvider::Net; }
145+
146+
virtual QgsDataItem* createDataItem( const QString& path, QgsDataItem* parentItem ) override
147+
{
148+
if ( path.isEmpty() )
149+
return new QgsXyzTileRootItem( parentItem, "Tile Server (XYZ)", "xyz:" );
150+
return nullptr;
151+
}
152+
};
153+
154+
108155
#endif // QGSWMSDATAITEMS_H
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***************************************************************************
2+
qgsxyzconnection.h
3+
---------------------
4+
begin : August 2016
5+
copyright : (C) 2016 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+
16+
#include "qgsxyzconnection.h"
17+
18+
#include "qgsdatasourceuri.h"
19+
20+
#include <QSettings>
21+
22+
QString QgsXyzConnection::encodedUri() const
23+
{
24+
QgsDataSourceUri uri;
25+
uri.setParam( "type", "xyz" );
26+
uri.setParam( "url", url );
27+
return uri.encodedUri();
28+
}
29+
30+
QStringList QgsXyzConnectionUtils::connectionList()
31+
{
32+
QSettings settings;
33+
settings.beginGroup( "/Qgis/connections-xyz" );
34+
return settings.childGroups();
35+
}
36+
37+
QgsXyzConnection QgsXyzConnectionUtils::connection( const QString &name )
38+
{
39+
QSettings settings;
40+
settings.beginGroup( "/Qgis/connections-xyz/" + name );
41+
42+
QgsXyzConnection conn;
43+
conn.name = name;
44+
conn.url = settings.value( "url" ).toString();
45+
return conn;
46+
}
47+
48+
void QgsXyzConnectionUtils::deleteConnection( const QString& name )
49+
{
50+
QSettings settings;
51+
settings.remove( "/Qgis/connections-xyz/" + name );
52+
}
53+
54+
void QgsXyzConnectionUtils::addConnection( const QgsXyzConnection &conn )
55+
{
56+
QSettings settings;
57+
settings.beginGroup( "/Qgis/connections-xyz/" + conn.name );
58+
settings.setValue( "url", conn.url );
59+
}

src/providers/wms/qgsxyzconnection.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgsxyzconnection.h
3+
---------------------
4+
begin : August 2016
5+
copyright : (C) 2016 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+
16+
#ifndef QGSXYZCONNECTION_H
17+
#define QGSXYZCONNECTION_H
18+
19+
#include <QStringList>
20+
21+
struct QgsXyzConnection
22+
{
23+
QString name;
24+
QString url;
25+
26+
QString encodedUri() const;
27+
};
28+
29+
/** Utility class for handling list of connections to XYZ tile layers */
30+
class QgsXyzConnectionUtils
31+
{
32+
public:
33+
//! Returns list of existing connections
34+
static QStringList connectionList();
35+
36+
//! Returns connection details
37+
static QgsXyzConnection connection( const QString& name );
38+
39+
//! Removes a connection from the list
40+
static void deleteConnection( const QString& name );
41+
42+
//! Adds a new connection to the list
43+
static void addConnection( const QgsXyzConnection& conn );
44+
};
45+
46+
47+
#endif // QGSXYZCONNECTION_H

0 commit comments

Comments
 (0)