Skip to content

Commit ec50cac

Browse files
committed
[FEATURE] New "preset" colors color ramp option
Allows use of a color ramp consisting of a list of selected colors. Currently there's no way in QGIS to classify a renderer using some list of colors you've previously selected. So you can modify the colors manually after classifying, but that's a pain if you're regularly using the same color scheme. Basically, it's like the color brewer color ramp options but allowing users to pick their own preset list of colors to use* (Because Cynthia Brewer isn't the only cartographic color expert!)
1 parent 8ac1460 commit ec50cac

13 files changed

+793
-3
lines changed

python/core/qgscolorramp.sip

+60
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class QgsColorRamp
1616
sipType = sipType_QgsLimitedRandomColorRamp;
1717
else if (sipCpp->type() == "randomcolors")
1818
sipType = sipType_QgsRandomColorRamp;
19+
else if (sipCpp->type() == "preset")
20+
sipType = sipType_QgsPresetSchemeColorRamp;
1921
else if (sipCpp->type() == "colorbrewer")
2022
sipType = sipType_QgsColorBrewerColorRamp;
2123
else if (sipCpp->type() == "cpt-city")
@@ -285,6 +287,64 @@ class QgsRandomColorRamp : QgsColorRamp
285287
QgsStringMap properties() const;
286288
};
287289

290+
/** \ingroup core
291+
* \class QgsPresetSchemeColorRamp
292+
* \brief A scheme based color ramp consisting of a list of predefined colors.
293+
* \note added in QGIS 3.0
294+
*/
295+
class QgsPresetSchemeColorRamp : QgsColorRamp, QgsColorScheme
296+
{
297+
%TypeHeaderCode
298+
#include <qgscolorramp.h>
299+
%End
300+
301+
public:
302+
303+
/** Constructor for QgsPresetSchemeColorRamp.
304+
* @param colors list of colors in ramp
305+
*/
306+
QgsPresetSchemeColorRamp( const QList< QColor >& colors = QList< QColor >() );
307+
308+
/** Constructor for QgsPresetColorRamp.
309+
* @param colors list of named colors in ramp
310+
* @note not available in Python bindings - use setColors instead
311+
*/
312+
//QgsPresetSchemeColorRamp( const QgsNamedColorList& colors );
313+
314+
/** Returns a new QgsPresetSchemeColorRamp color ramp created using the properties encoded in a string
315+
* map.
316+
* @param properties color ramp properties
317+
* @see properties()
318+
*/
319+
static QgsColorRamp* create( const QgsStringMap& properties = QgsStringMap() );
320+
321+
/** Sets the list of colors used by the ramp.
322+
* @param colors list of colors
323+
* @see colors()
324+
*/
325+
bool setColors( const QgsNamedColorList& colors, const QString& = QString(), const QColor& = QColor() );
326+
327+
/** Returns the list of colors used by the ramp.
328+
* @see setColors()
329+
*/
330+
QList< QColor > colors() const;
331+
332+
// QgsColorRamp interface
333+
virtual double value( int index ) const;
334+
virtual QColor color( double value ) const;
335+
virtual QString type() const;
336+
virtual QgsPresetSchemeColorRamp* clone() const /Factory/;
337+
virtual QgsStringMap properties() const;
338+
int count() const;
339+
340+
// QgsColorScheme interface
341+
QString schemeName() const;
342+
QgsNamedColorList fetchColors( const QString &context = QString(),
343+
const QColor &baseColor = QColor() );
344+
bool isEditable() const;
345+
346+
};
347+
288348
/** \ingroup core
289349
* \class QgsColorBrewerColorRamp
290350
*/

