Fixed those nasty "Client::updateLastSeen(): Unknown buffer $bufferId" messages.
[quassel.git] / src / uisupport / colorbutton.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "colorbutton.h"
22
23 #include <QPainter>
24 #include <QDebug>
25 #include <QPaintEvent>
26 #include <QApplication>
27 #include <QStyle>
28 #include <QStyleOptionFrame>
29
30 ColorButton::ColorButton(QWidget *parent) :
31     QPushButton(parent),
32     _color(QColor()) //default is white; 
33     {
34
35 }
36
37 void ColorButton::setColor(const QColor &color) {
38   _color = color;
39   update();
40 }
41
42 QColor ColorButton::color() const {
43   return _color;
44 }
45
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);
51
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;
55
56   QBrush brush;
57   if(isEnabled()) {
58     brush = QBrush(_color);
59   } else {
60     brush = QBrush(_color, Qt::Dense4Pattern);
61   }
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);
66
67   QApplication::style()->drawPrimitive(QStyle::PE_Frame, &option, &painter);
68 }