QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgssymbollayerv2utils.h"
22 #include "qgsapplication.h"
23 
24 #include <QSettings>
25 #include <QDir>
26 
28 {
29 
30 }
31 
33 {
34 
35 }
36 
37 bool QgsColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
38 {
39  //base implementation does nothing
40  Q_UNUSED( colors );
41  Q_UNUSED( context );
42  Q_UNUSED( baseColor );
43  return false;
44 }
45 
46 
47 //
48 // QgsRecentColorScheme
49 //
50 
52 {
53 
54 }
55 
57 {
58 
59 }
60 
62 {
63  Q_UNUSED( context );
64  Q_UNUSED( baseColor );
65 
66  //fetch recent colors
67  QSettings settings;
68  QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();
69 
70  //generate list from recent colors
71  QgsNamedColorList colorList;
72  Q_FOREACH ( const QVariant& color, recentColorVariants )
73  {
74  colorList.append( qMakePair( color.value<QColor>(), QgsSymbolLayerV2Utils::colorToName( color.value<QColor>() ) ) );
75  }
76  return colorList;
77 }
78 
80 {
81  return new QgsRecentColorScheme();
82 }
83 
85 {
86  if ( !color.isValid() )
87  {
88  return;
89  }
90 
91  //strip alpha from color
92  QColor opaqueColor = color;
93  opaqueColor.setAlpha( 255 );
94 
95  QSettings settings;
96  QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();
97 
98  //remove colors by name
99  for ( int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
100  {
101  if (( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
102  {
103  recentColorVariants.removeAt( colorIdx );
104  }
105  }
106 
107  //add color
108  QVariant colorVariant = QVariant( opaqueColor );
109  recentColorVariants.prepend( colorVariant );
110 
111  //trim to 20 colors
112  while ( recentColorVariants.count() > 20 )
113  {
114  recentColorVariants.pop_back();
115  }
116 
117  settings.setValue( QString( "/colors/recent" ), recentColorVariants );
118 }
119 
120 
122 {
123 
124 }
125 
127 {
128 
129 }
130 
132 {
133  Q_UNUSED( context );
134  Q_UNUSED( baseColor );
135 
136  //fetch predefined custom colors
137  QgsNamedColorList colorList;
138  QSettings settings;
139 
140  //check if settings contains custom palette
141  if ( !settings.contains( QString( "/colors/palettecolors" ) ) )
142  {
143  //no custom palette, return default colors
144  colorList.append( qMakePair( QColor( "#000000" ), QString() ) );
145  colorList.append( qMakePair( QColor( "#ffffff" ), QString() ) );
146  colorList.append( qMakePair( QColor( "#a6cee3" ), QString() ) );
147  colorList.append( qMakePair( QColor( "#1f78b4" ), QString() ) );
148  colorList.append( qMakePair( QColor( "#b2df8a" ), QString() ) );
149  colorList.append( qMakePair( QColor( "#33a02c" ), QString() ) );
150  colorList.append( qMakePair( QColor( "#fb9a99" ), QString() ) );
151  colorList.append( qMakePair( QColor( "#e31a1c" ), QString() ) );
152  colorList.append( qMakePair( QColor( "#fdbf6f" ), QString() ) );
153  colorList.append( qMakePair( QColor( "#ff7f00" ), QString() ) );
154 
155  return colorList;
156  }
157 
158  QList< QVariant > customColorVariants = settings.value( QString( "/colors/palettecolors" ) ).toList();
159  QList< QVariant > customColorLabels = settings.value( QString( "/colors/palettelabels" ) ).toList();
160 
161  //generate list from custom colors
162  int colorIndex = 0;
163  for ( QList< QVariant >::iterator it = customColorVariants.begin();
164  it != customColorVariants.end(); ++it )
165  {
166  QColor color = ( *it ).value<QColor>();
167  QString label;
168  if ( customColorLabels.length() > colorIndex )
169  {
170  label = customColorLabels.at( colorIndex ).toString();
171  }
172 
173  colorList.append( qMakePair( color, label ) );
174  colorIndex++;
175  }
176 
177  return colorList;
178 }
179 
180 bool QgsCustomColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
181 {
182  Q_UNUSED( context );
183  Q_UNUSED( baseColor );
184 
185  // save colors to settings
186  QSettings settings;
187  QList< QVariant > customColors;
188  QList< QVariant > customColorLabels;
189 
191  for ( ; colorIt != colors.constEnd(); ++colorIt )
192  {
193  QVariant color = ( *colorIt ).first;
194  QVariant label = ( *colorIt ).second;
195  customColors.append( color );
196  customColorLabels.append( label );
197  }
198  settings.setValue( QString( "/colors/palettecolors" ), customColors );
199  settings.setValue( QString( "/colors/palettelabels" ), customColorLabels );
200  return true;
201 }
202 
204 {
205  return new QgsCustomColorScheme();
206 }
207 
208 
210 {
211 
212 }
213 
215 {
216 
217 }
218 
220 {
221  Q_UNUSED( context );
222  Q_UNUSED( baseColor );
223 
224  QgsNamedColorList colorList;
225 
226  QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
227  QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );
228 
229  //generate list from custom colors
230  int colorIndex = 0;
231  for ( QStringList::iterator it = colorStrings.begin();
232  it != colorStrings.end(); ++it )
233  {
235  QString label;
236  if ( colorLabels.length() > colorIndex )
237  {
238  label = colorLabels.at( colorIndex );
239  }
240 
241  colorList.append( qMakePair( color, label ) );
242  colorIndex++;
243  }
244 
245  return colorList;
246 }
247 
248 bool QgsProjectColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
249 {
250  Q_UNUSED( context );
251  Q_UNUSED( baseColor );
252 
253  // save colors to project
254  QStringList customColors;
255  QStringList customColorLabels;
256 
258  for ( ; colorIt != colors.constEnd(); ++colorIt )
259  {
260  QString color = QgsSymbolLayerV2Utils::encodeColor(( *colorIt ).first );
261  QString label = ( *colorIt ).second;
262  customColors.append( color );
263  customColorLabels.append( label );
264  }
265  QgsProject::instance()->writeEntry( "Palette", "/Colors", customColors );
266  QgsProject::instance()->writeEntry( "Palette", "/Labels", customColorLabels );
267  return true;
268 }
269 
271 {
272  return new QgsProjectColorScheme();
273 }
274 
275 
276 //
277 // QgsGplColorScheme
278 //
279 
281  : QgsColorScheme()
282 {
283 
284 }
285 
287 {
288 
289 }
290 
292 {
293  Q_UNUSED( context );
294  Q_UNUSED( baseColor );
295 
296  QString sourceFilePath = gplFilePath();
297  if ( sourceFilePath.isEmpty() )
298  {
299  QgsNamedColorList noColors;
300  return noColors;
301  }
302 
303  bool ok;
304  QString name;
305  QFile sourceFile( sourceFilePath );
306  return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok, name );
307 }
308 
309 bool QgsGplColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
310 {
311  Q_UNUSED( context );
312  Q_UNUSED( baseColor );
313 
314  QString destFilePath = gplFilePath();
315  if ( destFilePath.isEmpty() )
316  {
317  return false;
318  }
319 
320  QFile destFile( destFilePath );
321  return QgsSymbolLayerV2Utils::saveColorsToGpl( destFile, schemeName(), colors );
322 }
323 
324 
325 //
326 // QgsUserColorScheme
327 //
328 
331  , mFilename( filename )
332 {
333  QFile sourceFile( gplFilePath() );
334 
335  //read in name
336  if ( sourceFile.open( QIODevice::ReadOnly ) )
337  {
338  QTextStream in( &sourceFile );
339 
340  //find name line
341  QString line;
342  while ( !in.atEnd() && !line.startsWith( "Name:" ) )
343  {
344  line = in.readLine();
345  }
346  if ( !in.atEnd() )
347  {
348  QRegExp rx( "Name:\\s*(\\S.*)$" );
349  if ( rx.indexIn( line ) != -1 )
350  {
351  mName = rx.cap( 1 );
352  }
353  }
354  }
355  if ( mName.isEmpty() )
356  {
357  mName = mFilename;
358  }
359 }
360 
362 {
363 
364 }
365 
367 {
368  return mName;
369 }
370 
372 {
373  return new QgsUserColorScheme( mFilename );
374 }
375 
376 QgsColorScheme::SchemeFlags QgsUserColorScheme::flags() const
377 {
378  QgsColorScheme::SchemeFlags f = QgsGplColorScheme::flags();
379 
380  QSettings s;
381  QStringList showInMenuSchemes = s.value( QString( "/colors/showInMenuList" ) ).toStringList();
382 
383  if ( showInMenuSchemes.contains( mName ) )
384  {
386  }
387 
388  return f;
389 }
390 
392 {
393  QString filePath = gplFilePath();
394  if ( filePath.isEmpty() )
395  {
396  return false;
397  }
398 
399  //try to erase gpl file
400  return QFile::remove( filePath );
401 }
402 
404 {
405  QSettings s;
406  QStringList showInMenuSchemes = s.value( QString( "/colors/showInMenuList" ) ).toStringList();
407 
408  if ( show && !showInMenuSchemes.contains( mName ) )
409  {
410  showInMenuSchemes << mName;
411  }
412  else if ( !show && showInMenuSchemes.contains( mName ) )
413  {
414  showInMenuSchemes.removeAll( mName );
415  }
416 
417  s.setValue( "/colors/showInMenuList", showInMenuSchemes );
418 }
419 
421 {
422  QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
423 
424  QDir localDir;
425  if ( !localDir.mkpath( palettesDir ) )
426  {
427  return QString();
428  }
429 
430  return QDir( palettesDir ).filePath( mFilename );
431 }
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 ...
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QString cap(int nth) const
virtual ~QgsColorScheme()
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.
QString readLine(qint64 maxlen)
QString name() const
virtual ~QgsRecentColorScheme()
int length() const
bool remove()
virtual SchemeFlags flags() const
Returns the current flags for the color scheme.
QList< QVariant > toList() const
Abstract base class for color schemes.
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
static QString encodeColor(const QColor &color)
int value() const
const T & at(int i) const
void removeAt(int i)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QString filePath(const QString &fileName) const
QgsRecentColorScheme * clone() const override
Clones a color scheme.
T value() const
void setAlpha(int alpha)
virtual QgsColorScheme::SchemeFlags flags() const override
Returns the current flags for the color scheme.
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QgsProjectColorScheme * clone() const override
Clones a color scheme.
void setValue(const QString &key, const QVariant &value)
int indexIn(const QString &str, int offset, CaretMode caretMode) const
bool writeEntry(const QString &scope, const QString &key, bool value)
int count(const T &value) const
void append(const T &value)
void setShowSchemeInMenu(bool show)
Sets whether a this scheme should be shown in color button menus.
bool atEnd() const
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
bool contains(const QString &key) const
bool isEmpty() const
int removeAll(const T &value)
QgsUserColorScheme(const QString &filename)
Constructs a new user color scheme, using a specified gpl palette file.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual 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.
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
void pop_back()
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
virtual QString gplFilePath() override
Returns the file path for the associated gpl palette file.
iterator end()
A color scheme which contains the most recently used colors.
QVariant value(const QString &key, const QVariant &defaultValue) const
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QStringList toStringList() const
virtual QString schemeName() const override
Gets the name for the color scheme.
virtual QgsUserColorScheme * clone() const override
Clones a color scheme.
virtual QString schemeName() const =0
Gets the name for the color scheme.
virtual 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:382
void prepend(const T &value)
static QColor decodeColor(const QString &str)
const_iterator constEnd() const
virtual ~QgsGplColorScheme()
const_iterator constBegin() const
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
static bool saveColorsToGpl(QFile &file, const QString &paletteName, const QgsNamedColorList &colors)
Exports colors to a gpl GIMP palette file.
iterator begin()
virtual ~QgsUserColorScheme()
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
A color scheme which stores its colors in a gpl palette file.
bool mkpath(const QString &dirPath) const
bool isValid() const