QGIS API Documentation  3.0.2-Girona (307d082)
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 
26 {
27  qDeleteAll( mColorSchemeList );
28  mColorSchemeList.clear();
29 }
30 
32 {
33  //get schemes from global instance
34  QList< QgsColorScheme * > schemeList = QgsApplication::colorSchemeRegistry()->schemes();
35 
36  //add to this scheme registry
37  QList< QgsColorScheme * >::iterator it = schemeList.begin();
38  for ( ; it != schemeList.end(); ++it )
39  {
40  addColorScheme( ( *it )->clone() );
41  }
42 }
43 
45 {
46  //default color schemes
51 }
52 
54 {
55  QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
56 
57  QDir localDir;
58  if ( !localDir.mkpath( palettesDir ) )
59  {
60  return;
61  }
62 
63  QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( QStringLiteral( "*.gpl" ) ), QDir::Files );
64  QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
65  for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
66  {
67  addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
68  }
69 }
70 
72 {
73  mColorSchemeList.append( scheme );
74 }
75 
76 QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
77 {
78  QList< QgsColorScheme * > allSchemes;
79  QList<QgsColorScheme *>::const_iterator schemeIt;
80  for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
81  {
82  allSchemes.append( ( *schemeIt ) );
83  }
84  return allSchemes;
85 }
86 
87 QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
88 {
89  QList< QgsColorScheme * > matchingSchemes;
90  QList<QgsColorScheme *>::const_iterator schemeIt;
91  for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
92  {
93  if ( ( *schemeIt )->flags().testFlag( flag ) )
94  {
95  matchingSchemes.append( ( *schemeIt ) );
96  }
97  }
98  return matchingSchemes;
99 }
100 
102 {
103  if ( mColorSchemeList.indexOf( scheme ) != -1 )
104  {
105  mColorSchemeList.removeAll( scheme );
106  return true;
107  }
108 
109  //not found
110  return false;
111 }
112 
113 
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 the ...
void addDefaultSchemes()
Adds all default color schemes to this color scheme.
A color scheme which contains project specific colors set through project properties dialog...
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
Abstract base class for color schemes.
bool removeColorScheme(QgsColorScheme *scheme)
Removes all matching color schemes from the registry.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
void addUserSchemes()
Creates schemes for all gpl palettes in the user&#39;s palettes folder.
void addColorScheme(QgsColorScheme *scheme)
Adds a color scheme to the registry.
A color scheme which contains the most recently used colors.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application&#39;s color scheme registry, used for managing color schemes. ...
SchemeFlag
Flags for controlling behavior of color scheme.