Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a signal to QgsProject for when project color scheme changes
  • Loading branch information
nyalldawson committed Jan 12, 2019
1 parent 8a80eeb commit f7745c9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
18 changes: 18 additions & 0 deletions python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions src/core/qgscolorscheme.cpp
Expand Up @@ -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;
}

Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -52,6 +52,7 @@
#include "qgsmaplayerstore.h"
#include "qgsziputils.h"
#include "qgsauxiliarystorage.h"
#include "qgssymbollayerutils.h"

#include <QApplication>
#include <QFileInfo>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -47,6 +47,7 @@
#include "qgstranslationcontext.h"
#include "qgsprojecttranslator.h"
#include "qgsattributeeditorelement.h"
#include "qgscolorscheme.h"

class QFileInfo;
class QDomDocument;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
//
Expand Down
16 changes: 15 additions & 1 deletion tests/src/python/test_qgsproject.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.