ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
colorbutton.cpp
Go to the documentation of this file.
1 #include "colorbutton.h"
2 #include <QColorDialog>
3 #include <QPainter>
4 
5 ColorButton::ColorButton(QWidget* parent, bool allowAlpha) :
6  QPushButton(parent), _alpha(allowAlpha), _setLabel(true), _bigIcons(false)
7 {
8  setAutoDefault(false);
9  setDefault(false);
10  updateImage();
11  // Pick color on click (use queued connection so that any on_*_clicked() events can be processed first)
12  connect(this, SIGNAL(clicked()), this, SLOT(pickColor()), Qt::QueuedConnection);
13 }
14 
15 void ColorButton::color(const QColor& newColor){
16  _color = newColor;
17  if(!_alpha)
18  _color.setAlpha(255);
19  updateImage();
20 }
21 
22 void ColorButton::allowAlpha(bool newAllowAlpha){
23  _alpha = newAllowAlpha;
24  updateImage();
25 }
26 
27 void ColorButton::bigIcons(bool newBigIcons){
28  _bigIcons = newBigIcons;
29  updateImage();
30 }
31 
33  if(!_color.isValid()){
34  setIcon(QIcon());
35  if(_setLabel)
36  setText("Change color...");
37  return;
38  }
39  const int w = 24, h = _bigIcons ? 24 : 12;
40  QImage image(w, h, QImage::Format_RGB888);
41  QPainter painter(&image);
42  painter.setPen(Qt::NoPen);
43  painter.fillRect(0, 0, w, h, QColor(0, 0, 0));
44  if(_alpha && _color.alpha() != 255){
45  painter.fillRect(1, 1, w / 2 - 1, h / 2 - 1, QColor(255, 255, 255));
46  painter.fillRect(w / 2, 1, w / 2 - 1, h / 2 - 1, QColor(192, 192, 192));
47  painter.fillRect(1, h / 2, w / 2 - 1, h / 2 - 1, QColor(192, 192, 192));
48  painter.fillRect(w / 2, h / 2, w / 2 - 1, h / 2 - 1, QColor(255, 255, 255));
49  }
50  painter.fillRect(1, 1, w - 2, h - 2, _color);
51  setIcon(QIcon(QPixmap::fromImage(image)));
52  if(_setLabel){
53  QString text = QString(" (%1, %2, %3)").arg(_color.red()).arg(_color.green()).arg(_color.blue());
54  if(_alpha)
55  text += QString(", %4%").arg(QString::number(_color.alphaF() * 100., 'f', 0));
56  setText(text);
57  }
58 }
59 
61  QColor newColor = QColorDialog::getColor(_color, this, QString(), QColorDialog::ColorDialogOptions(_alpha ? QColorDialog::ShowAlphaChannel : 0));
62  if(newColor.isValid()){
63  _color = newColor;
64  updateImage();
65  emit colorChanged(_color);
66  }
67 }
const QColor & color() const
Definition: colorbutton.h:13
QColor _color
Definition: colorbutton.h:33
void pickColor()
Definition: colorbutton.cpp:60
void colorChanged(QColor newColor)
bool allowAlpha() const
Definition: colorbutton.h:17
bool _bigIcons
Definition: colorbutton.h:34
void updateImage()
Definition: colorbutton.cpp:32
bool _setLabel
Definition: colorbutton.h:34
ColorButton(QWidget *parent=0, bool allowAlpha=false)
Definition: colorbutton.cpp:5
bool bigIcons() const
Definition: colorbutton.h:23