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
gradientbutton.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include <cstdio>
3 #include <QPainter>
4 #include "gradientbutton.h"
5 #include "gradientdialog.h"
6 
7 GradientButton::GradientButton(QWidget* parent, bool allowAlpha) :
8  QPushButton(parent), _alpha(allowAlpha)
9 {
10  connect(this, SIGNAL(clicked()), this, SLOT(pickGradient()));
11  setAutoDefault(false);
12  setDefault(false);
13  fromString("");
14  setText("");
15 }
16 
18  const int w = 130, h = 16;
19  QImage image(w, h, QImage::Format_RGB888);
20  QPainter painter(&image);
21  painter.setPen(Qt::NoPen);
22  painter.fillRect(0, 0, w, h, QColor(0, 0, 0));
23  if(_alpha){
24  for(int x = 1; x < w - 1; x += h){
25  painter.fillRect(x, 1, h / 2, h / 2 - 1, QColor(255, 255, 255));
26  painter.fillRect(x + h / 2, 1, h / 2, h / 2 - 1, QColor(192, 192, 192));
27  painter.fillRect(x, h / 2, h / 2, h / 2 - 1, QColor(192, 192, 192));
28  painter.fillRect(x + h / 2, h / 2, h / 2, h / 2 - 1, QColor(255, 255, 255));
29  }
30  }
31  QLinearGradient gradient(1., 0., w - 1, 0.);
32  gradient.setStops(_stops);
33  painter.fillRect(1, 1, w - 2, h - 2, QBrush(gradient));
34  setIconSize(QSize(w, h));
35  setIcon(QIcon(QPixmap::fromImage(image)));
36 }
37 
38 void GradientButton::fromString(const QString& string){
39  // Clear existing stops
40  _stops.clear();
41  // Parse string with sscanf
42  QByteArray cString = string.toLatin1();
43  const char* data = cString.data();
44  char pos = -1;
45  uchar a, r, g, b;
46  while(1){
47  int scanned = 0;
48  char newpos;
49  if(sscanf(data, "%hhd:%2hhx%2hhx%2hhx%2hhx%n", &newpos, &a, &r, &g, &b, &scanned) != 5)
50  break;
51  data += scanned;
52  // Don't allow stops out-of-order or past 100
53  if(newpos <= pos || newpos > 100)
54  break;
55  pos = newpos;
56  if(!_alpha)
57  a = 255;
58  _stops.append(QGradientStop(pos / 100., QColor(r, g, b, a)));
59  }
60  if(_stops.count() == 0){
61  // If nothing was read, try a single ARGB constant.
62  if(sscanf(data, "%2hhx%2hhx%2hhx%2hhx", &a, &r, &g, &b) == 4){
63  _stops.append(QGradientStop(0., QColor(r, g, b, a)));
64  _stops.append(QGradientStop(1., QColor(r, g, b, 0.)));
65  }
66  }
67  // If that still didn't work, fill with white
68  if(_stops.count() == 0){
69  _stops.append(QGradientStop(0., QColor(255, 255, 255)));
70  _stops.append(QGradientStop(1., QColor(255, 255, 255)));
71  }
72  // Make sure there are stops at 0 and at 100
73  if(_stops[0].first != 0.)
74  _stops.prepend(QGradientStop(0., _stops[0].second));
75  if(_stops.last().first != 1.)
76  _stops.prepend(QGradientStop(1., _stops.last().second));
77  updateImage();
78 }
79 
80 QString GradientButton::toString() const {
81  QStringList result;
82  foreach(const QGradientStop& stop, _stops){
83  QString string;
84  const QColor& color = stop.second;
85  result << string.sprintf("%d:%02x%02x%02x%02x", (int)round(stop.first * 100.f), color.alpha(), color.red(), color.green(), color.blue());
86  }
87  return result.join(" ");
88 }
89 
91  GradientDialog dialog(this);
92  _stops = dialog.getGradient(_stops);
93  updateImage();
94  emit gradientChanged();
95 }
96 
void fromString(const QString &string)
float x
Definition: main.c:66
QGradientStops getGradient(const QGradientStops &prevGradient)
unsigned char uchar
Definition: includes.h:24
GradientButton(QWidget *parent=0, bool allowAlpha=false)
QGradientStops _stops
QString toString() const