Skip to content

Commit

Permalink
Merge pull request #3810 from nirvn/colorrampbutton
Browse files Browse the repository at this point in the history
[FEATURE] color ramp button widget
  • Loading branch information
nirvn authored Nov 30, 2016
2 parents 57f17e3 + a755f08 commit 9bb9b58
Show file tree
Hide file tree
Showing 20 changed files with 1,245 additions and 134 deletions.
11 changes: 9 additions & 2 deletions python/core/qgscolorramp.sip
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class QgsColorRamp
*/
virtual QString type() const = 0;

virtual void invert();

/** Creates a clone of the color ramp.
*/
virtual QgsColorRamp* clone() const = 0 /Factory/;
Expand Down Expand Up @@ -118,6 +120,7 @@ class QgsGradientColorRamp : QgsColorRamp
virtual double value( int index ) const;
virtual QColor color( double value ) const;
virtual QString type() const;
virtual void invert();
virtual QgsGradientColorRamp* clone() const /Factory/;
virtual QgsStringMap properties() const;

Expand Down Expand Up @@ -333,6 +336,7 @@ class QgsPresetSchemeColorRamp : QgsColorRamp, QgsColorScheme
virtual double value( int index ) const;
virtual QColor color( double value ) const;
virtual QString type() const;
virtual void invert();
virtual QgsPresetSchemeColorRamp* clone() const /Factory/;
virtual QgsStringMap properties() const;
int count() const;
Expand All @@ -355,7 +359,8 @@ class QgsColorBrewerColorRamp : QgsColorRamp
%End
public:
QgsColorBrewerColorRamp( const QString& schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
int colors = DEFAULT_COLORBREWER_COLORS );
int colors = DEFAULT_COLORBREWER_COLORS,
bool inverted = false );

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

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

virtual QString type() const;

virtual void invert();

virtual QgsColorBrewerColorRamp* clone() const /Factory/;

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

virtual QString type() const;

virtual void invert();
virtual QgsCptCityColorRamp* clone() const /Factory/;
void copy( const QgsCptCityColorRamp* other );
QgsGradientColorRamp* cloneGradientRamp() const /Factory/;
Expand Down
7 changes: 1 addition & 6 deletions python/core/symbology-ng/qgscategorizedsymbolrenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,11 @@ class QgsCategorizedSymbolRenderer : QgsFeatureRenderer
*/
void setSourceColorRamp( QgsColorRamp* ramp /Transfer/ );

//! @note added in 2.1
bool invertedColorRamp();
void setInvertedColorRamp( bool inverted );

/** Update the color ramp used and all symbols colors.
* @param ramp color ramp. Ownership is transferred to the renderer
* @param inverted set to true to invert ramp colors
* @note added in 2.5
*/
void updateColorRamp( QgsColorRamp* ramp /Transfer/, bool inverted = false );
void updateColorRamp( QgsColorRamp* ramp /Transfer/ );

//! items of symbology items in legend should be checkable
//! @note added in 2.5
Expand Down
2 changes: 1 addition & 1 deletion python/gui/gui.sip
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
%Include qgscharacterselectdialog.sip
%Include qgscolorbrewercolorrampdialog.sip
%Include qgscolorbutton.sip
%Include qgscolorbutton.sip
%Include qgscolorrampbutton.sip
%Include qgscolordialog.sip
%Include qgscolorschemelist.sip
%Include qgscolorswatchgrid.sip
Expand Down
210 changes: 210 additions & 0 deletions python/gui/qgscolorrampbutton.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@

/** \ingroup gui
* \class QgsColorRampButton
* A cross platform button subclass for selecting color ramps. Will open color ramp dialogs when clicked.
* Offers live updates to button from color ramp dialog. An attached drop down menu allows for access to
* saved color ramps, as well as option to invert the current color ramp and create new ramps.
* \note Added in version 3.0
*/

