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 ColorButton::ColorButton(QWidget* parent)
27 : QPushButton(parent)
28 {
29  connect( this, SIGNAL( clicked() ), this, SLOT( thisClicked() ) );
30 
31  color.setNamedColor("#ffffff");
32  updateAppearance();
33 }
34 
35 QString ColorButton::colorHtml() const
36 {
37  return color.name();
38 }
39 
40 unsigned ColorButton::colorUnsigned() const
41 {
42  return color.rgb();
43 }
44 
45 void ColorButton::setColor(unsigned colorValue)
46 {
47  QColor newColor;
48  newColor.setRgb(colorValue);
49  updateColor(newColor);
50 }
51 
52 void ColorButton::setColorHtml(const QString& colorHtml)
53 {
54  QColor newColor;
55  newColor.setNamedColor(colorHtml);
56  updateColor(newColor);
57 }
58 
59 void ColorButton::thisClicked()
60 {
61  QColor colorTmp = QColorDialog::getColor(QColor(color), parentWidget());
62 
63  if(colorTmp.isValid())
64  {
65  updateColor(colorTmp);
66  }
67 }
68 
69 void ColorButton::updateAppearance()
70 {
71  static const QString COLOR_STYLE = "QPushButton { background-color : %1; }";
72 
73  QString styleSheet = COLOR_STYLE.arg(color.name());
74  setStyleSheet(styleSheet);
75 }
76 
77 void ColorButton::updateColor(const QColor& newColor)
78 {
79  QColor oldColor = color;
80  color = newColor;
81  updateAppearance();
82 
83  emit colorUpdated(oldColor, color);
84 }
void updateColor(const QColor &newColor)
Will always emit colorUpdated() signal.
Definition: colorbutton.cpp:77