]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiKeySymbol.cpp
reduce line noise
[features.git] / src / frontends / qt4 / GuiKeySymbol.cpp
1 /**
2  * \file qt4/KeySymbolFactory.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger & Jürgen
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "KeySymbol.h"
14
15 #include "qlkey.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19
20 #include "support/lstrings.h"
21 #include "support/environment.h"
22 #include "support/unicode.h"
23
24 #include "Encoding.h"
25 #include "Language.h"
26
27 #include <QKeyEvent>
28 #include <QKeySequence>
29 #include <QEvent>
30 #include <QTextCodec>
31
32 #include <map>
33
34
35 namespace lyx {
36
37 using std::endl;
38 using std::string;
39 using std::map;
40 using lyx::support::contains;
41 using lyx::support::getEnv;
42
43
44 #if 0
45 static char encode(string const & encoding, QString const & str)
46 {
47         typedef map<string, QTextCodec *> EncodingMap;
48         EncodingMap encoding_map;
49
50         QTextCodec * codec = 0;
51
52         EncodingMap::const_iterator cit = encoding_map.find(encoding);
53         if (cit == encoding_map.end()) {
54                 LYXERR(Debug::KEY, "Unrecognised encoding '" << encoding << "'.");
55                 codec = encoding_map.find("")->second;
56         } else {
57                 codec = cit->second;
58         }
59
60         if (!codec) {
61                 LYXERR(Debug::KEY, "No codec for encoding '" << encoding << "' found.");
62                 return 0;
63         }
64
65         LYXERR(Debug::KEY, "Using codec " << fromqstr(codec->name()));
66
67         if (!codec->canEncode(str)) {
68                 LYXERR(Debug::KEY, "Oof. Can't encode the text !");
69                 return 0;
70         }
71
72         return codec->fromUnicode(str).data()[0];
73 }
74 #endif
75
76
77 void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
78 {
79         sym->setKey(ev->key());
80         if (ev->text().isNull()) {
81                 LYXERR(Debug::KEY, "keyevent has isNull() text !");
82                 sym->setText(docstring());
83                 return;
84         }
85         LYXERR(Debug::KEY, "Getting key " << ev->key() << ", with text '"
86                 << fromqstr(ev->text()) << "'");
87         // This is unsafe because ev->text() is the unicode representation of the
88         // key, not the name of the key. For example, Ctrl-x and Alt-x produce 
89         // different texts.
90         sym->setText(qstring_to_ucs4(ev->text()));
91         LYXERR(Debug::KEY, "Setting key to " << sym->key() << ", "
92                 << to_utf8(sym->text()));
93 }
94
95
96 void KeySymbol::init(string const & symbolname)
97 {
98         key_ = string_to_qkey(symbolname);
99         text_ = from_utf8(symbolname);
100         LYXERR(Debug::KEY, "Init key to " << key_ << ", " << to_utf8(text_));
101 }
102
103
104 bool KeySymbol::isOK() const
105 {
106         bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
107         LYXERR(Debug::KEY, "isOK is " << ok);
108         return ok;
109 }
110
111
112 bool KeySymbol::isModifier() const
113 {
114         bool const mod = q_is_modifier(key_);
115         LYXERR(Debug::KEY, "isModifier is " << mod);
116         return mod;
117 }
118
119
120 string KeySymbol::getSymbolName() const
121 {
122         string name = qkey_to_string(key_);
123
124         // others
125         if (name.empty())
126                 name = to_utf8(text_);
127
128         return name;
129 }
130
131
132 char_type KeySymbol::getUCSEncoded() const
133 {
134         if (text_.empty())
135                 return 0;
136
137         // UTF16 has a maximum of two characters.
138         BOOST_ASSERT(text_.size() <= 2);
139
140         if (lyxerr.debugging() && text_.size() > 1) {
141                 // We don't know yet how well support the full ucs4 range.
142                 LYXERR(Debug::KEY, "KeySymbol::getUCSEncoded()");
143                 for (int i = 0; i != int(text_.size()); ++i)
144                         LYXERR(Debug::KEY, "char " << i << ": " << int(text_[i]));
145         }
146
147         return text_[0];
148 }
149
150
151 docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
152 {
153         int tmpkey = key_;
154
155         if (mod & ShiftModifier)
156                 tmpkey += Qt::ShiftModifier;
157         if (mod & ControlModifier)
158                 tmpkey += Qt::ControlModifier;
159         if (mod & AltModifier)
160                 tmpkey += Qt::AltModifier;
161
162         QKeySequence seq(tmpkey);
163
164         return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
165                                             : QKeySequence::PortableText));
166 }
167
168
169 bool KeySymbol::isText() const
170 {
171         if (!text_.empty())
172                 return true;
173         LYXERR(Debug::KEY, "text_ empty, isText() == false");
174         return false;
175 }
176
177
178 bool KeySymbol::operator==(KeySymbol const & ks) const
179 {
180         // we do not have enough info for a fair comparison, so return
181         // false. This works out OK because unknown text from Qt will
182         // get inserted anyway after the isText() check
183         if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
184                 return false;
185         return key_ == ks.key_;
186 }
187
188
189 KeyModifier q_key_state(Qt::KeyboardModifiers state)
190 {
191         KeyModifier k = NoModifier;
192         if (state & Qt::ControlModifier)
193                 k |= ControlModifier;
194         if (state & Qt::ShiftModifier)
195                 k |= ShiftModifier;
196         if (state & Qt::AltModifier || state & Qt::MetaModifier)
197                 k |= AltModifier;
198         return k;
199 }
200
201 } // namespace lyx