Skip to content

Commit 9bb9b58

Browse files
authoredNov 30, 2016
Merge pull request #3810 from nirvn/colorrampbutton
[FEATURE] color ramp button widget
2 parents 57f17e3 + a755f08 commit 9bb9b58

20 files changed

+1245
-134
lines changed
 

‎python/core/qgscolorramp.sip

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class QgsColorRamp
4848
*/
4949
virtual QString type() const = 0;
5050

51+
virtual void invert();
52+
5153
/** Creates a clone of the color ramp.
5254
*/
5355
virtual QgsColorRamp* clone() const = 0 /Factory/;
@@ -118,6 +120,7 @@ class QgsGradientColorRamp : QgsColorRamp
118120
virtual double value( int index ) const;
119121
virtual QColor color( double value ) const;
120122
virtual QString type() const;
123+
virtual void invert();
121124
virtual QgsGradientColorRamp* clone() const /Factory/;
122125
virtual QgsStringMap properties() const;
123126

@@ -333,6 +336,7 @@ class QgsPresetSchemeColorRamp : QgsColorRamp, QgsColorScheme
333336
virtual double value( int index ) const;
334337
virtual QColor color( double value ) const;
335338
virtual QString type() const;
339+
virtual void invert();
336340
virtual QgsPresetSchemeColorRamp* clone() const /Factory/;
337341
virtual QgsStringMap properties() const;
338342
int count() const;
@@ -355,7 +359,8 @@ class QgsColorBrewerColorRamp : QgsColorRamp
355359
%End
356360
public:
357361
QgsColorBrewerColorRamp( const QString& schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
358-
int colors = DEFAULT_COLORBREWER_COLORS );
362+
int colors = DEFAULT_COLORBREWER_COLORS,
363+
bool inverted = false );
359364

360365
static QgsColorRamp* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;
361366

@@ -365,6 +370,8 @@ class QgsColorBrewerColorRamp : QgsColorRamp
365370

366371
virtual QString type() const;
367372

373+
virtual void invert();
374+
368375
virtual QgsColorBrewerColorRamp* clone() const /Factory/;
369376

370377
virtual QgsStringMap properties() const;
@@ -403,7 +410,7 @@ class QgsCptCityColorRamp : QgsGradientColorRamp
403410
static QgsColorRamp* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;
404411

405412
virtual QString type() const;
406-
413+
virtual void invert();
407414
virtual QgsCptCityColorRamp* clone() const /Factory/;
408415
void copy( const QgsCptCityColorRamp* other );
409416
QgsGradientColorRamp* cloneGradientRamp() const /Factory/;

‎python/core/symbology-ng/qgscategorizedsymbolrenderer.sip

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,11 @@ class QgsCategorizedSymbolRenderer : QgsFeatureRenderer
157157
*/
158158
void setSourceColorRamp( QgsColorRamp* ramp /Transfer/ );
159159

160-
//! @note added in 2.1
161-
bool invertedColorRamp();
162-
void setInvertedColorRamp( bool inverted );
163-
164160
/** Update the color ramp used and all symbols colors.
165161
* @param ramp color ramp. Ownership is transferred to the renderer
166-
* @param inverted set to true to invert ramp colors
167162
* @note added in 2.5
168163
*/
169-
void updateColorRamp( QgsColorRamp* ramp /Transfer/, bool inverted = false );
164+
void updateColorRamp( QgsColorRamp* ramp /Transfer/ );
170165

171166
//! items of symbology items in legend should be checkable
172167
//! @note added in 2.5

‎python/gui/gui.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
%Include qgscharacterselectdialog.sip
4242
%Include qgscolorbrewercolorrampdialog.sip
4343
%Include qgscolorbutton.sip
44-
%Include qgscolorbutton.sip
44+
%Include qgscolorrampbutton.sip
4545
%Include qgscolordialog.sip
4646
%Include qgscolorschemelist.sip
4747
%Include qgscolorswatchgrid.sip

