forked from jlam55555/veikk-linux-driver-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveikkconfig.cpp
More file actions
401 lines (355 loc) · 15.7 KB
/
veikkconfig.cpp
File metadata and controls
401 lines (355 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "veikkconfig.h"
#include "ui_main.h"
#include "qpressurecurvescene.h"
#include "qscreenmapscene.h"
#include <QScreen>
#include <QGuiApplication>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
#include <QStatusBar>
// set up widgets, hook up handlers
MainWindow::MainWindow(QWidget *parent)
: QMainWindow{parent}, ui{new Ui::MainWindow} {
qint8 i;
// basic ui setup (auto-generated by qt designer)
ui->setupUi(this);
screen = QGuiApplication::screens().first();
connect(screen, &QScreen::geometryChanged,
this, &MainWindow::screenSizeChanged);
tabWidget = findChild<QTabWidget *>("tab_widget");
connect(tabWidget, &QTabWidget::currentChanged,
this, &MainWindow::tabChanged);
pressureCurveView = findChild<QGraphicsView *>("pressure_curve_view");
pressureCurveView->setScene(new QPressureCurveScene{});
pressureCurveView->scale(1, -1);
connect(static_cast<QPressureCurveScene *>(pressureCurveView->scene()),
&QPressureCurveScene::updatePressureForm,
this, &MainWindow::updatePressureForm);
connect(this, &MainWindow::updatePressureCurve,
static_cast<QPressureCurveScene *>(pressureCurveView->scene()),
&QPressureCurveScene::updatePressureCurve);
screenMapView = findChild<QGraphicsView *>("screen_map_view");
screenMapView->setScene(new QScreenMapScene{screen});
connect(static_cast<QScreenMapScene *>(screenMapView->scene()),
&QScreenMapScene::updateScreenMapForm,
this, &MainWindow::updateScreenMapForm);
connect(this, &MainWindow::updateScreenMapRect,
static_cast<QScreenMapScene *>(screenMapView->scene()),
&QScreenMapScene::updateScreenMapRect);
for(i=0; i<4; i++) {
pressureCoefSpinboxes[i] = findChild<QDoubleSpinBox *>
("pressure_coef_a" + QString::number(i));
connect(pressureCoefSpinboxes[i],
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this, &MainWindow::updatePressureCoefs);
}
screenMapSpinboxes[0] = screenMapXSpinBox = findChild<QSpinBox *>
("screen_map_x");
screenMapSpinboxes[1] = screenMapYSpinBox = findChild<QSpinBox *>
("screen_map_y");
screenMapSpinboxes[2] = screenMapWidthSpinBox = findChild<QSpinBox *>
("screen_map_width");
screenMapSpinboxes[3] = screenMapHeightSpinBox = findChild<QSpinBox *>
("screen_map_height");
screenWidthLineEdit = findChild<QLineEdit *>("screen_width");
screenHeightLineEdit = findChild<QLineEdit *>("screen_height");
screenDefaultMap = findChild<QCheckBox *>("screen_default_map");
connect(screenDefaultMap, &QCheckBox::stateChanged,
this, &MainWindow::setDefaultScreenMap);
for(i=0; i<4; i++)
connect(screenMapSpinboxes[i],
QOverload<int>::of(&QSpinBox::valueChanged),
this, &MainWindow::updateScreenMapParms);
screenSizeChanged(screen->geometry());
updateScreenMapForm(screen->geometry());
screenOrientation = findChild<QComboBox *>("screen_orientation");
screenOrientation->addItem("Default");
screenOrientation->addItem("90deg CCW");
screenOrientation->addItem("Flipped");
screenOrientation->addItem("90deg CW");
connect(screenOrientation,
QOverload<int>::of(&QComboBox::currentIndexChanged),
std::bind(&VeikkParms::setOrientation, ¤tParms,
std::placeholders::_1));
connect(findChild<QPushButton *>("apply_screen_changes"),
&QPushButton::clicked,
std::bind(&MainWindow::applyConfig, this, VEIKK_MP_SCREEN));
connect(findChild<QPushButton *>("apply_pressure_changes"),
&QPushButton::clicked,
std::bind(&MainWindow::applyConfig, this, VEIKK_MP_PRESSURE_MAP));
connect(findChild<QPushButton *>("reset_screen_changes"),
&QPushButton::clicked, this, &MainWindow::resetScreenChanges);
connect(findChild<QPushButton *>("reset_pressure_changes"),
&QPushButton::clicked, this, &MainWindow::resetPressureChanges);
pressureMapDefaults = findChild<QComboBox *>("pressure_map_defaults");
pressureMapDefaults->addItem("Linear (default)",
VeikkParms::serializePressureMap(0, 100, 0, 0));
pressureMapDefaults->addItem("Quadratic 1",
VeikkParms::serializePressureMap(0, 0, 100, 0));
pressureMapDefaults->addItem("Quadratic 2",
VeikkParms::serializePressureMap(0, 200, -100, 0));
pressureMapDefaults->addItem("Cubic 1",
VeikkParms::serializePressureMap(0, 0, 0, 100));
pressureMapDefaults->addItem("Cubic 2",
VeikkParms::serializePressureMap(0, 100, 100, -100));
pressureMapDefaults->addItem("Linear soft touch",
VeikkParms::serializePressureMap(0, 133, 0, 0));
pressureMapDefaults->addItem("Constant full pressure",
VeikkParms::serializePressureMap(100, 0, 0, 0));
connect(pressureMapDefaults, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &MainWindow::setComboBoxDefaultPressure);
statusbar = statusBar();
// connect menu actions
connect(findChild<QAction *>("action_apply_all"), &QAction::triggered,
std::bind(&MainWindow::applyConfig, this, VEIKK_MP_ALL));
connect(findChild<QAction *>("action_about"), &QAction::triggered,
std::bind(&MainWindow::launchDialog, this,
"Configuration Tool for the VEIKK Linux Driver\n"
"https://github.com/jlam55555/veikk-linux-driver-gui\n\n"
"Driver details:\n"
"Desc:\tUSB VEIKK drawing tablet driver\n"
"Version:\tv2.0\n"
"Author:\tJonathan Lam <jlam55555@gmail.com>\n"
"URL:\thttps://github.com/jlam55555/veikk-linux-driver\n"
"License:\tGPL", false));
connect(findChild<QAction *>("action_load_config_sysfs"),
&QAction::triggered, this, &MainWindow::loadParmsFromSysconfig);
connect(findChild<QAction *>("action_load_config_file"),
&QAction::triggered, this, &MainWindow::loadParmsFromConfigFile);
connect(findChild<QAction *>("action_save_config"),
&QAction::triggered,
std::bind(&MainWindow::saveConfigToFile, this, false));
connect(findChild<QAction *>("action_save_config_as"),
&QAction::triggered,
std::bind(&MainWindow::saveConfigToFile, this, true));
// load and apply configs from sysfs
loadParmsFromSysconfig();
}
MainWindow::~MainWindow() {
delete ui;
}
// resize views on startup, see: https://stackoverflow.com/questions/9858971
void MainWindow::showEvent(QShowEvent *evt) {
pressureCurveView->fitInView(pressureCurveView->sceneRect());
screenMapView->fitInView(screenMapView->sceneRect(), Qt::KeepAspectRatio);
QWidget::showEvent(evt);
}
// on window resizing
void MainWindow::resizeEvent(QResizeEvent *evt) {
screenMapView->fitInView(screenMapView->sceneRect(), Qt::KeepAspectRatio);
QWidget::resizeEvent(evt);
}
// get pressure coefficients from form
void MainWindow::getPressureCoefs(qint16 *coefs) {
qint8 i;
for(i=0; i<4; i++)
coefs[i] = qint16(pressureCoefSpinboxes[i]->value()*100);
}
// get screen map parms from form
QRect MainWindow::getScreenMapParms() {
return QRect{
screenMapXSpinBox->value(),
screenMapYSpinBox->value(),
screenMapWidthSpinBox->value(),
screenMapHeightSpinBox->value()
};
}
// TODO: implement this
// update statusbar -- happens any time an update or save/apply happens
void MainWindow::updateStatus() { }
// on screen size changed; update screen size form, sync with currentParms,
// adjust view matrix
void MainWindow::screenSizeChanged(QRect newScreenSize) {
screenWidthLineEdit->setText(QString::number(newScreenSize.width()));
screenHeightLineEdit->setText(QString::number(newScreenSize.height()));
currentParms.setScreenSize(screen->geometry());
// fitInView() only works if sceneRect is within scene's sceneRect,
// so resize scene's sceneRect first
screenMapView->scene()->setSceneRect(screen->geometry());
screenMapView->fitInView(screenMapView->scene()->sceneRect());
}
// handler for changing tab; adjust view matrices to sceneRect
void MainWindow::tabChanged(int curTab) {
switch(curTab) {
case 0: // screen map
screenMapView->fitInView(screenMapView->sceneRect(),
Qt::KeepAspectRatio);
break;
case 1: // pressure map
pressureCurveView->fitInView(pressureCurveView->sceneRect());
break;
}
}
// update pressure form with given values, sync with currentParms
void MainWindow::updatePressureForm(qint16 *newCoefs) {
qint8 i;
qint16 coefs[4];
for(i=0; i<4; i++) {
pressureCoefSpinboxes[i]->blockSignals(true);
pressureCoefSpinboxes[i]->setValue(newCoefs[i]/100.0);
pressureCoefSpinboxes[i]->blockSignals(false);
}
getPressureCoefs(coefs);
currentParms.setPressureMap(coefs);
}
// callback for changing pressure coefs in form; sync pressure visual and
// currentParms with new form values
void MainWindow::updatePressureCoefs() {
qint16 newCoefs[4];
getPressureCoefs(newCoefs);
currentParms.setPressureMap(newCoefs);
emit updatePressureCurve(newCoefs);
}
// update screen map form with given values, sync with currentParms
void MainWindow::updateScreenMapForm(QRect newScreenMap) {
qint8 i;
for(i=0; i<4; i++)
screenMapSpinboxes[i]->blockSignals(true);
screenMapXSpinBox->setValue(newScreenMap.x());
screenMapYSpinBox->setValue(newScreenMap.y());
screenMapWidthSpinBox->setValue(newScreenMap.width());
screenMapHeightSpinBox->setValue(newScreenMap.height());
for(i=0; i<4; i++)
screenMapSpinboxes[i]->blockSignals(false);
screenDefaultMap->setCheckState(newScreenMap==screen->geometry()
? Qt::Checked
: Qt::Unchecked);
currentParms.setScreenMap(getScreenMapParms());
}
// callback for changing screen map parms; sync screen map visual and
// currentParms with new screen map form values
void MainWindow::updateScreenMapParms() {
QRect newScreenMap = getScreenMapParms();
screenDefaultMap->setCheckState(newScreenMap==screen->geometry()
? Qt::Checked
: Qt::Unchecked);
currentParms.setScreenMap(newScreenMap);
emit updateScreenMapRect(newScreenMap);
}
// callback for checking the "default screen map" checkbox; sets screen map
// to full screen and updates form and visual (and syncs currentParms)
void MainWindow::setDefaultScreenMap(int checkState) {
if(checkState == Qt::Unchecked)
return;
updateScreenMapForm(screen->geometry());
emit updateScreenMapRect(screen->geometry());
}
// callback for selecting a new pressure mapping default from the combobox
void MainWindow::setComboBoxDefaultPressure() {
qint16 pressureCoefs[4];
currentParms.setPressureMap(pressureMapDefaults->currentData()
.toULongLong());
currentParms.getPressureMap(pressureCoefs);
updatePressureForm(pressureCoefs);
emit updatePressureCurve(pressureCoefs);
}
// callback to reset screen changes; doesn't make sense to "restore
// screen size" (screen size is fixed at current geometry), so that isn't
// ever changed
void MainWindow::resetScreenChanges() {
QRect screenMap;
quint32 orientation;
currentParms.restoreConfig(&restoreParms,
VEIKK_MP_SCREEN_MAP|VEIKK_MP_ORIENTATION);
screenMap = currentParms.getScreenMap();
orientation = currentParms.getOrientation();
screenOrientation->setCurrentIndex(qint32(orientation));
// if invalid screen map (e.g., default has value of 0)
if(currentParms.isInvalidScreenMap()) {
setDefaultScreenMap(Qt::Checked);
} else {
updateScreenMapForm(screenMap);
emit updateScreenMapRect(screenMap);
}
}
// callback to reset pressure changes
void MainWindow::resetPressureChanges() {
qint16 pressureCoefs[4];
currentParms.restoreConfig(&restoreParms, VEIKK_MP_PRESSURE_MAP);
currentParms.getPressureMap(pressureCoefs);
updatePressureForm(pressureCoefs);
emit updatePressureCurve(pressureCoefs);
}
// get parms from sysconfig (i.e., "effective config") and sync all ui
// elements (like in other sections, don't set screen size in current
// parms -- let that stay the current screen size)
void MainWindow::loadParmsFromSysconfig() {
VeikkParms::VPStatus err;
if((err=restoreParms.loadFromSysfs()))
return launchDialog("Loading default parms: "+VeikkParms::strerror(err),
true);
currentParms.restoreConfig(&restoreParms,
VEIKK_MP_ALL^VEIKK_MP_SCREEN_SIZE);
updateUiFromParms();
}
// update all UI elements from currentParms
void MainWindow::updateUiFromParms() {
QRect screenMap;
quint32 orientation;
qint16 pressureCoefs[4];
screenMap = currentParms.getScreenMap();
orientation = currentParms.getOrientation();
currentParms.getPressureMap(pressureCoefs);
// if invalid screen map (e.g., default has value of 0)
if(currentParms.isInvalidScreenMap()) {
setDefaultScreenMap(Qt::Checked);
} else {
updateScreenMapForm(screenMap);
emit updateScreenMapRect(screenMap);
}
screenOrientation->setCurrentIndex(qint32(orientation));
updatePressureForm(pressureCoefs);
emit updatePressureCurve(pressureCoefs);
}
// launch dialog box with arbitrary text
void MainWindow::launchDialog(QString text, bool isModal) {
QMessageBox *dialog = new QMessageBox{};
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setText(text);
dialog->setModal(isModal);
if(isModal)
dialog->exec();
else
dialog->show();
}
// get parms from a config file
void MainWindow::loadParmsFromConfigFile() {
VeikkParms::VPStatus err;
QString src = QFileDialog::getOpenFileName(this,
"Open VEIKK conf file", QDir::homePath(),
"VEIKK config files (*.conf)");
// exit if no file selected
if(src == QString{})
return;
if((err=restoreParms.loadFromFile(src)))
return launchDialog("Opening config file "+src+": "+
VeikkParms::strerror(err), true);
currentParms.restoreConfig(&restoreParms,
VEIKK_MP_ALL^VEIKK_MP_SCREEN_SIZE);
curParmPath = src;
updateUiFromParms();
}
// save config to file
void MainWindow::saveConfigToFile(bool saveAs) {
VeikkParms::VPStatus err;
if(saveAs || curParmPath == "")
curParmPath = QFileDialog::getSaveFileName(this,
"Save VEIKK conf file", QDir::homePath(),
"VEIKK config files (*.conf)");
// exit if no file selected
if(curParmPath == "")
return;
// export, save to restoreParms
if((err=currentParms.exportConfig(curParmPath)))
return launchDialog("Saving config to file "+curParmPath+": "+
VeikkParms::strerror(err), true);
restoreParms.restoreConfig(¤tParms, VEIKK_MP_ALL);
}
// wrapper for applying config parms
void MainWindow::applyConfig(int parms) {
VeikkParms::VPStatus err;
if((err=currentParms.applyConfig(restoreParms, parms)))
launchDialog("Applying current config: "+VeikkParms::strerror(err),
true);
}