Skip to content
Permalink
Browse files

New class QgsLocalDefaultSettings

This class contains a variety of default setting values. These values are local, profile
specific settings which may have been configured or tweaked by the user (as opposed to
global, fixed default settings).

The values encapsulated here should be inherited when creating new objects such as new
QGIS projects.

Typically, the QgsSettings backend is used to store and retrieve these local settings.

The intention here is to provide stable, structured and easily discoverable
methods for setting and retrieve these settings, rather than forcing raw QSetting
handling.
  • Loading branch information
nyalldawson committed Jan 8, 2020
1 parent 410da5e commit 1f7140bfc5314d8c316e8b3a36664aed056862f6
@@ -40,11 +40,6 @@ Default constructor
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;


void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );
%Docstring
Sets the format's ``configuration``.
%End

int numberDecimalPlaces() const;
%Docstring
Returns the maximum number of decimal places to show.
@@ -113,6 +108,11 @@ numberDecimalPlaces()).

protected:

virtual void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );
%Docstring
Sets the format's ``configuration``.
%End


};

@@ -65,6 +65,9 @@ described in the returned strings.
.. seealso:: :py:func:`directionFormat`
%End

virtual void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );


};

/************************************************************************
@@ -0,0 +1,58 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslocaldefaultsettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsLocalDefaultSettings
{
%Docstring
Contains local default settings which should be respected when creating new objects
such as QgsProjects.

This class contains a variety of default setting values. These values are local, profile
specific settings which may have been configured or tweaked by the user (as opposed to
global, fixed default settings).

The values encapsulated here should be inherited when creating new objects such as new
QGIS projects.

Typically, the QgsSettings backend is used to store and retrieve these local settings.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgslocaldefaultsettings.h"
%End
public:

static void setBearingFormat( const QgsBearingNumericFormat *format );
%Docstring
Sets the default bearing ``format``, which controls how angular bearings are displayed.

.. seealso:: :py:func:`bearingFormat`
%End

static QgsBearingNumericFormat *bearingFormat() /Factory/;
%Docstring
Returns the default bearing format, which controls how angular bearings are displayed.

This method returns a new object and the caller takes ownership of the returned value.

.. seealso:: :py:func:`setBearingFormat`
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslocaldefaultsettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -91,6 +91,7 @@
%Include auto_generated/qgslegendrenderer.sip
%Include auto_generated/qgslegendsettings.sip
%Include auto_generated/qgslegendstyle.sip
%Include auto_generated/qgslocaldefaultsettings.sip
%Include auto_generated/qgslogger.sip
%Include auto_generated/qgsmapdecoration.sip
%Include auto_generated/qgsmaphittest.sip
@@ -276,6 +276,7 @@ SET(QGIS_CORE_SRCS
qgslegendrenderer.cpp
qgslegendsettings.cpp
qgslegendstyle.cpp
qgslocaldefaultsettings.cpp
qgslocalec.cpp
qgslogger.cpp
qgsmapdecoration.cpp
@@ -773,6 +774,7 @@ SET(QGIS_CORE_HDRS
qgslegendrenderer.h
qgslegendsettings.h
qgslegendstyle.h
qgslocaldefaultsettings.h
qgslocalec.h
qgslogger.h
qgsmapdecoration.h
@@ -45,11 +45,6 @@ class CORE_EXPORT QgsBasicNumericFormat : public QgsNumericFormat
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
QVariantMap configuration( const QgsReadWriteContext &context ) const override;

/**
* Sets the format's \a configuration.
*/
void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );

/**
* Returns the maximum number of decimal places to show.
*
@@ -110,6 +105,11 @@ class CORE_EXPORT QgsBasicNumericFormat : public QgsNumericFormat

protected:

/**
* Sets the format's \a configuration.
*/
virtual void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );

bool mUseScientific = false;

private:
@@ -93,7 +93,6 @@ QgsNumericFormat *QgsBearingNumericFormat::create( const QVariantMap &configurat
{
std::unique_ptr< QgsBearingNumericFormat > res = qgis::make_unique< QgsBearingNumericFormat >();
res->setConfiguration( configuration, context );
res->mDirectionFormat = static_cast< FormatDirectionOption >( configuration.value( QStringLiteral( "direction_format" ), 0 ).toInt() );
return res.release();
}

@@ -113,3 +112,9 @@ void QgsBearingNumericFormat::setDirectionFormat( const FormatDirectionOption &d
{
mDirectionFormat = directionFormat;
}

void QgsBearingNumericFormat::setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context )
{
QgsBasicNumericFormat::setConfiguration( configuration, context );
mDirectionFormat = static_cast< FormatDirectionOption >( configuration.value( QStringLiteral( "direction_format" ), 0 ).toInt() );
}
@@ -70,6 +70,8 @@ class CORE_EXPORT QgsBearingNumericFormat : public QgsBasicNumericFormat
*/
void setDirectionFormat( const FormatDirectionOption &format );

void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context ) override;

private:

