]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/CustomizedWidgets.cpp
PrefShortcuts: use KeySequence::ForGui to display shortcuts
[lyx.git] / src / frontends / qt4 / CustomizedWidgets.cpp
1 /**
2  * \file GuiPrefs.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  * \author Edwin Leuven
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 /*
13         The code for the ShortcutLineEdit class was adapted from
14         kkeysequencewidget.cpp, which is part of the KDE libraries.
15         Copyright (C) 1998 Mark Donohoe <donohoe@kde.org>
16         Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>
17         Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com>
18         Licensed under version 2 of the General Public License and
19         used here in accordance with the terms of that license.
20 */
21
22 #include <config.h>
23
24 #include "CustomizedWidgets.h"
25 #include "FuncRequest.h"
26 #include "GuiKeySymbol.h"
27
28 #include <QApplication>
29 #include <QCloseEvent>
30
31 #include "support/qstring_helpers.h"
32
33
34 using lyx::KeySymbol;
35 using lyx::KeySequence;
36 using lyx::KeyModifier;
37 using lyx::toqstr;
38
39 namespace lyx {
40 namespace frontend {
41
42 ShortcutLineEdit::ShortcutLineEdit(QWidget * parent)
43         : QLineEdit(parent), keysequence_()
44 {
45         QApplication::instance()->installEventFilter(this);
46         has_cursor_ = false;
47 }
48
49
50 void ShortcutLineEdit::reset()
51 {
52         clear();
53         keysequence_ = KeySequence();
54 }
55
56
57 bool ShortcutLineEdit::eventFilter(QObject * obj, QEvent * e)
58 {
59         if (!has_cursor_)
60                 return false;
61
62         switch (e->type()) {
63                 // swallow these if we have focus and they come from elsewhere
64                 case QEvent::Shortcut:
65                 case QEvent::ShortcutOverride:
66                         if (obj != this)
67                                 return true;
68                 default: 
69                         break;
70         }        
71         return false;
72 }
73
74
75 KeySequence const ShortcutLineEdit::getKeySequence() const
76 {
77         return keysequence_;
78 }
79
80
81 void ShortcutLineEdit::keyPressEvent(QKeyEvent * e)
82 {
83         int const keyQt = e->key();
84         if (!keyQt)
85                 return;
86
87         switch(keyQt) {
88                 case Qt::Key_AltGr: //or else we get unicode salad
89                 case Qt::Key_Shift:
90                 case Qt::Key_Control:
91                 case Qt::Key_Alt:
92                 case Qt::Key_Meta:
93                         break;
94                 default:
95                         appendToSequence(e);
96                         setText(toqstr(keysequence_.print(KeySequence::ForGui)));
97         }
98 }
99
100
101 bool ShortcutLineEdit::event(QEvent * e)
102 {
103         switch (e->type()) {
104                 case QEvent::FocusOut:
105                         has_cursor_ = false;
106                         break;
107                 case QEvent::FocusIn:
108                         has_cursor_ = true;
109                         break;
110                 case QEvent::ShortcutOverride:
111                         keyPressEvent(static_cast<QKeyEvent *>(e));
112                         return true;
113                 case QEvent::KeyRelease:
114                 case QEvent::Shortcut:
115                 case QEvent::KeyPress:
116                         return true;
117                 default: 
118                         break;
119         }
120         return QLineEdit::event(e);
121 }
122
123
124 void ShortcutLineEdit::appendToSequence(QKeyEvent * e)
125 {
126         KeySymbol sym;
127         setKeySymbol(&sym, e);
128
129         KeyModifier mod = lyx::q_key_state(e->modifiers());
130         
131         keysequence_.addkey(sym, mod, lyx::NoModifier);
132 }
133
134 } // namespace frontend
135 } // namespace lyx
136
137 #include "CustomizedWidgets_moc.cpp"