QGIS API Documentation  2.12.0-Lyon
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 
84 
86 {
87 
88 }
89 
91 {
92 
93 }
94 
96 {
97  Q_UNUSED( context );
98  Q_UNUSED( baseColor );
99 
100  //fetch predefined custom colors
101  QgsNamedColorList colorList;
102  QSettings settings;
103 
104  //check if settings contains custom palette
105  if ( !settings.contains( QString( "/colors/palettecolors" ) ) )
106  {
107  //no custom palette, return default colors
108  colorList.append( qMakePair( QColor( "#000000" ), QString() ) );
109  colorList.append( qMakePair( QColor( "#ffffff" ), QString() ) );
110  colorList.append( qMakePair( QColor( "#a6cee3" ), QString() ) );
111  colorList.append( qMakePair( QColor( "#1f78b4" ), QString() ) );
112  colorList.append( qMakePair( QColor( "#b2df8a" ), QString() ) );
113  colorList.append( qMakePair( QColor( "#33a02c" ), QString() ) );
114  colorList.append( qMakePair( QColor( "#fb9a99" ), QString() ) );
115  colorList.append( qMakePair( QColor( "#e31a1c" ), QString() ) );
116  colorList.append( qMakePair( QColor( "#fdbf6f" ), QString() ) );
117  colorList.append( qMakePair( QColor( "#ff7f00" ), QString() ) );
118 
119  return colorList;
120  }
121 
122  QList< QVariant > customColorVariants = settings.value( QString( "/colors/palettecolors" ) ).toList();
123  QList< QVariant > customColorLabels = settings.value( QString( "/colors/palettelabels" ) ).toList();
124 
125  //generate list from custom colors
126  int colorIndex = 0;
127  for ( QList< QVariant >::iterator it = customColorVariants.begin();
128  it != customColorVariants.end(); ++it )
129  {
130  QColor color = ( *it ).value<QColor>();
131  QString label;
132  if ( customColorLabels.length() > colorIndex )
133  {
134  label = customColorLabels.at( colorIndex ).toString();
135  }
136 
137  colorList.append( qMakePair( color, label ) );
138  colorIndex++;
139  }
140 
141  return colorList;
142 }
143 
144 bool QgsCustomColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
145 {
146  Q_UNUSED( context );
147  Q_UNUSED( baseColor );
148 
149  // save colors to settings
150  QSettings settings;
151  QList< QVariant > customColors;
152  QList< QVariant > customColorLabels;
153 
155  for ( ; colorIt != colors.constEnd(); ++colorIt )
156  {
157  QVariant color = ( *colorIt ).first;
158  QVariant label = ( *colorIt ).second;
159  customColors.append( color );
160  customColorLabels.append( label );
161  }
162  settings.setValue( QString( "/colors/palettecolors" ), customColors );
163  settings.setValue( QString( "/colors/palettelabels" ), customColorLabels );
164  return true;
165 }
166 
168 {
169  return new QgsCustomColorScheme();
170 }
171 
172 
174 {
175 
176 }
177 
179 {
180 
181 }
182 
184 {
185  Q_UNUSED( context );
186  Q_UNUSED( baseColor );
187 
188  QgsNamedColorList colorList;
189 
190  QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
191  QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );
192 
193  //generate list from custom colors
194  int colorIndex = 0;
195  for ( QStringList::iterator it = colorStrings.begin();
196  it != colorStrings.end(); ++it )
197  {
199  QString label;
200  if ( colorLabels.length() > colorIndex )
201  {
202  label = colorLabels.at( colorIndex );
203  }
204 
205  colorList.append( qMakePair( color, label ) );
206  colorIndex++;
207  }
208 
209  return colorList;
210 }
211 
212 bool QgsProjectColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
213 {
214  Q_UNUSED( context );
215  Q_UNUSED( baseColor );
216 
217  // save colors to project
218  QStringList customColors;
219  QStringList customColorLabels;
220 
222  for ( ; colorIt != colors.constEnd(); ++colorIt )
223  {
224  QString color = QgsSymbolLayerV2Utils::encodeColor(( *colorIt ).first );
225  QString label = ( *colorIt ).second;
226  customColors.append( color );
227  customColorLabels.append( label );
228  }
229  QgsProject::instance()->writeEntry( "Palette", "/Colors", customColors );
230  QgsProject::instance()->writeEntry( "Palette", "/Labels", customColorLabels );
231  return true;
232 }
233 
235 {
236  return new QgsProjectColorScheme();
237 }
238 
239 
240 //
241 // QgsGplColorScheme
242 //
243 
245  : QgsColorScheme()
246 {
247 
248 }
249 
251 {
252 
253 }
254 
256 {
257  Q_UNUSED( context );
258  Q_UNUSED( baseColor );
259 
260  QString sourceFilePath = gplFilePath();
261  if ( sourceFilePath.isEmpty() )
262  {
263  QgsNamedColorList noColors;
264  return noColors;
265  }
266 
267  bool ok;
268  QString name;
269  QFile sourceFile( sourceFilePath );
270  return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok, name );
271 }
272 
273 bool QgsGplColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
274 {
275  Q_UNUSED( context );
276  Q_UNUSED( baseColor );
277 
278  QString destFilePath = gplFilePath();
279  if ( destFilePath.isEmpty() )
280  {
281  return false;
282  }
283 
284  QFile destFile( destFilePath );
285  return QgsSymbolLayerV2Utils::saveColorsToGpl( destFile, schemeName(), colors );
286 }
287 
288 
289 //
290 // QgsUserColorScheme
291 //
292 
295  , mFilename( filename )
296 {
297  QFile sourceFile( gplFilePath() );
298 
299  //read in name
300  if ( sourceFile.open( QIODevice::ReadOnly ) )
301  {
302  QTextStream in( &sourceFile );
303 
304  //find name line
305  QString line;
306  while ( !in.atEnd() && !line.startsWith( "Name:" ) )
307  {
308  line = in.readLine();
309  }
310  if ( !in.atEnd() )
311  {
312  QRegExp rx( "Name:\\s*(\\S.*)$" );
313  if ( rx.indexIn( line ) != -1 )
314  {
315  mName = rx.cap( 1 );
316  }
317  }
318  }
319  if ( mName.isEmpty() )
320  {
321  mName = mFilename;
322  }
323 }
324 
326 {
327 
328 }
329 
331 {
332  return mName;
333 }
334 
336 {
337  return new QgsUserColorScheme( mFilename );
338 }
339 
341 {
342  QString filePath = gplFilePath();
343  if ( filePath.isEmpty() )
344  {
345  return false;
346  }
347 
348  //try to erase gpl file
349  return QFile::remove( filePath );
350 }
351 
353 {
354  QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
355 
356  QDir localDir;
357  if ( !localDir.mkpath( palettesDir ) )
358  {
359  return QString();
360  }
361 
362  return QDir( palettesDir ).filePath( mFilename );
363 }
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()
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's home dir.
QString readLine(qint64 maxlen)
virtual ~QgsRecentColorScheme()
int length() const
bool remove()
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
virtual QgsColorScheme * clone() const override
Clones a color scheme.
const T & at(int i) const
QString filePath(const QString &fileName) const
T value() const
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QgsColorScheme * clone() const override
Clones a color scheme.
QgsColorScheme * 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
virtual ~QgsCustomColorScheme()
bool writeEntry(const QString &scope, const QString &key, bool value)
void append(const T &value)
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
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.
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual QString gplFilePath() override
Returns the file path for the associated gpl palette file.
iterator end()
QgsColorScheme * clone() const override
Clones a color scheme.
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.
virtual QString schemeName() const override
Gets the name for the 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()
access to canonical QgsProject instance
Definition: qgsproject.cpp:353
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
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=0) const
Key value accessors.