X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fcolorbutton.cpp;h=3b521339a3ca58d40b960341cf7dac9de9807a36;hp=61e6c14db5d304c461a8418be11b9b7f851c23dd;hb=6830a6398744950fc33c63d9d194641f189bbec1;hpb=1aef6bf0d3d2a7ef469b9a9f68ceb891cc8a896c diff --git a/src/uisupport/colorbutton.cpp b/src/uisupport/colorbutton.cpp index 61e6c14d..3b521339 100644 --- a/src/uisupport/colorbutton.cpp +++ b/src/uisupport/colorbutton.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel IRC Team * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -21,16 +21,10 @@ #include "colorbutton.h" #include -#include -#include -#include #include #include -ColorButton::ColorButton(QWidget *parent) : - QPushButton(parent), - _color(QColor()) //default is white; - { +ColorButton::ColorButton(QWidget *parent) : QPushButton(parent) { } @@ -43,21 +37,33 @@ QColor ColorButton::color() const { return _color; } -void ColorButton::paintEvent(QPaintEvent *event) { - QPushButton::paintEvent(event); +/* This has been heavily inspired by KDE's KColorButton, thanks! */ +void ColorButton::paintEvent(QPaintEvent *) { QPainter painter(this); - int border = QApplication::style()->pixelMetric(QStyle::PM_ButtonMargin); - painter.fillRect(rect().adjusted(border+1, border+1, -border-1, -border-1), QBrush(_color)); - QStyleOptionFrame option; - option.state = QStyle::State_Sunken; - option.rect = rect().adjusted(border, border, -border, -border); - //TODO: setBackground instead of the fillRect() - //painter.setBackground(_color); - //painter.setBackgroundMode(Qt::OpaqueMode); - //painter.fillRect(QApplication::style()->subElementRect(QStyle::SE_FrameContents, &option), QBrush(_color)); - //qDebug() << option << QApplication::style()->subElementRect(QStyle::SE_PushButtonContents, &option); - QApplication::style()->drawPrimitive(QStyle::PE_Frame, &option, &painter); - //painter.fillRect(QApplication::style()->subElementRect(QStyle::SE_FrameContents, &option), QBrush(_color)); - //border += QStyle::PM_DefaultFrameWidth; - //painter.fillRect(rect().adjusted(border, border, -border, -border), QBrush(_color)); + + QStyleOptionButton opt; + initStyleOption(&opt); + opt.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised; + opt.features = QStyleOptionButton::None; + if(isDefault()) + opt.features |= QStyleOptionButton::DefaultButton; + + // Draw bevel + style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &painter, this); + + // Calc geometry + QRect labelRect = style()->subElementRect(QStyle::SE_PushButtonContents, &opt, this); + int shift = style()->pixelMetric(QStyle::PM_ButtonMargin); + labelRect.adjust(shift, shift, -shift, -shift); + int x, y, w, h; + labelRect.getRect(&x, &y, &w, &h); + + if(isChecked() || isDown()) { + x += style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal); + y += style()->pixelMetric(QStyle::PM_ButtonShiftVertical); + } + + // Draw color rect + QBrush brush = isEnabled() ? color() : palette().color(backgroundRole()); + qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, &brush); }