Skip to content

Commit e79ff32

Browse files
committedSep 16, 2014
Add options for color buttons, allowing them to be used in a swatch
type mode.
1 parent 7f3a514 commit e79ff32

File tree

3 files changed

+158
-14
lines changed

3 files changed

+158
-14
lines changed
 

‎python/gui/qgscolorbuttonv2.sip

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class QgsColorButtonV2: QToolButton
1515

1616
public:
1717

18+
/*! Specifies the behaviour when the button is clicked
19+
*/
20+
enum Behaviour
21+
{
22+
ShowDialog, /*!< show a color picker dialog when clicked */
23+
SignalOnly /*!< emit colorClicked signal only, no dialog */
24+
};
25+
1826
/**Construct a new color button.
1927
* @param parent The parent QWidget for the dialog
2028
* @param cdt The title to show in the color chooser dialog
@@ -70,6 +78,32 @@ class QgsColorButtonV2: QToolButton
7078
*/
7179
void setAcceptLiveUpdates( const bool accept );
7280

81+
/**Sets whether the drop down menu should be shown for the button. The default behaviour is to
82+
* show the menu.
83+
* @param showMenu set to false to hide the drop down menu
84+
* @see showMenu
85+
*/
86+
void setShowMenu( const bool showMenu );
87+
88+
/**Returns whether the drop down menu is shown for the button.
89+
* @returns true if drop down menu is shown
90+
* @see setShowMenu
91+
*/
92+
bool showMenu() const;
93+
94+
/**Sets the behaviour for when the button is clicked. The default behaviour is to show
95+
* a color picker dialog.
96+
* @param behaviour behaviour when button is clicked
97+
* @see behaviour
98+
*/
99+
void setBehaviour( const Behaviour behaviour );
100+
101+
/**Returns the behaviour for when the button is clicked.
102+
* @returns behaviour when button is clicked
103+
* @see setBehaviour
104+
*/
105+
Behaviour behaviour() const;
106+
73107
/**Sets the default color for the button, which is shown in the button's drop down menu for the
74108
* "default color" option.
75109
* @param color default color for the button. Set to an invalid QColor to disable the default color
@@ -209,10 +243,18 @@ class QgsColorButtonV2: QToolButton
209243
*/
210244
void colorChanged( const QColor &color );
211245

246+
/**Emitted when the button is clicked, if the button's behaviour is set to SignalOnly
247+
* @param color button color
248+
* @see setBehaviour
249+
* @see behaviour
250+
*/
251+
void colorClicked( const QColor color );
252+
212253
protected:
213254

214255
void changeEvent( QEvent* e );
215256
void showEvent( QShowEvent* e );
257+
void resizeEvent( QResizeEvent *event );
216258

217259
/**Returns a checkboard pattern pixmap for use as a background to transparent colors
218260
*/

‎src/gui/qgscolorbuttonv2.cpp

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

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

5859
setAcceptDrops( true );
5960
setMinimumSize( QSize( 24, 16 ) );
60-
connect( this, SIGNAL( clicked() ), this, SLOT( showColorDialog() ) );
61+
connect( this, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
6162

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

334+
void QgsColorButtonV2::buttonClicked()
335+
{
336+
switch ( mBehaviour )
337+
{
338+
case ShowDialog:
339+
showColorDialog();
340+
return;
341+
case SignalOnly:
342+
emit colorClicked( mColor );
343+
return;
344+
}
345+
}
346+
333347
void QgsColorButtonV2::prepareMenu()
334348
{
335349
//we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
@@ -429,6 +443,14 @@ void QgsColorButtonV2::showEvent( QShowEvent* e )
429443
QToolButton::showEvent( e );
430444
}
431445

446+
void QgsColorButtonV2::resizeEvent( QResizeEvent *event )
447+
{
448+
QToolButton::resizeEvent( event );
449+
//recalculate icon size and redraw icon
450+
mIconSize = QSize();
451+
setButtonBackground( mColor );
452+
}
453+
432454
void QgsColorButtonV2::setColor( const QColor &color )
433455
{
434456
if ( !color.isValid() )
@@ -455,6 +477,11 @@ void QgsColorButtonV2::setColor( const QColor &color )
455477

456478
void QgsColorButtonV2::addRecentColor( const QColor color )
457479
{
480+
if ( !color.isValid() )
481+
{
482+
return;
483+
}
484+
458485
//strip alpha from color
459486
QColor opaqueColor = color;
460487
opaqueColor.setAlpha( 255 );
@@ -485,26 +512,41 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
485512

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

515+
QSize currentIconSize;
488516
//icon size is button size with a small margin
489-
if ( !mIconSize.isValid() )
490-
{
491-
//calculate size of push button part of widget (ie, without the menu dropdown button part)
492-
QStyleOptionToolButton opt;
493-
initStyleOption( &opt );
494-
QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
495-
this );
496-
//make sure height of icon looks good under different platforms
517+
if ( menu() )
518+
{
519+
if ( !mIconSize.isValid() )
520+
{
521+
//calculate size of push button part of widget (ie, without the menu dropdown button part)
522+
QStyleOptionToolButton opt;
523+
initStyleOption( &opt );
524+
QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
525+
this );
526+
//make sure height of icon looks good under different platforms
497527
#ifdef Q_WS_WIN
498-
mIconSize = QSize( buttonSize.width() - 10, height() - 14 );
528+
mIconSize = QSize( buttonSize.width() - 10, height() - 14 );
499529
#else
500-
mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
530+
mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
531+
#endif
532+
}
533+
currentIconSize = mIconSize;
534+
}
535+
else
536+
{
537+
//no menu
538+
#ifdef Q_WS_WIN
539+
currentIconSize = QSize( width() - 10, height() - 14 );
540+
#else
541+
currentIconSize = QSize( width() - 10, height() - 12 );
501542
#endif
502543
}
544+
503545
//create an icon pixmap
504-
QPixmap pixmap( mIconSize );
546+
QPixmap pixmap( currentIconSize );
505547
pixmap.fill( Qt::transparent );
506548

507-
QRect rect( 0, 0, mIconSize.width(), mIconSize.height() );
549+
QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
508550
QPainter p;
509551
p.begin( &pixmap );
510552
p.setRenderHint( QPainter::Antialiasing );
@@ -522,7 +564,7 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
522564
p.drawRoundedRect( rect, 3, 3 );
523565
p.end();
524566

525-
setIconSize( mIconSize );
567+
setIconSize( currentIconSize );
526568
setIcon( pixmap );
527569
}
528570

@@ -578,6 +620,20 @@ QString QgsColorButtonV2::colorDialogTitle() const
578620
return mColorDialogTitle;
579621
}
580622

