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