QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscolorschemeregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorschemeregistry.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 "qgscolorschemeregistry.h"
19 #include "qgscolorscheme.h"
20 #include "qgsapplication.h"
21 #include <QDir>
22 #include <QFileInfoList>
23 
24 //
25 // Static calls to enforce singleton behaviour
26 //
27 QgsColorSchemeRegistry *QgsColorSchemeRegistry::mInstance = 0;
29 {
30  if ( mInstance == 0 )
31  {
32  mInstance = new QgsColorSchemeRegistry();
33 
34  //add default color schemes
35  mInstance->addDefaultSchemes();
36  //add user schemes
37  mInstance->addUserSchemes();
38  }
39 
40  return mInstance;
41 }
42 
43 //
44 // Main class begins now...
45 //
46 
48 {
49 }
50 
52 {
53  qDeleteAll( mColorSchemeList );
54  mColorSchemeList.clear();
55 }
56 
58 {
59  //get schemes from global instance
61 
62  //add to this scheme registry
63  QList< QgsColorScheme* >::iterator it = schemeList.begin();
64  for ( ; it != schemeList.end(); ++it )
65  {
66  addColorScheme(( *it )->clone() );
67  }
68 }
69 
71 {
72  //default color schemes
76 
77 }
78 
80 {
81  QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
82 
83  QDir localDir;
84  if ( !localDir.mkpath( palettesDir ) )
85  {
86  return;
87  }
88 
89  QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( "*.gpl" ), QDir::Files );
90  QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
91  for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
92  {
93  addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
94  }
95 }
96 
98 {
99  mColorSchemeList.append( scheme );
100 }
101 
103 {
104  QList< QgsColorScheme* > allSchemes;
106  for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
107  {
108  allSchemes.append(( *schemeIt ) );
109  }
110  return allSchemes;
111 }
112 
114 {
115  QList< QgsColorScheme* > matchingSchemes;
117  for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
118  {
119  if (( *schemeIt )->flags().testFlag( flag ) )
120  {
121  matchingSchemes.append(( *schemeIt ) );
122  }
123  }
124  return matchingSchemes;
125 }
126 
128 {
129  if ( mColorSchemeList.indexOf( scheme ) != -1 )
130  {
131  mColorSchemeList.removeAll( scheme );
132  return true;
133  }
134 
135  //not found
136  return false;
137 }
138 
139 
void populateFromInstance()
Adds all color schemes from the global instance to this color scheme.
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 th...
void clear()
void addDefaultSchemes()
Adds all default color schemes to this color scheme.
A color scheme which contains project specific colors set through project properties dialog...
Registry of color schemes.
Abstract base class for color schemes.
bool removeColorScheme(QgsColorScheme *scheme)
Removes all matching color schemes from the registry.
static const QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
int indexOf(const T &value, int from) const
void append(const T &value)
void addUserSchemes()
Creates schemes for all gpl palettes in the user's palettes folder.
QFileInfoList entryInfoList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
int removeAll(const T &value)
QgsColorSchemeRegistry()
Constructor for an empty color scheme registry.
void addColorScheme(QgsColorScheme *scheme)
Adds a color scheme to the registry.
iterator end()
A color scheme which contains the most recently used colors.
static QgsColorSchemeRegistry * instance()
Returns the global instance pointer, creating the object on the first call.
const_iterator constEnd() const
const_iterator constBegin() const
SchemeFlag
Flags for controlling behaviour of color scheme.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
iterator begin()
bool mkpath(const QString &dirPath) const