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.
6 * \author Asger & Jürgen
8 * Full author contact details are available in file CREDITS.
13 #include "frontends/KeySymbol.h"
15 #include "KeySymbol.h"
18 #include "qt_helpers.h"
22 #include "support/lstrings.h"
23 #include "support/environment.h"
24 #include "support/unicode.h"
30 #include <QKeySequence>
42 using lyx::support::contains;
43 using lyx::support::getEnv;
48 char encode(string const & encoding, QString const & str)
50 typedef map<string, QTextCodec *> EncodingMap;
51 EncodingMap encoding_map;
53 QTextCodec * codec = 0;
55 EncodingMap::const_iterator cit = encoding_map.find(encoding);
56 if (cit == encoding_map.end()) {
57 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
59 codec = encoding_map.find("")->second;
65 LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
66 << "' found." << endl;
70 LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
72 if (!codec->canEncode(str)) {
73 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
77 return codec->fromUnicode(str).data()[0];
83 void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
85 sym->setKey(ev->key());
86 if (ev->text().isNull()) {
87 LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
88 sym->setText(docstring());
91 sym->setText(qstring_to_ucs4(ev->text()));
92 LYXERR(Debug::KEY) << "Setting key to " << sym->key() << ", "
93 << to_utf8(sym->text()) << endl;
97 void KeySymbol::init(string const & symbolname)
99 key_ = string_to_qkey(symbolname);
100 text_ = from_utf8(symbolname);
101 LYXERR(Debug::KEY) << "Init key to " << key_ << ", "
102 << to_utf8(text_) << endl;
106 bool KeySymbol::isOK() const
108 bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
109 LYXERR(Debug::KEY) << "isOK is " << ok << endl;
114 bool KeySymbol::isModifier() const
116 bool const mod = q_is_modifier(key_);
117 LYXERR(Debug::KEY) << "isMod is " << mod << endl;
122 string KeySymbol::getSymbolName() const
124 string name = qkey_to_string(key_);
126 // e.g. A-Za-z, and others
128 name = to_utf8(text_);
134 char_type KeySymbol::getUCSEncoded() const
139 // UTF16 has a maximum of two characters.
140 BOOST_ASSERT(text_.size() <= 2);
142 if (lyxerr.debugging() && text_.size() > 1) {
143 // We don't know yet how well support the full ucs4 range.
144 LYXERR(Debug::KEY) << "KeySymbol::getUCSEncoded()" << endl;
145 for (int i = 0; i != int(text_.size()); ++i) {
146 LYXERR(Debug::KEY) << "char " << i << ": "
147 << int(text_[i]) << endl;
155 docstring const KeySymbol::print(key_modifier::state mod, bool forgui) const
159 if (mod & key_modifier::shift)
161 if (mod & key_modifier::ctrl)
163 if (mod & key_modifier::alt)
166 QKeySequence seq(tmpkey);
168 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
169 : QKeySequence::PortableText));
173 bool KeySymbol::isText() const
177 LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
182 bool KeySymbol::operator==(KeySymbol const & ks) const
184 // we do not have enough info for a fair comparison, so return
185 // false. This works out OK because unknown text from Qt will
186 // get inserted anyway after the isText() check
187 if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
189 return key_ == ks.key_;
193 key_modifier::state q_key_state(Qt::KeyboardModifiers state)
195 key_modifier::state k = key_modifier::none;
196 if (state & Qt::ControlModifier)
197 k |= key_modifier::ctrl;
198 if (state & Qt::ShiftModifier)
199 k |= key_modifier::shift;
200 if (state & Qt::AltModifier || state & Qt::MetaModifier)
201 k |= key_modifier::alt;