‎python/gui/qgscolorrampbutton.sip

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
2+
/** \ingroup gui
3+
* \class QgsColorRampButton
4+
* A cross platform button subclass for selecting color ramps. Will open color ramp dialogs when clicked.
5+
* Offers live updates to button from color ramp dialog. An attached drop down menu allows for access to
6+
* saved color ramps, as well as option to invert the current color ramp and create new ramps.
7+
* \note Added in version 3.0
8+
*/
9+
10+
class QgsColorRampButton : QToolButton
11+
{
12+
%TypeHeaderCode
13+
#include <qgscolorrampbutton.h>
14+
%End
15+
16+
public:
17+
18+
/** Construct a new color ramp button.
19+
* @param parent The parent QWidget for the dialog
20+
* @param dialogTitle The title to show in the color ramp dialog
21+
*/
22+
QgsColorRampButton( QWidget *parent = nullptr, const QString& dialogTitle = QString() );
23+
24+
virtual ~QgsColorRampButton();
25+
26+
virtual QSize sizeHint() const;
27+
28+
/** Return the current color ramp.
29+
* @returns currently selected color
30+
* @see setColor
31+
*/
32+
QgsColorRamp* colorRamp() const;
33+
34+
/** Set the title for the color ramp dialog window.
35+
* @param title Title for the color ramp dialog
36+
* @see colorRampDialogTitle
37+
*/
38+
void setColorRampDialogTitle( const QString& title );
39+
40+
/** Returns the title for the color ramp dialog window.
41+
* @returns title for the color ramp dialog
42+
* @see setColorRampDialogTitle
43+
*/
44+
QString colorRampDialogTitle() const;
45+
46+
/** Returns whether the button accepts live updates from QgsColorRampDialog.
47+
* @returns true if the button will be accepted immediately when the dialog's color ramp changes
48+
* @see setAcceptLiveUpdates
49+
*/
50+
bool acceptLiveUpdates() const;
51+
52+
/** Sets whether the button accepts live updates from QgsColorRampDialog. Live updates may cause changes
53+
* that are not undoable on QColorRampDialog cancel.
54+
* @param accept set to true to enable live updates
55+
* @see acceptLiveUpdates
56+
*/
57+
void setAcceptLiveUpdates( const bool accept );
58+
59+
/** Sets whether the drop down menu should be shown for the button. The default behaviour is to
60+
* show the menu.
61+
* @param showMenu set to false to hide the drop down menu
62+
* @see showMenu
63+
*/
64+
void setShowMenu( const bool showMenu );
65+
66+
/** Returns whether the drop down menu is shown for the button.
67+
* @returns true if drop down menu is shown
68+
* @see setShowMenu
69+
*/
70+
bool showMenu() const;
71+
72+
/** Sets the default color ramp for the button, which is shown in the button's drop down menu for the
73+
* "default color ramp" option.
74+
* @param colorramp default color ramp for the button. Set to a null pointer to disable the default color
75+
* ramp option.
76+
* @see defaultColorRamp
77+
*/
78+
void setDefaultColorRamp( QgsColorRamp* colorramp );
79+
80+
/** Returns the default color ramp for the button, which is shown in the button's drop down menu for the
81+
* "default color ramp" option.
82+
* @returns default color ramp for the button. Returns a null pointer if the default color ramp
83+
* option is disabled.
84+
* @see setDefaultColorRamp
85+
*/
86+
QgsColorRamp* defaultColorRamp() const;
87+
88+
/** Sets whether a random colors option is shown in the button's drop down menu.
89+
* @param showNull set to true to show a null option
90+
* @see showRandom()
91+
*/
92+
void setShowRandomColorRamp( bool showRandom );
93+
94+
/** Returns whether random colors option is shown in the button's drop down menu.
95+
* @see setShowRandom()
96+
*/
97+
bool showRandomColorRamp() const;
98+
99+
/** Returns true if the current color is null.
100+
* @see setShowNull()
101+
* @see showNull()
102+
*/
103+
bool isRandomColorRamp() const;
104+
105+
/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
106+
* @param showNull set to true to show a null option
107+
* @see showNull()
108+
* @see isNull()
109+
*/
110+
void setShowNull( bool showNull );
111+
112+
/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
113+
* @see setShowNull()
114+
* @see isNull()
115+
*/
116+
bool showNull() const;
117+
118+
/** Returns true if the current color is null.
119+
* @see setShowNull()
120+
* @see showNull()
121+
*/
122+
bool isNull() const;
123+
124+
/** Sets the context string for the color ramp button. The context string is passed to all color ramp
125+
* preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors
126+
* based on the context.
127+
* @param context context string for the color dialog button's color ramp preview icons
128+
* @see context
129+
*/
130+
void setContext( const QString& context );
131+
132+
/** Returns the context string for the color ramp button. The context string is passed to all color ramp
133+
* preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors
134+
* based on the context.
135+
* @returns context context string for the color dialog button's color ramp preview icons
136+
* @see setContext
137+
*/
138+
QString context() const;
139+
140+
/** Sets whether the color ramp button only shows gradient type ramps
141+
* @param gradientonly set to true to show only gradient type ramps
142+
* @see showGradientOnly
143+
*/
144+
void setShowGradientOnly( bool gradientonly );
145+
146+
/** Returns true if the color ramp button only shows gradient type ramps
147+
* @see setShowGradientOnly
148+
*/
149+
bool showGradientOnly() const;
150+
151+
public slots:
152+
153+
/** Sets the current color ramp for the button. Will emit a colorRampChanged() signal if the color ramp is different
154+
* to the previous color ramp.
155+
* @param colorramp New color ramp for the button
156+
* @see setRandomColorRamp, setColorRampFromName, colorRamp
157+
*/
158+
void setColorRamp( QgsColorRamp* colorramp );
159+
160+
/** Sets the current color ramp for the button to random colors. Will emit a colorRampChanged() signal
161+
* if the color ramp is different to the previous color ramp.
162+
* @see setColorRamp, setColorRampFromName, colorRamp
163+
*/
164+
void setRandomColorRamp();
165+
166+
/** Sets the current color ramp for the button using a saved color ramp name. Will emit a colorRampChanged() signal
167+
* if the color ramp is different to the previous color ramp.
168+
* @param name Name of saved color ramp
169+
* @see setColorRamp, setRandomColorRamp, colorRamp
170+
*/
171+
void setColorRampFromName( QString name = QString() );
172+
173+
/** Sets the background pixmap for the button based upon current color ramp.
174+
* @param colorramp Color ramp for button background. If no color ramp is specified, the button's current
175+
* color ramp will be used
176+
*/
177+
void setButtonBackground( QgsColorRamp* colorramp = nullptr );
178+
179+
/** Sets color ramp to the button's default color ramp, if set.
180+
* @see setDefaultColorRamp
181+
* @see defaultColorRamp
182+
* @see setToNull()
183+
*/
184+
void setToDefaultColorRamp();
185+
186+
/** Sets color ramp to null.
187+
* @see setToDefaultColorRamp()
188+
*/
189+
void setToNull();
190+
191+
signals:
192+
193+
/** Emitted whenever a new color ramp is set for the button. The color ramp is always valid.
194+
* In case the new color ramp is the same, no signal is emitted to avoid infinite loops.
195+
*/
196+
void colorRampChanged();
197+
198+
protected:
199+
200+
bool event( QEvent *e );
201+
void changeEvent( QEvent* e );
202+
void showEvent( QShowEvent* e );
203+
void resizeEvent( QResizeEvent *event );
204+
205+
/**
206+
* Reimplemented to detect right mouse button clicks on the color ramp button
207+
*/
208+
void mousePressEvent( QMouseEvent* e );
209+
210+
};

‎python/gui/symbology-ng/qgscategorizedsymbolrendererwidget.sip

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ class QgsCategorizedSymbolRendererWidget : QgsRendererWidget
7272

7373
void changeCategorySymbol();
7474

75-
QgsColorRamp* getColorRamp();
76-
7775
QList<QgsSymbol*> selectedSymbols();
7876
QgsCategoryList selectedCategoryList();
7977
void refreshSymbolView();

‎src/core/qgscolorramp.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ QColor QgsGradientColorRamp::color( double value ) const
160160
}
161161
}
162162

