Skip to content

Commit

Permalink
Add options for color buttons, allowing them to be used in a swatch
Browse files Browse the repository at this point in the history
type mode.
  • Loading branch information
nyalldawson committed Sep 16, 2014
1 parent 7f3a514 commit e79ff32
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 14 deletions.
42 changes: 42 additions & 0 deletions python/gui/qgscolorbuttonv2.sip
Expand Up @@ -15,6 +15,14 @@ class QgsColorButtonV2: QToolButton

public:

/*! Specifies the behaviour when the button is clicked
*/
enum Behaviour
{
ShowDialog, /*!< show a color picker dialog when clicked */
SignalOnly /*!< emit colorClicked signal only, no dialog */
};

/**Construct a new color button.
* @param parent The parent QWidget for the dialog
* @param cdt The title to show in the color chooser dialog
Expand Down Expand Up @@ -70,6 +78,32 @@ class QgsColorButtonV2: QToolButton
*/
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 behaviour for when the button is clicked. The default behaviour is to show
* a color picker dialog.
* @param behaviour behaviour when button is clicked
* @see behaviour
*/
void setBehaviour( const Behaviour behaviour );

/**Returns the behaviour for when the button is clicked.
* @returns behaviour when button is clicked
* @see setBehaviour
*/
Behaviour behaviour() const;

/**Sets the default color for the button, which is shown in the button's drop down menu for the
* "default color" option.
* @param color default color for the button. Set to an invalid QColor to disable the default color
Expand Down Expand Up @@ -209,10 +243,18 @@ class QgsColorButtonV2: QToolButton
*/
void colorChanged( const QColor &color );

/**Emitted when the button is clicked, if the button's behaviour is set to SignalOnly
* @param color button color
* @see setBehaviour
* @see behaviour
*/
void colorClicked( const QColor color );

protected:

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

/**Returns a checkboard pattern pixmap for use as a background to transparent colors
*/
Expand Down
84 changes: 70 additions & 14 deletions src/gui/qgscolorbuttonv2.cpp
Expand Up @@ -40,6 +40,7 @@

QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, QString cdt, QColorDialog::ColorDialogOptions cdo, QgsColorSchemeRegistry* registry )
: QToolButton( parent )
, mBehaviour( QgsColorButtonV2::ShowDialog )
, mColorDialogTitle( cdt.isEmpty() ? tr( "Select Color" ) : cdt )
, mColor( Qt::black )
, mDefaultColor( QColor() ) //default to invalid color
Expand All @@ -57,7 +58,7 @@ QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, QString cdt, QColorDialog::

setAcceptDrops( true );
setMinimumSize( QSize( 24, 16 ) );
connect( this, SIGNAL( clicked() ), this, SLOT( showColorDialog() ) );
connect( this, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );

//setup dropdown menu
mMenu = new QMenu( this );
Expand Down Expand Up @@ -330,6 +331,19 @@ QPixmap QgsColorButtonV2::createMenuIcon( const QColor color, const bool showChe
return pixmap;
}

void QgsColorButtonV2::buttonClicked()
{
switch ( mBehaviour )
{
case ShowDialog:
showColorDialog();
return;
case SignalOnly:
emit colorClicked( mColor );
return;
}
}

void QgsColorButtonV2::prepareMenu()
{
//we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
Expand Down Expand Up @@ -429,6 +443,14 @@ void QgsColorButtonV2::showEvent( QShowEvent* e )
QToolButton::showEvent( e );
}

void QgsColorButtonV2::resizeEvent( QResizeEvent *event )
{
QToolButton::resizeEvent( event );
//recalculate icon size and redraw icon
mIconSize = QSize();
setButtonBackground( mColor );
}