class QgsColorRampButton : QToolButton
{
%TypeHeaderCode
#include <qgscolorrampbutton.h>
%End

public:

/** Construct a new color ramp button.
* @param parent The parent QWidget for the dialog
* @param dialogTitle The title to show in the color ramp dialog
*/
QgsColorRampButton( QWidget *parent = nullptr, const QString& dialogTitle = QString() );

virtual ~QgsColorRampButton();

virtual QSize sizeHint() const;

/** Return the current color ramp.
* @returns currently selected color
* @see setColor
*/
QgsColorRamp* colorRamp() const;

/** Set the title for the color ramp dialog window.
* @param title Title for the color ramp dialog
* @see colorRampDialogTitle
*/
void setColorRampDialogTitle( const QString& title );

/** Returns the title for the color ramp dialog window.
* @returns title for the color ramp dialog
* @see setColorRampDialogTitle
*/
QString colorRampDialogTitle() const;

/** Returns whether the button accepts live updates from QgsColorRampDialog.
* @returns true if the button will be accepted immediately when the dialog's color ramp changes
* @see setAcceptLiveUpdates
*/
bool acceptLiveUpdates() const;

/** Sets whether the button accepts live updates from QgsColorRampDialog. Live updates may cause changes
* that are not undoable on QColorRampDialog cancel.
* @param accept set to true to enable live updates
* @see acceptLiveUpdates
*/
void setAcceptLiveUpdates( const bool accept );

/** Sets whether the drop down menu should be shown for the button. The default behaviour is to
* show the menu.
* @param showMenu set to false to hide the drop down menu
* @see showMenu
*/
void setShowMenu( const bool showMenu );

/** Returns whether the drop down menu is shown for the button.
* @returns true if drop down menu is shown
* @see setShowMenu
*/
bool showMenu() const;

/** Sets the default color ramp for the button, which is shown in the button's drop down menu for the
* "default color ramp" option.
* @param colorramp default color ramp for the button. Set to a null pointer to disable the default color
* ramp option.
* @see defaultColorRamp
*/
void setDefaultColorRamp( QgsColorRamp* colorramp );

/** Returns the default color ramp for the button, which is shown in the button's drop down menu for the
* "default color ramp" option.
* @returns default color ramp for the button. Returns a null pointer if the default color ramp
* option is disabled.
* @see setDefaultColorRamp
*/
QgsColorRamp* defaultColorRamp() const;

/** Sets whether a random colors option is shown in the button's drop down menu.
* @param showNull set to true to show a null option
* @see showRandom()
*/
void setShowRandomColorRamp( bool showRandom );

/** Returns whether random colors option is shown in the button's drop down menu.
* @see setShowRandom()
*/
bool showRandomColorRamp() const;

/** Returns true if the current color is null.
* @see setShowNull()
* @see showNull()
*/
bool isRandomColorRamp() const;

/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
* @param showNull set to true to show a null option
* @see showNull()
* @see isNull()
*/
void setShowNull( bool showNull );

/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
* @see setShowNull()
* @see isNull()
*/
bool showNull() const;

/** Returns true if the current color is null.
* @see setShowNull()
* @see showNull()
*/
bool isNull() const;

/** Sets the context string for the color ramp button. The context string is passed to all color ramp
* preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors
* based on the context.
* @param context context string for the color dialog button's color ramp preview icons
* @see context
*/
void setContext( const QString& context );

/** Returns the context string for the color ramp button. The context string is passed to all color ramp
* preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors
* based on the context.
* @returns context context string for the color dialog button's color ramp preview icons
* @see setContext
*/
QString context() const;

/** Sets whether the color ramp button only shows gradient type ramps
* @param gradientonly set to true to show only gradient type ramps
* @see showGradientOnly
*/
void setShowGradientOnly( bool gradientonly );

/** Returns true if the color ramp button only shows gradient type ramps
* @see setShowGradientOnly
*/
bool showGradientOnly() const;

public slots:

/** Sets the current color ramp for the button. Will emit a colorRampChanged() signal if the color ramp is different
* to the previous color ramp.
* @param colorramp New color ramp for the button
* @see setRandomColorRamp, setColorRampFromName, colorRamp
*/
void setColorRamp( QgsColorRamp* colorramp );

/** Sets the current color ramp for the button to random colors. Will emit a colorRampChanged() signal
* if the color ramp is different to the previous color ramp.
* @see setColorRamp, setColorRampFromName, colorRamp
*/
void setRandomColorRamp();

/** Sets the current color ramp for the button using a saved color ramp name. Will emit a colorRampChanged() signal
* if the color ramp is different to the previous color ramp.
* @param name Name of saved color ramp
* @see setColorRamp, setRandomColorRamp, colorRamp
*/
void setColorRampFromName( QString name = QString() );

/** Sets the background pixmap for the button based upon current color ramp.
* @param colorramp Color ramp for button background. If no color ramp is specified, the button's current
* color ramp will be used
*/
void setButtonBackground( QgsColorRamp* colorramp = nullptr );

/** Sets color ramp to the button's default color ramp, if set.
* @see setDefaultColorRamp
* @see defaultColorRamp
* @see setToNull()
*/
void setToDefaultColorRamp();

/** Sets color ramp to null.
* @see setToDefaultColorRamp()
*/
void setToNull();

signals:

/** Emitted whenever a new color ramp is set for the button. The color ramp is always valid.
* In case the new color ramp is the same, no signal is emitted to avoid infinite loops.
*/
void colorRampChanged();

protected:

bool event( QEvent *e );
void changeEvent( QEvent* e );
void showEvent( QShowEvent* e );
void resizeEvent( QResizeEvent *event );

/**
* Reimplemented to detect right mouse button clicks on the color ramp button
*/
void mousePressEvent( QMouseEvent* e );

};
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class QgsCategorizedSymbolRendererWidget : QgsRendererWidget

void changeCategorySymbol();

QgsColorRamp* getColorRamp();

QList<QgsSymbol*> selectedSymbols();
QgsCategoryList selectedCategoryList();
void refreshSymbolView();
Expand Down
Loading

0 comments on commit 9bb9b58

Please sign in to comment.