Skip to content

Commit a7a1bee

Browse files
committedSep 17, 2014
Add flags for controlling behaviour of color schemes
1 parent dff326f commit a7a1bee

File tree

8 files changed

+81
-7
lines changed

8 files changed

+81
-7
lines changed
 

‎python/core/qgscolorscheme.sip

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ class QgsColorScheme
1616
#include <qgscolorscheme.h>
1717
%End
1818

19-
public:
19+
public:
20+
21+
/** Flags for controlling behaviour of color scheme
22+
*/
23+
enum SchemeFlag
24+
{
25+
ShowInColorDialog, /*< show scheme in color picker dialog */
26+
ShowInColorButtonMenu, /*< show scheme in color button drop down menu */
27+
ShowInAllContexts /*< show scheme in all contexts */
28+
};
29+
typedef QFlags<QgsColorScheme::SchemeFlag> SchemeFlags;
2030

2131
QgsColorScheme();
2232

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

40+
/**Returns the current flags for the color scheme.
41+
* @returns current flags
42+
*/
43+
virtual SchemeFlags flags() const;
44+
3045
/**Gets a list of colors from the scheme. The colors can optionally
3146
* be generated using the supplied context and base color.
3247
* @param context string specifiying an optional context for the returned
@@ -62,6 +77,8 @@ class QgsColorScheme
6277

6378
}; // class QgsColorScheme
6479

80+
QFlags<QgsColorScheme::SchemeFlag> operator|(QgsColorScheme::SchemeFlag f1, QFlags<QgsColorScheme::SchemeFlag> f2);
81+
6582

6683
/** \ingroup core
6784
* \class QgsGplColorScheme
@@ -146,6 +163,8 @@ class QgsRecentColorScheme : QgsColorScheme
146163

147164
virtual QString schemeName() const;
148165

166+
virtual SchemeFlags flags() const;
167+
149168
virtual QgsNamedColorList fetchColors( const QString context = QString(),
150169
const QColor baseColor = QColor() );
151170

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

172191
virtual QString schemeName() const;
173192

193+
virtual SchemeFlags flags() const;
194+
174195
virtual QgsNamedColorList fetchColors( const QString context = QString(),
175196
const QColor baseColor = QColor() );
176197

@@ -199,6 +220,8 @@ class QgsProjectColorScheme : QgsColorScheme
199220

200221
virtual QString schemeName() const;
201222

223+
virtual SchemeFlags flags() const;
224+
202225
virtual QgsNamedColorList fetchColors( const QString context = QString(),
203226
const QColor baseColor = QColor() );
204227

‎python/core/qgscolorschemeregistry.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ class QgsColorSchemeRegistry
5555
* @returns list of color schemes
5656
*/
5757
QList<QgsColorScheme *> schemes() const;
58+
59+
/**Returns all color schemes in the registry which have a specified flag set
60+
* @param flag flag to match
61+
* @returns list of color schemes with flag set
62+
*/
63+
QList<QgsColorScheme *> schemes( const QgsColorScheme::SchemeFlag flag ) const;
5864

5965
}; // class QgsColorSchemeRegistry

