forked from Raffarti/Touchegg-gce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.cpp
384 lines (330 loc) · 12.1 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
/**
*This work is licensed under the
*Creative Commons Attribuzione 3.0
*Unported License. To view a copy
*of this license, visit
*http://creativecommons.org/licenses/by/3.0/
*
*Please note that this is not Touchégg nor Touchégg-gui,
*which author is José Expósito <[email protected]>.
*This is a gui interface to edit
*Touchégg configuration file alternative to Touchégg-gui.
*
*@author Raffaele Pertile <[email protected]>
*/
#include "gui.h"
#include "ui_gui.h"
#include "button.h"
#include "ui_button.h"
#include "editdialog.h"
#include <QDir>
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui)
{
ui->setupUi(this);
filePath = new QString("~/.config/touchegg/touchegg.conf");
linePath = findChild<QLineEdit*>(QString("filePath"));
QScrollArea *sarea = findChild<QScrollArea*>("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 = findChild<QTreeView*>("treeView");
model = new QStandardItemModel(tree);
adv = findChild<QFrame*>("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 = findChild<QSlider*>("cgt");
}
Gui::~Gui()
{
delete ui;
}
QString Gui::getPath(){
QString ret = *filePath;
//parsing standard home notation
if (ret.startsWith(QChar('~'))){
ret.remove(0,1);
ret.prepend(QDir::homePath());
}
//is the path is a directory, adding touchegg.conf as file by default
if (QDir(ret).exists()){
if (ret.endsWith(QChar('/')))
ret.append(QString("touchegg.conf"));
else
ret.append(QString("/touchegg.conf"));
}
return ret;
}
void Gui::loadFile(QString path){
parser = new Parser(path);
if (!parser->init()) return;
if (!parser->loadAll())return;
//enable componentss requiring a memory loaded.
findChild<QPushButton*>("pushButton")->setEnabled(true);
findChild<QPushButton*>("pushButton_2")->setEnabled(true);
QPushButton *saveButton = findChild<QPushButton*>("saveButton");
saveButton->setEnabled(true);
QScrollArea *sarea = findChild<QScrollArea*>("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 = new QString(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
findChild<QLabel*>("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
linePath->setText("~/.config/touchegg/");
filePath = new QString("~/.config/touchegg/");
}
void Gui::on_pushButton_3_clicked()
{
//Load button trigger
qDebug(filePath->toAscii());
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 = findChild<QTreeView*>("treeView");
tree->collapseAll();
tree->expand(i);
QString name = (((QStandardItemModel*)i.model())->itemFromIndex(i))->text();
currentGroup = Memory::getGroup(name);
displayGroup();
}
void Gui::displayGroup(){
QScrollArea *sarea = findChild<QScrollArea*>("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 = findChild<QPushButton*>("addGesture");
addGesture->setEnabled(true);
}
void Gui::cleanLayout(QLayout* l){
QLayoutItem *wItem;
while ((wItem = l->takeAt(0)) != 0){
wItem->widget()->deleteLater();
delete wItem;
}
QPushButton *addGesture = findChild<QPushButton*>("addGesture");
//no group selected, no new gests!
addGesture->setEnabled(false);
}
void Gui::on_pushButton_5_clicked()
{
//loading provided file
loadFile("./default.conf");
}
void Gui::on_editDialog_done(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::on_deleteGesture(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 = findChild<QTreeView*>("treeView");
if(tree->model()->hasChildren()){
tree->model()->removeRows(0, tree->model()->rowCount());
}
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::on_pushButton_7_clicked()
{
//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 = new 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));
}
//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 = findChild<QTreeView*>("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::on_langCombo_textChanged(const QString &/*arg1*/)
{
}
void Gui::on_pushButton_4_clicked()
{
}
void Gui::on_langCombo_currentIndexChanged(const QString &/*arg1*/)
{
// dic->setLanguage(arg1);
}
void Gui::on_horizontalSlider_sliderMoved(int /*position*/)
{
}
void Gui::on_buttonBox_accepted()
{
}
void Gui::on_buttonBox_rejected()
{
}
void Gui::on_scrollArea_destroyed()
{
}
void Gui::on_cgt_sliderMoved(int /*position*/)
{}
void Gui::on_treeView_doubleClicked(const QModelIndex &/*index*/)
{
}