Skip to content

Commit

Permalink
Add method for editing color scheme colors
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 16, 2014
1 parent ff28fe9 commit f7f4c74
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
22 changes: 22 additions & 0 deletions python/core/qgscolorscheme.sip
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ class QgsColorScheme
virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() ) = 0;

/**Returns whether the color scheme is editable
* @returns true if scheme is editable
* @see setColors
*/
virtual bool isEditable() const;

/**Sets the colors for the scheme. This method is only valid for editable color schemes.
* @param colors list of colors for the scheme
* @param context to set colors for
* @param baseColor base color to set colors for
* @returns true if colors were set successfully
* @see isEditable
*/
virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );

/**Clones a color scheme
* @returns copy of color scheme
*/
virtual QgsColorScheme* clone() const = 0 /Factory/;

}; // class QgsColorScheme
Expand Down Expand Up @@ -90,6 +108,10 @@ class QgsCustomColorScheme : QgsColorScheme

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

virtual bool isEditable() const;

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

QgsColorScheme* clone() const /Factory/;
}; // class QgsCustomColorScheme
34 changes: 33 additions & 1 deletion src/core/qgscolorscheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ QgsColorScheme::~QgsColorScheme()

}

bool QgsColorScheme::setColors( const QgsNamedColorList colors, const QString context, const QColor baseColor )
{
//base implementation does nothing
Q_UNUSED( colors );
Q_UNUSED( context );
Q_UNUSED( baseColor );
return false;
}


//
// QgsRecentColorScheme
Expand Down Expand Up @@ -90,7 +99,7 @@ QgsNamedColorList QgsCustomColorScheme::fetchColors( const QString context, cons
QSettings settings;

//check if settings contains custom palette
if ( !settings.contains( QString( "/colors/ypalettecolors" ) ) )
if ( !settings.contains( QString( "/colors/palettecolors" ) ) )
{
//no custom palette, return default colors
colorList.append( qMakePair( QColor( "#000000" ), QString() ) );
Expand Down Expand Up @@ -129,6 +138,29 @@ QgsNamedColorList QgsCustomColorScheme::fetchColors( const QString context, cons
return colorList;
}

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

// save colors to settings
QSettings settings;
QList< QVariant > customColors;
QList< QVariant > customColorLabels;

QgsNamedColorList::const_iterator colorIt = colors.constBegin();
for ( ; colorIt != colors.constEnd(); ++colorIt )
{
QVariant color = ( *colorIt ).first;
QVariant label = ( *colorIt ).second;
customColors.append( color );
customColorLabels.append( label );
}
settings.setValue( QString( "/colors/palettecolors" ), customColors );
settings.setValue( QString( "/colors/palettelabels" ), customColorLabels );
return true;
}

QgsColorScheme *QgsCustomColorScheme::clone() const
{
return new QgsCustomColorScheme();
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgscolorscheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ class CORE_EXPORT QgsColorScheme
virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() ) = 0;

/**Returns whether the color scheme is editable
* @returns true if scheme is editable
* @see setColors
*/
virtual bool isEditable() const { return false; }

/**Sets the colors for the scheme. This method is only valid for editable color schemes.
* @param colors list of colors for the scheme
* @param context to set colors for
* @param baseColor base color to set colors for
* @returns true if colors were set successfully
* @see isEditable
*/
virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );

/**Clones a color scheme
* @returns copy of color scheme
*/
Expand Down Expand Up @@ -108,6 +123,10 @@ class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

virtual bool isEditable() const { return true; }

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

QgsColorScheme* clone() const;
};

Expand Down

0 comments on commit f7f4c74

Please sign in to comment.