Update QgsColorButton so it shows a color chooser on click

This commit is contained in:
Matthias Kuhn 2013-01-08 11:59:47 +01:00 committed by Larry Shaffer
parent 12f09471a4
commit 4339fdcd94
4 changed files with 188 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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