From a2504036e8a0a250193511e3da8f6d597874e7f2 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Tue, 18 Aug 2015 16:43:47 -0400 Subject: [PATCH] Improved color palette item widget. --- glabels/ColorPaletteItem.cpp | 78 +++++++++++++++++++++++++++++++----- glabels/ColorPaletteItem.h | 18 +++++---- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/glabels/ColorPaletteItem.cpp b/glabels/ColorPaletteItem.cpp index 9d55e9d..d97ae94 100644 --- a/glabels/ColorPaletteItem.cpp +++ b/glabels/ColorPaletteItem.cpp @@ -20,7 +20,8 @@ #include "ColorPaletteItem.h" -#include "ColorSwatch.h" + +#include // @@ -28,8 +29,13 @@ // namespace { - const double wSwatch = 20; - const double hSwatch = 20; + const int border = 4; + const int wSwatch = 25; + const int hSwatch = 25; + const QColor hoverColor( 170, 200, 255 ); + const QColor outlineColor( 0, 0, 0 ); + const QColor emptyOutlineColor( 192, 192, 192 ); + const int outlineWidthPixels = 1; } @@ -43,13 +49,11 @@ namespace glabels const QColor& color, const QString& tip, QWidget* parent ) - : QPushButton(parent), mId(id), mColor(color), mTip(tip) + : QWidget(parent), mId(id), mColor(color), mTip(tip), mHover(false) { - setFlat( true ); - setIcon( QIcon( ColorSwatch( wSwatch, hSwatch, color ) ) ); + setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ); + setMinimumSize( wSwatch+2*border+1, hSwatch+2*border+1 ); setToolTip( tip ); - - connect( this, SIGNAL(clicked()), this, SLOT(onClicked()) ); } @@ -64,15 +68,67 @@ namespace glabels mColor = color; mTip = tip; - setIcon( QIcon( ColorSwatch( wSwatch, hSwatch, color ) ) ); setToolTip( tip ); + update(); } /// - /// onClicked slot + /// Paint Event /// - void ColorPaletteItem::onClicked() + void ColorPaletteItem::paintEvent( QPaintEvent* event ) + { + QPainter painter(this); + + if ( mHover && mColor.alpha() ) + { + painter.setBrush( QBrush( hoverColor ) ); + painter.setPen( Qt::NoPen ); + painter.drawRect( rect() ); + } + + if ( mColor.alpha() ) + { + QPen pen( outlineColor ); + pen.setWidth( outlineWidthPixels ); + painter.setPen( pen ); + } + else + { + QPen pen( emptyOutlineColor ); + pen.setWidth( outlineWidthPixels ); + painter.setPen( pen ); + } + + painter.setBrush( QBrush( mColor ) ); + painter.drawRect( border, border, wSwatch, hSwatch ); + } + + + /// + /// Enter Event + /// + void ColorPaletteItem::enterEvent( QEvent* event ) + { + mHover = true; + update(); + } + + + /// + /// Leave Event + /// + void ColorPaletteItem::leaveEvent( QEvent* event ) + { + mHover = false; + update(); + } + + + /// + /// Mouse Press Event + /// + void ColorPaletteItem::mousePressEvent( QMouseEvent* event ) { emit activated( mId ); } diff --git a/glabels/ColorPaletteItem.h b/glabels/ColorPaletteItem.h index 7b851c9..a8f961e 100644 --- a/glabels/ColorPaletteItem.h +++ b/glabels/ColorPaletteItem.h @@ -21,7 +21,7 @@ #ifndef glabels_ColorPaletteItem_h #define glabels_ColorPaletteItem_h -#include +#include #include @@ -31,7 +31,7 @@ namespace glabels /// /// Color Palette Item /// - class ColorPaletteItem : public QPushButton + class ColorPaletteItem : public QWidget { Q_OBJECT @@ -53,7 +53,7 @@ namespace glabels ///////////////////////////////// - // Properties + // Public Methods ///////////////////////////////// public: void setColor( int id, @@ -62,10 +62,13 @@ namespace glabels ///////////////////////////////// - // Slots + // Event handlers ///////////////////////////////// - private slots: - void onClicked(); + protected: + void paintEvent( QPaintEvent* event ); + void enterEvent( QEvent* event ); + void leaveEvent( QEvent* event ); + void mousePressEvent( QMouseEvent* event ); ///////////////////////////////// @@ -75,7 +78,8 @@ namespace glabels int mId; QColor mColor; QString mTip; - + + bool mHover; };