Initial commit
[keepassx] / src / lib / ShortcutWidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2008 by Felix Geyer                                *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; version 2 of the License.               *
7
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20 #include "ShortcutWidget.h"
21
22 #if defined(GLOBAL_AUTOTYPE) && defined(Q_WS_X11)
23
24 #include <QKeyEvent>
25 #include <QX11Info>
26 #include <QPalette>
27 #include "HelperX11.h"
28 #include "AutoTypeGlobalX11.h"
29
30 ShortcutWidget::ShortcutWidget(QWidget* parent) : QLineEdit(parent), lock(false), failed(false){
31 }
32
33 Shortcut ShortcutWidget::shortcut(){
34         if (lock)
35                 return pShortcut;
36         else
37                 return Shortcut();
38 }
39
40 void ShortcutWidget::setShortcut(const Shortcut& s){
41         lock = true;
42         displayShortcut(s.key, false, s.ctrl, s.shift, s.alt, s.altgr, s.win);
43 }
44
45 void ShortcutWidget::keyPressEvent(QKeyEvent* event){
46         keyEvent(event, false);
47         event->accept();
48 }
49
50 void ShortcutWidget::keyReleaseEvent(QKeyEvent* event){
51         keyEvent(event, true);
52         event->accept();
53 }
54
55 void ShortcutWidget::keyEvent(QKeyEvent* event, bool release){
56         if (release && lock)
57                 return;
58         
59         AutoTypeGlobalX11* autoTypeGlobal = static_cast<AutoTypeGlobalX11*>(autoType);
60         
61         unsigned int mods = HelperX11::keyboardModifiers(QX11Info::display());
62         displayShortcut(event->nativeVirtualKey(), release, mods & ControlMask,
63                         mods & ShiftMask, mods & autoTypeGlobal->maskAlt(),
64                         mods & autoTypeGlobal->maskAltGr(), mods & autoTypeGlobal->maskMeta());
65 }
66
67 void ShortcutWidget::displayShortcut(quint32 key, bool release, bool ctrl, bool shift, bool alt, bool altgr, bool win){
68         QString text;
69         
70         if (ctrl)
71                 text.append(tr("Ctrl")).append(" + ");
72         if (shift)
73                 text.append(tr("Shift")).append(" + ");
74         if (alt)
75                 text.append(tr("Alt")).append(" + ");
76         if (altgr)
77                 text.append(tr("AltGr")).append(" + ");
78         if (win)
79                 text.append(tr("Win")).append(" + ");
80         
81         if ( !release && (key<XK_Shift_L || key>XK_Hyper_R) && (key<XK_ISO_Lock || key>XK_ISO_Last_Group_Lock) ){
82                 // converts key into orignal key on the keyboard
83                 KeySym keysym = XKeycodeToKeysym(QX11Info::display(), XKeysymToKeycode(QX11Info::display(),key), 0);
84                 if (keysym>=0xfd00 && keysym<=0xffff){
85                         text.append(XKeysymToString(keysym));
86                 }
87                 else{
88                         text.append(static_cast<quint32>(keysym));
89                 }
90
91                 lock = ctrl || shift || alt || altgr || win;
92                 if (lock){
93                         pShortcut.key = keysym;
94                         pShortcut.ctrl = ctrl;
95                         pShortcut.shift = shift;
96                         pShortcut.alt = alt;
97                         pShortcut.altgr = altgr;
98                         pShortcut.win = win;
99                         failed = autoType->registerGlobalShortcut(pShortcut);
100                         if (!failed)
101                                 setBackgroundColor(QColor(255, 150, 150));
102                         else
103                                 setBackgroundColor(Qt::white);
104                 }
105         }
106         else {
107                 lock = false;
108                 if (failed)
109                         setBackgroundColor(Qt::white);
110         }
111         
112         setText(text);
113 }
114
115 void ShortcutWidget::setBackgroundColor(const QColor& c){
116         QPalette p( palette() );
117         p.setColor(backgroundRole(), c);
118         setPalette(p);
119 }
120
121 #else
122
123 ShortcutWidget::ShortcutWidget(QWidget* parent) : QLineEdit(parent){
124 }
125
126 #endif