-
Notifications
You must be signed in to change notification settings - Fork 25
/
gui.cpp
488 lines (422 loc) · 15.3 KB
/
gui.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**************************************************************************
* Copyright (c) 2012-2015 Raffaele Pertile <[email protected]>
* This file is part of touchegg-gce.
*
* touchegg-gce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* touchegg-gce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with touchegg-gce. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "gui.h"
#include <QLineEdit>
#include <QLabel>
#include <QSettings>
#include "parser.h"
#include <QStandardItemModel>
#include <QGridLayout>
#include <QSlider>
#include <QObjectList>
#include <QBoxLayout>
#include "general.h"
#include "scribe.h"
#include "newgroupdialog.h"
#include "ui_gui.h"
#include "button.h"
#include "ui_button.h"
#include "editdialog.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui),
parser(nullptr)
{
ui->setupUi(this);
QSettings settings("touchegg-gce");
if (settings.contains("filePath"))
filePath = settings.value("filePath").toString();
else
filePath = "~/.config/touchegg/";
ui->filePath->setText(filePath);
linePath = ui->filePath;
QScrollArea *sarea = ui->scrollArea;
//scroll area for gesture list
QWidget *wsa = new QWidget();
wsa->setLayout(new QVBoxLayout(sarea));
wsa->layout()->setSizeConstraint(QLayout::SetFixedSize);
sarea->setWidget(wsa);
//tree for applications
QTreeView *tree = ui->treeView;
model = new QStandardItemModel(tree);
adv = ui->advFrame;
adv->hide();
tree->setModel(new QStandardItemModel());
tree->setModel(model);
//connecting tree with data struct
connect(model, SIGNAL(itemChanged(QStandardItem*)),this, SLOT(groupMoved(QStandardItem* )));
//composed gesture slider
slider = ui->cgt;
}
Gui::~Gui()
{
delete ui;
}
QString Gui::getPath(){
QString ret = filePath;
//parsing standard home notation
ret.replace(QRegExp("^~"),QDir::homePath());
//is the path is a directory, adding touchegg.conf as file by default
if (QFileInfo(ret).isDir()){
ret = QFileInfo(ret).canonicalFilePath().append("/touchegg.conf");
}
if (ret.endsWith("/"))
ret.append(QString("touchegg.conf"));
return ret;
}
void Gui::loadFile(QString path){
QFile *file = new QFile(path);
if (!file->exists()){
QMessageBox::warning(this, tr("File Error"), tr("Target file does not exists."));
return;
}
if (!file->open(QIODevice::ReadOnly)){
QMessageBox::warning(this, tr("File Error"), tr("Target file cannot be opened."));
return;
}
if (parser) delete parser;
parser = new Parser(file);
if (!parser->loadAll())return;
//enable componentss requiring a memory loaded.
ui->pushButton->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->saveButton->setEnabled(true);
ui->saveChooser->setEnabled(true);
QScrollArea *sarea = ui->scrollArea;
//clear gesture list
cleanLayout(sarea->widget()->layout());
//clear tree view and add application found in data struct memory
refreshGroupTree();
//setting props (only gesture time present atm)
foreach(QString key, Memory::getProps()){
if (key == "composed_gestures_time"){
slider->setEnabled(true);
slider->setValue(Memory::getProp(key).toInt());
}
}
}
void Gui::on_filePath_textEdited(const QString &arg1)
{
//update filePath on gui request
filePath = arg1;
}
void Gui::on_checkBox_toggled(bool checked)
{
//shows advanced inteface
if (checked) adv->show();
else adv->hide();
}
void Gui::on_cgt_valueChanged(int value)
{
//sync memory with gesture timer slider
ui->sliderValue->setText(QString(tr("Value: ")).append(QString::number(value)).append("ms"));
Memory::removeProp("composed_gestures_time");
Memory::addProp("composed_gestures_time",QString::number(value));
}
void Gui::on_pushButton_6_clicked()
{
//reset path button
QSettings settings("touchegg-gce");
if (settings.contains("filePath") && settings.value("filePath") != filePath)
filePath = settings.value("filePath").toString();
else
filePath = "~/.config/touchegg/";
linePath->setText(filePath);
}
void Gui::on_pushButton_3_clicked()
{
//Load button trigger
qDebug() << filePath;
loadFile(getPath());
}
void Gui::on_treeView_clicked(const QModelIndex &index)
{
//display gesture list on user tree interaction
QModelIndex i;
i = index.parent().isValid()?index.parent():index;
QTreeView *tree = ui->treeView;
tree->collapseAll();
tree->expand(i);
QString name = (((QStandardItemModel*)i.model())->itemFromIndex(i))->text();
currentGroup = Memory::getGroup(name);
displayGroup();
}
void Gui::displayGroup(){
QScrollArea *sarea = ui->scrollArea;
cleanLayout(sarea->widget()->layout());
//if no group is about to be displayed, only cleaning was required
if (currentGroup == NULL)return;
//sorted based on number of fingers
foreach(Gesture *g,currentGroup->getSortedGestures()){
//creating gestures frames inside the scroll area
Button *b = new Button(this);
b->setGesture(g);
b->bLabel.gesture->setText(Lists::toString(g->getType()));
b->bLabel.fingers->setText(QString::number(g->getFingers()));
b->bLabel.direction->setText(Lists::toString(g->getDirection()));
b->bLabel.layout->addWidget(new QLabel(tr("Action:")),0,0);
b->bLabel.action = new QLabel(Lists::toString(g->getAction()->getType()));
b->bLabel.layout->addWidget(b->bLabel.action,0,1);
int count = 1;
foreach(QString s, g->getAction()->getParamKeys()){
b->bLabel.layout->addWidget(new QLabel(s),count,0);
QString tmp = g->getAction()->getParamValue(s);
if (!tmp.isEmpty())b->bLabel.layout->addWidget(new QLabel(tmp),count,1);
else b->bLabel.layout->addItem(new QSpacerItem(10, 10, QSizePolicy::Ignored, QSizePolicy::Expanding),count,1);
count++;
}
//just for a better view, adding spaces inside empty cells
for (;count < 4;count++){
b->bLabel.layout->addItem(new QSpacerItem(10, 10, QSizePolicy::Ignored, QSizePolicy::Expanding),count,0);
b->bLabel.layout->addItem(new QSpacerItem(10, 10, QSizePolicy::Ignored, QSizePolicy::Expanding),count,1);
}
sarea->widget()->layout()->addWidget(b);
}
//once a group is selected, you can add gests
QPushButton *addGesture = ui->addGesture;
addGesture->setEnabled(true);
}
void Gui::cleanLayout(QLayout* l){
QLayoutItem *wItem;
while ((wItem = l->takeAt(0)) != 0){
wItem->widget()->deleteLater();
delete wItem;
}
QPushButton *addGesture = ui->addGesture;
//no group selected, no new gests!
addGesture->setEnabled(false);
}
void Gui::on_pushButton_5_clicked()
{
//loading provided file
loadFile("ress:default.conf");
}
void Gui::editDialogFinished(Gesture *gesture){
//called on edit or new gesture complete
if (gesture){
currentGroup->removeGesture(gesture->getFingers(),
gesture->getType(),
gesture->getDirection());
currentGroup->addGest(gesture);
}
//to make changes appears, it will reset scroll bar position, though
//maybe I'll add a backup for it
displayGroup();
}
void Gui::gestureDeleted(Gesture *gesture){
//called on edit or new gesture complete
if (gesture){
currentGroup->removeGesture(gesture->getFingers(),
gesture->getType(),
gesture->getDirection());
}
//to make changes appears, it will reset scroll bar position, though
//maybe I'll add a backup for it
displayGroup();
}
void Gui::groupMoved(QStandardItem *item){
if (item->parent() == NULL){
//if parent is null, an application is to be moved to a new group
inheritGroup = Memory::getApp(item->text())->getGroup();
QString group = Memory::addGroup(item->text());
newGroup = Memory::getGroup(group);
Memory::getApp(item->text())->changeGroup(group);
//you sould want to keep old settings, though
QMessageBox *conf = new QMessageBox(QMessageBox::Question,
QString(tr("New group")),
QString(tr("Moving an application to a new group,\ninherit old group gestures?")),
QMessageBox::Yes|QMessageBox::Cancel);
QObject::connect(conf,SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(inheritGestures(QAbstractButton*)));
conf->show();
refreshGroupTree();
}//if is moved to an existing group, nothing is really needed but changing the application pointer to the group
else Memory::getApp(item->text())->changeGroup(item->parent()->text());
}
void Gui::refreshGroupTree(){
QTreeView *tree = ui->treeView;
if(tree->model()->hasChildren()){
tree->model()->removeRows(0, tree->model()->rowCount());
}
if (!Memory::getGroupsNames().isEmpty())
foreach(QString key, Memory::getGroupsNames()){
QStandardItem *group = new QStandardItem(key);
group->setEditable(false); //no renaming! group names can't be saved, so no reason for that.
group->setDragEnabled(false);//groups have no reason to be moved, just expose to buggs.
foreach(QString part, Memory::getGroup(key)->getAppsNames()){
if(key != tr("All")){ //All group have no reason to own specific applications
QStandardItem *item = new QStandardItem(part);
item->setEditable(false); //well maybe someone could find it convenient to rename apps, I'll think about that another time
item->setDragEnabled(true); //apps can be moved
item->setDropEnabled(false); //apps can't own anything
group->appendRow(item);
}
else group->setDropEnabled(false); //no purpose on moving apps to All, just delete them
}
model->appendRow(group);
}
}
void Gui::pushButton7Clicked()
{
//Add gesture button trigger
EditDialog *a = new EditDialog(this);
a->open();
a->b = NULL;
a->setUp(true);
}
//Save button trigger
void Gui::on_saveButton_clicked()
{
Scribe scribe;
if(scribe.open(getPath()))
scribe.save();
}
//Add gesture button trigger (Hell, one of those two should be in 'trash [see below]' checking it later)
void Gui::on_addGesture_clicked()
{
EditDialog *a = new EditDialog(this);
a->open();
a->b = NULL;
a->setUp(true);
}
//inherit dialog confirmation handler
void Gui::inheritGestures(QAbstractButton *button){
if (button->text() != "&Yes")return;
foreach(Gesture *ges, inheritGroup->getGests())
newGroup->addGest(new Gesture(ges));
}
void Gui::setLanguage(int indx)
{
ui->langBox->setCurrentIndex(indx);
}
void Gui::setLanguageList(QStringList langs)
{
ui->langBox->clear();
ui->langBox->addItems(langs);
}
//Add group button trigger
void Gui::on_pushButton_2_clicked()
{
NewGroupDialog *ngDialog = new NewGroupDialog(this);
ngDialog->open();
connect(ngDialog, SIGNAL(done()), this, SLOT(newGroupAccepted()));
}
//add group dialog terminated (the dialog will handle the memory changes)
void Gui::newGroupAccepted(){
refreshGroupTree();
}
//remove button trigger
void Gui::on_pushButton_clicked()
{
QTreeView *tree = ui->treeView;
if (!tree->currentIndex().isValid())return;
QModelIndex i = tree->currentIndex();
QString name = (((QStandardItemModel*)i.model())->itemFromIndex(i))->text();
if (Memory::getAppsNames().contains(name) && name != tr("All"))
//that's for app remove
Memory::removeApp(name);
else{
//that's for group remove
Memory::removeGroup(name);
//this could be unnecessary in rare cases (selecting the group with keyboard)
//but, honestly, who is going to do that? (well well well... nice things you find commenting... I'll fix it later)
currentGroup = NULL;
displayGroup();
}
refreshGroupTree();
}
// ***TRASH*** can't figure out how to remove them without causing Qt going mad
void Gui::langCombo_textChanged(const QString &/*arg1*/)
{
}
void Gui::pushButton4Clicked()
{
}
void Gui::langCombo_currentIndexChanged(const QString &/*arg1*/)
{
// dic->setLanguage(arg1);
}
void Gui::horizontalSlider_sliderMoved(int /*position*/)
{
}
void Gui::buttonBoxAccepted()
{
}
void Gui::buttonBoxRejected()
{
}
void Gui::on_scrollArea_destroyed()
{
}
void Gui::on_cgt_sliderMoved(int /*position*/)
{}
void Gui::on_treeView_doubleClicked(const QModelIndex &/*index*/)
{
}
void Gui::on_langBox_currentIndexChanged(const QString &arg1)
{
if (isHidden())return;
//TODO
//emit languageChanged(arg1);
//WORKARROUND
QSettings conf("touchegg-gce");
conf.setValue("Locale/Language",arg1);
conf.sync();
if (conf.status() == QSettings::NoError)
QMessageBox::information(this,"Restart Required","Please restart the application to apply language selection.\nThis will be hopefuly fixed in future releases.");
else
QMessageBox::warning(this,"Config Error",QString("Cannot write into the configuration file.\n%1").arg(conf.fileName()));
}
void Gui::on_openChooser_clicked()
{
filePath = QFileDialog::getOpenFileName(this,tr("Select Configuration File"),ui->filePath->text(),tr("Configuration Files (*.conf);;All Files (*.*)"));
ui->filePath->setText(filePath);
}
void Gui::on_saveChooser_clicked()
{
QString ret = QFileDialog::getSaveFileName(this,tr("Select Configuration File"),filePath,tr("Configuration Files (*.conf);;All Files (*.*)"));
if (ret.isNull()) return;
if (QFileInfo(ret).exists() &&
QMessageBox::Ok != QMessageBox::question(this,
tr("File override"),
tr("A file with that name already exists, override it?"),
QMessageBox::Ok|QMessageBox::Cancel))
return;
filePath = ret;
ui->filePath->setText(filePath);
Scribe scribe;
if(scribe.open(getPath()))
scribe.save();
}
void Gui::on_savePath_toggled(bool checked)
{
QSettings settings("touchegg-gce");
if (checked)
settings.setValue("filePath",filePath);
else if (settings.value("filePath") == filePath)
settings.remove("filePath");
}
void Gui::on_filePath_textChanged(const QString &arg1)
{
QSettings settings("touchegg-gce");
if (settings.value("filePath").toString() == arg1)
ui->savePath->setChecked(true);
else ui->savePath->setChecked(false);
}