Skip to content
Permalink
Browse files

Update QgsColorButton so it shows a color chooser on click

  • Loading branch information
m-kuhn authored and dakcarto committed Mar 21, 2013
1 parent 12f0947 commit 4339fdcd94783c08f7954ba6deeb796b7e2e5265
Showing with 188 additions and 6 deletions.
  1. +65 −1 python/gui/qgscolorbutton.sip
  2. +2 −1 src/gui/CMakeLists.txt
  3. +48 −1 src/gui/qgscolorbutton.cpp
  4. +73 −3 src/gui/qgscolorbutton.h
@@ -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 );
};


@@ -113,6 +113,8 @@ ENDIF (WITH_TOUCH)

SET(QGIS_GUI_MOC_HDRS

qgscolorbutton.h

raster/qgsrasterminmaxwidget.h
raster/qgspalettedrendererwidget.h
raster/qgsmultibandcolorrendererwidget.h
@@ -203,7 +205,6 @@ SET(QGIS_GUI_HDRS
qgisgui.h
qgisinterface.h
qgscharacterselectdialog.h
qgscolorbutton.h
qgscursors.h
qgsencodingfiledialog.h
qgsfiledropedit.h
@@ -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.
*/
@@ -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;
}

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

@@ -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;
};


0 comments on commit 4339fdc

Please sign in to comment.
You can’t perform that action at this time.