1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
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 <QStyleOptionFrame>
27 ColorButton::ColorButton(QWidget *parent) : QPushButton(parent) {
31 void ColorButton::setColor(const QColor &color) {
36 QColor ColorButton::color() const {
40 /* This has been heavily inspired by KDE's KColorButton, thanks! */
41 void ColorButton::paintEvent(QPaintEvent *) {
42 QPainter painter(this);
44 QStyleOptionButton opt;
45 initStyleOption(&opt);
46 opt.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
47 opt.features = QStyleOptionButton::None;
49 opt.features |= QStyleOptionButton::DefaultButton;
52 style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &painter, this);
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);
59 labelRect.getRect(&x, &y, &w, &h);
61 if(isChecked() || isDown()) {
62 x += style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal);
63 y += style()->pixelMetric(QStyle::PM_ButtonShiftVertical);
67 QBrush brush = isEnabled() ? color() : palette().color(backgroundRole());
68 qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, &brush);