1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "colorbutton.h"
25 #include <QPaintEvent>
26 #include <QApplication>
28 #include <QStyleOptionFrame>
30 ColorButton::ColorButton(QWidget *parent) :
32 _color(QColor()) //default is white;
37 void ColorButton::setColor(const QColor &color) {
42 QColor ColorButton::color() const {
46 void ColorButton::paintEvent(QPaintEvent *event) {
47 //TODO: work on a good button style solution
48 QPushButton::paintEvent(event);
49 QPainter painter(this);
50 int border = QApplication::style()->pixelMetric(QStyle::PM_ButtonMargin);
52 // if twice buttonMargin (+2 px from the adjust) is greater than the button height
53 // then set the border to a third of the button height.
54 if(2*border+2 >= event->rect().height()) border = event->rect().height()/3;
58 brush = QBrush(_color);
60 brush = QBrush(_color, Qt::Dense4Pattern);
62 painter.fillRect(rect().adjusted(border+1, border+1, -border-1, -border-1), brush);
63 QStyleOptionFrame option;
64 option.state = QStyle::State_Sunken;
65 option.rect = rect().adjusted(border, border, -border, -border);
67 QApplication::style()->drawPrimitive(QStyle::PE_Frame, &option, &painter);