1 /***************************************************************************
2                              qgscolorschemeregistry.h
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 
18 #ifndef QGSCOLORSCHEMEREGISTRY_H
19 #define QGSCOLORSCHEMEREGISTRY_H
20 
21 #include "qgis_core.h"
22 #include "qgscolorscheme.h"
23 #include <QList>
24 
25 /**
26  * \ingroup core
27  * \class QgsColorSchemeRegistry
28  * \brief Registry of color schemes
29  *
30  * A registry of QgsColorScheme color schemes. This class can be created directly, or
31  * accessed via a QgsApplication::colorSchemeRegistry().
32  * \since QGIS 2.5
33  */
34 class CORE_EXPORT QgsColorSchemeRegistry
35 {
36 
37   public:
38 
39     /**
40      * Constructor for an empty color scheme registry
41      */
42     QgsColorSchemeRegistry() = default;
43 
44     virtual ~QgsColorSchemeRegistry();
45 
46     /**
47      * Adds all color schemes from the global instance to this color scheme.
48      * \see addDefaultSchemes
49      * \see addColorScheme
50      */
51     void populateFromInstance();
52 
53     /**
54      * Adds all default color schemes to this color scheme.
55      * \see populateFromInstance
56      * \see addColorScheme
57      * \see addUserSchemes
58      */
59     void addDefaultSchemes();
60 
61     /**
62      * Initializes the default random style color scheme for the user.
63      * \since QGIS 3.2
64      */
65     void initStyleScheme();
66 
67     /**
68      * Creates schemes for all gpl palettes in the user's palettes folder.
69      * \see populateFromInstance
70      * \see addDefaultSchemes
71      * \see addColorScheme
72      */
73     void addUserSchemes();
74 
75     /**
76      * Adds a color scheme to the registry. Ownership of the scheme is transferred
77      * to the registry.
78      * \param scheme color scheme to add
79      * \see populateFromInstance
80      * \see removeColorScheme
81      */
82     void addColorScheme( QgsColorScheme *scheme SIP_TRANSFER );
83 
84     /**
85      * Removes all matching color schemes from the registry
86      * \param scheme color scheme to remove
87      * \returns TRUE if scheme was found and removed
88      * \see addColorScheme
89      */
90     bool removeColorScheme( QgsColorScheme *scheme );
91 
92     /**
93      * Returns all color schemes in the registry
94      * \returns list of color schemes
95      */
96     QList<QgsColorScheme *> schemes() const;
97 
98     /**
99      * Returns all color schemes in the registry which have a specified flag set
100      * \param flag flag to match
101      * \returns list of color schemes with flag set
102      */
103     QList<QgsColorScheme *> schemes( QgsColorScheme::SchemeFlag flag ) const;
104 
105 
106     /**
107      * Returns color schemes of a specific type
108      * \param schemeList destination list for matching schemes
109      * \note not available in Python bindings
110      */
111 #ifndef SIP_RUN
schemes(QList<T * > & schemeList)112     template<class T> void schemes( QList<T *> &schemeList )
113     {
114       schemeList.clear();
115       QList<QgsColorScheme *> schemeInstanceList = schemes();
116       QList<QgsColorScheme *>::iterator schemeIt = schemeInstanceList.begin();
117       for ( ; schemeIt != schemeInstanceList.end(); ++schemeIt )
118       {
119         T *scheme = dynamic_cast<T *>( *schemeIt );
120         if ( scheme )
121         {
122           schemeList.push_back( scheme );
123         }
124       }
125     }
126 #endif
127 
128     /**
129      * Sets the color \a scheme to use when fetching random colors to use for symbol styles.
130      *
131      * \a scheme should match a color scheme which is already present in the registry.
132      *
133      * Note that calling this method takes a snapshot of the colors from the scheme's
134      * QgsColorScheme::fetchColors() list. Accordingly, any future changes to the colors
135      * in \a scheme are not automatically reflected by calls to fetchRandomStyleColor().
136      * If \a scheme is updated, then another call to setRandomStyleColorScheme() must
137      * be made in order to update the cached list of available style colors.
138      *
139      * \see randomStyleColorScheme()
140      * \see fetchRandomStyleColor()
141      *
142      * \since QGIS 3.2
143      */
144     void setRandomStyleColorScheme( QgsColorScheme *scheme );
145 
146     /**
147      * Returns the color scheme used when fetching random colors to use for symbol styles.
148      *
149      * This may be NULLPTR, in which case totally random colors are used for styles.
150      *
151      * \see setRandomStyleColorScheme()
152      * \see fetchRandomStyleColor()
153      *
154      * \since QGIS 3.2
155      */
156     QgsColorScheme *randomStyleColorScheme();
157 
158     /**
159      * Returns a random color for use with a new symbol style (e.g. for a newly created
160      * map layer).
161      *
162      * If a randomStyleColorScheme() is set then this color will be randomly taken from that
163      * color scheme. If no randomStyleColorScheme() is set then a totally random color
164      * will be generated.
165      *
166      * Note that calling setRandomStyleColorScheme() takes a snapshot of the colors from the scheme's
167      * QgsColorScheme::fetchColors() list. Accordingly, any future changes to the colors
168      * in the scheme are not automatically reflected by calls to fetchRandomStyleColor().
169      * If the scheme is updated, then another call to setRandomStyleColorScheme() must
170      * be made in order to update the cached list of available style colors from which
171      * fetchRandomStyleColor() selects colors.
172      *
173      * This method is thread safe.
174      *
175      * \see randomStyleColorScheme()
176      * \see setRandomStyleColorScheme()
177      * \since QGIS 3.2
178      */
179     QColor fetchRandomStyleColor() const;
180 
181   private:
182 
183     QList< QgsColorScheme * > mColorSchemeList;
184 
185     QgsColorScheme *mRandomStyleColorScheme = nullptr;
186     QgsNamedColorList mRandomStyleColors;
187 
188     mutable int mNextRandomStyleColorIndex = 0;
189 
190     int mNextRandomStyleColorDirection = 1;
191 
192 };
193 
194 
195 
196 #endif
197