-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
300 lines (251 loc) · 9.35 KB
/
mainwindow.cpp
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
#include <QCoreApplication>
#include <QFileDialog>
#include <QDir>
#include <QDirIterator>
#include <QPushButton>
#include <QGridLayout>
#include <QResizeEvent>
#include <QQuickView>
#include <QQuickWidget>
#include <QDateTime>
#include <QQmlContext>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editor/keycustomize.h"
#include "zjson.h"
#include "zlog.h"
enum {
TAB_NO_KB = 0,
TAB_FIRMWARE,
TAB_KEYMAP,
TAB_COMMANDS,
TAB_ABOUT,
};
inline QString toQStr(ZString str){
return QString::fromUtf8(str.raw(), str.size());
}
MainWindow::MainWindow(bool devel, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
scanning(false),
currcmd(CMD_NONE)
{
ui->setupUi(this);
keyCustomize = new KeyCustomize(this);
connect(keyCustomize, SIGNAL(acceptedKey(int,QString)), this, SLOT(updateRepr(int,QString)));
// Store and remove tabs
QList<QWidget*> tabs;
QList<QString> tabnames;
int count = ui->tabWidget->count();
for(int i = count; i > 0; --i){
tabs.push_front(ui->tabWidget->widget(i-1));
tabnames.push_front(ui->tabWidget->tabText(i-1));
ui->tabWidget->removeTab(i-1);
}
// Re-add tabs
ui->tabWidget->addTab(tabs[TAB_NO_KB], tabnames[TAB_NO_KB]);
ui->tabWidget->addTab(tabs[TAB_FIRMWARE], tabnames[TAB_FIRMWARE]);
ui->tabWidget->addTab(tabs[TAB_KEYMAP], tabnames[TAB_KEYMAP]);
if(devel){
// Show commands tab in developer mode
ui->tabWidget->addTab(tabs[TAB_COMMANDS], tabnames[TAB_COMMANDS]);
}
ui->tabWidget->addTab(tabs[TAB_ABOUT], tabnames[TAB_ABOUT]);
ui->version->setText("Version: " + QCoreApplication::applicationVersion());
ui->tabWidget->setCurrentIndex(0);
ui->fileEdit->setText(settings.value(CUSTOM_FIRMWARE_LOCATION).toString());
// Set keymap qml properties
ui->keymap->rootContext()->setContextProperty("mainwindow", this);
ui->keymap->setClearColor(Qt::transparent);
// Needed to make the background transparent
ui->keymap->setAttribute(Qt::WA_AlwaysStackOnTop, true);
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::connectWorker(MainWorker *worker){
connect(this, SIGNAL(doRescan()), worker, SLOT(onDoRescan()));
connect(worker, SIGNAL(rescanDone(ZArray<KeyboardDevice>)), this, SLOT(onRescanDone(ZArray<KeyboardDevice>)));
connect(this, SIGNAL(kbCommand(zu64,KeyboardCommand,QVariant,QVariant)), worker, SLOT(onKbCommand(zu64,KeyboardCommand,QVariant,QVariant)));
connect(this, SIGNAL(kbKmUpdate(zu64,ZPointer<Keymap>)), worker, SLOT(onKbKmUpdate(zu64,ZPointer<Keymap>)));
connect(worker, SIGNAL(commandDone(KeyboardCommand,bool)), this, SLOT(onCommandDone(KeyboardCommand,bool)));
}
void MainWindow::resizeEvent(QResizeEvent *event){
}
// Command start/done
void MainWindow::startCommand(KeyboardCommand cmd, QVariant arg1, QVariant arg2){
int index = ui->keyboardSelect->currentIndex();
if(index >= 0 && currcmd == CMD_NONE){
currcmd = cmd;
ui->progressBar->setMaximum(0);
ui->rebootButton->setEnabled(false);
ui->bootButton->setEnabled(false);
switch(cmd){
case CMD_KM_SET: {
ui->statusBar->showMessage("Update Keymap...");
// make copy of keymap for worker thread
ZPointer<Keymap> km = new Keymap(*currentkeymap.get());
emit kbKmUpdate(klist[index].key, km);
break;
}
default:
ui->statusBar->showMessage("Command...");
emit kbCommand(klist[index].key, cmd, arg1, arg2);
break;
}
}
}
void MainWindow::onCommandDone(KeyboardCommand cmd, bool ret){
currcmd = CMD_NONE;
ui->progressBar->setValue(100);
ui->progressBar->setMaximum(100);
ui->rebootButton->setEnabled(true);
ui->bootButton->setEnabled(true);
if(ret){
ui->statusBar->showMessage("Done");
} else {
ui->statusBar->showMessage("Error");
}
if(cmd == CMD_REBOOT || cmd == CMD_BOOTLOADER){
on_rescanButton_clicked();
} else if(cmd == CMD_KM_RELOAD || cmd == CMD_KM_RESET){
}
}
// Rescan start/done
void MainWindow::on_rescanButton_clicked(){
if(!scanning){
scanning = true;
emit doRescan();
ui->rescanButton->setEnabled(false);
ui->progressBar->setMaximum(0);
ui->statusBar->showMessage("Scanning...");
ui->keyboardSelect->clear();
ui->keyboardSelect->setEnabled(false);
ui->flashButton->setEnabled(false);
ui->tabWidget->insertTab(TAB_NO_KB, ui->noTab, "No Keyboard");
ui->tabWidget->setCurrentIndex(TAB_NO_KB);
for(int i = 1; i < ui->tabWidget->count() - 1; ++i)
ui->tabWidget->setTabEnabled(i, false);
}
}
void MainWindow::onRescanDone(ZArray<KeyboardDevice> list){
scanning = false;
klist = list;
ui->rescanButton->setEnabled(true);
ui->progressBar->setValue(100);
ui->progressBar->setMaximum(100);
if(list.size()){
ui->statusBar->showMessage("Scan Done");
ui->tabWidget->removeTab(TAB_NO_KB);
for(int i = 0; i < ui->tabWidget->count(); ++i)
ui->tabWidget->setTabEnabled(i, true);
QStringList slist;
for(auto it = list.begin(); it.more(); ++it){
slist.push_back(toQStr(it.get().name));
}
ui->keyboardSelect->addItems(slist);
ui->keyboardSelect->setEnabled(true);
} else {
ui->statusBar->showMessage("No Keyboards Detected");
}
}
void MainWindow::on_keyboardSelect_currentIndexChanged(int index){
// Change selected keyboard
if(index >= 0){
ui->keyboardName->setText(toQStr("Keyboard: " + klist[index].name));
ui->firmwareVersion->setText(toQStr("Firmware: " + klist[index].version));
if(klist[index].flags & FLAG_SUPPORTED){
ui->supported->setText("This keyboard is supported");
ui->supported->setStyleSheet("QLabel { color: green; }");
ui->flashButton->setEnabled(true);
} else {
ui->supported->setText("Keyboard is not yet supported");
ui->supported->setStyleSheet("QLabel { color: red; }");
ui->flashButton->setEnabled(false);
}
if(klist[index].keymap.get()){
ui->tabWidget->setTabEnabled(TAB_KEYMAP-1, true);
LOG("Layout: " << klist[index].keymap->layoutName());
updateKeyLayout(klist[index].keymap);
} else {
ui->tabWidget->setTabEnabled(TAB_KEYMAP-1, false);
}
}
}
void MainWindow::on_browseButton_clicked(){
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Firmware"), QDir::currentPath(), tr("Binary Files (*.bin)"));
ui->fileEdit->setText(fileName);
}
void MainWindow::on_uploadButton_clicked(){
if(!ui->fileEdit->text().isEmpty()){
ZString file = ui->fileEdit->text().toStdString();
LOG("Upload: " << file);
}
}
void MainWindow::on_rebootButton_clicked(){
startCommand(CMD_REBOOT);
}
void MainWindow::on_bootButton_clicked(){
startCommand(CMD_BOOTLOADER);
}
void MainWindow::on_commitButton_clicked(){
startCommand(CMD_KM_COMMIT);
}
void MainWindow::on_resetButton_clicked(){
startCommand(CMD_KM_RELOAD);
}
void MainWindow::on_defaultButton_clicked(){
startCommand(CMD_KM_RESET);
}
void MainWindow::on_fileEdit_textChanged(const QString &arg1){
settings.setValue(CUSTOM_FIRMWARE_LOCATION, arg1);
}
void MainWindow::on_layerSelection_currentIndexChanged(int index){
currentLayer = index;
// Change selected layer
if(index >= 0)
updateKeyLayer(index);
}
void MainWindow::customizeKey(int index){
keyCustomize->setKey(index);
keyCustomize->show();
//keyCustomize->accepted();
}
void MainWindow::updateRepr(int index, QString value){
ZString key = value.toStdString();
LOG("Map key: " << index << " -> " << key);
Keymap::keycode kc = currentkeymap->toKeycode(key);
ZString abbr = currentkeymap->keycodeAbbrev(kc);
QObject *obj = (QObject *)ui->keymap->rootObject();
QMetaObject::invokeMethod(obj, "updateRepr", Q_ARG(QVariant, index), Q_ARG(QVariant, toQStr(abbr)));
zassert(currentLayer >= 0 && currentLayer < currentkeymap->numLayers(), "bad key layer");
zassert(index >= 0 && index < currentkeymap->numKeys(), "bad key index");
currentkeymap->set(currentLayer, index, kc);
startCommand(CMD_KM_SET);
}
void MainWindow::updateKeyLayout(ZPointer<Keymap> keymap){
LOG("updateLayout " << keymap->layoutName());
currentkeymap = keymap;
QVariantList list;
ZArray<int> layout = currentkeymap->getLayout();
for(auto it = layout.begin(); it.more(); ++it){
list << it.get();
}
QObject *obj = (QObject *)ui->keymap->rootObject();
QMetaObject::invokeMethod(obj, "setKeyLayout", Q_ARG(QVariant, list));
ui->layerSelection->clear();
for(int i = 0; i < currentkeymap->numLayers(); ++i){
ui->layerSelection->addItem(QString("Layer %1").arg(i));
}
}
void MainWindow::updateKeyLayer(int index){
LOG("updateLayer " << index);
QVariantList list;
//ZArray<int> layer = currentkeymap->getLayer(index);
ZArray<ZString> layer = currentkeymap->getLayerAbbrev(index);
for(auto it = layer.begin(); it.more(); ++it){
list << toQStr(it.get());
}
QObject *obj = (QObject*) ui->keymap->rootObject();
QMetaObject::invokeMethod(obj, "updateLayer", Q_ARG(QVariant, list));
}