623+
void QgsColorButtonV2::setShowMenu( const bool showMenu )
624+
{
625+
setMenu( showMenu ? mMenu : 0 );
626+
setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
627+
//force recalculation of icon size
628+
mIconSize = QSize();
629+
setButtonBackground( mColor );
630+
}
631+
632+
void QgsColorButtonV2::setBehaviour( const QgsColorButtonV2::Behaviour behaviour )
633+
{
634+
mBehaviour = behaviour;
635+
}
636+
581637
void QgsColorButtonV2::setDefaultColor( const QColor color )
582638
{
583639
mDefaultColor = color;

‎src/gui/qgscolorbuttonv2.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
4242

4343
public:
4444

45+
/*! Specifies the behaviour when the button is clicked
46+
*/
47+
enum Behaviour
48+
{
49+
ShowDialog = 0, /*!< show a color picker dialog when clicked */
50+
SignalOnly /*!< emit colorClicked signal only, no dialog */
51+
};
52+
4553
/**Construct a new color button.
4654
* @param parent The parent QWidget for the dialog
4755
* @param cdt The title to show in the color chooser dialog
@@ -99,6 +107,32 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
99107
*/
100108
void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
101109

110+
/**Sets whether the drop down menu should be shown for the button. The default behaviour is to
111+
* show the menu.
112+
* @param showMenu set to false to hide the drop down menu
113+
* @see showMenu
114+
*/
115+
void setShowMenu( const bool showMenu );
116+
117+
/**Returns whether the drop down menu is shown for the button.
118+
* @returns true if drop down menu is shown
119+
* @see setShowMenu
120+
*/
121+
bool showMenu() const { return menu() ? true : false; }
122+
123+
/**Sets the behaviour for when the button is clicked. The default behaviour is to show
124+
* a color picker dialog.
125+
* @param behaviour behaviour when button is clicked
126+
* @see behaviour
127+
*/
128+
void setBehaviour( const Behaviour behaviour );
129+
130+
/**Returns the behaviour for when the button is clicked.
131+
* @returns behaviour when button is clicked
132+
* @see setBehaviour
133+
*/
134+
Behaviour behaviour() const { return mBehaviour; }
135+
102136
/**Sets the default color for the button, which is shown in the button's drop down menu for the
103137
* "default color" option.
104138
* @param color default color for the button. Set to an invalid QColor to disable the default color
@@ -238,10 +272,18 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
238272
*/
239273
void colorChanged( const QColor &color );
240274

275+
/**Emitted when the button is clicked, if the button's behaviour is set to SignalOnly
276+
* @param color button color
277+
* @see setBehaviour
278+
* @see behaviour
279+
*/
280+
void colorClicked( const QColor color );
281+
241282
protected:
242283

243284
void changeEvent( QEvent* e );
244285
void showEvent( QShowEvent* e );
286+
void resizeEvent( QResizeEvent *event );
245287

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

285327
private:
328+
329+
Behaviour mBehaviour;
286330
QString mColorDialogTitle;
287331
QColor mColor;
288332

@@ -334,6 +378,8 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
334378

335379
private slots:
336380

381+
void buttonClicked();
382+
337383
void showColorDialog();
338384

339385
/**Sets color for button, if valid.

0 commit comments

Comments
 (0)
Please sign in to comment.