]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiKeySymbol.cpp
0670a37198f99e5ca7cd8c42bda53ad745a6b72c
[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 static char encode(string const & encoding, QString const & str)
45 {
46         typedef map<string, QTextCodec *> EncodingMap;
47         EncodingMap encoding_map;
48
49         QTextCodec * codec = 0;
50
51         EncodingMap::const_iterator cit = encoding_map.find(encoding);
52         if (cit == encoding_map.end()) {
53                 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
54                                    << "'." << endl;
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
62                                    << "' found." << endl;
63                 return 0;
64         }
65
66         LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
67
68         if (!codec->canEncode(str)) {
69                 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
70                 return 0;
71         }
72
73         return codec->fromUnicode(str).data()[0];
74 }
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 !" << endl;
82                 sym->setText(docstring());
83                 return;
84         }
85         LYXERR(Debug::KEY) << "Getting key " << ev->key() << ", with text '"
86                 << fromqstr(ev->text()) << "'" << std::endl;
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()) << endl;
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_ << ", "
101                 << to_utf8(text_) << endl;
102 }
103
104
105 bool KeySymbol::isOK() const
106 {
107         bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
108         LYXERR(Debug::KEY) << "isOK is " << ok << endl;
109         return ok;
110 }
111
112
113 bool KeySymbol::isModifier() const
114 {
115         bool const mod = q_is_modifier(key_);
116         LYXERR(Debug::KEY) << "isMod is " << mod << endl;
117         return mod;
118 }
119
120
121 string KeySymbol::getSymbolName() const
122 {
123         string name = qkey_to_string(key_);
124
125         // others
126         if (name.empty())
127                 name = to_utf8(text_);
128
129         return name;
130 }
131
132
133 char_type KeySymbol::getUCSEncoded() const
134 {
135         if (text_.empty())
136                 return 0;
137
138         // UTF16 has a maximum of two characters.
139         BOOST_ASSERT(text_.size() <= 2);
140
141         if (lyxerr.debugging() && text_.size() > 1) {
142                 // We don't know yet how well support the full ucs4 range.
143                 LYXERR(Debug::KEY) << "KeySymbol::getUCSEncoded()" << endl;
144                 for (int i = 0; i != int(text_.size()); ++i) {
145                         LYXERR(Debug::KEY) << "char " << i << ": "
146                                 << int(text_[i]) << endl;
147                 }
148         }
149
150         return text_[0];
151 }
152
153
154 docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
155 {
156         int tmpkey = key_;
157
158         if (mod & ShiftModifier)
159                 tmpkey += Qt::ShiftModifier;
160         if (mod & ControlModifier)
161                 tmpkey += Qt::ControlModifier;
162         if (mod & AltModifier)
163                 tmpkey += Qt::AltModifier;
164
165         QKeySequence seq(tmpkey);
166
167         return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
168                                             : QKeySequence::PortableText));
169 }
170
171
172 bool KeySymbol::isText() const
173 {
174         if (!text_.empty())
175                 return true;
176         LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
177         return false;
178 }
179
180
181 bool KeySymbol::operator==(KeySymbol const & ks) const
182 {
183         // we do not have enough info for a fair comparison, so return
184         // false. This works out OK because unknown text from Qt will
185         // get inserted anyway after the isText() check
186         if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
187                 return false;
188         return key_ == ks.key_;
189 }
190
191
192 KeyModifier q_key_state(Qt::KeyboardModifiers state)
193 {
194         KeyModifier k = NoModifier;
195         if (state & Qt::ControlModifier)
196                 k |= ControlModifier;
197         if (state & Qt::ShiftModifier)
198                 k |= ShiftModifier;
199         if (state & Qt::AltModifier || state & Qt::MetaModifier)
200                 k |= AltModifier;
201         return k;
202 }
203
204 } // namespace lyx