Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #5000 from jgrocha/add-default-layers
Browse files Browse the repository at this point in the history
[FEATURE][needs-docs] Add XYZ connection to provide default OpenStreetMap tiles
  • Loading branch information
elpaso committed Sep 8, 2017
2 parents 25c27b3 + 858413e commit f0e021c
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 32 deletions.
5 changes: 5 additions & 0 deletions python/core/qgssettings.sip
Expand Up @@ -155,6 +155,11 @@ Returns a list of all top-level keys that can be read using the QSettings object
%Docstring
Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.
:rtype: list of str
%End
QStringList globalChildGroups() const;
%Docstring
Returns a list of all key top-level groups (same as childGroups) but only for groups defined in global settings.
:rtype: list of str
%End
static QString globalSettingsPath();
%Docstring
Expand Down
2 changes: 2 additions & 0 deletions resources/CMakeLists.txt
@@ -1,4 +1,6 @@
INSTALL(FILES srs.db qgis.db symbology-style.xml spatialite.db customization.xml
DESTINATION ${QGIS_DATA_DIR}/resources)
INSTALL(FILES qgis_global_settings.ini
DESTINATION ${QGIS_DATA_DIR})
INSTALL(DIRECTORY cpt-city-qgis-min DESTINATION ${QGIS_DATA_DIR}/resources)
INSTALL(DIRECTORY themes DESTINATION ${QGIS_DATA_DIR}/resources)
8 changes: 8 additions & 0 deletions resources/qgis_global_settings.ini
@@ -0,0 +1,8 @@
[qgis]
connections-xyz\OpenStreetMap\authcfg=
connections-xyz\OpenStreetMap\password=
connections-xyz\OpenStreetMap\referer=
connections-xyz\OpenStreetMap\url=http://a.tile.openstreetmap.org/{z}/{x}/{y}.png
connections-xyz\OpenStreetMap\username=
connections-xyz\OpenStreetMap\zmax=19
connections-xyz\OpenStreetMap\zmin=0
57 changes: 28 additions & 29 deletions src/app/main.cpp
Expand Up @@ -785,35 +785,6 @@ int main( int argc, char *argv[] )
QCoreApplication::setApplicationName( QgsApplication::QGIS_APPLICATION_NAME );
QCoreApplication::setAttribute( Qt::AA_DontShowIconsInMenus, false );