163+
void QgsGradientColorRamp::invert()
164+
{
165+
QColor tmpColor = mColor1;
166+
mColor1 = mColor2;
167+
mColor2 = tmpColor;
168+
169+
QgsGradientStopsList newStops;
170+
for ( int k = mStops.size() - 1; k >= 0; k-- )
171+
{
172+
newStops << QgsGradientStop( 1 - mStops.at( k ).offset, mStops.at( k ).color );
173+
}
174+
mStops = newStops;
175+
}
176+
163177
QgsGradientColorRamp* QgsGradientColorRamp::clone() const
164178
{
165179
QgsGradientColorRamp* r = new QgsGradientColorRamp( mColor1, mColor2,
@@ -473,9 +487,10 @@ QgsStringMap QgsRandomColorRamp::properties() const
473487

474488
////////////
475489

476-
QgsColorBrewerColorRamp::QgsColorBrewerColorRamp( const QString& schemeName, int colors )
490+
QgsColorBrewerColorRamp::QgsColorBrewerColorRamp( const QString& schemeName, int colors, bool inverted )
477491
: mSchemeName( schemeName )
478492
, mColors( colors )
493+
, mInverted( inverted )
479494
{
480495
loadPalette();
481496
}
@@ -484,18 +499,32 @@ QgsColorRamp* QgsColorBrewerColorRamp::create( const QgsStringMap& props )
484499
{
485500
QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME;
486501
int colors = DEFAULT_COLORBREWER_COLORS;
502+
bool inverted = false;
487503

488504
if ( props.contains( QStringLiteral( "schemeName" ) ) )
489505
schemeName = props[QStringLiteral( "schemeName" )];
490506
if ( props.contains( QStringLiteral( "colors" ) ) )
491507
colors = props[QStringLiteral( "colors" )].toInt();
508+
if ( props.contains( QStringLiteral( "inverted" ) ) )
509+
inverted = props[QStringLiteral( "inverted" )].toInt();
492510

493-
return new QgsColorBrewerColorRamp( schemeName, colors );
511+
return new QgsColorBrewerColorRamp( schemeName, colors, inverted );
494512
}
495513

496514
void QgsColorBrewerColorRamp::loadPalette()
497515
{
498516
mPalette = QgsColorBrewerPalette::listSchemeColors( mSchemeName, mColors );
517+
518+
if ( mInverted )
519+
{
520+
QList<QColor> tmpPalette;
521+
522+
for ( int k = mPalette.size() - 1; k >= 0; k-- )
523+
{
524+
tmpPalette << mPalette.at( k );
525+
}
526+
mPalette = tmpPalette;
527+
}
499528
}
500529

501530
QStringList QgsColorBrewerColorRamp::listSchemeNames()
@@ -525,16 +554,23 @@ QColor QgsColorBrewerColorRamp::color( double value ) const
525554
return mPalette.at( paletteEntry );
526555
}
527556

557+
void QgsColorBrewerColorRamp::invert()
558+
{
559+
mInverted = !mInverted;
560+
loadPalette();
561+
}
562+
528563
QgsColorBrewerColorRamp* QgsColorBrewerColorRamp::clone() const
529564
{
530-
return new QgsColorBrewerColorRamp( mSchemeName, mColors );
565+
return new QgsColorBrewerColorRamp( mSchemeName, mColors, mInverted );
531566
}
532567

533568
QgsStringMap QgsColorBrewerColorRamp::properties() const
534569
{
535570
QgsStringMap map;
536571
map[QStringLiteral( "schemeName" )] = mSchemeName;
537572
map[QStringLiteral( "colors" )] = QString::number( mColors );
573+
map[QStringLiteral( "inverted" )] = QString::number( mInverted );
538574
return map;
539575
}
540576

@@ -813,6 +849,17 @@ QColor QgsPresetSchemeColorRamp::color( double value ) const
813849
return QColor();
814850
}
815851

852+
void QgsPresetSchemeColorRamp::invert()
853+
{
854+
QgsNamedColorList tmpColors;
855+
856+
for ( int k = mColors.size() - 1; k >= 0; k-- )
857+
{
858+
tmpColors << mColors.at( k );
859+
}
860+
mColors = tmpColors;
861+
}
862+
816863
QgsPresetSchemeColorRamp* QgsPresetSchemeColorRamp::clone() const
817864
{
818865
return new QgsPresetSchemeColorRamp( *this );

‎src/core/qgscolorramp.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class CORE_EXPORT QgsColorRamp
5050
*/
5151
virtual QString type() const = 0;
5252

53+
54+
/** Inverts the ordering of the color ramp.
55+
*/
56+
virtual void invert() {}
57+
5358
/** Creates a clone of the color ramp.
5459
*/
5560
virtual QgsColorRamp* clone() const = 0;
@@ -123,6 +128,7 @@ class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
123128
virtual double value( int index ) const override;
124129
virtual QColor color( double value ) const override;
125130
virtual QString type() const override { return QStringLiteral( "gradient" ); }
131+
virtual void invert() override;
126132
virtual QgsGradientColorRamp* clone() const override;
127133
virtual QgsStringMap properties() const override;
128134

@@ -424,6 +430,7 @@ class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColo
424430
virtual double value( int index ) const override;
425431
virtual QColor color( double value ) const override;
426432
virtual QString type() const override { return QStringLiteral( "preset" ); }
433+
virtual void invert() override;
427434
virtual QgsPresetSchemeColorRamp* clone() const override;
428435
virtual QgsStringMap properties() const override;
429436
int count() const override;
@@ -455,9 +462,11 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
455462
/** Constructor for QgsColorBrewerColorRamp
456463
* @param schemeName color brewer scheme name
457464
* @param colors number of colors in ramp
465+
* @param inverted invert ramp ordering
458466
*/
459467
QgsColorBrewerColorRamp( const QString& schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
460-
int colors = DEFAULT_COLORBREWER_COLORS );
468+
int colors = DEFAULT_COLORBREWER_COLORS,
469+
bool inverted = false );
461470

