Skip to content

Commit

Permalink
Merge pull request #6592 from slarosa/save_load_xyz
Browse files Browse the repository at this point in the history
[FEATURE][needs-docs] Save/Load connections for XYZ Tiles
  • Loading branch information
slarosa authored Mar 20, 2018
2 parents a132c2e + 281a3ed commit 9a25d67
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 3 deletions.
3 changes: 2 additions & 1 deletion python/gui/qgsmanageconnectionsdialog.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class QgsManageConnectionsDialog : QDialog
DB2,
WCS,
Oracle,
GeoNode
GeoNode,
XyzTiles
};

QgsManageConnectionsDialog( QWidget *parent /TransferThis/ = 0, Mode mode = Export, Type type = WMS, const QString &fileName = QString() );
Expand Down
125 changes: 125 additions & 0 deletions src/gui/qgsmanageconnectionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ void QgsManageConnectionsDialog::doExportImport()
case GeoNode:
doc = saveGeonodeConnections( items );
break;
case XyzTiles:
doc = saveXyzTilesConnections( items );
break;
}

QFile file( mFileName );
Expand Down Expand Up @@ -201,6 +204,9 @@ void QgsManageConnectionsDialog::doExportImport()
case GeoNode:
loadGeonodeConnections( doc, items );
break;
case XyzTiles:
loadXyzTilesConnections( doc, items );
break;
}
// clear connections list and close window
listConnections->clear();
Expand Down Expand Up @@ -242,6 +248,9 @@ bool QgsManageConnectionsDialog::populateConnections()
case GeoNode:
settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
break;
case XyzTiles:
settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
break;
}
QStringList keys = settings.childGroups();
QStringList::Iterator it = keys.begin();
Expand Down Expand Up @@ -353,6 +362,14 @@ bool QgsManageConnectionsDialog::populateConnections()
return false;
}
break;
case XyzTiles:
if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
{
QMessageBox::information( this, tr( "Loading Connections" ),
tr( "The file is not a XYZ Tiles connections exchange file." ) );
return false;
}
break;
}

QDomElement child = root.firstChildElement();
Expand Down Expand Up @@ -622,6 +639,35 @@ QDomDocument QgsManageConnectionsDialog::saveGeonodeConnections( const QStringLi
return doc;
}

QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringList &connections )
{
QDomDocument doc( QStringLiteral( "connections" ) );
QDomElement root = doc.createElement( QStringLiteral( "qgsXYZTilesConnections" ) );
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QgsSettings settings;
QString path;
for ( int i = 0; i < connections.count(); ++i )
{
path = "qgis/connections-xyz/" + connections[ i ];
QDomElement el = doc.createElement( QStringLiteral( "xyztiles" ) );

el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url", "" ).toString() );
el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg", "" ).toString() );
el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer", "" ).toString() );

root.appendChild( el );
}

return doc;
}

void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
{
QDomElement root = doc.documentElement();
Expand Down Expand Up @@ -1226,6 +1272,85 @@ void QgsManageConnectionsDialog::loadGeonodeConnections( const QDomDocument &doc
}
}

void QgsManageConnectionsDialog::loadXyzTilesConnections( const QDomDocument &doc, const QStringList &items )
{
QDomElement root = doc.documentElement();
if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
{
QMessageBox::information( this, tr( "Loading Connections" ),
tr( "The file is not a XYZ Tiles connections exchange file." ) );
return;
}

QString connectionName;
QgsSettings settings;
settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
QStringList keys = settings.childGroups();
settings.endGroup();
QDomElement child = root.firstChildElement();
bool prompt = true;
bool overwrite = true;

while ( !child.isNull() )
{
connectionName = child.attribute( QStringLiteral( "name" ) );
if ( !items.contains( connectionName ) )
{
child = child.nextSiblingElement();
continue;
}

// check for duplicates
if ( keys.contains( connectionName ) && prompt )
{
int res = QMessageBox::warning( this,
tr( "Loading Connections" ),
tr( "Connection with name '%1' already exists. Overwrite?" )
.arg( connectionName ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );

switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
child = child.nextSiblingElement();
continue;
case QMessageBox::Yes:
overwrite = true;
break;
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll:
prompt = false;
overwrite = false;
break;
}
}

if ( keys.contains( connectionName ) && !overwrite )
{
child = child.nextSiblingElement();
continue;
}

settings.beginGroup( "qgis/connections-xyz/" + connectionName );
settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
settings.endGroup();

child = child.nextSiblingElement();
}
}


