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
This commit is contained in:
Nyall Dawson 2018-02-05 15:19:30 +10:00
parent 149b23e08b
commit 7e6796c657
5 changed files with 31 additions and 2 deletions

View File

@ -50,6 +50,13 @@ Adds all default color schemes to this color scheme.
.. seealso:: :py:func:`addColorScheme`
.. seealso:: :py:func:`addUserSchemes`
%End
void initStyleScheme();
%Docstring
Initializes the default random style color scheme for the user.
.. versionadded:: 3.2
%End
void addUserSchemes();

View File

@ -271,6 +271,9 @@ void QgsApplication::init( QString profileFolder )
// so we read actual value in main.cpp
ABISYM( mMaxThreads ) = -1;
colorSchemeRegistry()->addDefaultSchemes();
colorSchemeRegistry()->initStyleScheme();
ABISYM( mInitialized ) = true;
}
@ -1718,7 +1721,6 @@ QgsApplication::ApplicationMembers::ApplicationMembers()
mFieldFormatterRegistry = new QgsFieldFormatterRegistry();
mSvgCache = new QgsSvgCache();
mColorSchemeRegistry = new QgsColorSchemeRegistry();
mColorSchemeRegistry->addDefaultSchemes();
mPaintEffectRegistry = new QgsPaintEffectRegistry();
mSymbolLayerRegistry = new QgsSymbolLayerRegistry();
mRendererRegistry = new QgsRendererRegistry();

View File

@ -51,6 +51,17 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
addUserSchemes();
}
void QgsColorSchemeRegistry::initStyleScheme()
{
QString stylePalette = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/new_layer_colors.gpl" );
if ( QFileInfo::exists( stylePalette ) )
{
QgsUserColorScheme *scheme = new QgsUserColorScheme( stylePalette );
addColorScheme( scheme );
setRandomStyleColorScheme( scheme );
}
}
void QgsColorSchemeRegistry::addUserSchemes()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";

View File

@ -58,6 +58,12 @@ class CORE_EXPORT QgsColorSchemeRegistry
*/
void addDefaultSchemes();
/**
* Initializes the default random style color scheme for the user.
* \since QGIS 3.2
*/
void initStyleScheme();
/**
* Creates schemes for all gpl palettes in the user's palettes folder.
* \see populateFromInstance

View File

@ -104,7 +104,8 @@ class TestQgsColorSchemeRegistry : public QObject
void TestQgsColorSchemeRegistry::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
}
void TestQgsColorSchemeRegistry::cleanupTestCase()
@ -249,6 +250,8 @@ void TestQgsColorSchemeRegistry::fetchRandomStyleColor()
QVERIFY( registry->fetchRandomStyleColor().isValid() );
}
// we expect the default application color scheme registry to have a randomStyleColorScheme set
QVERIFY( QgsApplication::colorSchemeRegistry()->randomStyleColorScheme() );
}
QGSTEST_MAIN( TestQgsColorSchemeRegistry )