462471
/** Returns a new QgsColorBrewerColorRamp color ramp created using the properties encoded in a string
463472
* map.
@@ -469,6 +478,7 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
469478
virtual double value( int index ) const override;
470479
virtual QColor color( double value ) const override;
471480
virtual QString type() const override { return QStringLiteral( "colorbrewer" ); }
481+
virtual void invert() override;
472482
virtual QgsColorBrewerColorRamp* clone() const override;
473483
virtual QgsStringMap properties() const override;
474484
virtual int count() const override { return mColors; }
@@ -517,6 +527,7 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
517527
QString mSchemeName;
518528
int mColors;
519529
QList<QColor> mPalette;
530+
bool mInverted;
520531
};
521532

522533

‎src/core/symbology-ng/qgscategorizedsymbolrenderer.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ void QgsRendererCategory::toSld( QDomDocument &doc, QDomElement &element, QgsStr
152152
QgsCategorizedSymbolRenderer::QgsCategorizedSymbolRenderer( const QString& attrName, const QgsCategoryList& categories )
153153
: QgsFeatureRenderer( QStringLiteral( "categorizedSymbol" ) )
154154
, mAttrName( attrName )
155-
, mInvertedColorRamp( false )
156155
, mAttrNum( -1 )
157156
, mCounting( false )
158157
{
@@ -472,7 +471,6 @@ QgsCategorizedSymbolRenderer* QgsCategorizedSymbolRenderer::clone() const
472471
if ( mSourceColorRamp.data() )
473472
{
474473
r->setSourceColorRamp( mSourceColorRamp->clone() );
475-
r->setInvertedColorRamp( mInvertedColorRamp );
476474
}
477475
r->setUsingSymbolLevels( usingSymbolLevels() );
478476

@@ -631,9 +629,6 @@ QgsFeatureRenderer* QgsCategorizedSymbolRenderer::create( QDomElement& element )
631629
if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
632630
{
633631
r->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
634-
QDomElement invertedColorRampElem = element.firstChildElement( QStringLiteral( "invertedcolorramp" ) );
635-
if ( !invertedColorRampElem.isNull() )
636-
r->setInvertedColorRamp( invertedColorRampElem.attribute( QStringLiteral( "value" ) ) == QLatin1String( "1" ) );
637632
}
638633

639634
QDomElement rotationElem = element.firstChildElement( QStringLiteral( "rotation" ) );
@@ -721,9 +716,6 @@ QDomElement QgsCategorizedSymbolRenderer::save( QDomDocument& doc )
721716
{
722717
QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), mSourceColorRamp.data(), doc );
723718
rendererElem.appendChild( colorRampElem );
724-
QDomElement invertedElem = doc.createElement( QStringLiteral( "invertedcolorramp" ) );
725-
invertedElem.setAttribute( QStringLiteral( "value" ), mInvertedColorRamp );
726-
rendererElem.appendChild( invertedElem );
727719
}
728720

729721
QDomElement rotationElem = doc.createElement( QStringLiteral( "rotation" ) );
@@ -864,10 +856,9 @@ void QgsCategorizedSymbolRenderer::setSourceColorRamp( QgsColorRamp* ramp )
864856
mSourceColorRamp.reset( ramp );
865857
}
866858

867-
void QgsCategorizedSymbolRenderer::updateColorRamp( QgsColorRamp* ramp, bool inverted )
859+
void QgsCategorizedSymbolRenderer::updateColorRamp( QgsColorRamp* ramp )
868860
{
869861
setSourceColorRamp( ramp );
870-
setInvertedColorRamp( inverted );
871862
double num = mCategories.count() - 1;
872863
double count = 0;
873864

@@ -882,7 +873,6 @@ void QgsCategorizedSymbolRenderer::updateColorRamp( QgsColorRamp* ramp, bool inv
882873
Q_FOREACH ( const QgsRendererCategory &cat, mCategories )
883874
{
884875
double value = count / num;
885-
if ( mInvertedColorRamp ) value = 1.0 - value;
886876
cat.symbol()->setColor( mSourceColorRamp->color( value ) );
887877
count += 1;
888878
}

‎src/core/symbology-ng/qgscategorizedsymbolrenderer.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,11 @@ class CORE_EXPORT QgsCategorizedSymbolRenderer : public QgsFeatureRenderer
167167
*/
168168
void setSourceColorRamp( QgsColorRamp* ramp );
169169

170-
//! @note added in 2.1
171-
bool invertedColorRamp() { return mInvertedColorRamp; }
172-
void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
173-
174170
/** Update the color ramp used and all symbols colors.
175171
* @param ramp color ramp. Ownership is transferred to the renderer
176-
* @param inverted set to true to invert ramp colors
177172
* @note added in 2.5
178173
*/
179-
void updateColorRamp( QgsColorRamp* ramp, bool inverted = false );
174+
void updateColorRamp( QgsColorRamp* ramp );
180175

181176
virtual bool legendSymbolItemsCheckable() const override;
182177
virtual bool legendSymbolItemChecked( const QString& key ) override;
@@ -194,7 +189,6 @@ class CORE_EXPORT QgsCategorizedSymbolRenderer : public QgsFeatureRenderer
194189
QgsCategoryList mCategories;
195190
QScopedPointer<QgsSymbol> mSourceSymbol;
196191
QScopedPointer<QgsColorRamp> mSourceColorRamp;
197-
bool mInvertedColorRamp;
198192
QScopedPointer<QgsExpression> mExpression;
199193

