Skip to content

Commit dff326f

Browse files
committed
Load gpl files from user folder to schemes
1 parent e6a259d commit dff326f

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed

python/core/qgscolorscheme.sip

+65
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,71 @@ class QgsColorScheme
6262

6363
}; // class QgsColorScheme
6464

65+
66+
/** \ingroup core
67+
* \class QgsGplColorScheme
68+
* \brief A color scheme which stores its colors in a gpl palette file.
69+
* \note Added in version 2.5
70+
*/
71+
class QgsGplColorScheme : QgsColorScheme
72+
{
73+
%TypeHeaderCode
74+
#include <qgscolorscheme.h>
75+
%End
76+
77+
public:
78+
79+
QgsGplColorScheme();
80+
81+
virtual ~QgsGplColorScheme();
82+
83+
virtual QgsNamedColorList fetchColors( const QString context = QString(),
84+
const QColor baseColor = QColor() );
85+
86+
virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );
87+
88+
protected:
89+
90+
/**Returns the file path for the associated gpl palette file
91+
* @returns gpl file path
92+
*/
93+
virtual QString gplFilePath() = 0;
94+
95+
};
96+
97+
/** \ingroup core
98+
* \class QgsUserColorScheme
99+
* \brief A color scheme which stores its colors in a gpl palette file within the "palettes"
100+
* subfolder off the user's QGIS settings folder.
101+
* \note Added in version 2.5
102+
*/
103+
class QgsUserColorScheme : QgsGplColorScheme
104+
{
105+
%TypeHeaderCode
106+
#include <qgscolorscheme.h>
107+
%End
108+
109+
public:
110+
111+
/**Constructs a new user color scheme, using a specified gpl palette file
112+
* @param filename filename of gpl palette file stored in the users "palettes" folder
113+
*/
114+
QgsUserColorScheme( const QString filename );
115+
116+
virtual ~QgsUserColorScheme();
117+
118+
virtual QString schemeName() const;
119+
120+
virtual QgsColorScheme* clone() const;
121+
122+
virtual bool isEditable() const;
123+
124+
protected:
125+
126+
virtual QString gplFilePath();
127+
128+
};
129+
65130
/** \ingroup core
66131
* \class QgsRecentColorScheme
67132
* \brief A color scheme which contains the most recently used colors.

src/core/qgscolorscheme.cpp

+116
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <QSettings>
2121
#include "qgsproject.h"
2222
#include "qgssymbollayerv2utils.h"
23+
#include "qgsapplication.h"
24+
#include "qgssymbollayerv2utils.h"
25+
#include <QDir>
2326

2427
QgsColorScheme::QgsColorScheme()
2528
{
@@ -233,3 +236,116 @@ QgsColorScheme *QgsProjectColorScheme::clone() const
233236
{
234237
return new QgsProjectColorScheme();
235238
}
239+
240+
241+
//
242+
// QgsGplColorScheme
243+
//
244+
245+
QgsGplColorScheme::QgsGplColorScheme()
246+
: QgsColorScheme()
247+
{
248+
249+
}
250+
251+
QgsGplColorScheme::~QgsGplColorScheme()
252+
{
253+
254+
}
255+
256+
QgsNamedColorList QgsGplColorScheme::fetchColors( const QString context, const QColor baseColor )
257+
{
258+
Q_UNUSED( context );
259+
Q_UNUSED( baseColor );
260+
261+
QString sourceFilePath = gplFilePath();
262+
if ( sourceFilePath.isEmpty() )
263+
{
264+
QgsNamedColorList noColors;
265+
return noColors;
266+
}
267+
268+
bool ok;
269+
QFile sourceFile( sourceFilePath );
270+
return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok );
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 )
294+
: QgsGplColorScheme()
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*(.*)$" );
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+
325+
QgsUserColorScheme::~QgsUserColorScheme()
326+
{
327+
328+
}
329+
330+
QString QgsUserColorScheme::schemeName() const
331+
{
332+
return mName;
333+
}
334+
335+
QgsColorScheme *QgsUserColorScheme::clone() const
336+
{
337+
return new QgsUserColorScheme( mFilename );
338+
}
339+
340+
QString QgsUserColorScheme::gplFilePath()
341+
{
342+
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
343+
344+
QDir localDir;
345+
if ( !localDir.mkpath( palettesDir ) )
346+
{
347+
return QString();
348+
}
349+
350+
return QDir( palettesDir ).filePath( mFilename );
351+
}

src/core/qgscolorscheme.h

+60
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,66 @@ class CORE_EXPORT QgsColorScheme
8383
virtual QgsColorScheme* clone() const = 0;
8484
};
8585

86+
/** \ingroup core
87+
* \class QgsGplColorScheme
88+
* \brief A color scheme which stores its colors in a gpl palette file.
89+
* \note Added in version 2.5
90+
*/
91+
class CORE_EXPORT QgsGplColorScheme : public QgsColorScheme
92+
{
93+
public:
94+
95+
QgsGplColorScheme();
96+
97+
virtual ~QgsGplColorScheme();
98+
99+
virtual QgsNamedColorList fetchColors( const QString context = QString(),
100+
const QColor baseColor = QColor() );
101+
102+
virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );
103+
104+
protected:
105+
106+
/**Returns the file path for the associated gpl palette file
107+
* @returns gpl file path
108+
*/
109+
virtual QString gplFilePath() = 0;
110+
111+
};
112+
113+
/** \ingroup core
114+
* \class QgsUserColorScheme
115+
* \brief A color scheme which stores its colors in a gpl palette file within the "palettes"
116+
* subfolder off the user's QGIS settings folder.
117+
* \note Added in version 2.5
118+
*/
119+
class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
120+
{
121+
public:
122+
123+
/**Constructs a new user color scheme, using a specified gpl palette file
124+
* @param filename filename of gpl palette file stored in the users "palettes" folder
125+
*/
126+
QgsUserColorScheme( const QString filename );
127+
128+
virtual ~QgsUserColorScheme();
129+
130+
virtual QString schemeName() const;
131+
132+
virtual QgsColorScheme* clone() const;
133+
134+
virtual bool isEditable() const { return true; }
135+
136+
protected:
137+
138+
QString mName;
139+
140+
QString mFilename;
141+
142+
virtual QString gplFilePath();
143+
144+
};
145+
86146
/** \ingroup core
87147
* \class QgsRecentColorScheme
88148
* \brief A color scheme which contains the most recently used colors.

src/core/qgscolorschemeregistry.cpp

100755100644
+24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include "qgscolorschemeregistry.h"
1919
#include "qgscolorscheme.h"
20+
#include "qgsapplication.h"
21+
#include <QDir>
22+
#include <QFileInfoList>
2023

2124
//
2225
// Static calls to enforce singleton behaviour
@@ -30,6 +33,8 @@ QgsColorSchemeRegistry *QgsColorSchemeRegistry::instance()
3033

3134
//add default color schemes
3235
mInstance->addDefaultSchemes();
36+
//add user schemes
37+
mInstance->addUserSchemes();
3338
}
3439

3540
return mInstance;
@@ -69,6 +74,25 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
6974
addColorScheme( new QgsRecentColorScheme() );
7075
addColorScheme( new QgsCustomColorScheme() );
7176
addColorScheme( new QgsProjectColorScheme() );
77+
78+
}
79+
80+
void QgsColorSchemeRegistry::addUserSchemes()
81+
{
82+
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
83+
84+
QDir localDir;
85+
if ( !localDir.mkpath( palettesDir ) )
86+
{
87+
return;
88+
}
89+
90+
QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( "*.gpl" ), QDir::Files );
91+
QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
92+
for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
93+
{
94+
addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
95+
}
7296
}
7397

7498
void QgsColorSchemeRegistry::addColorScheme( QgsColorScheme *scheme )

src/core/qgscolorschemeregistry.h

+8
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ class CORE_EXPORT QgsColorSchemeRegistry
5454
/**Adds all default color schemes to this color scheme.
5555
* @see populateFromInstance
5656
* @see addColorScheme
57+
* @see addUserSchemes
5758
*/
5859
void addDefaultSchemes();
5960

61+
/**Creates schemes for all gpl palettes in the user's palettes folder.
62+
* @see populateFromInstance
63+
* @see addDefaultSchemes
64+
* @see addColorScheme
65+
*/
66+
void addUserSchemes();
67+
6068
/**Adds a color scheme to the registry. Ownership of the scheme is transferred
6169
* to the registry.
6270
* @param scheme color scheme to add

0 commit comments

Comments
 (0)