506ad6ecd2663a6be31cc20a805253ed4fc4810e
[presencevnc] / src / keymenu.cpp
1 /*
2     Presence VNC
3     Copyright (C) 2010 Christian Pulvermacher
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "keymenu.h"
21
22
23 KeyMenu::KeyMenu(QWidget *parent):
24         QDialog(parent)
25 {
26         setWindowTitle(tr("Additional Keys"));
27         QTabWidget *tabwidget = new QTabWidget(this);
28
29         //modifiers
30         ActionTab *mod_tab = new ActionTab(this);
31         QAction *win = new QAction(tr("Win"), this);
32         win->setShortcut(Qt::META);
33         win->setCheckable(true);
34         mod_tab->addAction(win);
35         QAction *alt = new QAction(tr("Alt"), this);
36         alt->setShortcut(Qt::ALT);
37         alt->setCheckable(true);
38         mod_tab->addAction(alt);
39         tabwidget->addTab(mod_tab, tr("Modifiers"));
40
41         //movement/text editing keys
42         ActionTab *other_tab = new ActionTab(this);
43         other_tab->addAction(tr("Insert"), Qt::Key_Insert);
44         other_tab->addAction(tr("Delete"), Qt::Key_Delete);
45         other_tab->addAction(tr("Backspace"), Qt::Key_Backspace);
46         other_tab->addAction(tr("Home"), Qt::Key_Home);
47         other_tab->addAction(tr("End"), Qt::Key_End);
48         tabwidget->addTab(other_tab, tr("Editing"));
49
50         //F1-F12
51         ActionTab *fx_tab = new ActionTab(this);
52         for(int i = 1; i<=12; i++)
53                 fx_tab->addAction(tr("F%1").arg(i), QString("F%1").arg(i));
54         tabwidget->addTab(fx_tab, tr("F1-F12"));
55
56         //common key sequences
57         ActionTab *complex_tab = new ActionTab(this);
58         complex_tab->addAction(tr("Ctrl+Alt+Del"), QString("Ctrl+Alt+Delete"));
59         complex_tab->addAction(tr("Ctrl+Alt+Backspace"), QString("Ctrl+Alt+Backspace"));
60         tabwidget->addTab(complex_tab, tr("Misc"));
61
62         QVBoxLayout *layout = new QVBoxLayout();
63         layout->addWidget(tabwidget);
64         setLayout(layout);
65 }
66
67 void KeyMenu::accept()
68 {
69         QAction* selected_action = qobject_cast<QAction* >(sender());
70         if(!selected_action) {
71                 keysequence = QKeySequence();
72         } else {
73                 keysequence = selected_action->shortcut();
74         }
75
76         QDialog::accept();
77 }
78
79 ActionTab::ActionTab(KeyMenu *parent):
80         QScrollArea(parent),
81         keymenu(parent)
82 {
83         setWidgetResizable(true);
84         setWidget(&widget);
85         widget.setLayout(&layout);
86 }
87
88 void ActionTab::addAction(QString text, QKeySequence keysequence)
89 {
90         QAction *action = new QAction(text, this);
91         action->setShortcut(keysequence);
92
93         addAction(action);
94 }
95
96 void ActionTab::addAction(QAction *action)
97 {
98         connect(action, SIGNAL(triggered()),
99                 keymenu, SLOT(accept()));
100
101         QToolButton *button = new QToolButton();
102         button->setDefaultAction(action);
103         layout.addWidget(button);
104 }