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