Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add flags for controlling behaviour of color schemes
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent dff326f commit a7a1bee
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 7 deletions.
25 changes: 24 additions & 1 deletion python/core/qgscolorscheme.sip
Expand Up @@ -16,7 +16,17 @@ class QgsColorScheme
#include <qgscolorscheme.h>
%End

public:
public:

/** Flags for controlling behaviour of color scheme
*/
enum SchemeFlag
{
ShowInColorDialog, /*< show scheme in color picker dialog */
ShowInColorButtonMenu, /*< show scheme in color button drop down menu */
ShowInAllContexts /*< show scheme in all contexts */
};
typedef QFlags<QgsColorScheme::SchemeFlag> SchemeFlags;

QgsColorScheme();

Expand All @@ -27,6 +37,11 @@ class QgsColorScheme
*/
virtual QString schemeName() const = 0;

/**Returns the current flags for the color scheme.
* @returns current flags
*/
virtual SchemeFlags flags() const;

/**Gets a list of colors from the scheme. The colors can optionally
* be generated using the supplied context and base color.
* @param context string specifiying an optional context for the returned
Expand Down Expand Up @@ -62,6 +77,8 @@ class QgsColorScheme

}; // class QgsColorScheme

QFlags<QgsColorScheme::SchemeFlag> operator|(QgsColorScheme::SchemeFlag f1, QFlags<QgsColorScheme::SchemeFlag> f2);


/** \ingroup core
* \class QgsGplColorScheme
Expand Down Expand Up @@ -146,6 +163,8 @@ class QgsRecentColorScheme : QgsColorScheme

virtual QString schemeName() const;

virtual SchemeFlags flags() const;

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

Expand All @@ -171,6 +190,8 @@ class QgsCustomColorScheme : QgsColorScheme

virtual QString schemeName() const;

virtual SchemeFlags flags() const;

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

Expand Down Expand Up @@ -199,6 +220,8 @@ class QgsProjectColorScheme : QgsColorScheme

virtual QString schemeName() const;

virtual SchemeFlags flags() const;

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

Expand Down
6 changes: 6 additions & 0 deletions python/core/qgscolorschemeregistry.sip
Expand Up @@ -55,5 +55,11 @@ class QgsColorSchemeRegistry
* @returns list of color schemes
*/
QList<QgsColorScheme *> schemes() const;

