]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/CustomizedWidgets.cpp
2e9bbca6d551eaec234ad655a75dfd7d2820af59
[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
30 #include "support/qstring_helpers.h"
31
32
33 using lyx::KeySymbol;
34 using lyx::KeySequence;
35 using lyx::KeyModifier;
36 using lyx::toqstr;
37
38 namespace lyx {
39 namespace frontend {
40
41 ShortcutLineEdit::ShortcutLineEdit(QWidget * parent)
42         : QLineEdit(parent), keysequence_()
43 {
44         QApplication::instance()->installEventFilter(this);
45         has_cursor_ = false;
46 }
47
48
49 void ShortcutLineEdit::reset()
50 {
51         clear();
52         keysequence_ = KeySequence();
53 }
54
55
56 bool ShortcutLineEdit::eventFilter(QObject * obj, QEvent * e)
57 {
58         if (!has_cursor_)
59                 return false;
60
61         switch (e->type()) {
62                 // swallow these if we have focus and they come from elsewhere
63                 case QEvent::Shortcut:
64                 case QEvent::ShortcutOverride:
65                         if (obj != this)
66                                 return true;
67                 default: 
68                         break;
69         }        
70         return false;
71 }
72
73
74 KeySequence const ShortcutLineEdit::getKeySequence() const
75 {
76         return keysequence_;
77 }
78
79
80 void ShortcutLineEdit::keyPressEvent(QKeyEvent * e)
81 {
82         int const keyQt = e->key();
83         if (!keyQt)
84                 return;
85
86         switch(keyQt) {
87                 case Qt::Key_AltGr: //or else we get unicode salad
88                 case Qt::Key_Shift:
89                 case Qt::Key_Control:
90                 case Qt::Key_Alt:
91                 case Qt::Key_Meta:
92                         break;
93                 default:
94                         appendToSequence(e);
95                         setText(toqstr(keysequence_.print(KeySequence::ForGui)));
96         }
97 }
98
99
100 bool ShortcutLineEdit::event(QEvent * e)
101 {
102         switch (e->type()) {
103                 case QEvent::FocusOut:
104                         has_cursor_ = false;
105                         break;
106                 case QEvent::FocusIn:
107                         has_cursor_ = true;
108                         break;
109                 case QEvent::ShortcutOverride:
110                         keyPressEvent(static_cast<QKeyEvent *>(e));
111                         return true;
112                 case QEvent::KeyRelease:
113                 case QEvent::Shortcut:
114                 case QEvent::KeyPress:
115                         return true;
116                 default: 
117                         break;
118         }
119         return QLineEdit::event(e);
120 }
121
122
123 void ShortcutLineEdit::appendToSequence(QKeyEvent * e)
124 {
125         KeySymbol sym;
126         setKeySymbol(&sym, e);
127
128         KeyModifier mod = lyx::q_key_state(e->modifiers());
129         
130         keysequence_.addkey(sym, mod, lyx::NoModifier);
131 }
132
133 } // namespace frontend
134 } // namespace lyx
135
136 #include "CustomizedWidgets_moc.cpp"