‎src/core/qgscolorscheme.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ QgsUserColorScheme::QgsUserColorScheme( const QString filename )
309309
}
310310
if ( !in.atEnd() )
311311
{
312-
QRegExp rx( "Name:\\s*(.*)$" );
312+
QRegExp rx( "Name:\\s*(\\S.*)$" );
313313
if ( rx.indexIn( line ) != -1 )
314314
{
315315
mName = rx.cap( 1 );

‎src/core/qgscolorscheme.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class CORE_EXPORT QgsColorScheme
4040
{
4141
public:
4242

43+
/** Flags for controlling behaviour of color scheme
44+
*/
45+
enum SchemeFlag
46+
{
47+
ShowInColorDialog = 0x01, /*< show scheme in color picker dialog */
48+
ShowInColorButtonMenu = 0x02, /*< show scheme in color button drop down menu */
49+
ShowInAllContexts = ShowInColorDialog | ShowInColorButtonMenu /*< show scheme in all contexts */
50+
};
51+
Q_DECLARE_FLAGS( SchemeFlags, SchemeFlag )
52+
4353
QgsColorScheme();
4454

4555
virtual ~QgsColorScheme();
@@ -49,6 +59,11 @@ class CORE_EXPORT QgsColorScheme
4959
*/
5060
virtual QString schemeName() const = 0;
5161

62+
/**Returns the current flags for the color scheme.
63+
* @returns current flags
64+
*/
65+
virtual SchemeFlags flags() const { return ShowInColorDialog; }
66+
5267
/**Gets a list of colors from the scheme. The colors can optionally
5368
* be generated using the supplied context and base color.
5469
* @param context string specifiying an optional context for the returned
@@ -83,6 +98,8 @@ class CORE_EXPORT QgsColorScheme
8398
virtual QgsColorScheme* clone() const = 0;
8499
};
85100

101+
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsColorScheme::SchemeFlags )
102+
86103
/** \ingroup core
87104
* \class QgsGplColorScheme
88105
* \brief A color scheme which stores its colors in a gpl palette file.
@@ -158,6 +175,8 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
158175

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

178+
virtual SchemeFlags flags() const { return ShowInAllContexts; }
179+
161180
virtual QgsNamedColorList fetchColors( const QString context = QString(),
162181
const QColor baseColor = QColor() );
163182

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

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

201+
virtual SchemeFlags flags() const { return ShowInAllContexts; }
202+
182203
virtual QgsNamedColorList fetchColors( const QString context = QString(),
183204
const QColor baseColor = QColor() );
184205

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

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

228+
virtual SchemeFlags flags() const { return ShowInAllContexts; }
229+
207230
virtual QgsNamedColorList fetchColors( const QString context = QString(),
208231
const QColor baseColor = QColor() );
209232

‎src/core/qgscolorschemeregistry.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes() const
111111
return allSchemes;
112112
}
113113

114+
QList<QgsColorScheme *> QgsColorSchemeRegistry::schemes( const QgsColorScheme::SchemeFlag flag ) const
115+
{
116+
QList< QgsColorScheme* > matchingSchemes;
117+
QList<QgsColorScheme*>::const_iterator schemeIt;
118+
for ( schemeIt = mColorSchemeList.constBegin(); schemeIt != mColorSchemeList.constEnd(); ++schemeIt )
119+
{
120+
if (( *schemeIt )->flags().testFlag( flag ) )
121+
{
122+
matchingSchemes.append(( *schemeIt ) );
123+
}
124+
}
125+
return matchingSchemes;
126+
}
127+
114128
bool QgsColorSchemeRegistry::removeColorScheme( QgsColorScheme *scheme )
115129
{
116130
if ( mColorSchemeList.indexOf( scheme ) != -1 )

‎src/core/qgscolorschemeregistry.h

100755100644
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
#ifndef QGSCOLORSCHEMEREGISTRY_H
1919
#define QGSCOLORSCHEMEREGISTRY_H
2020

21+
#include "qgscolorscheme.h"
2122
#include <QList>
2223

23-
class QgsColorScheme;
24-
2524
/** \ingroup core
2625
* \class QgsColorSchemeRegistry
2726
* \brief Registry of color schemes
@@ -85,6 +84,12 @@ class CORE_EXPORT QgsColorSchemeRegistry
8584
*/
8685
QList<QgsColorScheme *> schemes() const;
8786

87+
/**Returns all color schemes in the registry which have a specified flag set
88+
* @param flag flag to match
89+
* @returns list of color schemes with flag set
90+
*/
91+
QList<QgsColorScheme *> schemes( const QgsColorScheme::SchemeFlag flag ) const;
92+
8893
/**Return color schemes of a specific type
8994
* @param schemeList destination list for matching schemes
9095
* @note not available in python bindings

‎src/gui/qgscolorbuttonv2.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ void QgsColorButtonV2::prepareMenu()
405405

406406
if ( mColorSchemeRegistry )
407407
{
408-
QList< QgsColorScheme* > schemeList = mColorSchemeRegistry->schemes();
408+
//get schemes with ShowInColorButtonMenu flag set
409+
QList< QgsColorScheme* > schemeList = mColorSchemeRegistry->schemes( QgsColorScheme::ShowInColorButtonMenu );
409410
QList< QgsColorScheme* >::iterator it = schemeList.begin();
410411
for ( ; it != schemeList.end(); ++it )
411412
{

‎src/gui/qgscolordialog.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
7979
mSchemeList->header()->hide();
8080
mSchemeList->setColumnWidth( 0, 44 );
8181

82-
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes();
82+
//get schemes with ShowInColorDialog set
83+
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
8384
QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
8485
for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
8586
{
@@ -418,7 +419,8 @@ void QgsColorDialogV2::schemeIndexChanged( int index )
418419
mSchemeList->saveColorsToScheme();
419420
}
420421

421-
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes();
422+
//get schemes with ShowInColorDialog set
423+
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
422424
if ( index >= schemeList.length() )
423425
{
424426
return;

0 commit comments

Comments
 (0)
Please sign in to comment.