/**Returns all color schemes in the registry which have a specified flag set
* @param flag flag to match
* @returns list of color schemes with flag set
*/
QList<QgsColorScheme *> schemes( const QgsColorScheme::SchemeFlag flag ) const;

}; // class QgsColorSchemeRegistry
2 changes: 1 addition & 1 deletion src/core/qgscolorscheme.cpp
Expand Up @@ -309,7 +309,7 @@ QgsUserColorScheme::QgsUserColorScheme( const QString filename )
}
if ( !in.atEnd() )
{
QRegExp rx( "Name:\\s*(.*)$" );
QRegExp rx( "Name:\\s*(\\S.*)$" );
if ( rx.indexIn( line ) != -1 )
{
mName = rx.cap( 1 );
Expand Down
23 changes: 23 additions & 0 deletions src/core/qgscolorscheme.h
Expand Up @@ -40,6 +40,16 @@ class CORE_EXPORT QgsColorScheme
{
public:

/** Flags for controlling behaviour of color scheme
*/
enum SchemeFlag
{
ShowInColorDialog = 0x01, /*< show scheme in color picker dialog */
ShowInColorButtonMenu = 0x02, /*< show scheme in color button drop down menu */
ShowInAllContexts = ShowInColorDialog | ShowInColorButtonMenu /*< show scheme in all contexts */
};
Q_DECLARE_FLAGS( SchemeFlags, SchemeFlag )

QgsColorScheme();

virtual ~QgsColorScheme();
Expand All @@ -49,6 +59,11 @@ class CORE_EXPORT QgsColorScheme
*/
virtual QString schemeName() const = 0;

/**Returns the current flags for the color scheme.
* @returns current flags
*/
virtual SchemeFlags flags() const { return ShowInColorDialog; }

/**Gets a list of colors from the scheme. The colors can optionally
* be generated using the supplied context and base color.
* @param context string specifiying an optional context for the returned
Expand Down Expand Up @@ -83,6 +98,8 @@ class CORE_EXPORT QgsColorScheme
virtual QgsColorScheme* clone() const = 0;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsColorScheme::SchemeFlags )

/** \ingroup core
* \class QgsGplColorScheme
* \brief A color scheme which stores its colors in a gpl palette file.
Expand Down Expand Up @@ -158,6 +175,8 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme

virtual QString schemeName() const { return QT_TR_NOOP( "Recent colors" ); }

virtual SchemeFlags flags() const { return ShowInAllContexts; }

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

Expand All @@ -179,6 +198,8 @@ class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme

virtual QString schemeName() const { return QT_TR_NOOP( "Standard colors" ); }

virtual SchemeFlags flags() const { return ShowInAllContexts; }

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

Expand All @@ -204,6 +225,8 @@ class CORE_EXPORT QgsProjectColorScheme : public QgsColorScheme

virtual QString schemeName() const { return QT_TR_NOOP( "Project colors" ); }

virtual SchemeFlags flags() const { return ShowInAllContexts; }

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

Expand Down
14 changes: 14 additions & 0 deletions src/core/qgscolorschemeregistry.cpp
Expand Up @@ -111,6 +111,20 @@ QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
return allSchemes;
}

QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
{
QList< QgsColorScheme* > matchingSchemes;
QList<QgsColorScheme*>::const_iterator schemeIt;
for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
{
if (( *schemeIt )->flags().testFlag( flag ) )
{
matchingSchemes.append(( *schemeIt ) );
}
}
return matchingSchemes;
}

bool QgsColorSchemeRegistry::removeColorScheme( QgsColorScheme *scheme )
{
if ( mColorSchemeList.indexOf( scheme ) != -1 )
Expand Down
9 changes: 7 additions & 2 deletions src/core/qgscolorschemeregistry.h 100755 → 100644
Expand Up @@ -18,10 +18,9 @@
#ifndef QGSCOLORSCHEMEREGISTRY_H
#define QGSCOLORSCHEMEREGISTRY_H

#include "qgscolorscheme.h"
#include <QList>

class QgsColorScheme;

/** \ingroup core
* \class QgsColorSchemeRegistry
* \brief Registry of color schemes
Expand Down Expand Up @@ -85,6 +84,12 @@ class CORE_EXPORT QgsColorSchemeRegistry
*/
QList<QgsColorScheme *> schemes() const;

/**Returns all color schemes in the registry which have a specified flag set
* @param flag flag to match
* @returns list of color schemes with flag set
*/
QList<QgsColorScheme *> schemes( const QgsColorScheme::SchemeFlag flag ) const;

/**Return color schemes of a specific type
* @param schemeList destination list for matching schemes
* @note not available in python bindings
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgscolorbuttonv2.cpp
Expand Up @@ -405,7 +405,8 @@ void QgsColorButtonV2::prepareMenu()

if ( mColorSchemeRegistry )
{
QList< QgsColorScheme* > schemeList = mColorSchemeRegistry->schemes();
//get schemes with ShowInColorButtonMenu flag set
QList< QgsColorScheme* > schemeList = mColorSchemeRegistry->schemes( QgsColorScheme::ShowInColorButtonMenu );
QList< QgsColorScheme* >::iterator it = schemeList.begin();
for ( ; it != schemeList.end(); ++it )
{
Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgscolordialog.cpp
Expand Up @@ -79,7 +79,8 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
mSchemeList->header()->hide();
mSchemeList->setColumnWidth( 0, 44 );

QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes();
//get schemes with ShowInColorDialog set
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
{
Expand Down Expand Up @@ -418,7 +419,8 @@ void QgsColorDialogV2::schemeIndexChanged( int index )
mSchemeList->saveColorsToScheme();
}

QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes();
//get schemes with ShowInColorDialog set
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
if ( index >= schemeList.length() )
{
return;
Expand Down

0 comments on commit a7a1bee

Please sign in to comment.