200194
//! attribute index (derived from attribute name in startRender)

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ SET(QGIS_GUI_SRCS
188188
qgscollapsiblegroupbox.cpp
189189
qgscolorbrewercolorrampdialog.cpp
190190
qgscolorbutton.cpp
191+
qgscolorrampbutton.cpp
191192
qgscolordialog.cpp
192193
qgscolorschemelist.cpp
193194
qgscolorswatchgrid.cpp
@@ -356,6 +357,7 @@ SET(QGIS_GUI_MOC_HDRS
356357
qgscollapsiblegroupbox.h
357358
qgscolorbrewercolorrampdialog.h
358359
qgscolorbutton.h
360+
qgscolorrampbutton.h
359361
qgscolordialog.h
360362
qgscolorschemelist.h
361363
qgscolorswatchgrid.h

‎src/gui/qgscolorrampbutton.cpp

Lines changed: 579 additions & 0 deletions
Large diffs are not rendered by default.

‎src/gui/qgscolorrampbutton.h

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
/***************************************************************************
2+
qgscolorrampbutton.h - Color ramp button
3+
--------------------------------------
4+
Date : November 27, 2016
5+
Copyright : (C) 2016 by Mathieu Pellerin
6+
Email : nirvn dot asia at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSCOLORRAMPBUTTON_H
16+
#define QGSCOLORRAMPBUTTON_H
17+
18+
#include "qgscolorrampbutton.h"
19+
20+
#include "qgscolorramp.h"
21+
#include "qgsstyle.h"
22+
23+
#include <QToolButton>
24+
25+
class QgsPanelWidget;
26+
27+
/** \ingroup gui
28+
* \class QgsColorRampButton
29+
* A cross platform button subclass for selecting color ramps. Will open color ramp dialogs when clicked.
30+
* Offers live updates to button from color ramp dialog. An attached drop down menu allows for access to
31+
* saved color ramps, as well as option to invert the current color ramp and create new ramps.
32+
* \note Added in version 3.0
33+
*/
34+
35+
class GUI_EXPORT QgsColorRampButton : public QToolButton
36+
{
37+
Q_OBJECT
38+
Q_PROPERTY( QString colorRampDialogTitle READ colorRampDialogTitle WRITE setColorRampDialogTitle )
39+
Q_PROPERTY( bool acceptLiveUpdates READ acceptLiveUpdates WRITE setAcceptLiveUpdates )
40+
Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
41+
Q_PROPERTY( QgsColorRamp* defaultColorRamp READ defaultColorRamp WRITE setDefaultColorRamp )
42+
Q_PROPERTY( QString context READ context WRITE setContext )
43+
44+
public:
45+
46+
/** Construct a new color ramp button.
47+
* @param parent The parent QWidget for the dialog
48+
* @param dialogTitle The title to show in the color ramp dialog
49+
*/
50+
QgsColorRampButton( QWidget *parent = nullptr, const QString& dialogTitle = QString() );
51+
52+
virtual ~QgsColorRampButton();
53+
54+
virtual QSize sizeHint() const override;
55+
56+
/** Return the current color ramp.
57+
* @returns currently selected color
58+
* @see setColor
59+
*/
60+
QgsColorRamp* colorRamp() const;
61+
62+
/** Set the title for the color ramp dialog window.
63+
* @param title Title for the color ramp dialog
64+
* @see colorRampDialogTitle
65+
*/
66+
void setColorRampDialogTitle( const QString& title );
67+
68+
/** Returns the title for the color ramp dialog window.
69+
* @returns title for the color ramp dialog
70+
* @see setColorRampDialogTitle
71+
*/
72+
QString colorRampDialogTitle() const;
73+
74+
/** Returns whether the button accepts live updates from QgsColorRampDialog.
75+
* @returns true if the button will be accepted immediately when the dialog's color ramp changes
76+
* @see setAcceptLiveUpdates
77+
*/
78+
bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
79+
80+
/** Sets whether the button accepts live updates from QgsColorRampDialog. Live updates may cause changes
81+
* that are not undoable on QColorRampDialog cancel.
82+
* @param accept set to true to enable live updates
83+
* @see acceptLiveUpdates
84+
*/
85+
void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
86+
87+
/** Sets whether the drop down menu should be shown for the button. The default behaviour is to
88+
* show the menu.
89+
* @param showMenu set to false to hide the drop down menu
90+
* @see showMenu
91+
*/
92+
void setShowMenu( const bool showMenu );
93+
94+
/** Returns whether the drop down menu is shown for the button.
95+
* @returns true if drop down menu is shown
96+
* @see setShowMenu
97+
*/
98+
bool showMenu() const { return menu() ? true : false; }
99+
100+
/** Sets the default color ramp for the button, which is shown in the button's drop down menu for the
101+
* "default color ramp" option.
102+
* @param colorramp default color ramp for the button. Set to a null pointer to disable the default color
103+
* ramp option.
104+
* @see defaultColorRamp
105+
*/
106+
void setDefaultColorRamp( QgsColorRamp* colorramp );
107+
108+
/** Returns the default color ramp for the button, which is shown in the button's drop down menu for the
109+
* "default color ramp" option.
110+
* @returns default color ramp for the button. Returns a null pointer if the default color ramp
111+
* option is disabled.
112+
* @see setDefaultColorRamp
113+
*/
114+
QgsColorRamp* defaultColorRamp() const { return mDefaultColorRamp; }
115+
116+
/** Sets whether a random colors option is shown in the button's drop down menu.
117+
* @param showRandom set to true to show a random colors option
118+
* @see showRandom()
119+
*/
120+
void setShowRandomColorRamp( bool showRandom ) { mShowRandomColorRamp = showRandom; }
121+
122+
/** Returns whether random colors option is shown in the button's drop down menu.
123+
* @see setShowRandom()
124+
*/
125+
bool showRandomColorRamp() const { return mShowRandomColorRamp; }
126+
127+
/** Returns true if the current color is null.
128+
* @see setShowNull()
129+
* @see showNull()
130+
*/
131+
bool isRandomColorRamp() const;
132+
133+
/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
134+
* @param showNull set to true to show a null option
135+
* @see showNull()
136+
* @see isNull()
137+
*/
138+
void setShowNull( bool showNull );
139+
140+
/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
141+
* @see setShowNull()
142+
* @see isNull()
143+
*/
144+
bool showNull() const;
145+
146+
/** Returns true if the current color is null.
147+
* @see setShowNull()
148+
* @see showNull()
149+
*/
150+
bool isNull() const;
151+
152+
/** Sets the context string for the color ramp button. The context string is passed to all color ramp
153+
* preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors
154+
* based on the context.
155+
* @param context context string for the color dialog button's color ramp preview icons
156+
* @see context
157+
*/
158+
void setContext( const QString& context ) { mContext = context; }
159+
160+
/** Returns the context string for the color ramp button. The context string is passed to all color ramp
161+
* preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors
162+
* based on the context.
163+
* @returns context context string for the color dialog button's color ramp preview icons
164+
* @see setContext
165+
*/
166+
QString context() const { return mContext; }
167+
168+
/** Sets whether the color ramp button only shows gradient type ramps
169+
* @param gradientonly set to true to show only gradient type ramps
170+
* @see showGradientOnly
171+
*/
172+
void setShowGradientOnly( bool gradientonly ) { mShowGradientOnly = gradientonly; }
173+
174+
/** Returns true if the color ramp button only shows gradient type ramps
175+
* @see setShowGradientOnly
176+
*/
177+
bool showGradientOnly() const { return mShowGradientOnly; }
178+
179+
public slots:
180+
181+
/** Sets the current color ramp for the button. Will emit a colorRampChanged() signal if the color ramp is different
182+
* to the previous color ramp.
183+
* @param colorramp New color ramp for the button
184+
* @see setRandomColorRamp, setColorRampFromName, colorRamp
185+
*/
186+
void setColorRamp( QgsColorRamp* colorramp );
187+
188+
/** Sets the current color ramp for the button to random colors. Will emit a colorRampChanged() signal
189+
* if the color ramp is different to the previous color ramp.
190+
* @see setColorRamp, setColorRampFromName, colorRamp
191+
*/
192+
void setRandomColorRamp();
193+
194+
/** Sets the current color ramp for the button using a saved color ramp name. Will emit a colorRampChanged() signal
195+
* if the color ramp is different to the previous color ramp.
196+
* @param name Name of saved color ramp
197+
* @see setColorRamp, setRandomColorRamp, colorRamp
198+
*/
199+
void setColorRampFromName( QString name = QString() );
200+
201+
/** Sets the background pixmap for the button based upon current color ramp.
202+
* @param colorramp Color ramp for button background. If no color ramp is specified, the button's current
203+
* color ramp will be used
204+
*/
205+
void setButtonBackground( QgsColorRamp* colorramp = nullptr );
206+
207+
/** Sets color ramp to the button's default color ramp, if set.
208+
* @see setDefaultColorRamp
209+
* @see defaultColorRamp
210+
* @see setToNull()
211+
*/
212+
void setToDefaultColorRamp();
213+
214+
/** Sets color ramp to null.
215+
* @see setToDefaultColorRamp()
216+
*/
217+
void setToNull();
218+
219+
private slots:
220+
221+
void rampWidgetUpdated();
222+
223+
signals:
224+
225+
/** Emitted whenever a new color ramp is set for the button. The color ramp is always valid.
226+
* In case the new color ramp is the same, no signal is emitted to avoid infinite loops.
227+
*/
228+
void colorRampChanged();
229+
230+
protected:
231+
232+
bool event( QEvent *e ) override;
233+
void changeEvent( QEvent* e ) override;
234+
void showEvent( QShowEvent* e ) override;
235+
void resizeEvent( QResizeEvent *event ) override;
236+
237+
/**
238+
* Reimplemented to detect right mouse button clicks on the color ramp button
239+
*/
240+
void mousePressEvent( QMouseEvent* e ) override;
241+
242+
private:
243+
244+
QString mColorRampDialogTitle;
245+
bool mShowGradientOnly;
246+
QgsColorRamp* mColorRamp;
247+
QgsStyle* mStyle;
248+
249+
QgsColorRamp* mDefaultColorRamp;
250+
QString mContext;
251+
bool mAcceptLiveUpdates;
252+
bool mColorRampSet;
253+
bool mShowRandomColorRamp;
254+
bool mShowNull;
255+
256+
QMenu* mMenu;
257+
QMenu *mAllRampsMenu;
258+
259+
QSize mIconSize;
260+
261+
/** Create a color ramp icon for display in the drop down menu
262+
* @param colorramp Color ramp to create an icon from
263+
*/
264+
QPixmap createMenuIcon( QgsColorRamp* colorramp );
265+
266+
private slots:
267+
268+
void buttonClicked();
269+
270+
/** Show a color ramp dialog based on the ramp type
271+
*/
272+
void showColorRampDialog();
273+
274+
/** Creates a new color ramp
275+
*/
276+
void createColorRamp();
277+
278+
/** Inverts the current color ramp
279+
*/
280+
void invertColorRamp();
281+
282+
/** Load a color ramp from a menu entry
283+
*/
284+
void loadColorRamp();
285+
286+
/** Creates the drop down menu entries
287+
*/
288+
void prepareMenu();
289+
};
290+
291+
#endif

‎src/gui/symbology-ng/qgscategorizedsymbolrendererwidget.cpp

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgssymbol.h"
2222
#include "qgssymbollayerutils.h"
2323
#include "qgscolorramp.h"
24+
#include "qgscolorrampbutton.h"
2425
#include "qgsstyle.h"
2526
#include "qgslogger.h"
2627

@@ -410,21 +411,15 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
410411

411412
mExpressionWidget->setLayer( mLayer );
412413

413-
cboCategorizedColorRamp->populate( mStyle );
414-
int randomIndex = cboCategorizedColorRamp->findText( tr( "Random colors" ) );
415-
if ( randomIndex != -1 )
416-
{
417-
cboCategorizedColorRamp->setCurrentIndex( randomIndex );
418-
mButtonEditRamp->setEnabled( false );
419-
}
414+
// initiate color ramp button to random
415+
btnColorRamp->setShowRandomColorRamp( true );
416+
btnColorRamp->setRandomColorRamp();
420417

421418
// set project default color ramp
422419
QString defaultColorRamp = QgsProject::instance()->readEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/ColorRamp" ), QLatin1String( "" ) );
423420
if ( defaultColorRamp != QLatin1String( "" ) )
424421
{
425-
int index = cboCategorizedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
426-
if ( index >= 0 )
427-
cboCategorizedColorRamp->setCurrentIndex( index );
422+
btnColorRamp->setColorRampFromName( defaultColorRamp );
428423
}
429424

430425
mCategorizedSymbol = QgsSymbol::defaultSymbol( mLayer->geometryType() );
@@ -455,10 +450,8 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
455450
connect( btnDeleteCategories, SIGNAL( clicked() ), this, SLOT( deleteCategories() ) );
456451
connect( btnDeleteAllCategories, SIGNAL( clicked() ), this, SLOT( deleteAllCategories() ) );
457452
connect( btnAddCategory, SIGNAL( clicked() ), this, SLOT( addCategory() ) );
458-
connect( cbxInvertedColorRamp, SIGNAL( toggled( bool ) ), this, SLOT( applyColorRamp() ) );
459-
connect( cboCategorizedColorRamp, SIGNAL( currentIndexChanged( int ) ), this, SLOT( applyColorRamp() ) );
460-
connect( cboCategorizedColorRamp, SIGNAL( sourceRampEdited() ), this, SLOT( applyColorRamp() ) );
461-
connect( mButtonEditRamp, SIGNAL( clicked() ), cboCategorizedColorRamp, SLOT( editSourceRamp() ) );
453+
454+
connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsCategorizedSymbolRendererWidget::applyColorRamp );
462455

463456
// menus for data-defined rotation/size
464457
QMenu* advMenu = new QMenu;
@@ -501,17 +494,11 @@ void QgsCategorizedSymbolRendererWidget::updateUiFromRenderer()
501494
updateCategorizedSymbolIcon();
502495
}
503496

