QGIS API Documentation  3.0.2-Girona (307d082)
qgscolorscheme.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorscheme.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 
18 #include "qgscolorscheme.h"
19 
20 #include "qgsproject.h"
21 #include "qgssymbollayerutils.h"
22 #include "qgsapplication.h"
23 #include "qgssettings.h"
24 
25 #include <QDir>
26 
27 bool QgsColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
28 {
29  //base implementation does nothing
30  Q_UNUSED( colors );
31  Q_UNUSED( context );
32  Q_UNUSED( baseColor );
33  return false;
34 }
35 
36 
37 //
38 // QgsRecentColorScheme
39 //
40 
41 QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString &context, const QColor &baseColor )
42 {
43  Q_UNUSED( context );
44  Q_UNUSED( baseColor );
45 
46  //fetch recent colors
47  QgsSettings settings;
48  QList< QVariant > recentColorVariants = settings.value( QStringLiteral( "colors/recent" ) ).toList();
49 
50  //generate list from recent colors
51  QgsNamedColorList colorList;
52  Q_FOREACH ( const QVariant &color, recentColorVariants )
53  {
54  colorList.append( qMakePair( color.value<QColor>(), QgsSymbolLayerUtils::colorToName( color.value<QColor>() ) ) );
55  }
56  return colorList;
57 }
58 
60 {
61  return new QgsRecentColorScheme();
62 }
63 
64 void QgsRecentColorScheme::addRecentColor( const QColor &color )
65 {
66  if ( !color.isValid() )
67  {
68  return;
69  }
70 
71  //strip alpha from color
72  QColor opaqueColor = color;
73  opaqueColor.setAlpha( 255 );
74 
75  QgsSettings settings;
76  QList< QVariant > recentColorVariants = settings.value( QStringLiteral( "colors/recent" ) ).toList();
77 
78  //remove colors by name
79  for ( int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
80  {
81  if ( ( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
82  {
83  recentColorVariants.removeAt( colorIdx );
84  }
85  }
86 
87  //add color
88  QVariant colorVariant = QVariant( opaqueColor );
89  recentColorVariants.prepend( colorVariant );
90 
91  //trim to 20 colors
92  while ( recentColorVariants.count() > 20 )
93  {
94  recentColorVariants.pop_back();
95  }
96 
97  settings.setValue( QStringLiteral( "colors/recent" ), recentColorVariants );
98 }
99 
101 {
102  //fetch recent colors
103  QgsSettings settings;
104  QList< QVariant > recentColorVariants = settings.value( QStringLiteral( "colors/recent" ) ).toList();
105 
106  if ( recentColorVariants.isEmpty() )
107  return QColor();
108 
109  return recentColorVariants.at( 0 ).value<QColor>();
110 }
111 
112 QgsNamedColorList QgsCustomColorScheme::fetchColors( const QString &context, const QColor &baseColor )
113 {
114  Q_UNUSED( context );
115  Q_UNUSED( baseColor );
116 
117  //fetch predefined custom colors
118  QgsNamedColorList colorList;
119  QgsSettings settings;
120 
121  //check if settings contains custom palette
122  if ( !settings.contains( QStringLiteral( "/colors/palettecolors" ) ) )
123  {
124  //no custom palette, return default colors
125  colorList.append( qMakePair( QColor( 0, 0, 0 ), QString() ) );
126  colorList.append( qMakePair( QColor( 255, 255, 255 ), QString() ) );
127  colorList.append( qMakePair( QColor( 166, 206, 227 ), QString() ) );
128  colorList.append( qMakePair( QColor( 31, 120, 180 ), QString() ) );
129  colorList.append( qMakePair( QColor( 178, 223, 138 ), QString() ) );
130  colorList.append( qMakePair( QColor( 51, 160, 44 ), QString() ) );
131  colorList.append( qMakePair( QColor( 251, 154, 153 ), QString() ) );
132  colorList.append( qMakePair( QColor( 227, 26, 28 ), QString() ) );
133  colorList.append( qMakePair( QColor( 253, 191, 111 ), QString() ) );
134  colorList.append( qMakePair( QColor( 255, 127, 0 ), QString() ) );
135 
136  return colorList;
137  }
138 
139  QList< QVariant > customColorVariants = settings.value( QStringLiteral( "colors/palettecolors" ) ).toList();
140  QList< QVariant > customColorLabels = settings.value( QStringLiteral( "colors/palettelabels" ) ).toList();
141 
142  //generate list from custom colors
143  int colorIndex = 0;
144  for ( QList< QVariant >::iterator it = customColorVariants.begin();
145  it != customColorVariants.end(); ++it )
146  {
147  QColor color = ( *it ).value<QColor>();
148  QString label;
149  if ( customColorLabels.length() > colorIndex )
150  {
151  label = customColorLabels.at( colorIndex ).toString();
152  }
153 
154  colorList.append( qMakePair( color, label ) );
155  colorIndex++;
156  }
157 
158  return colorList;
159 }
160 
161 bool QgsCustomColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
162 {
163  Q_UNUSED( context );
164  Q_UNUSED( baseColor );
165 
166  // save colors to settings
167  QgsSettings settings;
168  QList< QVariant > customColors;
169  QList< QVariant > customColorLabels;
170 
171  QgsNamedColorList::const_iterator colorIt = colors.constBegin();
172  for ( ; colorIt != colors.constEnd(); ++colorIt )
173  {
174  QVariant color = ( *colorIt ).first;
175  QVariant label = ( *colorIt ).second;
176  customColors.append( color );
177  customColorLabels.append( label );
178  }
179  settings.setValue( QStringLiteral( "colors/palettecolors" ), customColors );
180  settings.setValue( QStringLiteral( "colors/palettelabels" ), customColorLabels );
181  return true;
182 }
183 
185 {
186  return new QgsCustomColorScheme();
187 }
188 
189 
190 QgsNamedColorList QgsProjectColorScheme::fetchColors( const QString &context, const QColor &baseColor )
191 {
192  Q_UNUSED( context );
193  Q_UNUSED( baseColor );
194 
195  QgsNamedColorList colorList;
196 
197  QStringList colorStrings = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ) );
198  QStringList colorLabels = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ) );
199 
200  //generate list from custom colors
201  int colorIndex = 0;
202  for ( QStringList::iterator it = colorStrings.begin();
203  it != colorStrings.end(); ++it )
204  {
205  QColor color = QgsSymbolLayerUtils::decodeColor( *it );
206  QString label;
207  if ( colorLabels.length() > colorIndex )
208  {
209  label = colorLabels.at( colorIndex );
210  }
211 
212  colorList.append( qMakePair( color, label ) );
213  colorIndex++;
214  }
215 
216  return colorList;
217 }
218 
219 bool QgsProjectColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
220 {
221  Q_UNUSED( context );
222  Q_UNUSED( baseColor );
223 
224  // save colors to project
225  QStringList customColors;
226  QStringList customColorLabels;
227 
228  QgsNamedColorList::const_iterator colorIt = colors.constBegin();
229  for ( ; colorIt != colors.constEnd(); ++colorIt )
230  {
231  QString color = QgsSymbolLayerUtils::encodeColor( ( *colorIt ).first );
232  QString label = ( *colorIt ).second;
233  customColors.append( color );
234  customColorLabels.append( label );
235  }
236  QgsProject::instance()->writeEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ), customColors );
237  QgsProject::instance()->writeEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ), customColorLabels );
238  return true;
239 }
240 
242 {
243  return new QgsProjectColorScheme();
244 }
245 
246 
247 //
248 // QgsGplColorScheme
249 //
250 
251 QgsNamedColorList QgsGplColorScheme::fetchColors( const QString &context, const QColor &baseColor )
252 {
253  Q_UNUSED( context );
254  Q_UNUSED( baseColor );
255 
256  QString sourceFilePath = gplFilePath();
257  if ( sourceFilePath.isEmpty() )
258  {
259  QgsNamedColorList noColors;
260  return noColors;
261  }
262 
263  bool ok;
264  QString name;
265  QFile sourceFile( sourceFilePath );
266  return QgsSymbolLayerUtils::importColorsFromGpl( sourceFile, ok, name );
267 }
268 
269 bool QgsGplColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
270 {
271  Q_UNUSED( context );
272  Q_UNUSED( baseColor );
273 
274  QString destFilePath = gplFilePath();
275  if ( destFilePath.isEmpty() )
276  {
277  return false;
278  }
279 
280  QFile destFile( destFilePath );
281  return QgsSymbolLayerUtils::saveColorsToGpl( destFile, schemeName(), colors );
282 }
283 
284 
285 //
286 // QgsUserColorScheme
287 //
288 
289 QgsUserColorScheme::QgsUserColorScheme( const QString &filename )
290  : mFilename( filename )
291 {
292  QFile sourceFile( gplFilePath() );
293 
294  //read in name
295  if ( sourceFile.open( QIODevice::ReadOnly ) )
296  {
297  QTextStream in( &sourceFile );
298 
299  //find name line
300  QString line;
301  while ( !in.atEnd() && !line.startsWith( QLatin1String( "Name:" ) ) )
302  {
303  line = in.readLine();
304  }
305  if ( !in.atEnd() )
306  {
307  QRegExp rx( "Name:\\s*(\\S.*)$" );
308  if ( rx.indexIn( line ) != -1 )
309  {
310  mName = rx.cap( 1 );
311  }
312  }
313  }
314  if ( mName.isEmpty() )
315  {
316  mName = mFilename;
317  }
318 }
319 
321 {
322  return mName;
323 }
324 
326 {
327  return new QgsUserColorScheme( mFilename );
328 }
329 
330 QgsColorScheme::SchemeFlags QgsUserColorScheme::flags() const
331 {
332  QgsColorScheme::SchemeFlags f = QgsGplColorScheme::flags();
333 
334  QgsSettings s;
335  QStringList showInMenuSchemes = s.value( QStringLiteral( "/colors/showInMenuList" ) ).toStringList();
336 
337  if ( showInMenuSchemes.contains( mName ) )
338  {
340  }
341 
342  return f;
343 }
344 
346 {
347  QString filePath = gplFilePath();
348  if ( filePath.isEmpty() )
349  {
350  return false;
351  }
352 
353  //try to erase gpl file
354  return QFile::remove( filePath );
355 }
356 
358 {
359  QgsSettings s;
360  QStringList showInMenuSchemes = s.value( QStringLiteral( "/colors/showInMenuList" ) ).toStringList();
361 
362  if ( show && !showInMenuSchemes.contains( mName ) )
363  {
364  showInMenuSchemes << mName;
365  }
366  else if ( !show && showInMenuSchemes.contains( mName ) )
367  {
368  showInMenuSchemes.removeAll( mName );
369  }
370 
371  s.setValue( QStringLiteral( "/colors/showInMenuList" ), showInMenuSchemes );
372 }
373 
375 {
376  QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
377 
378  QDir localDir;
379  if ( !localDir.mkpath( palettesDir ) )
380  {
381  return QString();
382  }
383 
384  return QDir( palettesDir ).filePath( mFilename );
385 }
A color scheme which contains custom colors set through QGIS app options dialog.
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
A color scheme which contains project specific colors set through project properties dialog...
bool erase()
Erases the associated gpl palette file from the users "palettes" folder.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
QgsCustomColorScheme * clone() const override
Clones a color scheme.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
bool contains(const QString &key, const QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
virtual SchemeFlags flags() const
Returns the current flags for the color scheme.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
QgsRecentColorScheme * clone() const override
Clones a color scheme.
QgsColorScheme::SchemeFlags flags() const override
Returns the current flags for the color scheme.
Show scheme in color button drop-down menu.
QgsProjectColorScheme * clone() const override
Clones a color scheme.
static QString encodeColor(const QColor &color)
bool writeEntry(const QString &scope, const QString &key, bool value)
Write a boolean entry to the project file.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
void setShowSchemeInMenu(bool show)
Sets whether a this scheme should be shown in color button menus.
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QgsUserColorScheme(const QString &filename)
Constructs a new user color scheme, using a specified gpl palette file.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Key value accessors.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
QString gplFilePath() override
Returns the file path for the associated gpl palette file.
A color scheme which contains the most recently used colors.
static bool saveColorsToGpl(QFile &file, const QString &paletteName, const QgsNamedColorList &colors)
Exports colors to a gpl GIMP palette file.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QString schemeName() const override
Gets the name for the color scheme.
QgsUserColorScheme * clone() const override
Clones a color scheme.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
virtual QString schemeName() const =0
Gets the name for the color scheme.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:383
static QColor lastUsedColor()
Returns the most recently used color.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
static QColor decodeColor(const QString &str)