Developer Documentation
QtColorChooserButton.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 #include "QtColorChooserButton.hh"
44 
45 #include <QStyleOption>
46 #include <QStylePainter>
47 #include <QColorDialog>
48 
49 #include <iostream>
50 
51 QtColorChooserButton::QtColorChooserButton(QWidget *parent) :
52  QPushButton(parent), color_(0xE0, 0x20, 0x20) {
53 
54  init();
55 }
56 QtColorChooserButton::QtColorChooserButton(const QString &text, QWidget *parent) :
57  QPushButton(text, parent), color_(0xE0, 0x20, 0x20) {
58 
59  init();
60 }
61 QtColorChooserButton::QtColorChooserButton(const QIcon& icon, const QString &text, QWidget *parent) :
62  QPushButton(icon, text, parent), color_(0xE0, 0x20, 0x20) {
63 
64  init();
65 }
66 
67 QtColorChooserButton::~QtColorChooserButton() {
68 }
69 
70 void QtColorChooserButton::init() {
71  connect(this, SIGNAL( clicked() ), this, SLOT( onClick() ) );
72 }
73 
74 void QtColorChooserButton::onClick() {
75  QColor newColor = QColorDialog::getColor(color_, this, "Pick Color", QColorDialog::ShowAlphaChannel);
76  if (newColor.isValid()) {
77  color_ = newColor;
78  emit colorChanged(color_);
79  }
80 }
81 
82 void QtColorChooserButton::paintEvent(QPaintEvent *ev) {
83  QStyleOptionButton buttonOptions;
84  initStyleOption(&buttonOptions);
85  QStylePainter painter(this);
86 
87  const int textWd = buttonOptions.fontMetrics.width(buttonOptions.text);
88  QRect textRect = buttonOptions.rect;
89  textRect.setWidth(std::min(textRect.width(), textWd));
90  painter.drawItemText(textRect, Qt::TextSingleLine | Qt::TextShowMnemonic | Qt::AlignVCenter,
91  this->palette(), this->isEnabled(), buttonOptions.text, QPalette::ButtonText);
92 
93  buttonOptions.rect.adjust(textWd, 0, 0, 0);
94  painter.drawControl(QStyle::CE_PushButtonBevel, buttonOptions);
95 
96  QRect colorRect = this->style()->subElementRect(QStyle::SE_PushButtonFocusRect, &buttonOptions, this);
97  QStyleOptionFrame frameOptions;
98  frameOptions.state = QStyle::State_Sunken;
99  frameOptions.rect = colorRect;
100  frameOptions.features = QStyleOptionFrame::None;
101 
102  /*
103  * Draw checker board pattern.
104  */
105  if (this->isEnabled()) {
106  static const int checkerSize = 7;
107  static const QColor checkerColA(0xFF, 0xFF, 0xFF);
108  static const QColor checkerColB(0x30, 0x30, 0x30);
109  painter.setClipRect(colorRect, Qt::IntersectClip);
110  for (int x = 0; x < colorRect.width() / checkerSize + 1; ++x) {
111  for (int y = 0; y < colorRect.height() / checkerSize + 1; ++y) {
112  painter.fillRect(colorRect.x() + x * checkerSize, colorRect.y() + y * checkerSize,
113  checkerSize, checkerSize,
114  (x % 2 == y % 2) ? checkerColA : checkerColB);
115  }
116  }
117 
118  painter.fillRect(colorRect, color_);
119  }
120  painter.drawPrimitive(QStyle::PE_FrameButtonBevel, frameOptions);
121 }
Clear all attribute bits.
Definition: Attributes.hh:81