From f7745c94d9881ee404e1d90f178f42eed89458e1 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 12 Jan 2019 10:41:46 +1000 Subject: [PATCH] Add a signal to QgsProject for when project color scheme changes --- python/core/auto_generated/qgsproject.sip.in | 18 ++++++++++++++++ src/core/qgscolorscheme.cpp | 16 +------------- src/core/qgsproject.cpp | 22 ++++++++++++++++++++ src/core/qgsproject.h | 17 +++++++++++++++ tests/src/python/test_qgsproject.py | 16 +++++++++++++- 5 files changed, 73 insertions(+), 16 deletions(-) diff --git a/python/core/auto_generated/qgsproject.sip.in b/python/core/auto_generated/qgsproject.sip.in index aef8b9ade4b..8456257ef07 100644 --- a/python/core/auto_generated/qgsproject.sip.in +++ b/python/core/auto_generated/qgsproject.sip.in @@ -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 diff --git a/src/core/qgscolorscheme.cpp b/src/core/qgscolorscheme.cpp index cea419a69ac..67e58a0a84f 100644 --- a/src/core/qgscolorscheme.cpp +++ b/src/core/qgscolorscheme.cpp @@ -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; } diff --git a/src/core/qgsproject.cpp b/src/core/qgsproject.cpp index 4def2414c71..988c695861c 100644 --- a/src/core/qgsproject.cpp +++ b/src/core/qgsproject.cpp @@ -52,6 +52,7 @@ #include "qgsmaplayerstore.h" #include "qgsziputils.h" #include "qgsauxiliarystorage.h" +#include "qgssymbollayerutils.h" #include #include @@ -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 &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; diff --git a/src/core/qgsproject.h b/src/core/qgsproject.h index bc25b3c5563..de186f6859a 100644 --- a/src/core/qgsproject.h +++ b/src/core/qgsproject.h @@ -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 &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 // diff --git a/tests/src/python/test_qgsproject.py b/tests/src/python/test_qgsproject.py index 6b8fa60051e..07f1548dbee 100644 --- a/tests/src/python/test_qgsproject.py +++ b/tests/src/python/test_qgsproject.py @@ -27,12 +27,14 @@ from qgis.core import (QgsProject, 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 @@ class TestQgsProject(unittest.TestCase): 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()