Improved color palette item widget.

This commit is contained in:
Jim Evins
2015-08-18 16:43:47 -04:00
parent 4131bedba5
commit a2504036e8
2 changed files with 78 additions and 18 deletions
+67 -11
View File
@@ -20,7 +20,8 @@
#include "ColorPaletteItem.h"
#include "ColorSwatch.h"
#include <QPainter>
//
@@ -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 );
}
+10 -6
View File
@@ -21,7 +21,7 @@
#ifndef glabels_ColorPaletteItem_h
#define glabels_ColorPaletteItem_h
#include <QPushButton>
#include <QWidget>
#include <QColor>
@@ -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 );
/////////////////////////////////
@@ -76,6 +79,7 @@ namespace glabels
QColor mColor;
QString mTip;
bool mHover;
};