void QgsColorButtonV2::setColor( const QColor &color )
{
if ( !color.isValid() )
Expand All @@ -455,6 +477,11 @@ void QgsColorButtonV2::setColor( const QColor &color )

void QgsColorButtonV2::addRecentColor( const QColor color )
{
if ( !color.isValid() )
{
return;
}

//strip alpha from color
QColor opaqueColor = color;
opaqueColor.setAlpha( 255 );
Expand Down Expand Up @@ -485,26 +512,41 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )

bool useAlpha = ( mColorDialogOptions & QColorDialog::ShowAlphaChannel );

QSize currentIconSize;
//icon size is button size with a small margin
if ( !mIconSize.isValid() )
{
//calculate size of push button part of widget (ie, without the menu dropdown button part)
QStyleOptionToolButton opt;
initStyleOption( &opt );
QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
this );
//make sure height of icon looks good under different platforms
if ( menu() )
{
if ( !mIconSize.isValid() )
{
//calculate size of push button part of widget (ie, without the menu dropdown button part)
QStyleOptionToolButton opt;
initStyleOption( &opt );
QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
this );
//make sure height of icon looks good under different platforms
#ifdef Q_WS_WIN
mIconSize = QSize( buttonSize.width() - 10, height() - 14 );
mIconSize = QSize( buttonSize.width() - 10, height() - 14 );
#else
mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
#endif
}
currentIconSize = mIconSize;
}
else
{
//no menu
#ifdef Q_WS_WIN
currentIconSize = QSize( width() - 10, height() - 14 );
#else
currentIconSize = QSize( width() - 10, height() - 12 );
#endif
}

//create an icon pixmap
QPixmap pixmap( mIconSize );
QPixmap pixmap( currentIconSize );
pixmap.fill( Qt::transparent );

QRect rect( 0, 0, mIconSize.width(), mIconSize.height() );
QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
QPainter p;
p.begin( &pixmap );
p.setRenderHint( QPainter::Antialiasing );
Expand All @@ -522,7 +564,7 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
p.drawRoundedRect( rect, 3, 3 );
p.end();

setIconSize( mIconSize );
setIconSize( currentIconSize );
setIcon( pixmap );
}

Expand Down Expand Up @@ -578,6 +620,20 @@ QString QgsColorButtonV2::colorDialogTitle() const
return mColorDialogTitle;
}

void QgsColorButtonV2::setShowMenu( const bool showMenu )
{
setMenu( showMenu ? mMenu : 0 );
setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
//force recalculation of icon size
mIconSize = QSize();
setButtonBackground( mColor );
}

void QgsColorButtonV2::setBehaviour( const QgsColorButtonV2::Behaviour behaviour )
{
mBehaviour = behaviour;
}

void QgsColorButtonV2::setDefaultColor( const QColor color )
{
mDefaultColor = color;
Expand Down
46 changes: 46 additions & 0 deletions src/gui/qgscolorbuttonv2.h
Expand Up @@ -42,6 +42,14 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton

public:

/*! Specifies the behaviour when the button is clicked
*/
enum Behaviour
{
ShowDialog = 0, /*!< show a color picker dialog when clicked */
SignalOnly /*!< emit colorClicked signal only, no dialog */
};

/**Construct a new color button.
* @param parent The parent QWidget for the dialog
* @param cdt The title to show in the color chooser dialog
Expand Down Expand Up @@ -99,6 +107,32 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
*/
void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = 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 { return menu() ? true : false; }

/**Sets the behaviour for when the button is clicked. The default behaviour is to show
* a color picker dialog.
* @param behaviour behaviour when button is clicked
* @see behaviour
*/
void setBehaviour( const Behaviour behaviour );

/**Returns the behaviour for when the button is clicked.
* @returns behaviour when button is clicked
* @see setBehaviour
*/
Behaviour behaviour() const { return mBehaviour; }

/**Sets the default color for the button, which is shown in the button's drop down menu for the
* "default color" option.
* @param color default color for the button. Set to an invalid QColor to disable the default color
Expand Down Expand Up @@ -238,10 +272,18 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
*/
void colorChanged( const QColor &color );

/**Emitted when the button is clicked, if the button's behaviour is set to SignalOnly
* @param color button color
* @see setBehaviour
* @see behaviour
*/
void colorClicked( const QColor color );

protected:

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

/**Returns a checkboard pattern pixmap for use as a background to transparent colors
*/
Expand Down Expand Up @@ -283,6 +325,8 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
void dropEvent( QDropEvent *e );

private:

Behaviour mBehaviour;
QString mColorDialogTitle;
QColor mColor;

Expand Down Expand Up @@ -334,6 +378,8 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton

private slots:

void buttonClicked();

void showColorDialog();

/**Sets color for button, if valid.
Expand Down

0 comments on commit e79ff32

Please sign in to comment.