python/gui/gui.sip

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
%Include qgspanelwidgetstack.sip
142142
%Include qgspixmaplabel.sip
143143
%Include qgspluginmanagerinterface.sip
144+
%Include qgspresetcolorrampdialog.sip
144145
%Include qgsprevieweffect.sip
145146
%Include qgsprojectbadlayerguihandler.sip
146147
%Include qgsprojectionselectionwidget.sip
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** \ingroup gui
2+
* \class QgsPresetColorRampWidget
3+
* A widget which allows users to modify the properties of a QgsPresetSchemeColorRamp.
4+
* \note added in QGIS 3.0
5+
*/
6+
7+
class QgsPresetColorRampWidget : QgsPanelWidget
8+
{
9+
%TypeHeaderCode
10+
#include <qgspresetcolorrampdialog.h>
11+
%End
12+
13+
public:
14+
15+
/** Constructor for QgsPresetColorRampWidget.
16+
* @param ramp initial ramp to show in dialog
17+
* @param parent parent widget
18+
*/
19+
QgsPresetColorRampWidget( const QgsPresetSchemeColorRamp& ramp, QWidget* parent /TransferThis/ = nullptr );
20+
21+
~QgsPresetColorRampWidget();
22+
23+
/** Returns a color ramp representing the current settings from the dialog.
24+
* @see setRamp()
25+
*/
26+
QgsPresetSchemeColorRamp ramp() const;
27+
28+
/** Sets the color ramp to show in the dialog.
29+
* @param ramp color ramp
30+
* @see ramp()
31+
*/
32+
void setRamp( const QgsPresetSchemeColorRamp& ramp );
33+
34+
signals:
35+
36+
//! Emitted when the dialog settings change
37+
void changed();
38+
};
39+
40+
/** \ingroup gui
41+
* \class QgsPresetColorRampDialog
42+
* A dialog which allows users to modify the properties of a QgsPresetSchemeColorRamp.
43+
* \note added in QGIS 3.0
44+
*/
45+
46+
class QgsPresetColorRampDialog : QDialog
47+
{
48+
%TypeHeaderCode
49+
#include <qgspresetcolorrampdialog.h>
50+
%End
51+
52+
public:
53+
54+
/** Constructor for QgsPresetColorRampDialog.
55+
* @param ramp initial ramp to show in dialog
56+
* @param parent parent widget
57+
*/
58+
QgsPresetColorRampDialog( const QgsPresetSchemeColorRamp& ramp, QWidget* parent /TransferThis/ = nullptr );
59+
60+
/** Returns a color ramp representing the current settings from the dialog.
61+
* @see setRamp()
62+
*/
63+
QgsPresetSchemeColorRamp ramp() const;
64+
65+
/** Sets the color ramp to show in the dialog.
66+
* @param ramp color ramp
67+
* @see ramp()
68+
*/
69+
void setRamp( const QgsPresetSchemeColorRamp& ramp );
70+
71+
signals:
72+
73+
//! Emitted when the dialog settings change
74+
void changed();
75+
};

src/core/qgscolorramp.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,102 @@ bool QgsCptCityColorRamp::loadFile()
738738
mFileLoaded = true;
739739
return true;
740740
}
741+
742+
743+
//
744+
// QgsPresetColorRamp
745+
//
746+
747+
QgsPresetSchemeColorRamp::QgsPresetSchemeColorRamp( const QList<QColor>& colors )
748+
{
749+
Q_FOREACH ( const QColor& color, colors )
750+
{
751+
mColors << qMakePair( color, color.name() );
752+
}
753+
// need at least one color
754+
if ( mColors.isEmpty() )
755+
mColors << qMakePair( QColor( 250, 75, 60 ), QString( "#fa4b3c" ) );
756+
}
757+
758+
QgsPresetSchemeColorRamp::QgsPresetSchemeColorRamp( const QgsNamedColorList& colors )
759+
: mColors( colors )
760+
{
761+
// need at least one color
762+
if ( mColors.isEmpty() )
763+
mColors << qMakePair( QColor( 250, 75, 60 ), QString( "#fa4b3c" ) );
764+
}
765+
766+
QgsColorRamp* QgsPresetSchemeColorRamp::create( const QgsStringMap& properties )
767+
{
768+
QgsNamedColorList colors;
769+
770+
int i = 0;
771+
QString colorString = properties.value( QString( "preset_color_%1" ).arg( i ), QString() );
772+
QString colorName = properties.value( QString( "preset_color_name_%1" ).arg( i ), QString() );
773+
while ( !colorString.isEmpty() )
774+
{
775+
colors << qMakePair( QgsSymbolLayerUtils::decodeColor( colorString ), colorName );
776+
i++;
777+
colorString = properties.value( QString( "preset_color_%1" ).arg( i ), QString() );
778+
colorName = properties.value( QString( "preset_color_name_%1" ).arg( i ), QString() );
779+
}
780+
781+
return new QgsPresetSchemeColorRamp( colors );
782+
}
783+
784+
QList<QColor> QgsPresetSchemeColorRamp::colors() const
785+
{
786+
QList< QColor > l;
787+
for ( int i = 0; i < mColors.count(); ++i )
788+
{
789+
l << mColors.at( i ).first;
790+
}
791+
return l;
792+
}
793+
794+
double QgsPresetSchemeColorRamp::value( int index ) const
795+
{
796+
if ( mColors.size() < 1 )
797+
return 0;
798+
return static_cast< double >( index ) / ( mColors.size() - 1 );
799+
}
800+
801+
QColor QgsPresetSchemeColorRamp::color( double value ) const
802+
{
803+
if ( value < 0 || value > 1 )
804+
return QColor();
805+
806+
int colorCnt = mColors.count();
807+
int colorIdx = qMin( static_cast< int >( value * colorCnt ), colorCnt - 1 );
808+
809+
if ( colorIdx >= 0 && colorIdx < colorCnt )
810+
return mColors.at( colorIdx ).first;
811+
812+
return QColor();
813+
}
814+
815+
QgsPresetSchemeColorRamp* QgsPresetSchemeColorRamp::clone() const
816+
{
817+
return new QgsPresetSchemeColorRamp( *this );
818+
}
819+
820+
QgsStringMap QgsPresetSchemeColorRamp::properties() const
821+
{
822+
QgsStringMap props;
823+
for ( int i = 0; i < mColors.count(); ++i )
824+
{
825+
props.insert( QString( "preset_color_%1" ).arg( i ), QgsSymbolLayerUtils::encodeColor( mColors.at( i ).first ) );
826+
props.insert( QString( "preset_color_name_%1" ).arg( i ), mColors.at( i ).second );
827+
}
828+
return props;
829+
}
830+
831+
int QgsPresetSchemeColorRamp::count() const
832+
{
833+
return mColors.count();
834+
}
835+
836+
QgsNamedColorList QgsPresetSchemeColorRamp::fetchColors( const QString& , const QColor& )
837+
{
838+
return mColors;
839+
}