// SetUp the QgsSettings Global Settings:
// - use the path specified with --globalsettingsfile path,
// - use the environment if not found
// - use a default location as a fallback
if ( globalsettingsfile.isEmpty() )
{
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
}
if ( globalsettingsfile.isEmpty() )
{
QString default_globalsettingsfile = QgsApplication::pkgDataPath() + "/qgis_global_settings.ini";
if ( QFile::exists( default_globalsettingsfile ) )
{
globalsettingsfile = default_globalsettingsfile;
}
}
if ( !globalsettingsfile.isEmpty() )
{
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
{
QgsMessageLog::logMessage( QString( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
else
{
QgsMessageLog::logMessage( QString( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
}

QgsSettings settings;
if ( configLocalStorageLocation.isEmpty() )
{
Expand Down Expand Up @@ -848,6 +819,34 @@ int main( int argc, char *argv[] )

QgsApplication myApp( argc, argv, myUseGuiFlag, profileFolder );

// SetUp the QgsSettings Global Settings:
// - use the path specified with --globalsettingsfile path,
// - use the environment if not found
// - use a default location as a fallback
if ( globalsettingsfile.isEmpty() )
{
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
}
if ( globalsettingsfile.isEmpty() )
{
QString default_globalsettingsfile = QgsApplication::pkgDataPath() + "/qgis_global_settings.ini";
if ( QFile::exists( default_globalsettingsfile ) )
{
globalsettingsfile = default_globalsettingsfile;
}
}
if ( !globalsettingsfile.isEmpty() )
{
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
{
QgsMessageLog::logMessage( QString( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
else
{
QgsMessageLog::logMessage( QString( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
}

#ifdef Q_OS_MAC
// Set hidpi icons; use SVG icons, as PNGs will be relatively too small
QCoreApplication::setAttribute( Qt::AA_UseHighDpiPixmaps );
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgssettings.cpp
Expand Up @@ -20,6 +20,7 @@
#include <QDir>

#include "qgssettings.h"
#include "qgslogger.h"

QString QgsSettings::sGlobalSettingsPath = QString();

Expand Down Expand Up @@ -149,6 +150,15 @@ QStringList QgsSettings::childGroups() const
}
return keys;
}
QStringList QgsSettings::globalChildGroups() const
{
QStringList keys;
if ( mGlobalSettings )
{
keys = mGlobalSettings->childGroups();
}
return keys;
}

QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgssettings.h
Expand Up @@ -150,6 +150,8 @@ class CORE_EXPORT QgsSettings : public QObject
QStringList childKeys() const;
//! Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.
QStringList childGroups() const;
//! Returns a list of all key top-level groups (same as childGroups) but only for groups defined in global settings.
QStringList globalChildGroups() const;
//! Return the path to the Global Settings QSettings storage file
static QString globalSettingsPath() { return sGlobalSettingsPath; }
//! Set the Global Settings QSettings storage file
Expand Down
49 changes: 48 additions & 1 deletion src/providers/wms/qgsxyzconnection.cpp
Expand Up @@ -13,6 +13,7 @@
* *
***************************************************************************/

#include <qgslogger.h>
#include "qgsxyzconnection.h"

#include "qgsdatasourceuri.h"
Expand Down Expand Up @@ -41,8 +42,27 @@ QString QgsXyzConnection::encodedUri() const
QStringList QgsXyzConnectionUtils::connectionList()
{
QgsSettings settings;
QStringList connList;

settings.beginGroup( QStringLiteral( "qgis/connections-xyz" ) );
return settings.childGroups();
connList = settings.childGroups();

const QStringList global = settings.globalChildGroups();
settings.endGroup();

for ( const auto &s : global )
{
settings.beginGroup( "qgis/connections-xyz/" + s );
bool isHidden = settings.value( QStringLiteral( "hidden" ), false ).toBool();
QString url = settings.value( QStringLiteral( "url" ), "" ).toString();
settings.endGroup();
if ( isHidden )
{
connList.removeOne( s );
}
}

return connList;
}

QgsXyzConnection QgsXyzConnectionUtils::connection( const QString &name )
Expand All @@ -59,18 +79,40 @@ QgsXyzConnection QgsXyzConnectionUtils::connection( const QString &name )
conn.username = settings.value( QStringLiteral( "username" ) ).toString();
conn.password = settings.value( QStringLiteral( "password" ) ).toString();
conn.referer = settings.value( QStringLiteral( "referer" ) ).toString();
conn.hidden = settings.value( QStringLiteral( "hidden" ) ).toBool();
return conn;
}

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

settings.beginGroup( QStringLiteral( "qgis/connections-xyz" ) );
QStringList global = settings.globalChildGroups();

if ( global.contains( name ) )
{
QgsSettings settings;
settings.beginGroup( "qgis/connections-xyz/" + name );
settings.setValue( QStringLiteral( "hidden" ), true );
}

}

void QgsXyzConnectionUtils::addConnection( const QgsXyzConnection &conn )
{
QgsSettings settings;
bool addHiddenProperty = false;

settings.beginGroup( QStringLiteral( "qgis/connections-xyz" ) );
QStringList global = settings.globalChildGroups();
if ( global.contains( conn.name ) )
{
addHiddenProperty = true;
}
settings.endGroup();

settings.beginGroup( "qgis/connections-xyz/" + conn.name );
settings.setValue( QStringLiteral( "url" ), conn.url );
settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
Expand All @@ -79,4 +121,9 @@ void QgsXyzConnectionUtils::addConnection( const QgsXyzConnection &conn )
settings.setValue( QStringLiteral( "username" ), conn.username );
settings.setValue( QStringLiteral( "password" ), conn.password );
settings.setValue( QStringLiteral( "referer" ), conn.referer );
if ( addHiddenProperty )
{
settings.setValue( QStringLiteral( "hidden" ), false );
}

}
5 changes: 3 additions & 2 deletions src/providers/wms/qgsxyzconnection.h
Expand Up @@ -32,6 +32,7 @@ struct QgsXyzConnection
QString password;
// Referer
QString referer;
bool hidden;

QString encodedUri() const;
};
Expand All @@ -40,7 +41,7 @@ struct QgsXyzConnection
class QgsXyzConnectionUtils
{
public:
//! Returns list of existing connections
//! Returns list of existing connections, unless the hidden ones
static QStringList connectionList();

//! Returns connection details
Expand All @@ -54,4 +55,4 @@ class QgsXyzConnectionUtils
};


#endif // QGSXYZCONNECTION_H
#endif // QGSXYZCONNECTION_H
27 changes: 27 additions & 0 deletions tests/src/python/test_qgssettings.py
Expand Up @@ -197,6 +197,33 @@ def test_groups(self):
self.assertEqual('qgisrocks-1', self.settings.value('testqgissettings/names/name1'))
self.assertEqual('qgisrocks-4', self.settings.value('testqgissettings/names/name4'))

def test_global_groups(self):
self.assertEqual(self.settings.allKeys(), [])
self.assertEqual(self.globalsettings.allKeys(), [])

self.addToDefaults('testqgissettings/foo/first', 'qgis')
self.addToDefaults('testqgissettings/foo/last', 'rocks')

self.settings.beginGroup('testqgissettings')
self.assertEqual(['foo'], self.settings.childGroups())
self.assertEqual(['foo'], self.settings.globalChildGroups())
self.settings.endGroup()

self.settings.setValue('testqgissettings/bar/first', 'qgis')
self.settings.setValue('testqgissettings/bar/last', 'rocks')

self.settings.beginGroup('testqgissettings')
self.assertEqual(sorted(['bar', 'foo']), sorted(self.settings.childGroups()))
self.assertEqual(['foo'], self.settings.globalChildGroups())
self.settings.endGroup()

self.globalsettings.remove('testqgissettings/foo')

self.settings.beginGroup('testqgissettings')
self.assertEqual(['bar'], self.settings.childGroups())
self.assertEqual([], self.settings.globalChildGroups())
self.settings.endGroup()

def test_array(self):
self.assertEqual(self.settings.allKeys(), [])
self.addArrayToDefaults('testqgissettings', 'key', ['qgisrocks1', 'qgisrocks2', 'qgisrocks3'])
Expand Down

0 comments on commit f0e021c

Please sign in to comment.