QGIS API Documentation  2.8.2-Wien
 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
60  QList< QgsColorScheme* > schemeList = QgsColorSchemeRegistry::instance()->schemes();
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 
102 QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
103 {
104  QList< QgsColorScheme* > allSchemes;
105  QList<QgsColorScheme*>::const_iterator schemeIt;
106  for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
107  {
108  allSchemes.append(( *schemeIt ) );
109  }
110  return allSchemes;
111 }
112 
113 QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
114 {
115  QList< QgsColorScheme* > matchingSchemes;
116  QList<QgsColorScheme*>::const_iterator schemeIt;
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