Skip to content

Commit

Permalink
Update QgsColorButton so it shows a color chooser on click
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn authored and dakcarto committed Mar 21, 2013
1 parent 12f0947 commit 4339fdc
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 6 deletions.
66 changes: 65 additions & 1 deletion python/gui/qgscolorbutton.sip
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,79 @@ class QgsColorButton : QToolButton
%End

public:
QgsColorButton( QWidget *parent = 0 );
/**
* Construct a new color button.
*
* @param parent The parent QWidget for the dialog
* @param cdt The title to show in the color chooser dialog
* @param cdo Options for the color chooser dialog
* @note changed in 1.9
*/
QgsColorButton( QWidget *parent = 0, QString cdt = "Select Color", QColorDialog::ColorDialogOptions cdo = 0 );
~QgsColorButton();

/**
* Specify the current color. Will emit a colorChanged signal if the color is different to the previous.
*
* @param color the new color
* @note added in 1.9
*/
void setColor( const QColor &color );
/**
* Return the currently selected color.
*
* @return the currently selected color
* @note added in 1.9
*/
QColor color() const;

/**
* Specify the options for the color chooser dialog (e.g. alpha).
*
* @param cdo Options for the color chooser dialog
* @note added in 1.9
*/
void setColorDialogOptions( QColorDialog::ColorDialogOptions cdo );

/**
* Returns the options for the color chooser dialog.
*
* @return Options for the color chooser dialog
* @note added in 1.9
*/
QColorDialog::ColorDialogOptions colorDialogOptions();

/**
* Set the title, which the color chooser dialog will show.
*
* @param cdt Title for the color chooser dialog
* @note added in 1.9
*/
void setColorDialogTitle( QString cdt );

/**
* Returns the title, which the color chooser dialog shows.
*
* @return Title for the color chooser dialog
* @note added in 1.9
*/
QString colorDialogTitle();

protected:
void paintEvent( QPaintEvent *e );

public slots:
void onButtonClicked();

signals:
/**
* Is emitted, whenever a new color is accepted. The color is always valid.
* In case the new color is the same, no signal is emitted, to avoid infinite loops.
*
* @param color New color
* @note added in 1.9
*/
void colorChanged( const QColor &color );
};


Expand Down
3 changes: 2 additions & 1 deletion src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ ENDIF (WITH_TOUCH)

SET(QGIS_GUI_MOC_HDRS

qgscolorbutton.h

raster/qgsrasterminmaxwidget.h
raster/qgspalettedrendererwidget.h
raster/qgsmultibandcolorrendererwidget.h
Expand Down Expand Up @@ -203,7 +205,6 @@ SET(QGIS_GUI_HDRS
qgisgui.h
qgisinterface.h
qgscharacterselectdialog.h
qgscolorbutton.h
qgscursors.h
qgsencodingfiledialog.h
qgsfiledropedit.h
Expand Down
49 changes: 48 additions & 1 deletion src/gui/qgscolorbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,31 @@
by Qt Designer to do the same thing.
*/

QgsColorButton::QgsColorButton( QWidget *parent )
QgsColorButton::QgsColorButton( QWidget *parent, QString cdt, QColorDialog::ColorDialogOptions cdo )
: QToolButton( parent )
, mColorDialogTitle( cdt )
, mColorDialogOptions( cdo )
{
setToolButtonStyle( Qt::ToolButtonTextOnly ); // decrease default button height
connect( this, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
}

QgsColorButton::~QgsColorButton()
{}

void QgsColorButton::onButtonClicked()
{
#if QT_VERSION >= 0x040500
QColor newColor = QColorDialog::getColor( color(), 0, mColorDialogTitle, mColorDialogOptions );
#else
QColor newColor = QColorDialog::getColor( color() );
#endif
if ( newColor.isValid() )
{
setColor( newColor );
}
}

/*!
Paints button in response to a paint event.
*/
Expand All @@ -62,10 +78,41 @@ void QgsColorButton::paintEvent( QPaintEvent *e )

void QgsColorButton::setColor( const QColor &color )
{
QColor oldColor = mColor;

mColor = color;
update();

if ( oldColor != mColor )
{
emit( colorChanged( mColor ) );
}
}

QColor QgsColorButton::color() const
{
return mColor;
}

void QgsColorButton::setColorDialogOptions( QColorDialog::ColorDialogOptions cdo )
{
mColorDialogOptions = cdo;
}

QColorDialog::ColorDialogOptions QgsColorButton::colorDialogOptions()
{
return mColorDialogOptions;
}

void QgsColorButton::setColorDialogTitle( QString cdt )
{
mColorDialogTitle = cdt;
}

QString QgsColorButton::colorDialogTitle()
{
return mColorDialogTitle;
}

//////////////////

Expand Down
76 changes: 73 additions & 3 deletions src/gui/qgscolorbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,96 @@
#ifndef QGSCOLORBUTTON_H
#define QGSCOLORBUTTON_H

#include <QColorDialog>
#include <QToolButton>
#include <QPushButton>

/** \ingroup gui
* A cross platform button subclass for selecting colors.
* A cross platform button subclass for selecting colors. Will open a color chooser dialog when clicked.
*/
class GUI_EXPORT QgsColorButton: public QToolButton
{
Q_OBJECT

public:
QgsColorButton( QWidget *parent = 0 );
/**
* Construct a new color button.
*
* @param parent The parent QWidget for the dialog
* @param cdt The title to show in the color chooser dialog
* @param cdo Options for the color chooser dialog
* @note changed in 1.9
*/
QgsColorButton( QWidget *parent = 0, QString cdt = tr( "Select Color" ), QColorDialog::ColorDialogOptions cdo = 0 );
~QgsColorButton();

/**
* Specify the current color. Will emit a colorChanged signal if the color is different to the previous.
*
* @param color the new color
* @note added in 1.9
*/
void setColor( const QColor &color );
QColor color() const { return mColor; }
/**
* Return the currently selected color.
*
* @return the currently selected color
* @note added in 1.9
*/
QColor color() const;

/**
* Specify the options for the color chooser dialog (e.g. alpha).
*
* @param cdo Options for the color chooser dialog
* @note added in 1.9
*/
void setColorDialogOptions( QColorDialog::ColorDialogOptions cdo );

/**
* Returns the options for the color chooser dialog.
*
* @return Options for the color chooser dialog
* @note added in 1.9
*/
QColorDialog::ColorDialogOptions colorDialogOptions();

/**
* Set the title, which the color chooser dialog will show.
*
* @param cdt Title for the color chooser dialog
* @note added in 1.9
*/
void setColorDialogTitle( QString cdt );

/**
* Returns the title, which the color chooser dialog shows.
*
* @return Title for the color chooser dialog
* @note added in 1.9
*/
QString colorDialogTitle();

protected:
void paintEvent( QPaintEvent *e );

public slots:
void onButtonClicked();

signals:
/**
* Is emitted, whenever a new color is accepted. The color is always valid.
* In case the new color is the same, no signal is emitted, to avoid infinite loops.
*
* @param color New color
* @note added in 1.9
*/
void colorChanged( const QColor &color );

private:
QString mColorDialogTitle;
QColor mColor;
QColorDialog::ColorDialogOptions mColorDialogOptions;
};


Expand Down

0 comments on commit 4339fdc

Please sign in to comment.