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;
45 static char encode(string const & encoding, QString const & str)
47 typedef map<string, QTextCodec *> EncodingMap;
48 EncodingMap encoding_map;
50 QTextCodec * codec = 0;
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;
61 LYXERR(Debug::KEY, "No codec for encoding '" << encoding << "' found.");
65 LYXERR(Debug::KEY, "Using codec " << fromqstr(codec->name()));
67 if (!codec->canEncode(str)) {
68 LYXERR(Debug::KEY, "Oof. Can't encode the text !");
72 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 !");
82 sym->setText(docstring());
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
90 sym->setText(qstring_to_ucs4(ev->text()));
91 LYXERR(Debug::KEY, "Setting key to " << sym->key() << ", "
92 << to_utf8(sym->text()));
96 void KeySymbol::init(string const & symbolname)
98 key_ = string_to_qkey(symbolname);
99 text_ = from_utf8(symbolname);
100 LYXERR(Debug::KEY, "Init key to " << key_ << ", " << to_utf8(text_));
104 bool KeySymbol::isOK() const
106 bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
107 LYXERR(Debug::KEY, "isOK is " << ok);
112 bool KeySymbol::isModifier() const
114 bool const mod = q_is_modifier(key_);
115 LYXERR(Debug::KEY, "isModifier is " << mod);
120 string KeySymbol::getSymbolName() const
122 string name = qkey_to_string(key_);
126 name = to_utf8(text_);
132 char_type KeySymbol::getUCSEncoded() const
137 // UTF16 has a maximum of two characters.
138 BOOST_ASSERT(text_.size() <= 2);
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]));
151 docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
155 if (mod & ShiftModifier)
156 tmpkey += Qt::ShiftModifier;
157 if (mod & ControlModifier)
158 tmpkey += Qt::ControlModifier;
159 if (mod & AltModifier)
160 tmpkey += Qt::AltModifier;
162 QKeySequence seq(tmpkey);
164 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
165 : QKeySequence::PortableText));
169 bool KeySymbol::isText() const
173 LYXERR(Debug::KEY, "text_ empty, isText() == false");
178 bool KeySymbol::operator==(KeySymbol const & ks) const
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)
185 return key_ == ks.key_;
189 KeyModifier q_key_state(Qt::KeyboardModifiers state)
191 KeyModifier k = NoModifier;
192 if (state & Qt::ControlModifier)
193 k |= ControlModifier;
194 if (state & Qt::ShiftModifier)
196 if (state & Qt::AltModifier || state & Qt::MetaModifier)