61e6c14db5d304c461a8418be11b9b7f851c23dd
[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   QPushButton::paintEvent(event);
48   QPainter painter(this);
49   int border = QApplication::style()->pixelMetric(QStyle::PM_ButtonMargin);
50   painter.fillRect(rect().adjusted(border+1, border+1, -border-1, -border-1), QBrush(_color));
51   QStyleOptionFrame option;
52   option.state = QStyle::State_Sunken;
53   option.rect = rect().adjusted(border, border, -border, -border);
54   //TODO: setBackground instead of the fillRect()
55   //painter.setBackground(_color);
56   //painter.setBackgroundMode(Qt::OpaqueMode);
57   //painter.fillRect(QApplication::style()->subElementRect(QStyle::SE_FrameContents, &option), QBrush(_color));
58   //qDebug() << option << QApplication::style()->subElementRect(QStyle::SE_PushButtonContents, &option);
59   QApplication::style()->drawPrimitive(QStyle::PE_Frame, &option, &painter);
60   //painter.fillRect(QApplication::style()->subElementRect(QStyle::SE_FrameContents, &option), QBrush(_color));
61   //border += QStyle::PM_DefaultFrameWidth;
62   //painter.fillRect(rect().adjusted(border, border, -border, -border), QBrush(_color));
63 }