QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QSettings>
21 #include "qgsproject.h"
22 #include "qgssymbollayerv2utils.h"
23 #include "qgsapplication.h"
24 #include "qgssymbollayerv2utils.h"
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 
61 QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString &context, const QColor &baseColor )
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  foreach ( 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 
95 QgsNamedColorList QgsCustomColorScheme::fetchColors( const QString &context, const QColor &baseColor )
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 
154  QgsNamedColorList::const_iterator colorIt = colors.constBegin();
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 
183 QgsNamedColorList QgsProjectColorScheme::fetchColors( const QString &context, const QColor &baseColor )
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  {
198  QColor color = QgsSymbolLayerV2Utils::decodeColor( *it );
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 
221  QgsNamedColorList::const_iterator colorIt = colors.constBegin();
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 
255 QgsNamedColorList QgsGplColorScheme::fetchColors( const QString &context, const QColor &baseColor )
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 
293 QgsUserColorScheme::QgsUserColorScheme( const QString &filename )
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 }