void QgsManageConnectionsDialog::selectAll()
{
listConnections->selectAll();
Expand Down
5 changes: 4 additions & 1 deletion src/gui/qgsmanageconnectionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class GUI_EXPORT QgsManageConnectionsDialog : public QDialog, private Ui::QgsMan
DB2,
WCS,
Oracle,
GeoNode
GeoNode,
XyzTiles
};

/**
Expand All @@ -72,6 +73,7 @@ class GUI_EXPORT QgsManageConnectionsDialog : public QDialog, private Ui::QgsMan
QDomDocument saveOracleConnections( const QStringList &connections );
QDomDocument saveDb2Connections( const QStringList &connections );
QDomDocument saveGeonodeConnections( const QStringList &connections );
QDomDocument saveXyzTilesConnections( const QStringList &connections );

void loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service );
void loadWfsConnections( const QDomDocument &doc, const QStringList &items );
Expand All @@ -80,6 +82,7 @@ class GUI_EXPORT QgsManageConnectionsDialog : public QDialog, private Ui::QgsMan
void loadOracleConnections( const QDomDocument &doc, const QStringList &items );
void loadDb2Connections( const QDomDocument &doc, const QStringList &items );
void loadGeonodeConnections( const QDomDocument &doc, const QStringList &items );
void loadXyzTilesConnections( const QDomDocument &doc, const QStringList &items );

QString mFileName;
Mode mDialogMode;
Expand Down
35 changes: 34 additions & 1 deletion src/providers/wms/qgswmsdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#include "qgsnewhttpconnection.h"
#include "qgstilescalewidget.h"
#include "qgsxyzconnectiondialog.h"
#include "qgsmanageconnectionsdialog.h"
#endif
#include "qgsgeonodeconnection.h"
#include "qgsgeonoderequest.h"
#include "qgssettings.h"

#include <QInputDialog>
#include <QFileDialog>

// ---------------------------------------------------------------------------
QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem *parent, QString name, QString path, QString uri )
Expand Down Expand Up @@ -474,9 +476,20 @@ QVector<QgsDataItem *> QgsXyzTileRootItem::createChildren()
#ifdef HAVE_GUI
QList<QAction *> QgsXyzTileRootItem::actions( QWidget *parent )
{
QList<QAction *> lst;

QAction *actionNew = new QAction( tr( "New Connection…" ), parent );
connect( actionNew, &QAction::triggered, this, &QgsXyzTileRootItem::newConnection );
return QList<QAction *>() << actionNew;
QAction *saveXyzTilesServers = new QAction( tr( "Save Connections…" ), parent );
connect( saveXyzTilesServers, &QAction::triggered, this, &QgsXyzTileRootItem::saveXyzTilesServers );
QAction *loadXyzTilesServers = new QAction( tr( "Load Connections…" ), parent );
connect( loadXyzTilesServers, &QAction::triggered, this, &QgsXyzTileRootItem::loadXyzTilesServers );

lst.append( actionNew );
lst.append( saveXyzTilesServers );
lst.append( loadXyzTilesServers );

return lst;
}

void QgsXyzTileRootItem::newConnection()
Expand All @@ -488,6 +501,26 @@ void QgsXyzTileRootItem::newConnection()
QgsXyzConnectionUtils::addConnection( dlg.connection() );
refreshConnections();
}

void QgsXyzTileRootItem::saveXyzTilesServers()
{
QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::XyzTiles );
dlg.exec();
}

void QgsXyzTileRootItem::loadXyzTilesServers()
{
QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
tr( "XML files (*.xml *XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::XyzTiles, fileName );
dlg.exec();
refreshConnections();
}
#endif


Expand Down
2 changes: 2 additions & 0 deletions src/providers/wms/qgswmsdataitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class QgsXyzTileRootItem : public QgsDataCollectionItem
private slots:
#ifdef HAVE_GUI
void newConnection();
void saveXyzTilesServers();
void loadXyzTilesServers();
#endif
};

Expand Down

0 comments on commit 9a25d67

Please sign in to comment.