QGIS API Documentation 3.27.0-Master (c6eca784ad)
qgscolorschemeregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorschemeregistry.cpp
3 -------------------
4 begin : July 2014
5 copyright : (C) 2014 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgscolorscheme.h"
20#include "qgsapplication.h"
21#include <QDir>
22#include <QFileInfoList>
23#include <QMutex>
24#include <random>
25
27{
28 qDeleteAll( mColorSchemeList );
29 mColorSchemeList.clear();
30}
31
33{
34 //get schemes from global instance
35 QList< QgsColorScheme * > schemeList = QgsApplication::colorSchemeRegistry()->schemes();
36
37 //add to this scheme registry
38 QList< QgsColorScheme * >::iterator it = schemeList.begin();
39 for ( ; it != schemeList.end(); ++it )
40 {
41 addColorScheme( ( *it )->clone() );
42 }
43}
44
46{
47 //default color schemes
52}
53
55{
56 const QString stylePalette = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/palettes/new_layer_colors.gpl" );
57 if ( QFileInfo::exists( stylePalette ) )
58 {
59 QgsUserColorScheme *scheme = new QgsUserColorScheme( stylePalette );
60 addColorScheme( scheme );
62 }
63}
64
66{
67 const QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
68
69 const QDir localDir;
70 if ( !localDir.mkpath( palettesDir ) )
71 {
72 return;
73 }
74
75 const QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( QStringLiteral( "*.gpl" ) ), QDir::Files );
76 QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
77 for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
78 {
79 addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
80 }
81}
82
84{
85 mColorSchemeList.append( scheme );
86}
87
88QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
89{
90 QList< QgsColorScheme * > allSchemes;
91 QList<QgsColorScheme *>::const_iterator schemeIt;
92 for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
93 {
94 allSchemes.append( ( *schemeIt ) );
95 }
96 return allSchemes;
97}
98
99QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
100{
101 QList< QgsColorScheme * > matchingSchemes;
102 QList<QgsColorScheme *>::const_iterator schemeIt;
103 for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
104 {
105 if ( ( *schemeIt )->flags().testFlag( flag ) )
106 {
107 matchingSchemes.append( ( *schemeIt ) );
108 }
109 }
110 return matchingSchemes;
111}
112
114{
115 mRandomStyleColorScheme = scheme;
116 if ( scheme )
117 {
118 mRandomStyleColors = scheme->fetchColors();
119
120 if ( mRandomStyleColors.count() > 0 )
121 {
122 std::random_device rd;
123 std::mt19937 mt( rd() );
124 std::uniform_int_distribution<int> colorDist( 0, mRandomStyleColors.count() - 1 );
125 mNextRandomStyleColorIndex = colorDist( mt );
126 std::uniform_int_distribution<int> colorDir( 0, 1 );
127 mNextRandomStyleColorDirection = colorDir( mt ) == 0 ? -1 : 1;
128 }
129 }
130 else
131 {
132 mRandomStyleColors.clear();
133 }
134}
135
137{
138 return mRandomStyleColorScheme;
139}
140
142{
143 if ( mRandomStyleColors.empty() )
144 {
145 // no random color scheme available - so just use totally random colors
146
147 // Make sure we use get uniquely seeded random numbers, and not the same sequence of numbers
148 std::random_device rd;
149 std::mt19937 mt( rd() );
150 std::uniform_int_distribution<int> hueDist( 0, 359 );
151 std::uniform_int_distribution<int> satDist( 64, 255 );
152 std::uniform_int_distribution<int> valueDist( 128, 255 );
153 return QColor::fromHsv( hueDist( mt ), satDist( mt ), valueDist( mt ) );
154 }
155 else
156 {
157 static QMutex sMutex;
158 const QMutexLocker locker( &sMutex );
159 QColor res = mRandomStyleColors.at( mNextRandomStyleColorIndex ).first;
160 mNextRandomStyleColorIndex += mNextRandomStyleColorDirection;
161 if ( mNextRandomStyleColorIndex < 0 )
162 mNextRandomStyleColorIndex = mRandomStyleColors.count() - 1;
163 if ( mNextRandomStyleColorIndex >= mRandomStyleColors.count() )
164 mNextRandomStyleColorIndex = 0;
165 return res;
166 }
167}
168
170{
171 if ( mRandomStyleColorScheme == scheme )
172 {
173 mRandomStyleColorScheme = nullptr;
174 mRandomStyleColors.clear();
175 }
176
177 if ( mColorSchemeList.indexOf( scheme ) != -1 )
178 {
179 mColorSchemeList.removeAll( scheme );
180 return true;
181 }
182
183 //not found
184 return false;
185}
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
static QString pkgDataPath()
Returns the common root path of all application data directories.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
void addDefaultSchemes()
Adds all default color schemes to this color scheme.
void addColorScheme(QgsColorScheme *scheme)
Adds a color scheme to the registry.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
void populateFromInstance()
Adds all color schemes from the global instance to this color scheme.
QgsColorScheme * randomStyleColorScheme()
Returns the color scheme used when fetching random colors to use for symbol styles.
bool removeColorScheme(QgsColorScheme *scheme)
Removes all matching color schemes from the registry.
void initStyleScheme()
Initializes the default random style color scheme for the user.
void setRandomStyleColorScheme(QgsColorScheme *scheme)
Sets the color scheme to use when fetching random colors to use for symbol styles.
void addUserSchemes()
Creates schemes for all gpl palettes in the user's palettes folder.
Abstract base class for color schemes.
SchemeFlag
Flags for controlling behavior of color scheme.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
A color scheme which contains custom colors set through QGIS app options dialog.
A color scheme which contains project specific colors set through project properties dialog.
A color scheme which contains the most recently used colors.
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...