From 4339fdcd94783c08f7954ba6deeb796b7e2e5265 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Tue, 8 Jan 2013 11:59:47 +0100 Subject: [PATCH] Update QgsColorButton so it shows a color chooser on click --- python/gui/qgscolorbutton.sip | 66 +++++++++++++++++++++++++++++- src/gui/CMakeLists.txt | 3 +- src/gui/qgscolorbutton.cpp | 49 +++++++++++++++++++++- src/gui/qgscolorbutton.h | 76 +++++++++++++++++++++++++++++++++-- 4 files changed, 188 insertions(+), 6 deletions(-) diff --git a/python/gui/qgscolorbutton.sip b/python/gui/qgscolorbutton.sip index 4133215fb33..767f1dc0f16 100644 --- a/python/gui/qgscolorbutton.sip +++ b/python/gui/qgscolorbutton.sip @@ -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 ); }; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index ed31f92f6a8..81324d27cb3 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -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 diff --git a/src/gui/qgscolorbutton.cpp b/src/gui/qgscolorbutton.cpp index 379af70c72a..a8c1bf782ce 100644 --- a/src/gui/qgscolorbutton.cpp +++ b/src/gui/qgscolorbutton.cpp @@ -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; +} ////////////////// diff --git a/src/gui/qgscolorbutton.h b/src/gui/qgscolorbutton.h index 43ac9869db7..65385d934f4 100644 --- a/src/gui/qgscolorbutton.h +++ b/src/gui/qgscolorbutton.h @@ -15,26 +15,96 @@ #ifndef QGSCOLORBUTTON_H #define QGSCOLORBUTTON_H +#include #include #include /** \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; };