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
gradientdialog.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include <QMessageBox>
3 #include <QPainter>
4 #include <QSettings>
5 #include "ckbsettings.h"
6 #include "gradientdialog.h"
7 #include "ui_gradientdialog.h"
8 
9 static const QString prefsPath = "Stored Gradients";
10 
12  QDialog(parent),
13  ui(new Ui::GradientDialog)
14 {
15  ui->setupUi(this);
16  connect(ui->widget, SIGNAL(currentChanged(QColor,bool,int)), this, SLOT(currentChanged(QColor,bool,int)));
17  connect(ui->stopColor, SIGNAL(colorChanged(QColor)), this, SLOT(colorChanged(QColor)));
18 
19  // Add built-in presets
20  Preset blackAndWhite("B & W", true);
21  blackAndWhite.gradient.append(QGradientStop(0., QColor(0, 0, 0)));
22  blackAndWhite.gradient.append(QGradientStop(1., QColor(255, 255, 255)));
23  Preset fade("Fade", true);
24  fade.gradient.append(QGradientStop(0., QColor(255, 255, 255)));
25  fade.gradient.append(QGradientStop(1., QColor(255, 255, 255, 0)));
26  Preset rainbow("Rainbow", true);
27  rainbow.gradient.append(QGradientStop(0.00, QColor(255, 0, 0)));
28  rainbow.gradient.append(QGradientStop(0.17, QColor(255, 255, 0)));
29  rainbow.gradient.append(QGradientStop(0.33, QColor(0, 255, 0)));
30  rainbow.gradient.append(QGradientStop(0.50, QColor(0, 255, 255)));
31  rainbow.gradient.append(QGradientStop(0.67, QColor(0, 0, 255)));
32  rainbow.gradient.append(QGradientStop(0.83, QColor(255, 0, 255)));
33  rainbow.gradient.append(QGradientStop(1.00, QColor(255, 0, 0)));
34  addPreset(blackAndWhite);
35  addPreset(fade);
36  addPreset(rainbow);
37 
38  // Load stored presets
39  CkbSettings settings(prefsPath);
40  foreach(const QString& name, settings.childGroups()){
41  QString pName = name.toLower();
42  if(presets.contains(pName))
43  continue;
44  SGroup group(settings, name);
45  // Create keys in a map to ensure correct order and no duplicates
46  QMap<int, QColor> colors;
47  foreach(const QString& position, settings.childKeys()){
48  int pos = position.toInt();
49  if(pos < 0 || pos > 100)
50  continue;
51  colors[pos] = settings.value(position).value<QColor>();
52  }
53  if(colors.count() < 2)
54  continue;
55  Preset preset(name);
56  QMapIterator<int, QColor> i(colors);
57  while(i.hasNext()){
58  i.next();
59  preset.gradient.append(QGradientStop(i.key() / 100., i.value()));
60  }
61  presets[pName] = preset;
62  }
63 
64  updatePresets();
65 }
66 
67 QGradientStops GradientDialog::getGradient(const QGradientStops& prevGradient){
68  ui->presetDelete->setEnabled(false);
69  ui->presetSave->setEnabled(true);
70  ui->widget->setStops(prevGradient);
71  exec();
72  if(result() != QDialog::Accepted)
73  return prevGradient;
74  return ui->widget->stops();
75 }
76 
78  // Save presets
79  CkbSettings settings(prefsPath, true);
80  QMapIterator<QString, Preset> i(presets);
81  while(i.hasNext()){
82  Preset preset = i.next().value();
83  if(!preset.builtIn){
84  SGroup group(settings, preset.name);
85  foreach(const QGradientStop& stop, preset.gradient)
86  settings.setValue(QString::number((int)(stop.first * 100.)), stop.second);
87  }
88  }
89  delete ui;
90 }
91 
92 QIcon GradientDialog::makeIcon(const Preset& preset){
93  // Paint gradient into a square
94  int w = 60, h = 60;
95  QImage image(w, h, QImage::Format_RGB888);
96  QPainter painter(&image);
97  // Draw background
98  for(int x = 0; x < w; x += 30){
99  for(int y = 0; y < h; y += 30){
100  painter.fillRect(x, y, 15, 15, QColor(255, 255, 255));
101  painter.fillRect(x + 15, y, 15, 15, QColor(192, 192, 192));
102  painter.fillRect(x, y + 15, 15, 15, QColor(192, 192, 192));
103  painter.fillRect(x + 15, y + 15, 15, 15, QColor(255, 255, 255));
104  }
105  }
106  // Draw gradient
107  painter.setPen(QColor(0, 0, 0));
108  QLinearGradient gradient(1., 1., w - 1., h - 1.);
109  gradient.setStops(preset.gradient);
110  painter.setBrush(gradient);
111  painter.drawRect(0, 0, w - 1, h - 1);
112  return QPixmap::fromImage(image);
113 }
114 
116  ui->presetList->clear();
117  QMapIterator<QString, Preset> i(presets);
118  while(i.hasNext()){
119  i.next();
120  const Preset& preset = i.value();
121  QListWidgetItem* item = new QListWidgetItem(makeIcon(preset), preset.name, ui->presetList);
122  if(currentPreset == i.key())
123  item->setSelected(true);
124  ui->presetList->addItem(item);
125  }
126 }
127 
128 void GradientDialog::currentChanged(QColor color, bool spontaneous, int position){
129  int pCount = ui->widget->stopCount();
130  if(pCount == 1)
131  ui->stopBox->setTitle("1 point");
132  else
133  ui->stopBox->setTitle(QString("%1 points").arg(pCount));
134  if(!color.isValid()){
135  ui->stopBox->setEnabled(false);
136  ui->stopPos->setValue(0);
137  ui->stopColor->color(QColor(0, 0, 0));
138  ui->stopOpacity->setValue(0);
139  } else {
140  ui->stopBox->setEnabled(true);
141  ui->stopPos->setValue(position);
142  ui->stopColor->color(color);
143  ui->stopOpacity->setValue(round(color.alphaF() * 100.f));
144  }
145  // Un-set current preset
146  if(spontaneous)
147  setPreset("");
148  setFocus();
149 }
150 
151 void GradientDialog::setPreset(const QString &newPreset){
152  if(currentPreset == newPreset)
153  return;
154  if(newPreset == ""){
155  ui->presetList->clearSelection();
156  ui->presetList->setCurrentItem(0);
158  QString curName = ui->presetName->text().trimmed();
159  if(curName == "" || (curName == current.name && current.builtIn))
160  ui->presetName->setText("Custom");
161  ui->presetDelete->setEnabled(false);
162  ui->presetSave->setEnabled(true);
163  } else {
164  Preset current = presets.value(newPreset);
165  ui->presetName->setText(current.name);
166  ui->presetDelete->setEnabled(!current.builtIn);
167  ui->presetSave->setEnabled(false);
168  ui->widget->setStops(current.gradient);
169  }
170  currentPreset = newPreset;
171  setFocus();
172 }
173 
174 void GradientDialog::on_presetList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous){
175  setPreset(current ? current->text().toLower() : "");
176 }
177 
179  if(currentPreset != "" && arg1.trimmed() != presets[currentPreset].name)
180  setPreset("");
181 }
182 
184  if(currentPreset != "")
185  return;
186  QString name = ui->presetName->text().trimmed();
187  if(name == "")
188  ui->presetName->setText(name = "Custom");
189  QString pName = name.toLower();
190  // Make sure not to overwrite a built-in preset
191  Preset previous = presets[pName];
192  if(previous.builtIn){
193  QMessageBox::warning(this, "Error", "Can't overwrite a built-in preset. Please choose a different name.");
194  setFocus();
195  return;
196  } else if(previous.name != ""){
197  // Warn when overwriting an existing preset
198  if(QMessageBox::warning(this, "Warning", QString("Preset \"%1\" already exists. Replace?").arg(name), QMessageBox::StandardButtons(QMessageBox::Save | QMessageBox::Cancel)) != QMessageBox::Save){
199  setFocus();
200  return;
201  }
202  }
203  presets[pName] = Preset(name, ui->widget->stops());
204  currentPreset = pName;
205  ui->presetDelete->setEnabled(true);
206  ui->presetSave->setEnabled(false);
207  updatePresets();
208  setFocus();
209 }
210 
213  if(current.name == "" || current.builtIn)
214  return;
215  if(QMessageBox::warning(this, "Warning", QString("Delete preset \"%1\"?").arg(current.name), QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No)) != QMessageBox::Yes){
216  setFocus();
217  return;
218  }
219  presets.remove(currentPreset);
220  currentPreset = "";
221  ui->presetDelete->setEnabled(false);
222  ui->presetSave->setEnabled(true);
223  updatePresets();
224  setFocus();
225 }
226 
228  int res = ui->widget->moveCurrent(arg1);
229  if(res != arg1)
230  ui->stopPos->setValue(res);
231  setPreset("");
232 }
233 
234 void GradientDialog::colorChanged(QColor color){
235  color.setAlphaF(ui->stopOpacity->value() / 100.f);
236  ui->widget->setCurrentColor(color);
237  setPreset("");
238 }
239 
242  setPreset("");
243 }
void currentChanged(QColor color, bool spontaneous, int position)
void setValue(const QString &key, const QVariant &value)
rgb * current
Definition: main.c:46
QListWidget * presetList
QStringList childKeys() const
float y
Definition: main.c:66
GradientDialogWidget * widget
QMap< QString, Preset > presets
QString currentPreset
const QColor & color() const
Definition: colorbutton.h:13
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
void on_presetDelete_clicked()
float x
Definition: main.c:66
void on_stopPos_valueChanged(int arg1)
void on_presetList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
QPushButton * presetSave
void setupUi(QDialog *GradientDialog)
void setStops(const QGradientStops &stops)
void on_stopOpacity_valueChanged(int arg1)
Ui::GradientDialog * ui
QLineEdit * presetName
QGradientStops getGradient(const QGradientStops &prevGradient)
void setPreset(const QString &newPreset)
void on_presetName_textEdited(const QString &arg1)
ColorButton * stopColor
void setCurrentColor(const QColor &color)
static const QString prefsPath
QGradientStops gradient
GradientDialog(QWidget *parent=0)
void on_presetSave_clicked()
QStringList childGroups() const
Definition: ckbsettings.cpp:89
QPushButton * presetDelete
void colorChanged(QColor color)
void addPreset(const Preset &preset)
QIcon makeIcon(const Preset &preset)