]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/CustomizedWidgets.cpp
ed9dfee8fd3870bf16dbeeb9f068e4b519cb2227
[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 "GuiKeySymbol.h"
26
27 #include <QApplication>
28 #include <QCloseEvent>
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 ShortcutLineEdit::ShortcutLineEdit(QWidget * parent)
39         : QLineEdit(parent), keysequence_()
40 {
41         QApplication::instance()->installEventFilter(this);
42         has_cursor_ = false;
43 }
44
45
46 void ShortcutLineEdit::reset()
47 {
48         clear();
49         keysequence_ = KeySequence();
50 }
51
52
53 bool ShortcutLineEdit::eventFilter(QObject * obj, QEvent * e)
54 {
55         if (!has_cursor_)
56                 return false;
57
58         switch (e->type()) {
59                 // swallow these if we have focus and they come from elsewhere
60                 case QEvent::Shortcut:
61                 case QEvent::ShortcutOverride:
62                         if (obj != this)
63                                 return true;
64                 default: 
65                         break;
66         }        
67         return false;
68 }
69
70
71 KeySequence const ShortcutLineEdit::getKeySequence() const
72 {
73         return keysequence_;
74 }
75
76
77 void ShortcutLineEdit::keyPressEvent(QKeyEvent * e)
78 {
79         int const keyQt = e->key();
80         if (!keyQt)
81                 return;
82
83         switch(keyQt) {
84                 case Qt::Key_AltGr: //or else we get unicode salad
85                 case Qt::Key_Shift:
86                 case Qt::Key_Control:
87                 case Qt::Key_Alt:
88                 case Qt::Key_Meta:
89                         break;
90                 default:
91                         appendToSequence(e);
92                         setText(toqstr(keysequence_.print(KeySequence::BindFile)));
93         }
94 }
95
96
97 bool ShortcutLineEdit::event(QEvent * e)
98 {
99         switch (e->type()) {
100                 case QEvent::FocusOut:
101                         has_cursor_ = false;
102                         break;
103                 case QEvent::FocusIn:
104                         has_cursor_ = true;
105                         break;
106                 case QEvent::ShortcutOverride:
107                         keyPressEvent(static_cast<QKeyEvent *>(e));
108                         return true;
109                 case QEvent::KeyRelease:
110                 case QEvent::Shortcut:
111                 case QEvent::KeyPress:
112                         return true;
113                 default: 
114                         break;
115         }
116         return QLineEdit::event(e);
117 }
118
119
120 void ShortcutLineEdit::appendToSequence(QKeyEvent * e)
121 {
122         KeySymbol sym;
123         setKeySymbol(&sym, e);
124
125         KeyModifier mod = lyx::NoModifier;
126         if (e->modifiers() & Qt::SHIFT)
127                 mod |= lyx::ShiftModifier;
128         if (e->modifiers() & Qt::CTRL)
129                 mod |= lyx::ControlModifier;
130         if (e->modifiers() & Qt::ALT | e->modifiers() & Qt::META)
131                 mod |= lyx::AltModifier;
132         
133         keysequence_.addkey(sym, mod, lyx::NoModifier);
134 }
135
136
137 QString const SearchLineEdit::hintMessage() const
138 {
139         return toqstr("Search ...");
140 }
141
142
143 void SearchLineEdit::focusInEvent(QFocusEvent * e)
144 {
145         if (text() == hintMessage())
146                 clear();
147 }
148
149
150 void SearchLineEdit::focusOutEvent(QFocusEvent * e)
151 {
152         if (text().isEmpty())
153                 setText(hintMessage());
154 }
155
156
157 #include "CustomizedWidgets_moc.cpp"