504-
// set source color ramp
497+
// if a color ramp attached to the renderer, enable the color ramp button
505498
if ( mRenderer->sourceColorRamp() )
506499
{
507-
cboCategorizedColorRamp->setSourceColorRamp( mRenderer->sourceColorRamp() );
508-
cbxInvertedColorRamp->setChecked( mRenderer->invertedColorRamp() );
500+
btnColorRamp->setColorRamp( mRenderer->sourceColorRamp() );
509501
}
510-
511-
if ( cboCategorizedColorRamp->currentText() == tr( "Random colors" ) )
512-
mButtonEditRamp->setEnabled( false );
513-
else
514-
mButtonEditRamp->setEnabled( true );
515502
}
516503

517504
QgsFeatureRenderer* QgsCategorizedSymbolRendererWidget::renderer()
@@ -628,20 +615,6 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q
628615
}
629616
}
630617

631-
QgsColorRamp* QgsCategorizedSymbolRendererWidget::getColorRamp()
632-
{
633-
QgsColorRamp* ramp = cboCategorizedColorRamp->currentColorRamp();
634-
if ( !ramp )
635-
{
636-
if ( cboCategorizedColorRamp->count() == 0 )
637-
QMessageBox::critical( this, tr( "Error" ), tr( "There are no available color ramps. You can add them in Style Manager." ) );
638-
else if ( !cboCategorizedColorRamp->createNewColorRampSelected() )
639-
QMessageBox::critical( this, tr( "Error" ), tr( "The selected color ramp is not available." ) );
640-
}
641-
return ramp;
642-
}
643-
644-
645618
void QgsCategorizedSymbolRendererWidget::addCategories()
646619
{
647620
QString attrName = mExpressionWidget->currentField();
@@ -759,8 +732,7 @@ void QgsCategorizedSymbolRendererWidget::addCategories()
759732
// recreate renderer
760733
QgsCategorizedSymbolRenderer *r = new QgsCategorizedSymbolRenderer( attrName, cats );
761734
r->setSourceSymbol( mCategorizedSymbol->clone() );
762-
r->setInvertedColorRamp( cbxInvertedColorRamp->isChecked() );
763-
QScopedPointer< QgsColorRamp > ramp( getColorRamp() );
735+
QScopedPointer< QgsColorRamp > ramp( btnColorRamp->colorRamp() );
764736
if ( ramp )
765737
r->setSourceColorRamp( ramp->clone() );
766738

@@ -777,15 +749,9 @@ void QgsCategorizedSymbolRendererWidget::addCategories()
777749

778750
void QgsCategorizedSymbolRendererWidget::applyColorRamp()
779751
{
780-
if ( cboCategorizedColorRamp->currentText() == tr( "Random colors" ) )
781-
mButtonEditRamp->setEnabled( false );
782-
else
783-
mButtonEditRamp->setEnabled( true );
784-
785-
QgsColorRamp* ramp = getColorRamp();
786-
if ( ramp )
752+
if ( !btnColorRamp->isNull() )
787753
{
788-
mRenderer->updateColorRamp( ramp, cbxInvertedColorRamp->isChecked() );
754+
mRenderer->updateColorRamp( btnColorRamp->colorRamp()->clone() );
789755
}
790756
mModel->updateSymbology();
791757
}

‎src/gui/symbology-ng/qgscategorizedsymbolrendererwidget.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ class GUI_EXPORT QgsCategorizedSymbolRendererWidget : public QgsRendererWidget,
107107
void categoriesDoubleClicked( const QModelIndex & idx );
108108
void addCategory();
109109
void addCategories();
110+
111+
/** Applies the color ramp passed on by the color ramp button
112+
*/
110113
void applyColorRamp();
114+
111115
void deleteCategories();
112116
void deleteAllCategories();
113117

@@ -156,8 +160,6 @@ class GUI_EXPORT QgsCategorizedSymbolRendererWidget : public QgsRendererWidget,
156160

157161
void changeCategorySymbol();
158162

159-
QgsColorRamp* getColorRamp();
160-
161163
QList<QgsSymbol*> selectedSymbols() override;
162164
QgsCategoryList selectedCategoryList();
163165
void refreshSymbolView() override;

‎src/gui/symbology-ng/qgssymbollayerwidget.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,7 @@ QgsGradientFillSymbolLayerWidget::QgsGradientFillSymbolLayerWidget( const QgsVec
10121012
setupUi( this );
10131013
mOffsetUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderMapUnits << QgsUnitTypes::RenderPixels );
10141014

1015-
cboGradientColorRamp->setShowGradientOnly( true );
1016-
cboGradientColorRamp->populate( QgsStyle::defaultStyle() );
1015+
btnColorRamp->setShowGradientOnly( true );
10171016

10181017
btnChangeColor->setAllowAlpha( true );
10191018
btnChangeColor->setColorDialogTitle( tr( "Select gradient color" ) );
@@ -1031,9 +1030,7 @@ QgsGradientFillSymbolLayerWidget::QgsGradientFillSymbolLayerWidget( const QgsVec
10311030

10321031
connect( btnChangeColor, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor( const QColor& ) ) );
10331032
connect( btnChangeColor2, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor2( const QColor& ) ) );
1034-
connect( cboGradientColorRamp, SIGNAL( currentIndexChanged( int ) ), this, SLOT( applyColorRamp() ) );
1035-
connect( cboGradientColorRamp, SIGNAL( sourceRampEdited() ), this, SLOT( applyColorRamp() ) );
1036-
connect( mButtonEditRamp, SIGNAL( clicked() ), cboGradientColorRamp, SLOT( editSourceRamp() ) );
1033+
connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsGradientFillSymbolLayerWidget::applyColorRamp );
10371034
connect( cboGradientType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGradientType( int ) ) );
10381035
connect( cboCoordinateMode, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setCoordinateMode( int ) ) );
10391036
connect( cboGradientSpread, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGradientSpread( int ) ) );
@@ -1067,7 +1064,7 @@ void QgsGradientFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer* layer )
10671064
if ( mLayer->gradientColorType() == QgsGradientFillSymbolLayer::SimpleTwoColor )
10681065
{
10691066
radioTwoColor->setChecked( true );
1070-
cboGradientColorRamp->setEnabled( false );
1067+
btnColorRamp->setEnabled( false );
10711068
}
10721069
else
10731070
{
@@ -1079,9 +1076,9 @@ void QgsGradientFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer* layer )
10791076
// set source color ramp
10801077
if ( mLayer->colorRamp() )
10811078
{
1082-
cboGradientColorRamp->blockSignals( true );
1083-
cboGradientColorRamp->setSourceColorRamp( mLayer->colorRamp() );
1084-
cboGradientColorRamp->blockSignals( false );
1079+
btnColorRamp->blockSignals( true );
1080+
btnColorRamp->setColorRamp( mLayer->colorRamp() );
1081+
btnColorRamp->blockSignals( false );
10851082
}
10861083

