QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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#include "qgsproject.h"
25
26#include <QDir>
27#include <QFileInfoList>
28#include <QMutex>
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
34{
35 qDeleteAll( mColorSchemeList );
36 mColorSchemeList.clear();
37}
38
40{
41 //get schemes from global instance
42 QList< QgsColorScheme * > schemeList = QgsApplication::colorSchemeRegistry()->schemes();
43
44 //add to this scheme registry
45 QList< QgsColorScheme * >::iterator it = schemeList.begin();
46 for ( ; it != schemeList.end(); ++it )
47 {
48 addColorScheme( ( *it )->clone() );
49 }
50}
51
53{
54 //default color schemes
57 if ( mProject )
58 addColorScheme( new QgsProjectColorScheme( mProject ) );
60}
61
63{
64 const QString stylePalette = QgsApplication::pkgDataPath() + u"/resources/palettes/new_layer_colors.gpl"_s;
65 if ( QFileInfo::exists( stylePalette ) )
66 {
67 QgsUserColorScheme *scheme = new QgsUserColorScheme( stylePalette );
68 addColorScheme( scheme );
70 }
71}
72
74{
75 const QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
76
77 const QDir localDir;
78 if ( !localDir.mkpath( palettesDir ) )
79 {
80 return;
81 }
82
83 const QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( u"*.gpl"_s ), QDir::Files );
84 QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
85 for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
86 {
87 addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
88 }
89}
90
92{
93 mColorSchemeList.append( scheme );
94}
95
96QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
97{
98 QList< QgsColorScheme * > allSchemes;
99 QList<QgsColorScheme *>::const_iterator schemeIt;
100 for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
101 {
102 allSchemes.append( ( *schemeIt ) );
103 }
104 return allSchemes;
105}
106
107QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
108{
109 QList< QgsColorScheme * > matchingSchemes;
110 QList<QgsColorScheme *>::const_iterator schemeIt;
111 for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
112 {
113 if ( ( *schemeIt )->flags().testFlag( flag ) )
114 {
115 matchingSchemes.append( ( *schemeIt ) );
116 }
117 }
118 return matchingSchemes;
119}
120
122{
123 mRandomStyleColorScheme = scheme;
124 if ( scheme )
125 {
126 mRandomStyleColors = scheme->fetchColors();
127
128 if ( mRandomStyleColors.count() > 0 )
129 {
130 std::random_device rd;
131 std::mt19937 mt( rd() );
132 std::uniform_int_distribution<int> colorDist( 0, mRandomStyleColors.count() - 1 );
133 mNextRandomStyleColorIndex = colorDist( mt );
134 std::uniform_int_distribution<int> colorDir( 0, 1 );
135 mNextRandomStyleColorDirection = colorDir( mt ) == 0 ? -1 : 1;
136 }
137 }
138 else
139 {
140 mRandomStyleColors.clear();
141 }
142}
143
145{
146 return mRandomStyleColorScheme;
147}
148
150{
151 if ( mRandomStyleColors.empty() )
152 {
153 // no random color scheme available - so just use totally random colors
154
155 // Make sure we use get uniquely seeded random numbers, and not the same sequence of numbers
156 std::random_device rd;
157 std::mt19937 mt( rd() );
158 std::uniform_int_distribution<int> hueDist( 0, 359 );
159 std::uniform_int_distribution<int> satDist( 64, 255 );
160 std::uniform_int_distribution<int> valueDist( 128, 255 );
161 return QColor::fromHsv( hueDist( mt ), satDist( mt ), valueDist( mt ) );
162 }
163 else
164 {
165 static QMutex sMutex;
166 const QMutexLocker locker( &sMutex );
167 QColor res = mRandomStyleColors.at( mNextRandomStyleColorIndex ).first;
168 mNextRandomStyleColorIndex += mNextRandomStyleColorDirection;
169 if ( mNextRandomStyleColorIndex < 0 )
170 mNextRandomStyleColorIndex = mRandomStyleColors.count() - 1;
171 if ( mNextRandomStyleColorIndex >= mRandomStyleColors.count() )
172 mNextRandomStyleColorIndex = 0;
173 return res;
174 }
175}
176
178{
179 if ( project == mProject )
180 return;
181
182 mProject = project;
183
184 QList< QgsProjectColorScheme * > existingSchemes;
185 schemes( existingSchemes );
186 const bool hadScheme = !existingSchemes.isEmpty();
187 for ( QgsProjectColorScheme *scheme : existingSchemes )
188 {
189 removeColorScheme( scheme );
190 delete scheme;
191 }
192
193 if ( mProject && hadScheme )
194 addColorScheme( new QgsProjectColorScheme( mProject ) );
195}
196
198{
199 if ( mRandomStyleColorScheme == scheme )
200 {
201 mRandomStyleColorScheme = nullptr;
202 mRandomStyleColors.clear();
203 }
204
205 if ( mColorSchemeList.indexOf( scheme ) != -1 )
206 {
207 mColorSchemeList.removeAll( scheme );
208 return true;
209 }
210
211 //not found
212 return false;
213}
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 setProject(QgsProject *project)
Sets the project associated with the registry, used to provide the "Project colors" scheme.
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
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 ...