Yearly copyright bump :)
[quassel.git] / src / uisupport / colorbutton.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
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 <QStyle>
25 #include <QStyleOptionFrame>
26
27 ColorButton::ColorButton(QWidget *parent) : QPushButton(parent) {
28
29 }
30
31 void ColorButton::setColor(const QColor &color) {
32   _color = color;
33   update();
34 }
35
36 QColor ColorButton::color() const {
37   return _color;
38 }
39
40 /* This has been heavily inspired by KDE's KColorButton, thanks! */
41 void ColorButton::paintEvent(QPaintEvent *) {
42   QPainter painter(this);
43
44   QStyleOptionButton opt;
45   initStyleOption(&opt);
46   opt.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
47   opt.features = QStyleOptionButton::None;
48   if(isDefault())
49     opt.features |= QStyleOptionButton::DefaultButton;
50
51   // Draw bevel
52   style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &painter, this);
53
54   // Calc geometry
55   QRect labelRect = style()->subElementRect(QStyle::SE_PushButtonContents, &opt, this);
56   int shift = style()->pixelMetric(QStyle::PM_ButtonMargin);
57   labelRect.adjust(shift, shift, -shift, -shift);
58   int x, y, w, h;
59   labelRect.getRect(&x, &y, &w, &h);
60
61   if(isChecked() || isDown()) {
62     x += style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal);
63     y += style()->pixelMetric(QStyle::PM_ButtonShiftVertical);
64   }
65
66   // Draw color rect
67   QBrush brush = isEnabled() ? color() : palette().color(backgroundRole());
68   qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, &brush);
69 }