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
mperfwidget.cpp
Go to the documentation of this file.
1 #include "mperfwidget.h"
2 #include "ui_mperfwidget.h"
3 #include "modeselectdialog.h"
4 #include <cmath>
5 
6 const static QString xyLinkPath = "UI/DPI/UnlinkXY";
7 
9  QWidget(parent), ui(new Ui::MPerfWidget), perf(0), profile(0), _xyLink(!CkbSettings::get(xyLinkPath).toBool()), colorLink(false), isSetting(false) {
10  ui->setupUi(this);
11  ui->xyBox->setChecked(!_xyLink);
12  // Set up DPI stages
19  ui->iButtonO->setLabel(false);
20  ui->iButtonO->bigIcons(true);
21  ui->iButtonO->allowAlpha(true);
22  for(int i = 0; i < DPI_COUNT; i++){
23  stages[i].indicator->setLabel(false);
24  stages[i].indicator->bigIcons(true);
25  stages[i].indicator->allowAlpha(true);
26  // Map signals
27  connect(stages[i].indicator, SIGNAL(clicked(bool)), &buttonMapper1, SLOT(map()));
28  connect(stages[i].indicator, SIGNAL(colorChanged(QColor)), &buttonMapper2, SLOT(map()));
29  connect(stages[i].xSlider, SIGNAL(valueChanged(int)), &sliderXMapper, SLOT(map()));
30  connect(stages[i].ySlider, SIGNAL(valueChanged(int)), &sliderYMapper, SLOT(map()));
31  connect(stages[i].xBox, SIGNAL(valueChanged(int)), &boxXMapper, SLOT(map()));
32  connect(stages[i].yBox, SIGNAL(valueChanged(int)), &boxYMapper, SLOT(map()));
33  if(stages[i].enableCheck)
34  // Sniper has no enable
35  connect(stages[i].enableCheck, SIGNAL(stateChanged(int)), &enableMapper, SLOT(map()));
36  // Set names
37  buttonMapper1.setMapping(stages[i].indicator, i);
38  buttonMapper2.setMapping(stages[i].indicator, i);
39  sliderXMapper.setMapping(stages[i].xSlider, i);
40  sliderYMapper.setMapping(stages[i].ySlider, i);
41  boxXMapper.setMapping(stages[i].xBox, i);
42  boxYMapper.setMapping(stages[i].yBox, i);
43  if(stages[i].enableCheck)
44  enableMapper.setMapping(stages[i].enableCheck, i);
45  }
46  // Connect to slots
47  connect(&buttonMapper1, SIGNAL(mapped(int)), this, SLOT(colorClicked(int)));
48  connect(&buttonMapper2, SIGNAL(mapped(int)), this, SLOT(colorChanged(int)));
49  connect(&sliderXMapper, SIGNAL(mapped(int)), this, SLOT(sliderXMoved(int)));
50  connect(&sliderYMapper, SIGNAL(mapped(int)), this, SLOT(sliderYMoved(int)));
51  connect(&boxXMapper, SIGNAL(mapped(int)), this, SLOT(boxXChanged(int)));
52  connect(&boxYMapper, SIGNAL(mapped(int)), this, SLOT(boxYChanged(int)));
53  connect(&enableMapper, SIGNAL(mapped(int)), this, SLOT(enableChanged(int)));
54 }
55 
57  delete ui;
58 }
59 
60 void MPerfWidget::setPerf(KbPerf *newPerf, KbProfile *newProfile){
61  perf = newPerf;
62  profile = newProfile;
63  ui->spinBox->setValue(round(perf->iOpacity() * 100.f));
64  for(int i = 0; i < DPI_COUNT; i++){
66  bool oldLink = _xyLink;
67  // Don't force X/Y to the same value if they were set differently in the past
68  _xyLink = false;
69  stages[i].xBox->setValue(perf->dpi(i).x());
70  stages[i].yBox->setValue(perf->dpi(i).y());
71  _xyLink = oldLink;
72  if(stages[i].enableCheck)
73  stages[i].enableCheck->setChecked(perf->dpiEnabled(i));
74  }
76  ui->aSnapBox->setChecked(perf->angleSnap());
77  ui->lHeightBox->setCurrentIndex(perf->liftHeight() - 1);
78  ui->indicBox->setChecked(perf->dpiIndicator());
79 }
80 
81 void MPerfWidget::colorClicked(int index){
82  // Set all colors at once if Alt is held down
83  colorLink = !!(qApp->keyboardModifiers() & Qt::AltModifier);
84 }
85 
86 void MPerfWidget::colorChanged(int index){
87  QColor color = stages[index].indicator->color();
88  if(colorLink){
89  // Alt was held down - set all
90  for(int i = 0; i < DPI_COUNT; i++){
91  stages[i].indicator->color(color);
92  perf->dpiColor(i, color);
93  }
94  ui->iButtonO->color(color);
95  perf->dpiColor(KbPerf::OTHER, color);
96  } else {
97  // Set one
98  perf->dpiColor(index, color);
99  }
100  colorLink = false;
101 }
102 
103 static const double DPI_BASE = (double)KbPerf::DPI_MAX / (double)KbPerf::DPI_MIN;
104 
105 inline int dpiExp(int value, int min, int max){
106  double val = (value - min) / (double)(max - min);
107  val = pow(DPI_BASE, val);
108  return round(KbPerf::DPI_MIN * val);
109 }
110 
111 inline int dpiLog(int value, int min, int max){
112  double val = value / (double)KbPerf::DPI_MIN;
113  val = log(val) / log(DPI_BASE);
114  return round(val * (max - min) + min);
115 }
116 
117 inline int dpiRound(int value){
118  if(value > 1000)
119  return round(value / 100.) * 100;
120  else
121  return round(value / 50.) * 50;
122 }
123 
124 // Potential recursion in sliderMoved/boxMoved due to rounding
125 #define SET_START if(isSetting) return; isSetting = true
126 #define SET_END isSetting = false
127 
129  SET_START;
130  QSlider* slider = stages[index].xSlider;
131  int value = dpiRound(dpiExp(slider->value(), slider->minimum(), slider->maximum()));
132  stages[index].xBox->setValue(value);
133  if(_xyLink)
134  perf->dpi(index, value);
135  else
136  perf->dpi(index, QPoint(value, perf->dpi(index).y()));
137  SET_END;
138  if(_xyLink)
139  stages[index].ySlider->setValue(slider->value());
140 }
141 
143  SET_START;
144  QSlider* slider = stages[index].ySlider;
145  int value = dpiRound(dpiExp(slider->value(), slider->minimum(), slider->maximum()));
146  stages[index].yBox->setValue(value);
147  if(_xyLink)
148  perf->dpi(index, value);
149  else
150  perf->dpi(index, QPoint(perf->dpi(index).x(), value));
151  SET_END;
152  if(!ui->xyBox->isChecked())
153  // X/Y linked?
154  stages[index].xSlider->setValue(slider->value());
155 }
156 
157 void MPerfWidget::boxXChanged(int index){
158  SET_START;
159  QSpinBox* box = stages[index].xBox;
160  QSlider* slider = stages[index].xSlider;
161  int value = box->value();
162  slider->setValue(dpiLog(value, slider->minimum(), slider->maximum()));
163  if(_xyLink)
164  perf->dpi(index, value);
165  else
166  perf->dpi(index, QPoint(value, perf->dpi(index).y()));
167  SET_END;
168  if(_xyLink)
169  stages[index].yBox->setValue(value);
170 }
171 
172 void MPerfWidget::boxYChanged(int index){
173  SET_START;
174  QSpinBox* box = stages[index].yBox;
175  QSlider* slider = stages[index].ySlider;
176  int value = box->value();
177  slider->setValue(dpiLog(value, slider->minimum(), slider->maximum()));
178  if(_xyLink)
179  perf->dpi(index, value);
180  else
181  perf->dpi(index, QPoint(perf->dpi(index).x(), value));
182  SET_END;
183  if(_xyLink)
184  stages[index].xBox->setValue(value);
185 }
186 
188  perf->dpiEnabled(index, stages[index].enableCheck->isChecked());
189 }
190 
191 void MPerfWidget::on_xyBox_clicked(bool checked){
192  _xyLink = !checked;
193  CkbSettings::set(xyLinkPath, checked);
194  // FIXME: update other MPerfWidgets, since this is a global setting
195 }
196 
198  perf->dpiIndicator(checked);
199 }
200 
202  perf->angleSnap(checked);
203 }
204 
206  perf->liftHeight((KbPerf::height)(index + 1));
207 }
208 
210  ModeSelectDialog dialog(this, profile->currentMode(), profile->modes(), "Copy performance settings to:");
211  if(dialog.exec() != QDialog::Accepted)
212  return;
213  QList<KbMode*> selectedModes = dialog.selection();
214  foreach(KbMode* mode, selectedModes){
215  *mode->perf() = *perf;
216  }
217 }
218 
220  if(!perf)
221  return;
222  perf->iOpacity(arg1 / 100.f);
223 }
KbMode * currentMode() const
Definition: kbprofile.h:54
void on_lHeightBox_activated(int index)
void sliderXMoved(int index)
MPerfWidget(QWidget *parent=0)
Definition: mperfwidget.cpp:8
ColorButton * iButton5
ColorButton * iButton0
const ModeList & modes() const
Definition: kbprofile.h:42
void on_xyBox_clicked(bool checked)
QSlider * xSlider3
ColorButton * iButtonO
QCheckBox * eBox3
int dpiLog(int value, int min, int max)
QCheckBox * eBox1
static void set(const QString &key, const QVariant &value)
bool _xyLink
Definition: mperfwidget.h:44
Ui::MPerfWidget * ui
Definition: mperfwidget.h:32
#define SET_START
static const int DPI_COUNT
Definition: mperfwidget.h:28
QSpinBox * xBox5
static const double DPI_BASE
QSlider * ySlider1
QSpinBox * yBox3
bool dpiEnabled(int index) const
Definition: kbperf.h:68
const QColor & color() const
Definition: colorbutton.h:13
QSlider * ySlider4
QCheckBox * enableCheck
Definition: mperfwidget.h:41
static const int DPI_MIN
Definition: kbperf.h:47
QSpinBox * xBox1
QSignalMapper sliderXMapper
Definition: mperfwidget.h:48
void setupUi(QWidget *MPerfWidget)
QColor dpiColor(int index) const
Definition: kbperf.h:85
static const int DPI_MAX
Definition: kbperf.h:47
QSpinBox * xBox2
QSpinBox * xBox3
QSpinBox * xBox
Definition: mperfwidget.h:40
QCheckBox * eBox4
ColorButton * indicator
Definition: mperfwidget.h:38
QSpinBox * xBox0
QSignalMapper boxYMapper
Definition: mperfwidget.h:49
QSignalMapper buttonMapper2
Definition: mperfwidget.h:47
height liftHeight() const
Definition: kbperf.h:38
QSpinBox * yBox4
QCheckBox * aSnapBox
void enableChanged(int index)
void colorClicked(int index)
Definition: mperfwidget.cpp:81
bool dpiIndicator() const
Definition: kbperf.h:82
QSpinBox * yBox0
QSlider * ySlider
Definition: mperfwidget.h:39
QCheckBox * indicBox
KbProfile * profile
Definition: mperfwidget.h:35
ColorButton * iButton1
QSpinBox * spinBox
QSlider * xSlider2
height
Definition: kbperf.h:31
QSlider * ySlider2
QSignalMapper enableMapper
Definition: mperfwidget.h:50
QPoint dpi(int index) const
Definition: kbperf.h:50
Definition: kbmode.h:36
Definition: kbperf.h:15
QSlider * xSlider5
void on_indicBox_clicked(bool checked)
void colorChanged(int index)
Definition: mperfwidget.cpp:86
void on_spinBox_valueChanged(int arg1)
QSlider * xSlider1
QCheckBox * eBox2
QComboBox * lHeightBox
QSignalMapper sliderYMapper
Definition: mperfwidget.h:48
bool angleSnap() const
Definition: kbperf.h:42
void boxYChanged(int index)
int max(int a, int b)
Definition: main.c:97
bool colorLink
Definition: mperfwidget.h:45
void on_aSnapBox_clicked(bool checked)
QSlider * ySlider0
int min(int a, int b)
Definition: main.c:101
#define SET_END
bool allowAlpha() const
Definition: colorbutton.h:17
int dpiRound(int value)
static const QString xyLinkPath
Definition: mperfwidget.cpp:6
QSpinBox * yBox1
QSpinBox * yBox
Definition: mperfwidget.h:40
QSpinBox * yBox2
KbPerf * perf
Definition: mperfwidget.h:34
QSlider * xSlider4
QSpinBox * yBox5
ColorButton * iButton2
QCheckBox * xyBox
ColorButton * iButton4
QSignalMapper boxXMapper
Definition: mperfwidget.h:49
QList< KbMode * > selection()
int dpiExp(int value, int min, int max)
QSlider * ySlider3
QCheckBox * eBox5
QSlider * xSlider
Definition: mperfwidget.h:39
QSignalMapper buttonMapper1
Definition: mperfwidget.h:47
QSpinBox * xBox4
static const int OTHER
Definition: kbperf.h:84
float iOpacity() const
Definition: kbperf.h:79
void on_copyButton_clicked()
DpiUi stages[DPI_COUNT]
Definition: mperfwidget.h:43
KbPerf * perf()
Definition: kbmode.h:62
QSlider * xSlider0
void setPerf(KbPerf *newPerf, KbProfile *newProfile)
Definition: mperfwidget.cpp:60
bool bigIcons() const
Definition: colorbutton.h:23
bool setLabel() const
Definition: colorbutton.h:20
void sliderYMoved(int index)
ColorButton * iButton3
void boxXChanged(int index)
QSlider * ySlider5