Skip to content

Commit

Permalink
Load gpl files from user folder to schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent e6a259d commit dff326f
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 0 deletions.
65 changes: 65 additions & 0 deletions python/core/qgscolorscheme.sip
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,71 @@ class QgsColorScheme

}; // class QgsColorScheme


/** \ingroup core
* \class QgsGplColorScheme
* \brief A color scheme which stores its colors in a gpl palette file.
* \note Added in version 2.5
*/
class QgsGplColorScheme : QgsColorScheme
{
%TypeHeaderCode
#include <qgscolorscheme.h>
%End

public:

QgsGplColorScheme();

virtual ~QgsGplColorScheme();

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );

protected:

/**Returns the file path for the associated gpl palette file
* @returns gpl file path
*/
virtual QString gplFilePath() = 0;

};

/** \ingroup core
* \class QgsUserColorScheme
* \brief A color scheme which stores its colors in a gpl palette file within the "palettes"
* subfolder off the user's QGIS settings folder.
* \note Added in version 2.5
*/
class QgsUserColorScheme : QgsGplColorScheme
{
%TypeHeaderCode
#include <qgscolorscheme.h>
%End

public:

/**Constructs a new user color scheme, using a specified gpl palette file
* @param filename filename of gpl palette file stored in the users "palettes" folder
*/
QgsUserColorScheme( const QString filename );

virtual ~QgsUserColorScheme();

virtual QString schemeName() const;

virtual QgsColorScheme* clone() const;

virtual bool isEditable() const;

protected:

virtual QString gplFilePath();

};

/** \ingroup core
* \class QgsRecentColorScheme
* \brief A color scheme which contains the most recently used colors.
Expand Down
116 changes: 116 additions & 0 deletions src/core/qgscolorscheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <QSettings>
#include "qgsproject.h"
#include "qgssymbollayerv2utils.h"
#include "qgsapplication.h"
#include "qgssymbollayerv2utils.h"
#include <QDir>

QgsColorScheme::QgsColorScheme()
{
Expand Down Expand Up @@ -233,3 +236,116 @@ QgsColorScheme *QgsProjectColorScheme::clone() const
{
return new QgsProjectColorScheme();
}


//
// QgsGplColorScheme
//

QgsGplColorScheme::QgsGplColorScheme()
: QgsColorScheme()
{

}

QgsGplColorScheme::~QgsGplColorScheme()
{

}

QgsNamedColorList QgsGplColorScheme::fetchColors( const QString context, const QColor baseColor )
{
Q_UNUSED( context );
Q_UNUSED( baseColor );

QString sourceFilePath = gplFilePath();
if ( sourceFilePath.isEmpty() )
{
QgsNamedColorList noColors;
return noColors;
}

bool ok;
QFile sourceFile( sourceFilePath );
return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok );
}

bool QgsGplColorScheme::setColors( const QgsNamedColorList colors, const QString context, const QColor baseColor )
{
Q_UNUSED( context );
Q_UNUSED( baseColor );

QString destFilePath = gplFilePath();
if ( destFilePath.isEmpty() )
{
return false;
}

QFile destFile( destFilePath );
return QgsSymbolLayerV2Utils::saveColorsToGpl( destFile, schemeName(), colors );
}


//
// QgsUserColorScheme
//

QgsUserColorScheme::QgsUserColorScheme( const QString filename )
: QgsGplColorScheme()
, mFilename( filename )
{
QFile sourceFile( gplFilePath() );

//read in name
if ( sourceFile.open( QIODevice::ReadOnly ) )
{
QTextStream in( &sourceFile );

//find name line
QString line;
while ( !in.atEnd() && !line.startsWith( "Name:" ) )
{
line = in.readLine();
}
if ( !in.atEnd() )
{
QRegExp rx( "Name:\\s*(.*)$" );
if ( rx.indexIn( line ) != -1 )
{
mName = rx.cap( 1 );
}
}
}
if ( mName.isEmpty() )
{
mName = mFilename;
}
}

QgsUserColorScheme::~QgsUserColorScheme()
{

}

QString QgsUserColorScheme::schemeName() const
{
return mName;
}

QgsColorScheme *QgsUserColorScheme::clone() const
{
return new QgsUserColorScheme( mFilename );
}

QString QgsUserColorScheme::gplFilePath()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";

QDir localDir;
if ( !localDir.mkpath( palettesDir ) )
{
return QString();
}

return QDir( palettesDir ).filePath( mFilename );
}
60 changes: 60 additions & 0 deletions src/core/qgscolorscheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,66 @@ class CORE_EXPORT QgsColorScheme
virtual QgsColorScheme* clone() const = 0;
};

/** \ingroup core
* \class QgsGplColorScheme
* \brief A color scheme which stores its colors in a gpl palette file.
* \note Added in version 2.5
*/
class CORE_EXPORT QgsGplColorScheme : public QgsColorScheme
{
public:

QgsGplColorScheme();

virtual ~QgsGplColorScheme();

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );

protected:

/**Returns the file path for the associated gpl palette file
* @returns gpl file path
*/
virtual QString gplFilePath() = 0;

};

/** \ingroup core
* \class QgsUserColorScheme
* \brief A color scheme which stores its colors in a gpl palette file within the "palettes"
* subfolder off the user's QGIS settings folder.
* \note Added in version 2.5
*/
class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
{
public:

/**Constructs a new user color scheme, using a specified gpl palette file
* @param filename filename of gpl palette file stored in the users "palettes" folder
*/
QgsUserColorScheme( const QString filename );

virtual ~QgsUserColorScheme();

virtual QString schemeName() const;

virtual QgsColorScheme* clone() const;

virtual bool isEditable() const { return true; }

protected:

QString mName;

QString mFilename;

virtual QString gplFilePath();

};

/** \ingroup core
* \class QgsRecentColorScheme
* \brief A color scheme which contains the most recently used colors.
Expand Down
24 changes: 24 additions & 0 deletions src/core/qgscolorschemeregistry.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include "qgscolorschemeregistry.h"
#include "qgscolorscheme.h"
#include "qgsapplication.h"
#include <QDir>
#include <QFileInfoList>

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

//add default color schemes
mInstance->addDefaultSchemes();
//add user schemes
mInstance->addUserSchemes();
}

return mInstance;
Expand Down Expand Up @@ -69,6 +74,25 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
addColorScheme( new QgsRecentColorScheme() );
addColorScheme( new QgsCustomColorScheme() );
addColorScheme( new QgsProjectColorScheme() );

}

void QgsColorSchemeRegistry::addUserSchemes()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";

QDir localDir;
if ( !localDir.mkpath( palettesDir ) )
{
return;
}

QFileInfoList fileInfoList = QDir( palettesDir ).entryInfoList( QStringList( "*.gpl" ), QDir::Files );
QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
{
addColorScheme( new QgsUserColorScheme( infoIt->fileName() ) );
}
}

void QgsColorSchemeRegistry::addColorScheme( QgsColorScheme *scheme )
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgscolorschemeregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ class CORE_EXPORT QgsColorSchemeRegistry
/**Adds all default color schemes to this color scheme.
* @see populateFromInstance
* @see addColorScheme
* @see addUserSchemes
*/
void addDefaultSchemes();

/**Creates schemes for all gpl palettes in the user's palettes folder.
* @see populateFromInstance
* @see addDefaultSchemes
* @see addColorScheme
*/
void addUserSchemes();

/**Adds a color scheme to the registry. Ownership of the scheme is transferred
* to the registry.
* @param scheme color scheme to add
Expand Down

0 comments on commit dff326f

Please sign in to comment.