10871084
cboGradientType->blockSignals( true );
@@ -1219,11 +1216,10 @@ void QgsGradientFillSymbolLayerWidget::colorModeChanged()
12191216

12201217
void QgsGradientFillSymbolLayerWidget::applyColorRamp()
12211218
{
1222-
QgsColorRamp* ramp = cboGradientColorRamp->currentColorRamp();
1223-
if ( !ramp )
1219+
if ( btnColorRamp->isNull() )
12241220
return;
12251221

1226-
mLayer->setColorRamp( ramp );
1222+
mLayer->setColorRamp( btnColorRamp->colorRamp()->clone() );
12271223
emit changed();
12281224
}
12291225

‎src/gui/symbology-ng/qgssymbollayerwidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ class GUI_EXPORT QgsGradientFillSymbolLayerWidget : public QgsSymbolLayerWidget,
314314
public slots:
315315
void setColor( const QColor& color );
316316
void setColor2( const QColor& color );
317+
318+
/** Applies the color ramp passed on by the color ramp button
319+
*/
317320
void applyColorRamp();
321+
318322
void setGradientType( int index );
319323
void setCoordinateMode( int index );
320324
void setGradientSpread( int index );

‎src/ui/qgscategorizedsymbolrendererv2widget.ui

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,24 @@
7676
<item row="2" column="1">
7777
<layout class="QHBoxLayout" name="horizontalLayout">
7878
<item>
79-
<widget class="QgsColorRampComboBox" name="cboCategorizedColorRamp">
79+
<widget class="QgsColorRampButton" name="btnColorRamp">
8080
<property name="sizePolicy">
8181
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
82-
<horstretch>1</horstretch>
83-
<verstretch>0</verstretch>
84-
</sizepolicy>
85-
</property>
86-
</widget>
87-
</item>
88-
<item>
89-
<widget class="QPushButton" name="mButtonEditRamp">
90-
<property name="text">
91-
<string>Edit</string>
92-
</property>
93-
</widget>
94-
</item>
95-
<item>
96-
<widget class="QCheckBox" name="cbxInvertedColorRamp">
97-
<property name="sizePolicy">
98-
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
9982
<horstretch>0</horstretch>
10083
<verstretch>0</verstretch>
10184
</sizepolicy>
10285
</property>
103-
<property name="text">
104-
<string>Invert</string>
86+
<property name="minimumSize">
87+
<size>
88+
<width>120</width>
89+
<height>0</height>
90+
</size>
91+
</property>
92+
<property name="maximumSize">
93+
<size>
94+
<width>16777215</width>
95+
<height>16777215</height>
96+
</size>
10597
</property>
10698
</widget>
10799
</item>
@@ -223,9 +215,10 @@
223215
<container>1</container>
224216
</customwidget>
225217
<customwidget>
226-
<class>QgsColorRampComboBox</class>
227-
<extends>QComboBox</extends>
228-
<header>qgscolorrampcombobox.h</header>
218+
<class>QgsColorRampButton</class>
219+
<extends>QToolButton</extends>
220+
<header>qgscolorrampbutton.h</header>
221+
<container>1</container>
229222
</customwidget>
230223
</customwidgets>
231224
<tabstops>

