58 #include "QtColorChooserButton.hh" 60 #include <QStyleOption> 61 #include <QStylePainter> 62 #include <QColorDialog> 66 QtColorChooserButton::QtColorChooserButton(QWidget *parent) :
67 QPushButton(parent), color_(0xE0, 0x20, 0x20) {
71 QtColorChooserButton::QtColorChooserButton(
const QString &text, QWidget *parent) :
72 QPushButton(text, parent), color_(0xE0, 0x20, 0x20) {
76 QtColorChooserButton::QtColorChooserButton(
const QIcon& icon,
const QString &text, QWidget *parent) :
77 QPushButton(icon, text, parent), color_(0xE0, 0x20, 0x20) {
82 QtColorChooserButton::~QtColorChooserButton() {
85 void QtColorChooserButton::init() {
86 connect(
this, SIGNAL( clicked() ),
this, SLOT( onClick() ) );
89 void QtColorChooserButton::onClick() {
90 QColor newColor = QColorDialog::getColor(color_,
this,
"Pick Color", QColorDialog::ShowAlphaChannel);
91 if (newColor.isValid()) {
93 emit colorChanged(color_);
97 void QtColorChooserButton::paintEvent(QPaintEvent *ev) {
98 QStyleOptionButton buttonOptions;
99 initStyleOption(&buttonOptions);
100 QStylePainter painter(
this);
102 const int textWd = buttonOptions.fontMetrics.width(buttonOptions.text);
103 QRect textRect = buttonOptions.rect;
104 textRect.setWidth(std::min(textRect.width(), textWd));
105 painter.drawItemText(textRect, Qt::TextSingleLine | Qt::TextShowMnemonic | Qt::AlignVCenter,
106 this->palette(), this->isEnabled(), buttonOptions.text, QPalette::ButtonText);
108 buttonOptions.rect.adjust(textWd, 0, 0, 0);
109 painter.drawControl(QStyle::CE_PushButtonBevel, buttonOptions);
111 QRect colorRect = this->style()->subElementRect(QStyle::SE_PushButtonFocusRect, &buttonOptions,
this);
112 QStyleOptionFrameV3 frameOptions;
113 frameOptions.state = QStyle::State_Sunken;
114 frameOptions.rect = colorRect;
120 if (this->isEnabled()) {
121 static const int checkerSize = 7;
122 static const QColor checkerColA(0xFF, 0xFF, 0xFF);
123 static const QColor checkerColB(0x30, 0x30, 0x30);
124 painter.setClipRect(colorRect, Qt::IntersectClip);
125 for (
int x = 0; x < colorRect.width() / checkerSize + 1; ++x) {
126 for (
int y = 0; y < colorRect.height() / checkerSize + 1; ++y) {
127 painter.fillRect(colorRect.x() + x * checkerSize, colorRect.y() + y * checkerSize,
128 checkerSize, checkerSize,
129 (x % 2 == y % 2) ? checkerColA : checkerColB);
133 painter.fillRect(colorRect, color_);
135 painter.drawPrimitive(QStyle::PE_FrameButtonBevel, frameOptions);
Clear all attribute bits.