40
40
41
41
QgsColorButtonV2::QgsColorButtonV2 ( QWidget *parent, QString cdt, QColorDialog::ColorDialogOptions cdo, QgsColorSchemeRegistry* registry )
42
42
: QToolButton( parent )
43
+ , mBehaviour( QgsColorButtonV2::ShowDialog )
43
44
, mColorDialogTitle( cdt.isEmpty() ? tr( " Select Color" ) : cdt )
44
45
, mColor( Qt::black )
45
46
, mDefaultColor( QColor() ) // default to invalid color
@@ -57,7 +58,7 @@ QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, QString cdt, QColorDialog::
57
58
58
59
setAcceptDrops ( true );
59
60
setMinimumSize ( QSize ( 24 , 16 ) );
60
- connect ( this , SIGNAL ( clicked () ), this , SLOT ( showColorDialog () ) );
61
+ connect ( this , SIGNAL ( clicked () ), this , SLOT ( buttonClicked () ) );
61
62
62
63
// setup dropdown menu
63
64
mMenu = new QMenu ( this );
@@ -330,6 +331,19 @@ QPixmap QgsColorButtonV2::createMenuIcon( const QColor color, const bool showChe
330
331
return pixmap;
331
332
}
332
333
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
+
333
347
void QgsColorButtonV2::prepareMenu ()
334
348
{
335
349
// 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 )
429
443
QToolButton::showEvent ( e );
430
444
}
431
445
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
+
432
454
void QgsColorButtonV2::setColor ( const QColor &color )
433
455
{
434
456
if ( !color.isValid () )
@@ -455,6 +477,11 @@ void QgsColorButtonV2::setColor( const QColor &color )
455
477
456
478
void QgsColorButtonV2::addRecentColor ( const QColor color )
457
479
{
480
+ if ( !color.isValid () )
481
+ {
482
+ return ;
483
+ }
484
+
458
485
// strip alpha from color
459
486
QColor opaqueColor = color;
460
487
opaqueColor.setAlpha ( 255 );
@@ -485,26 +512,41 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
485
512
486
513
bool useAlpha = ( mColorDialogOptions & QColorDialog::ShowAlphaChannel );
487
514
515
+ QSize currentIconSize;
488
516
// 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
497
527
#ifdef Q_WS_WIN
498
- mIconSize = QSize ( buttonSize.width () - 10 , height () - 14 );
528
+ mIconSize = QSize ( buttonSize.width () - 10 , height () - 14 );
499
529
#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 );
501
542
#endif
502
543
}
544
+
503
545
// create an icon pixmap
504
- QPixmap pixmap ( mIconSize );
546
+ QPixmap pixmap ( currentIconSize );
505
547
pixmap.fill ( Qt::transparent );
506
548
507
- QRect rect ( 0 , 0 , mIconSize .width (), mIconSize .height () );
549
+ QRect rect ( 0 , 0 , currentIconSize .width (), currentIconSize .height () );
508
550
QPainter p;
509
551
p.begin ( &pixmap );
510
552
p.setRenderHint ( QPainter::Antialiasing );
@@ -522,7 +564,7 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
522
564
p.drawRoundedRect ( rect, 3 , 3 );
523
565
p.end ();
524
566
525
- setIconSize ( mIconSize );
567
+ setIconSize ( currentIconSize );
526
568
setIcon ( pixmap );
527
569
}
528
570
@@ -578,6 +620,20 @@ QString QgsColorButtonV2::colorDialogTitle() const
578
620
return mColorDialogTitle ;
579
621
}
580
622
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
+
581
637
void QgsColorButtonV2::setDefaultColor ( const QColor color )
582
638
{
583
639
mDefaultColor = color;
0 commit comments