Skip to content
Permalink
Browse files

Add a signal to QgsProject for when project color scheme changes

  • Loading branch information
nyalldawson committed Jan 12, 2019
1 parent 8a80eeb commit f7745c94d9881ee404e1d90f178f42eed89458e1
@@ -1003,6 +1003,15 @@ in the project. The removeMapLayer(), removeMapLayers() calls do not block remov
.. deprecated:: since QGIS 3.4 use QgsMapLayer.setFlags() instead

.. versionadded:: 3.2
%End

void setProjectColors( const QgsNamedColorList &colors );
%Docstring
Sets the ``colors`` for the project's color scheme (see QgsProjectColorScheme).

.. seealso:: :py:func:`projectColorsChanged`

.. versionadded:: 3.6
%End

void generateTsFile( const QString &locale );
@@ -1230,6 +1239,15 @@ Emitted when the project's metadata is changed.
.. versionadded:: 3.2
%End

void projectColorsChanged();
%Docstring
Emitted whenever the project's color scheme has been changed.

.. seealso:: :py:func:`setProjectColors`

.. versionadded:: 3.6
%End


void layersWillBeRemoved( const QStringList &layerIds );
%Docstring
@@ -221,21 +221,7 @@ bool QgsProjectColorScheme::setColors( const QgsNamedColorList &colors, const QS
{
Q_UNUSED( context );
Q_UNUSED( baseColor );

// save colors to project
QStringList customColors;
QStringList customColorLabels;

QgsNamedColorList::const_iterator colorIt = colors.constBegin();
for ( ; colorIt != colors.constEnd(); ++colorIt )
{
QString color = QgsSymbolLayerUtils::encodeColor( ( *colorIt ).first );
QString label = ( *colorIt ).second;
customColors.append( color );
customColorLabels.append( label );
}
QgsProject::instance()->writeEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ), customColors );
QgsProject::instance()->writeEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ), customColorLabels );
QgsProject::instance()->setProjectColors( colors );
return true;
}

@@ -52,6 +52,7 @@
#include "qgsmaplayerstore.h"
#include "qgsziputils.h"
#include "qgsauxiliarystorage.h"
#include "qgssymbollayerutils.h"

#include <QApplication>
#include <QFileInfo>
@@ -713,6 +714,7 @@ void QgsProject::clear()
mArchive->clear();

emit labelingEngineSettingsChanged();
emit projectColorsChanged();

// reset some default project properties
// XXX THESE SHOULD BE MOVED TO STATUSBAR RELATED SOURCE
@@ -1378,6 +1380,7 @@ bool QgsProject::readProjectFile( const QString &filename )
emit readProjectWithContext( *doc, context );
emit snappingConfigChanged( mSnappingConfig );
emit topologicalEditingChanged();
emit projectColorsChanged();

// if all went well, we're allegedly in pristine state
if ( clean )
@@ -2942,6 +2945,25 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
}
}

void QgsProject::setProjectColors( const QgsNamedColorList &colors )
{
// save colors to project
QStringList customColors;
QStringList customColorLabels;

QgsNamedColorList::const_iterator colorIt = colors.constBegin();
for ( ; colorIt != colors.constEnd(); ++colorIt )
{
QString color = QgsSymbolLayerUtils::encodeColor( ( *colorIt ).first );
QString label = ( *colorIt ).second;
customColors.append( color );
customColorLabels.append( label );
}
writeEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ), customColors );
writeEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ), customColorLabels );
emit projectColorsChanged();
}

void QgsProject::generateTsFile( const QString &locale )
{
QgsTranslationContext translationContext;
@@ -47,6 +47,7 @@
#include "qgstranslationcontext.h"
#include "qgsprojecttranslator.h"
#include "qgsattributeeditorelement.h"
#include "qgscolorscheme.h"

class QFileInfo;
class QDomDocument;
@@ -1001,6 +1002,14 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
Q_DECL_DEPRECATED void setRequiredLayers( const QSet<QgsMapLayer *> &layers );

/**
* Sets the \a colors for the project's color scheme (see QgsProjectColorScheme).
*
* \see projectColorsChanged()
* \since QGIS 3.6
*/
void setProjectColors( const QgsNamedColorList &colors );

/**
* Triggers the collection strings of .qgs to be included in ts file and calls writeTsFile()
* \since QGIS 3.4
@@ -1201,6 +1210,14 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void metadataChanged();

/**
* Emitted whenever the project's color scheme has been changed.
* \see setProjectColors()
* \since QGIS 3.6
*/
void projectColorsChanged();

//
// signals from QgsMapLayerRegistry
//
@@ -27,12 +27,14 @@
QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer,
QgsExpressionContextUtils)
QgsExpressionContextUtils,
QgsProjectColorScheme)
from qgis.gui import (QgsLayerTreeMapCanvasBridge,
QgsMapCanvas)

from qgis.PyQt.QtTest import QSignalSpy
from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryDir
from qgis.PyQt.QtGui import QColor
from qgis.PyQt import sip

from qgis.testing import start_app, unittest
@@ -1169,6 +1171,18 @@ def testDirtying(self):
project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148'))
self.assertFalse(project.isDirty())

def testColorScheme(self):
p = QgsProject.instance()
spy = QSignalSpy(p.projectColorsChanged)
p.setProjectColors([[QColor(255, 0, 0), 'red'], [QColor(0, 255, 0), 'green']])
self.assertEqual(len(spy), 1)
scheme = [s for s in QgsApplication.colorSchemeRegistry().schemes() if isinstance(s, QgsProjectColorScheme)][0]
self.assertEqual([[c[0].name(), c[1]] for c in scheme.fetchColors()], [['#ff0000', 'red'], ['#00ff00', 'green']])
# except color changed signal when clearing project
p.clear()
self.assertEqual(len(spy), 2)
self.assertEqual([[c[0].name(), c[1]] for c in scheme.fetchColors()], [])


if __name__ == '__main__':
unittest.main()

0 comments on commit f7745c9

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