QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
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
20#include <random>
21
22#include "qgsapplication.h"
23#include "qgscolorscheme.h"
24
25#include <QDir>
26#include <QFileInfoList>
27#include <QMutex>
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
33{
34 qDeleteAll( mColorSchemeList );
35 mColorSchemeList.clear();
36}
37
39{
40 //get schemes from global instance
41 QList< QgsColorScheme * > schemeList = QgsApplication::colorSchemeRegistry()->schemes();
42
43 //add to this scheme registry
44 QList< QgsColorScheme * >::iterator it = schemeList.begin();
45 for ( ; it != schemeList.end(); ++it )
46 {
47 addColorScheme( ( *it )->clone() );
48 }
49}
50
59
61{
62 const QString stylePalette = QgsApplication::pkgDataPath() + u"/resources/palettes/new_layer_colors.gpl"_s;
63 if ( QFileInfo::exists( stylePalette ) )
64 {
65 QgsUserColorScheme *scheme = new QgsUserColorScheme( stylePalette );
66 addColorScheme( scheme );
68 }
69}
70
72{
73 const QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
74
75 const QDir localDir;
76 if ( !localDir.mkpath( palettesDir ) )
77 {
78 return;
79 }
80
81 const QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( u"*.gpl"_s ), QDir::Files );
82 QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
83 for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
84 {
85 addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
86 }
87}
88
90{
91 mColorSchemeList.append( scheme );
92}
93
94QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
95{
96 QList< QgsColorScheme * > allSchemes;
97 QList<QgsColorScheme *>::const_iterator schemeIt;
98 for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
99 {
100 allSchemes.append( ( *schemeIt ) );
101 }
102 return allSchemes;
103}
104
105QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
106{
107 QList< QgsColorScheme * > matchingSchemes;
108 QList<QgsColorScheme *>::const_iterator schemeIt;
109 for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
110 {
111 if ( ( *schemeIt )->flags().testFlag( flag ) )
112 {
113 matchingSchemes.append( ( *schemeIt ) );
114 }
115 }
116 return matchingSchemes;
117}
118
120{
121 mRandomStyleColorScheme = scheme;
122 if ( scheme )
123 {
124 mRandomStyleColors = scheme->fetchColors();
125
126 if ( mRandomStyleColors.count() > 0 )
127 {
128 std::random_device rd;
129 std::mt19937 mt( rd() );
130 std::uniform_int_distribution<int> colorDist( 0, mRandomStyleColors.count() - 1 );
131 mNextRandomStyleColorIndex = colorDist( mt );
132 std::uniform_int_distribution<int> colorDir( 0, 1 );
133 mNextRandomStyleColorDirection = colorDir( mt ) == 0 ? -1 : 1;
134 }
135 }
136 else
137 {
138 mRandomStyleColors.clear();
139 }
140}
141
143{
144 return mRandomStyleColorScheme;
145}
146
148{
149 if ( mRandomStyleColors.empty() )
150 {
151 // no random color scheme available - so just use totally random colors
152
153 // Make sure we use get uniquely seeded random numbers, and not the same sequence of numbers
154 std::random_device rd;
155 std::mt19937 mt( rd() );
156 std::uniform_int_distribution<int> hueDist( 0, 359 );
157 std::uniform_int_distribution<int> satDist( 64, 255 );
158 std::uniform_int_distribution<int> valueDist( 128, 255 );
159 return QColor::fromHsv( hueDist( mt ), satDist( mt ), valueDist( mt ) );
160 }
161 else
162 {
163 static QMutex sMutex;
164 const QMutexLocker locker( &sMutex );
165 QColor res = mRandomStyleColors.at( mNextRandomStyleColorIndex ).first;
166 mNextRandomStyleColorIndex += mNextRandomStyleColorDirection;
167 if ( mNextRandomStyleColorIndex < 0 )
168 mNextRandomStyleColorIndex = mRandomStyleColors.count() - 1;
169 if ( mNextRandomStyleColorIndex >= mRandomStyleColors.count() )
170 mNextRandomStyleColorIndex = 0;
171 return res;
172 }
173}
174
176{
177 if ( mRandomStyleColorScheme == scheme )
178 {
179 mRandomStyleColorScheme = nullptr;
180 mRandomStyleColors.clear();
181 }
182
183 if ( mColorSchemeList.indexOf( scheme ) != -1 )
184 {
185 mColorSchemeList.removeAll( scheme );
186 return true;
187 }
188
189 //not found
190 return false;
191}
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 ...