-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuiPreferencesWindow.cpp
371 lines (340 loc) · 14.9 KB
/
GuiPreferencesWindow.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
/*
* Copyright (C) 2013 Rajendran Thirupugalsamy
* See LICENSE for full copyright and license information.
* See COPYING for distribution information.
*/
#include <QDebug>
#include <QPushButton>
#include <QInputDialog>
#include <QMessageBox>
#include "GuiPreferencesWindow.h"
#include "GuiMainWindow.h"
#include "ui_GuiPreferencesWindow.h"
void GuiPreferencesWindow::addItemToTree(QTreeWidgetItem *par, qutty_menu_id_t menu_index,
const char *text, const char *desc)
{
if (!text)
text = qutty_menu_actions[menu_index].name;
if (!desc)
desc = qutty_menu_actions[menu_index].tooltip;
QTreeWidgetItem *item = new QTreeWidgetItem(par, menu_index+QTreeWidgetItem::UserType);
item->setText(0, text);
item->setToolTip(0, desc);
item->setText(1, mainWindow->menuGetShortcutById(menu_index).toString());
par->addChild(item);
}
GuiPreferencesWindow::GuiPreferencesWindow(GuiMainWindow *parent) :
QDialog(parent),
mainWindow(parent),
shkey_len(0),
shkey_changed(false),
ui(new Ui::GuiPreferencesWindow)
{
QTreeWidget *tree;
QTreeWidgetItem *item;
memset(shkey_entered, 0, sizeof(shkey_entered));
ui->setupUi(this);
/******************** Keyboard Shortcuts Tab ****************************************/
tree = this->ui->tree_keysh;
QFont font = tree->font();
font.setBold(true);
item = new QTreeWidgetItem(tree, QStringList("New Session"));
item->setFont(0, font);
addItemToTree(item, MENU_NEW_TAB);
addItemToTree(item, MENU_SPLIT_HORIZONTAL, "New Horizontal Split");
addItemToTree(item, MENU_SPLIT_VERTICAL, "New Vertical Split");
addItemToTree(item, MENU_NEW_WINDOW);
item = new QTreeWidgetItem(tree, QStringList("Duplicate Session"));
item->setFont(0, font);
addItemToTree(item, MENU_DUPLICATE_SESSION, "Duplicate to new Tab");
addItemToTree(item, MENU_DUPLICATE_HSPLIT);
addItemToTree(item, MENU_DUPLICATE_VSPLIT);
shkey_root_custom_saved_session = new QTreeWidgetItem(tree, QStringList("Saved Session"));
item = shkey_root_custom_saved_session;
item->setFont(0, font);
item = new QTreeWidgetItem(item);
item->setText(0, "Create custom shortcut to open a saved session");
QPushButton *button = new QPushButton("Create");
this->ui->tree_keysh->setItemWidget(item, 1, button);
connect(button, SIGNAL(clicked()), SLOT(slot_keysh_custom_saved_session_shortcut_create()));
item = new QTreeWidgetItem(tree, QStringList("Close Session"));
item->setFont(0, font);
addItemToTree(item, MENU_CLOSE_SESSION, "Close Session");
addItemToTree(item, MENU_EXIT, "Close Window");
item = new QTreeWidgetItem(tree, QStringList("Navigation"));
item->setFont(0, font);
addItemToTree(item, MENU_SWITCH_LEFT_TAB);
addItemToTree(item, MENU_SWITCH_RIGHT_TAB);
addItemToTree(item, MENU_SWITCH_MRU_TAB);
addItemToTree(item, MENU_SWITCH_LRU_TAB);
addItemToTree(item, MENU_SWITCH_UP_PANE);
addItemToTree(item, MENU_SWITCH_BOTTOM_PANE);
addItemToTree(item, MENU_SWITCH_LEFT_PANE);
addItemToTree(item, MENU_SWITCH_RIGHT_PANE);
addItemToTree(item, MENU_SWITCH_MRU_PANE);
addItemToTree(item, MENU_SWITCH_LRU_PANE);
item = new QTreeWidgetItem(tree, QStringList("Miscellaneous"));
item->setFont(0, font);
addItemToTree(item, MENU_CHANGE_SETTINGS);
addItemToTree(item, MENU_RENAME_TAB);
addItemToTree(item, MENU_PASTE);
addItemToTree(item, MENU_FULLSCREEN);
addItemToTree(item, MENU_ALWAYSONTOP);
addItemToTree(item, MENU_PREFERENCES);
addItemToTree(item, MENU_FIND);
addItemToTree(item, MENU_FIND_NEXT);
addItemToTree(item, MENU_FIND_PREVIOUS);
item = new QTreeWidgetItem(tree, QStringList("Import and Export"));
item->setFont(0, font);
addItemToTree(item, MENU_IMPORT_FILE);
addItemToTree(item, MENU_IMPORT_REGISTRY);
addItemToTree(item, MENU_EXPORT_FILE);
/***** setup custom saved session list ******/
auto it_begin = qutty_config.menu_action_list.lower_bound(MENU_CUSTOM_OPEN_SAVED_SESSION);
auto it_end = qutty_config.menu_action_list.upper_bound(MENU_CUSTOM_OPEN_SAVED_SESSION_END);
for (auto it = it_begin; it != it_end; ++it) {
keyshAddCustomSavedSessionToTree(it->second.str_data, it->second.int_data,
it->second.shortcut);
}
tree->expandAll();
tree->resizeColumnToContents(0);
tree->setMinimumHeight(200);
this->ui->le_keysh_shortcut->installEventFilter(this);
ui->le_keysh_shortcut->setDisabled(true);
/*************************************************************************************/
this->ui->tabWidget->removeTab(1);
// resize to minimum needed dimension
this->resize(0, 0);
this->connect(this, SIGNAL(accepted()), SLOT(slot_GuiPreferencesWindow_accepted()));
this->connect(this, SIGNAL(rejected()), SLOT(slot_GuiPreferencesWindow_rejected()));
}
GuiPreferencesWindow::~GuiPreferencesWindow()
{
delete ui;
}
void GuiPreferencesWindow::slot_GuiPreferencesWindow_rejected()
{
this->close();
this->deleteLater();
}
bool GuiPreferencesWindow::eventFilter(QObject *src, QEvent *e)
{
if (src == this->ui->le_keysh_shortcut) {
if (e->type() == QEvent::KeyPress) {
QKeyEvent *k = static_cast<QKeyEvent*>(e);
if (k->key() == Qt::Key_Control || k->key() == Qt::Key_Shift ||
k->key() == Qt::Key_Alt || shkey_len >= 4)
return true;
if (shkey_len==0)
memset(shkey_entered, 0, sizeof(shkey_entered));
shkey_entered[shkey_len++] = k->key() | k->modifiers();
ui->le_keysh_shortcut->setText(QKeySequence(shkey_entered[0], shkey_entered[1],
shkey_entered[2], shkey_entered[3]).toString());
return true;
}
return false;
}
return QDialog::eventFilter(src, e);
}
void GuiPreferencesWindow::on_tree_keysh_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
int menu_ind;
keysh_saveShortcutChange(previous);
ui->lbl_keysh_desc->clear();
ui->le_keysh_shortcut->clear();
ui->le_keysh_shortcut->setDisabled(true);
shkey_len = 0;
memset(shkey_entered, 0, sizeof(shkey_entered));
menu_ind = !current ? -1 : current->type() - QTreeWidgetItem::UserType;
if (menu_ind >= 0 && menu_ind < MENU_MAX_ACTION) {
ui->lbl_keysh_desc->setText(current->toolTip(0));
ui->le_keysh_shortcut->setText(current->text(1));
ui->le_keysh_shortcut->setEnabled(true);
QKeySequence keyseq(current->text(1));
for (uint i=0; i<keyseq.count(); i++)
shkey_entered[i] = keyseq[i];
shkey_len = 0;
}
}
void GuiPreferencesWindow::keysh_saveShortcutChange(QTreeWidgetItem *item)
{
qutty_menu_id_t menu_ind;
menu_ind = (qutty_menu_id_t)(!item ? -1 : item->type() - QTreeWidgetItem::UserType);
if (menu_ind >= 0 && menu_ind < MENU_MAX_ACTION) {
QString newseq = QKeySequence(shkey_entered[0], shkey_entered[1],
shkey_entered[2], shkey_entered[3]).toString();
QString oldseq = item->text(1);
if (oldseq != newseq) {
item->setText(1, newseq);
QFont f = item->font(1);
f.setBold(newseq != mainWindow->menuGetShortcutById(menu_ind).toString());
item->setFont(1, f);
shkey_changed = true;
}
}
}
void GuiPreferencesWindow::on_btn_keysh_clear_clicked()
{
ui->le_keysh_shortcut->clear();
shkey_len = 0;
memset(shkey_entered, 0, sizeof(shkey_entered));
}
void GuiPreferencesWindow::slot_keysh_custom_saved_session_shortcut_create()
{
QString session, opentype;
QStringList sesslist, opentypelist;
int opentypeind;
bool ok;
sesslist.append(tr("--Select Session--"));
for(auto it=qutty_config.config_list.begin();
it != qutty_config.config_list.end(); ++it) {
if (it->first == QUTTY_DEFAULT_CONFIG_SETTINGS)
continue;
sesslist.append(it->first);
}
session = QInputDialog::getItem(this, "Select session",
"Select a session to create shortcut for",
sesslist, 0, false, &ok);
if (!ok || session == sesslist[0])
return;
opentypelist.append(tr("--Select open mode--"));
opentypelist.append(tr("Open in a New Tab"));
opentypelist.append(tr("Open in Horizontal Split"));
opentypelist.append(tr("Open in Vertical Split"));
opentype = QInputDialog::getItem(this, tr("Select open mode"),
tr("How do you want to open the session"),
opentypelist, 0, false, &ok);
if (!ok || opentype == opentypelist[0])
return;
opentypeind = opentype==opentypelist[1] ? GuiBase::TYPE_LEAF :
opentype==opentypelist[2] ? GuiBase::TYPE_HORIZONTAL :
GuiBase::TYPE_VERTICAL;
keyshAddCustomSavedSessionToTree(session, opentypeind, QKeySequence());
}
void GuiPreferencesWindow::keyshAddCustomSavedSessionToTree(QString session, int opentypeind, QKeySequence key)
{
QString opentypestr = opentypeind==GuiBase::TYPE_LEAF ? "New Tab" :
opentypeind==GuiBase::TYPE_HORIZONTAL ? "Horizontal Split" :
"Vertical Split";
QTreeWidgetItem *item = new QTreeWidgetItem(QTreeWidgetItem::UserType + MENU_CUSTOM_OPEN_SAVED_SESSION);
item->setText(0, "Open '" + session + "' in " + opentypestr);
item->setToolTip(0, "Custom keyboard shortcut to open saved session '" + session +
"' in " + opentypestr);
item->setData(0, QTreeWidgetItem::UserType, session);
item->setData(0, QTreeWidgetItem::UserType+1, opentypeind);
item->setText(1, key.toString());
shkey_root_custom_saved_session->insertChild(shkey_root_custom_saved_session->childCount()-1,
item);
}
void GuiPreferencesWindow::slot_GuiPreferencesWindow_accepted()
{
bool is_config_changed = false;
QTreeWidgetItem *root, *ch;
/******************** Keyboard Shortcuts Tab ****************************************/
if (!shkey_changed)
goto keyboard_shortcut_done;
root = ui->tree_keysh->invisibleRootItem();
for(int i = 0; i < root->childCount(); i++) {
QTreeWidgetItem *item = root->child(i);
if (item == shkey_root_custom_saved_session)
continue;
for(int j = 0; j < item->childCount(); j++) {
ch = item->child(j);
qutty_menu_id_t menu_ind = (qutty_menu_id_t)(ch->type() - QTreeWidgetItem::UserType);
if (menu_ind < 0 || menu_ind >= MENU_MAX_ACTION)
continue;
QKeySequence newseq = ch->text(1);
if (newseq != mainWindow->menuGetShortcutById(menu_ind)) {
// we have a new shortcut configured
mainWindow->menuSetShortcutById(menu_ind, newseq);
if (newseq != QString::fromLatin1(qutty_menu_actions[menu_ind].key)) {
// shortcut needs to be saved in config
QtMenuActionConfig actioncfg(menu_ind, newseq);
qutty_config.menu_action_list.erase(menu_ind);
qutty_config.menu_action_list.insert(std::make_pair(menu_ind, actioncfg));
} else {
// shortcut needs to be removed from config
qutty_config.menu_action_list.erase(menu_ind);
}
is_config_changed = true;
}
}
}
// clear the saved-session in config & recreate
{
auto it_begin = qutty_config.menu_action_list.lower_bound(MENU_CUSTOM_OPEN_SAVED_SESSION);
auto it_end = qutty_config.menu_action_list.upper_bound(MENU_CUSTOM_OPEN_SAVED_SESSION_END);
qutty_config.menu_action_list.erase(it_begin, it_end);
}
for(int i = 0; i < shkey_root_custom_saved_session->childCount()-1; i++) {
QTreeWidgetItem *ch = shkey_root_custom_saved_session->child(i);
int menu_ind = ch->type() - QTreeWidgetItem::UserType;
QString session = ch->data(0, QTreeWidgetItem::UserType).toString();
int opentypemode = ch->data(0, QTreeWidgetItem::UserType+1).toInt();
if (menu_ind != MENU_CUSTOM_OPEN_SAVED_SESSION || ch->text(1).isEmpty() ||
session.isEmpty() || opentypemode < 1 || opentypemode > 4)
continue;
QKeySequence newseq = ch->text(1);
QtMenuActionConfig action(MENU_CUSTOM_OPEN_SAVED_SESSION+i, newseq, "",
session, opentypemode);
qutty_config.menu_action_list.insert(std::make_pair(MENU_CUSTOM_OPEN_SAVED_SESSION+i,
action));
}
mainWindow->initializeCustomSavedSessionShortcuts();
is_config_changed = true;
/*************************************************************************************/
keyboard_shortcut_done:
if (is_config_changed)
qutty_config.saveConfig();
return;
}
void GuiPreferencesWindow::on_btn_ok_clicked()
{
QTreeWidgetItem *root, *ch;
map<QKeySequence, QTreeWidgetItem*> dup_find;
/******************** Keyboard Shortcuts Tab ****************************************/
keysh_saveShortcutChange(ui->tree_keysh->currentItem());
if (!shkey_changed)
goto keyboard_shortcut_done;
root = ui->tree_keysh->invisibleRootItem();
// find duplicates in keyboard shortcuts
for(int i = 0; i < root->childCount(); i++) {
QTreeWidgetItem *item = root->child(i);
for(int j = 0; j < item->childCount(); j++) {
ch = item->child(j);
int menu_ind = ch->type() - QTreeWidgetItem::UserType;
if (menu_ind < 0 || menu_ind >= MENU_MAX_ACTION)
continue;
QKeySequence newseq = ch->text(1);
if (newseq.isEmpty())
continue;
auto it = dup_find.find(newseq);
if(it != dup_find.end()) {
QString msg("The following commands have duplicate keyboard shortcut configured.\n"
" * " + it->second->text(0) + "\n" +
" * " + ch->text(0) + "\n");
QMessageBox::critical(this, tr("Duplicate keyboard shortcut"), msg);
//this->reject();
return;
}
dup_find[newseq] = ch;
}
}
keyboard_shortcut_done:
this->accept();
}
void GuiPreferencesWindow::on_btn_cancel_clicked()
{
this->reject();
}
void GuiPreferencesWindow::on_menu_cb_sel_currentIndexChanged(int index)
{
if (index == 1) {
ui->menu_lbl_desc->setText(tr("This menu is shown when right-clicking on a Tab in the Tabbar\n"
"or when Ctrl + right-clicking inside a Terminal."));
} else if (index == 2) {
ui->menu_lbl_desc->setText(tr("This menu is shown when the drop-down button at top-right corner is clicked."));
} else {
ui->menu_lbl_desc->clear();
}
}