src/core/qgscolorramp.h

+59
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QColor>
2020
#include <QGradient>
2121
#include "qgis.h"
22+
#include "qgscolorscheme.h"
2223

2324
/** \ingroup core
2425
* \class QgsColorRamp
@@ -381,6 +382,64 @@ class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp
381382
};
382383

383384

385+
/** \ingroup core
386+
* \class QgsPresetSchemeColorRamp
387+
* \brief A scheme based color ramp consisting of a list of predefined colors.
388+
* \note added in QGIS 3.0
389+
*/
390+
class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColorScheme
391+
{
392+
public:
393+
394+
/** Constructor for QgsPresetSchemeColorRamp.
395+
* @param colors list of colors in ramp
396+
*/
397+
QgsPresetSchemeColorRamp( const QList< QColor >& colors = QList< QColor >() );
398+
399+
/** Constructor for QgsPresetColorRamp.
400+
* @param colors list of named colors in ramp
401+
* @note not available in Python bindings - use setColors instead
402+
*/
403+
QgsPresetSchemeColorRamp( const QgsNamedColorList& colors );
404+
405+
/** Returns a new QgsPresetSchemeColorRamp color ramp created using the properties encoded in a string
406+
* map.
407+
* @param properties color ramp properties
408+
* @see properties()
409+
*/
410+
static QgsColorRamp* create( const QgsStringMap& properties = QgsStringMap() );
411+
412+
/** Sets the list of colors used by the ramp.
413+
* @param colors list of colors
414+
* @see colors()
415+
*/
416+
bool setColors( const QgsNamedColorList& colors, const QString& = QString(), const QColor& = QColor() ) override { mColors = colors; return true; }
417+
418+
/** Returns the list of colors used by the ramp.
419+
* @see setColors()
420+
*/
421+
QList< QColor > colors() const;
422+
423+
// QgsColorRamp interface
424+
virtual double value( int index ) const override;
425+
virtual QColor color( double value ) const override;
426+
virtual QString type() const override { return "preset"; }
427+
virtual QgsPresetSchemeColorRamp* clone() const override;
428+
virtual QgsStringMap properties() const override;
429+
int count() const override;
430+
431+
// QgsColorScheme interface
432+
QString schemeName() const override { return "preset"; }
433+
QgsNamedColorList fetchColors( const QString &context = QString(),
434+
const QColor &baseColor = QColor() ) override;
435+
bool isEditable() const override { return true; }
436+
437+
private:
438+
439+
QgsNamedColorList mColors;
440+
};
441+
442+
384443
#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
385444
#define DEFAULT_COLORBREWER_COLORS 5
386445

src/core/symbology-ng/qgssymbollayerutils.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,8 @@ QgsColorRamp* QgsSymbolLayerUtils::loadColorRamp( QDomElement& element )
26652665
return QgsColorBrewerColorRamp::create( props );
26662666
else if ( rampType == "cpt-city" )
26672667
return QgsCptCityColorRamp::create( props );
2668+
else if ( rampType == "preset" )
2669+
return QgsPresetSchemeColorRamp::create( props );
26682670
else
26692671
{
26702672
QgsDebugMsg( "unknown colorramp type " + rampType );

src/gui/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ SET(QGIS_GUI_SRCS
279279
qgspanelwidgetstack.cpp
280280
qgspixmaplabel.cpp
281281
qgspluginmanagerinterface.cpp
282+
qgspresetcolorrampdialog.cpp
282283
qgsprevieweffect.cpp
283284
qgsprojectbadlayerguihandler.cpp
284285
qgsprojectionselectionwidget.cpp
@@ -434,6 +435,7 @@ SET(QGIS_GUI_MOC_HDRS
434435
qgspanelwidgetstack.h
435436
qgspixmaplabel.h
436437
qgspluginmanagerinterface.h
438+
qgspresetcolorrampdialog.h
437439
qgsprevieweffect.h
438440
qgsprojectbadlayerguihandler.h
439441
qgsprojectionselectionwidget.h

0 commit comments

Comments
 (0)