FormatDirectionOption mDirectionFormat = UseRange0To180WithEWDirectionalSuffix;
@@ -0,0 +1,55 @@
/***************************************************************************
qgslocaldefaultsettings.cpp
-----------------------------
begin : January 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson 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 "qgslocaldefaultsettings.h"
#include "qgsbearingnumericformat.h"
#include "qgis.h"
#include "qgsreadwritecontext.h"
#include "qgssettings.h"
#include "qgsapplication.h"
#include "qgsnumericformatregistry.h"

#include <memory>

void QgsLocalDefaultSettings::setBearingFormat( const QgsBearingNumericFormat *format )
{
const QVariantMap config = format->configuration( QgsReadWriteContext() );

QSettings s;
s.beginGroup( QStringLiteral( "defaults/bearing_format" ) );
for ( auto it = config.constBegin(); it != config.constEnd(); ++it )
{
s.setValue( it.key(), it.value() );
}
s.endGroup();
}

QgsBearingNumericFormat *QgsLocalDefaultSettings::bearingFormat()
{
QVariantMap config;
QSettings s;
s.beginGroup( QStringLiteral( "defaults/bearing_format" ) );
const QStringList keys = s.childKeys();
for ( const QString &key : keys )
{
const QVariant value = s.value( key );
config.insert( key, value );
}
s.endGroup();

std::unique_ptr< QgsBearingNumericFormat > res = qgis::make_unique< QgsBearingNumericFormat >();
res->setConfiguration( config, QgsReadWriteContext() );
return res.release();
}
@@ -0,0 +1,62 @@
/***************************************************************************
qgslocaldefaultsettings.h
---------------------------
begin : January 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson 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 QGSLOCALDEFAULTSETTINGS_H
#define QGSLOCALDEFAULTSETTINGS_H

#include "qgis_core.h"
#include "qgis_sip.h"

class QgsBearingNumericFormat;

/**
* Contains local default settings which should be respected when creating new objects
* such as QgsProjects.
*
* This class contains a variety of default setting values. These values are local, profile
* specific settings which may have been configured or tweaked by the user (as opposed to
* global, fixed default settings).
*
* The values encapsulated here should be inherited when creating new objects such as new
* QGIS projects.
*
* Typically, the QgsSettings backend is used to store and retrieve these local settings.
*
* \ingroup core
* \since QGIS 3.12
*/
class CORE_EXPORT QgsLocalDefaultSettings
{

public:

/**
* Sets the default bearing \a format, which controls how angular bearings are displayed.
*
* \see bearingFormat()
*/
static void setBearingFormat( const QgsBearingNumericFormat *format );

/**
* Returns the default bearing format, which controls how angular bearings are displayed.
*
* This method returns a new object and the caller takes ownership of the returned value.
*
* \see setBearingFormat()
*/
static QgsBearingNumericFormat *bearingFormat() SIP_FACTORY;

};

#endif // QGSLOCALDEFAULTSETTINGS_H
@@ -131,6 +131,7 @@ ADD_PYTHON_TEST(PyQgsLayoutSnapper test_qgslayoutsnapper.py)
ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)
ADD_PYTHON_TEST(PyQgsLineSegment test_qgslinesegment.py)
ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py)
ADD_PYTHON_TEST(PyQgsLocalDefaultSettings test_qgslocaldefaultsettings.py)
ADD_PYTHON_TEST(PyQgsLocator test_qgslocator.py)
ADD_PYTHON_TEST(PyQgsMapCanvas test_qgsmapcanvas.py)
ADD_PYTHON_TEST(PyQgsMapCanvasAnnotationItem test_qgsmapcanvasannotationitem.py)
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsLocalDefaultSettings.
.. note:: 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.
"""
__author__ = 'Nyall Dawson'
__date__ = '09/01/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA

from qgis.core import (QgsSettings,
QgsLocalDefaultSettings,
QgsBearingNumericFormat)

from qgis.PyQt.QtCore import QCoreApplication

from qgis.testing import start_app, unittest
from utilities import (unitTestDataPath)

TEST_DATA_DIR = unitTestDataPath()


class TestQgsLocalDefaultSettings(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""

QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain("TestPyQgsWFSProvider.com")
QCoreApplication.setApplicationName("TestPyQgsWFSProvider")
QgsSettings().clear()
start_app()

def testBearingFormat(self):
s = QgsLocalDefaultSettings()

format = QgsBearingNumericFormat()
format.setNumberDecimalPlaces(9)
format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360)

s.setBearingFormat(format)
self.assertEqual(s.bearingFormat().numberDecimalPlaces(), 9)
self.assertEqual(s.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360)

format = QgsBearingNumericFormat()
format.setNumberDecimalPlaces(3)
format.setDirectionFormat(QgsBearingNumericFormat.UseRangeNegative180ToPositive180)
s.setBearingFormat(format)
self.assertEqual(s.bearingFormat().numberDecimalPlaces(), 3)
self.assertEqual(s.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180)

# new settings object, should persist.
s2 = QgsLocalDefaultSettings()
self.assertEqual(s2.bearingFormat().numberDecimalPlaces(), 3)
self.assertEqual(s2.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180)


if __name__ == '__main__':
unittest.main()
@@ -41,13 +41,15 @@ def testBearingFormat(self):
p.setBearingFormat(format)
self.assertEqual(len(spy), 1)
self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 9)
self.assertEqual(p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360)

format = QgsBearingNumericFormat()
format.setNumberDecimalPlaces(3)
format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360)
format.setDirectionFormat(QgsBearingNumericFormat.UseRangeNegative180ToPositive180)
p.setBearingFormat(format)
self.assertEqual(len(spy), 2)
self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 3)
self.assertEqual(p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180)

p.reset()
self.assertEqual(len(spy), 3)

0 comments on commit 1f7140b

Please sign in to comment.
You can’t perform that action at this time.