Skip to content

Commit 7e6796c

Browse files
committed
By default, generate new layer colors from a random selection
in a preset palette The palette is packaged as resources/new_layer_colors.gpl TODO: allow users to set a different palette
1 parent 149b23e commit 7e6796c

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

python/core/qgscolorschemeregistry.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ Adds all default color schemes to this color scheme.
5050
.. seealso:: :py:func:`addColorScheme`
5151

5252
.. seealso:: :py:func:`addUserSchemes`
53+
%End
54+
55+
void initStyleScheme();
56+
%Docstring
57+
Initializes the default random style color scheme for the user.
58+
59+
.. versionadded:: 3.2
5360
%End
5461

5562
void addUserSchemes();

src/core/qgsapplication.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ void QgsApplication::init( QString profileFolder )
271271
// so we read actual value in main.cpp
272272
ABISYM( mMaxThreads ) = -1;
273273

274+
colorSchemeRegistry()->addDefaultSchemes();
275+
colorSchemeRegistry()->initStyleScheme();
276+
274277
ABISYM( mInitialized ) = true;
275278
}
276279

@@ -1718,7 +1721,6 @@ QgsApplication::ApplicationMembers::ApplicationMembers()
17181721
mFieldFormatterRegistry = new QgsFieldFormatterRegistry();
17191722
mSvgCache = new QgsSvgCache();
17201723
mColorSchemeRegistry = new QgsColorSchemeRegistry();
1721-
mColorSchemeRegistry->addDefaultSchemes();
17221724
mPaintEffectRegistry = new QgsPaintEffectRegistry();
17231725
mSymbolLayerRegistry = new QgsSymbolLayerRegistry();
17241726
mRendererRegistry = new QgsRendererRegistry();

src/core/qgscolorschemeregistry.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
5151
addUserSchemes();
5252
}
5353

54+
void QgsColorSchemeRegistry::initStyleScheme()
55+
{
56+
QString stylePalette = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/new_layer_colors.gpl" );
57+
if ( QFileInfo::exists( stylePalette ) )
58+
{
59+
QgsUserColorScheme *scheme = new QgsUserColorScheme( stylePalette );
60+
addColorScheme( scheme );
61+
setRandomStyleColorScheme( scheme );
62+
}
63+
}
64+
5465
void QgsColorSchemeRegistry::addUserSchemes()
5566
{
5667
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";

src/core/qgscolorschemeregistry.h

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class CORE_EXPORT QgsColorSchemeRegistry
5858
*/
5959
void addDefaultSchemes();
6060

61+
/**
62+
* Initializes the default random style color scheme for the user.
63+
* \since QGIS 3.2
64+
*/
65+
void initStyleScheme();
66+
6167
/**
6268
* Creates schemes for all gpl palettes in the user's palettes folder.
6369
* \see populateFromInstance

tests/src/core/testqgscolorschemeregistry.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ class TestQgsColorSchemeRegistry : public QObject
104104

105105
void TestQgsColorSchemeRegistry::initTestCase()
106106
{
107-
107+
QgsApplication::init();
108+
QgsApplication::initQgis();
108109
}
109110

110111
void TestQgsColorSchemeRegistry::cleanupTestCase()
@@ -249,6 +250,8 @@ void TestQgsColorSchemeRegistry::fetchRandomStyleColor()
249250
QVERIFY( registry->fetchRandomStyleColor().isValid() );
250251
}
251252

253+
// we expect the default application color scheme registry to have a randomStyleColorScheme set
254+
QVERIFY( QgsApplication::colorSchemeRegistry()->randomStyleColorScheme() );
252255
}
253256

254257
QGSTEST_MAIN( TestQgsColorSchemeRegistry )

0 commit comments

Comments
 (0)