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 "KeySymbol.h"
16 #include "qt_helpers.h"
20 #include "support/lstrings.h"
21 #include "support/environment.h"
22 #include "support/unicode.h"
28 #include <QKeySequence>
40 using lyx::support::contains;
41 using lyx::support::getEnv;
44 static char encode(string const & encoding, QString const & str)
46 typedef map<string, QTextCodec *> EncodingMap;
47 EncodingMap encoding_map;
49 QTextCodec * codec = 0;
51 EncodingMap::const_iterator cit = encoding_map.find(encoding);
52 if (cit == encoding_map.end()) {
53 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
55 codec = encoding_map.find("")->second;
61 LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
62 << "' found." << endl;
66 LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
68 if (!codec->canEncode(str)) {
69 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
73 return codec->fromUnicode(str).data()[0];
77 void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
79 sym->setKey(ev->key());
80 if (ev->text().isNull()) {
81 LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
82 sym->setText(docstring());
85 sym->setText(qstring_to_ucs4(ev->text()));
86 LYXERR(Debug::KEY) << "Setting key to " << sym->key() << ", "
87 << to_utf8(sym->text()) << endl;
91 void KeySymbol::init(string const & symbolname)
93 key_ = string_to_qkey(symbolname);
94 text_ = from_utf8(symbolname);
95 LYXERR(Debug::KEY) << "Init key to " << key_ << ", "
96 << to_utf8(text_) << endl;
100 bool KeySymbol::isOK() const
102 bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
103 LYXERR(Debug::KEY) << "isOK is " << ok << endl;
108 bool KeySymbol::isModifier() const
110 bool const mod = q_is_modifier(key_);
111 LYXERR(Debug::KEY) << "isMod is " << mod << endl;
116 string KeySymbol::getSymbolName() const
118 string name = qkey_to_string(key_);
120 // e.g. A-Za-z, and others
122 name = to_utf8(text_);
128 char_type KeySymbol::getUCSEncoded() const
133 // UTF16 has a maximum of two characters.
134 BOOST_ASSERT(text_.size() <= 2);
136 if (lyxerr.debugging() && text_.size() > 1) {
137 // We don't know yet how well support the full ucs4 range.
138 LYXERR(Debug::KEY) << "KeySymbol::getUCSEncoded()" << endl;
139 for (int i = 0; i != int(text_.size()); ++i) {
140 LYXERR(Debug::KEY) << "char " << i << ": "
141 << int(text_[i]) << endl;
149 docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
153 if (mod & ShiftModifier)
154 tmpkey += Qt::ShiftModifier;
155 if (mod & ControlModifier)
156 tmpkey += Qt::ControlModifier;
157 if (mod & AltModifier)
158 tmpkey += Qt::AltModifier;
160 QKeySequence seq(tmpkey);
162 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
163 : QKeySequence::PortableText));
167 bool KeySymbol::isText() const
171 LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
176 bool KeySymbol::operator==(KeySymbol const & ks) const
178 // we do not have enough info for a fair comparison, so return
179 // false. This works out OK because unknown text from Qt will
180 // get inserted anyway after the isText() check
181 if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
183 return key_ == ks.key_;
187 KeyModifier q_key_state(Qt::KeyboardModifiers state)
189 KeyModifier k = NoModifier;
190 if (state & Qt::ControlModifier)
191 k |= ControlModifier;
192 if (state & Qt::ShiftModifier)
194 if (state & Qt::AltModifier || state & Qt::MetaModifier)