‎src/ui/qgssinglebandpseudocolorrendererwidgetbase.ui

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,10 @@ suffix</string>
331331
</widget>
332332
<customwidgets>
333333
<customwidget>
334-
<class>QgsColorRampComboBox</class>
335-
<extends>QComboBox</extends>
336-
<header>qgscolorrampcombobox.h</header>
334+
<class>QgsColorRampButton</class>
335+
<extends>QToolButton</extends>
336+
<header>qgscolorrampbutton.h</header>
337+
<container>1</container>
337338
</customwidget>
338339
</customwidgets>
339340
<resources>

‎src/ui/symbollayer/widget_gradientfill.ui

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,25 @@
152152
<item row="2" column="1">
153153
<layout class="QHBoxLayout" name="horizontalLayout_8">
154154
<item>
155-
<widget class="QgsColorRampComboBox" name="cboGradientColorRamp"/>
156-
</item>
157-
<item>
158-
<widget class="QPushButton" name="mButtonEditRamp">
155+
<widget class="QgsColorRampButton" name="btnColorRamp">
156+
<property name="sizePolicy">
157+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
158+
<horstretch>0</horstretch>
159+
<verstretch>0</verstretch>
160+
</sizepolicy>
161+
</property>
162+
<property name="minimumSize">
163+
<size>
164+
<width>120</width>
165+
<height>0</height>
166+
</size>
167+
</property>
159168
<property name="maximumSize">
160169
<size>
161-
<width>100</width>
170+
<width>16777215</width>
162171
<height>16777215</height>
163172
</size>
164173
</property>
165-
<property name="text">
166-
<string>Edit</string>
167-
</property>
168174
</widget>
169175
</item>
170176
</layout>
@@ -544,6 +550,12 @@
544550
<header>qgscolorbutton.h</header>
545551
<container>1</container>
546552
</customwidget>
553+
<customwidget>
554+
<class>QgsColorRampButton</class>
555+
<extends>QToolButton</extends>
556+
<header>qgscolorrampbutton.h</header>
557+
<container>1</container>
558+
</customwidget>
547559
<customwidget>
548560
<class>QgsDataDefinedButton</class>
549561
<extends>QToolButton</extends>
@@ -560,18 +572,13 @@
560572
<header>qgsunitselectionwidget.h</header>
561573
<container>1</container>
562574
</customwidget>
563-
<customwidget>
564-
<class>QgsColorRampComboBox</class>
565-
<extends>QComboBox</extends>
566-
<header>qgscolorrampcombobox.h</header>
567-
</customwidget>
568575
</customwidgets>
569576
<tabstops>
570577
<tabstop>radioTwoColor</tabstop>
571578
<tabstop>btnChangeColor</tabstop>
572579
<tabstop>btnChangeColor2</tabstop>
573580
<tabstop>radioColorRamp</tabstop>
574-
<tabstop>cboGradientColorRamp</tabstop>
581+
<tabstop>btnColorRamp</tabstop>
575582
<tabstop>mButtonEditRamp</tabstop>
576583
<tabstop>cboGradientType</tabstop>
577584
<tabstop>cboCoordinateMode</tabstop>
@@ -623,7 +630,7 @@
623630
<connection>
624631
<sender>radioColorRamp</sender>
625632
<signal>toggled(bool)</signal>
626-
<receiver>cboGradientColorRamp</receiver>
633+
<receiver>btnColorRamp</receiver>
627634
<slot>setEnabled(bool)</slot>
628635
<hints>
629636
<hint type="sourcelabel">

‎tests/src/python/test_qgsvectorcolorramp.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def testQgsVectorGradientRampV2(self):
161161
self.assertEqual(s[3].offset, 0.8)
162162
self.assertEqual(s[3].color, QColor(50, 20, 10))
163163

164+
# test invert function
165+
r.invert()
166+
self.assertEqual(r.color(0), QColor(0, 200, 0))
167+
self.assertEqual(r.color(1), QColor(200, 0, 0))
168+
self.assertEqual(r.color(0.2), QColor(50, 20, 10))
169+
164170
def testQgsLimitedRandomColorRampV2(self):
165171
# test random color ramp
166172
r = QgsLimitedRandomColorRamp(5)
@@ -310,6 +316,11 @@ def testQgsPresetSchemeColorRamp(self):
310316
self.assertEqual(cloned.count(), 2)
311317
self.assertEqual(cloned.fetchColors(), r.fetchColors())
312318

319+
# test invert function
320+
r.invert()
321+
self.assertEqual(r.color(0), QColor(0, 255, 0))
322+
self.assertEqual(r.color(1), QColor(255, 0, 0))
323+
313324
def testQgsColorBrewerColorRampV2(self):
314325
# test color brewer color ramps
315326
r = QgsColorBrewerColorRamp('OrRd', 6)
@@ -373,6 +384,13 @@ def testQgsColorBrewerColorRampV2(self):
373384
self.assertEqual(r.color(0.8), QColor(222, 45, 38))
374385
self.assertEqual(r.color(1.0), QColor(165, 15, 21))
375386

387+
# test invert function
388+
r.invert()
389+
self.assertEqual(r.color(0), QColor(165, 15, 21))
390+
self.assertEqual(r.color(0.2), QColor(222, 45, 38))
391+
self.assertEqual(r.color(1), QColor(254, 229, 217))
392+
r.invert()
393+
376394
# set colors
377395
r.setColors(3)
378396
self.assertEqual(r.colors(), 3)

0 commit comments

Comments
 (0)
Please sign in to comment.