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 Class Reference

#include <src/ckb/gradientbutton.h>

+ Inheritance diagram for GradientButton:
+ Collaboration diagram for GradientButton:

Signals

void gradientChanged ()
 

Public Member Functions

 GradientButton (QWidget *parent=0, bool allowAlpha=false)
 
void fromString (const QString &string)
 
QString toString () const
 

Private Slots

void pickGradient ()
 

Private Member Functions

void updateImage ()
 

Private Attributes

QGradientStops _stops
 
bool _alpha
 

Detailed Description

Definition at line 6 of file gradientbutton.h.

Constructor & Destructor Documentation

GradientButton::GradientButton ( QWidget parent = 0,
bool  allowAlpha = false 
)
explicit

Definition at line 7 of file gradientbutton.cpp.

References fromString(), and pickGradient().

7  :
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 }
void fromString(const QString &string)

+ Here is the call graph for this function:

Member Function Documentation

void GradientButton::fromString ( const QString &  string)

Definition at line 38 of file gradientbutton.cpp.

References _alpha, _stops, and updateImage().

Referenced by GradientButton().

38  {
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 }
unsigned char uchar
Definition: includes.h:24
QGradientStops _stops

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void GradientButton::gradientChanged ( )
signal

Definition at line 132 of file moc_gradientbutton.cpp.

Referenced by pickGradient().

133 {
134  QMetaObject::activate(this, &staticMetaObject, 0, Q_NULLPTR);
135 }

+ Here is the caller graph for this function:

void GradientButton::pickGradient ( )
privateslot

Definition at line 90 of file gradientbutton.cpp.

References _stops, GradientDialog::getGradient(), gradientChanged(), and updateImage().

Referenced by GradientButton().

90  {
91  GradientDialog dialog(this);
92  _stops = dialog.getGradient(_stops);
93  updateImage();
94  emit gradientChanged();
95 }
QGradientStops _stops

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

QString GradientButton::toString ( ) const

Definition at line 80 of file gradientbutton.cpp.

References _stops.

Referenced by AnimSettingDialog::updateParam().

80  {
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 }
QGradientStops _stops

+ Here is the caller graph for this function:

void GradientButton::updateImage ( )
private

Definition at line 17 of file gradientbutton.cpp.

References _alpha, _stops, and x.

Referenced by fromString(), and pickGradient().

17  {
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 }
float x
Definition: main.c:66
QGradientStops _stops

+ Here is the caller graph for this function:

Field Documentation

bool GradientButton::_alpha
private

Definition at line 23 of file gradientbutton.h.

Referenced by fromString(), and updateImage().

QGradientStops GradientButton::_stops
private

Definition at line 22 of file gradientbutton.h.

Referenced by fromString(), pickGradient(), toString(), and updateImage().


The documentation for this class was generated from the following files: