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