colorbutton.cpp
1 //------------------------------------------------------------------------------
2 // colorbutton.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
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 Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "colorbutton.h"
24 #include <QColorDialog>
25 
26 #define UTF8_FULL_BLOCK "\xE2\x96\x88"
27 
28 ColorButton::ColorButton(QWidget* parent)
29 : QPushButton(QString::fromUtf8(UTF8_FULL_BLOCK UTF8_FULL_BLOCK), parent)
30 {
31  connect( this, SIGNAL( clicked() ), this, SLOT( thisClicked() ) );
32 
33  setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
34  color.setNamedColor("#ffffff");
35  updateAppearance();
36 }
37 
38 QString ColorButton::colorHtml() const
39 {
40  return color.name();
41 }
42 
43 unsigned ColorButton::colorUnsigned() const
44 {
45  return color.rgb();
46 }
47 
48 void ColorButton::setColor(unsigned colorValue)
49 {
50  QColor newColor;
51  newColor.setRgb(colorValue);
52  updateColor(newColor);
53 }
54 
55 void ColorButton::setColorHtml(const QString& colorHtml)
56 {
57  QColor newColor;
58  newColor.setNamedColor(colorHtml);
59  updateColor(newColor);
60 }
61 
62 QSize ColorButton::sizeHint() const
63 {
64  QSize size = QPushButton::sizeHint();
65  // 50 should be the smallest size before the Mac changes to ugly buttons
66  size.setWidth(50);
67  return size;
68 }
69 
70 void ColorButton::thisClicked()
71 {
72  QColor colorTmp = QColorDialog::getColor(QColor(color), parentWidget());
73 
74  if(colorTmp.isValid())
75  {
76  updateColor(colorTmp);
77  }
78 }
79 
80 void ColorButton::updateAppearance()
81 {
82  static const QString COLOR_STYLE = "QPushButton { color : %1; }";
83 
84  QString styleSheet = COLOR_STYLE.arg(color.name());
85  setStyleSheet(styleSheet);
86 }
87 
88 void ColorButton::updateColor(const QColor& newColor)
89 {
90  QColor oldColor = color;
91  color = newColor;
92  updateAppearance();
93 
94  emit colorUpdated(oldColor, color);
95 }
void updateColor(const QColor &newColor)
Will always emit colorUpdated() signal.
Definition: colorbutton.cpp:88