2 * \file qt4/GuiKeySymbol.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"
18 #include "support/debug.h"
24 #include <QKeySequence>
28 #include "boost/assert.hpp"
37 static char encode(string const & encoding, QString const & str)
39 typedef map<string, QTextCodec *> EncodingMap;
40 EncodingMap encoding_map;
42 QTextCodec * codec = 0;
44 EncodingMap::const_iterator cit = encoding_map.find(encoding);
45 if (cit == encoding_map.end()) {
46 LYXERR(Debug::KEY, "Unrecognised encoding '" << encoding << "'.");
47 codec = encoding_map.find("")->second;
53 LYXERR(Debug::KEY, "No codec for encoding '" << encoding << "' found.");
57 LYXERR(Debug::KEY, "Using codec " << fromqstr(codec->name()));
59 if (!codec->canEncode(str)) {
60 LYXERR(Debug::KEY, "Oof. Can't encode the text !");
64 return codec->fromUnicode(str).data()[0];
69 void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
71 sym->setKey(ev->key());
72 if (ev->text().isNull()) {
73 LYXERR(Debug::KEY, "keyevent has isNull() text !");
74 sym->setText(docstring());
77 LYXERR(Debug::KEY, "Getting key " << ev->key() << ", with text '"
78 << fromqstr(ev->text()) << "'");
79 // This is unsafe because ev->text() is the unicode representation of the
80 // key, not the name of the key. For example, Ctrl-x and Alt-x produce
82 sym->setText(qstring_to_ucs4(ev->text()));
83 LYXERR(Debug::KEY, "Setting key to " << sym->key() << ", "
84 << to_utf8(sym->text()));
88 void KeySymbol::init(string const & symbolname)
90 key_ = string_to_qkey(symbolname);
91 text_ = from_utf8(symbolname);
92 LYXERR(Debug::KEY, "Init key to " << key_ << ", " << to_utf8(text_));
96 bool KeySymbol::isOK() const
98 bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
99 LYXERR(Debug::KEY, "isOK is " << ok);
104 bool KeySymbol::isModifier() const
106 bool const mod = q_is_modifier(key_);
107 LYXERR(Debug::KEY, "isModifier is " << mod);
112 string KeySymbol::getSymbolName() const
114 string name = qkey_to_string(key_);
118 name = to_utf8(text_);
124 char_type KeySymbol::getUCSEncoded() const
129 // UTF16 has a maximum of two characters.
130 BOOST_ASSERT(text_.size() <= 2);
132 if (lyxerr.debugging() && text_.size() > 1) {
133 // We don't know yet how well support the full ucs4 range.
134 LYXERR(Debug::KEY, "KeySymbol::getUCSEncoded()");
135 for (int i = 0; i != int(text_.size()); ++i)
136 LYXERR(Debug::KEY, "char " << i << ": " << int(text_[i]));
143 docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
147 if (mod & ShiftModifier)
148 tmpkey += Qt::ShiftModifier;
149 if (mod & ControlModifier)
150 tmpkey += Qt::ControlModifier;
151 if (mod & AltModifier)
152 tmpkey += Qt::AltModifier;
154 QKeySequence seq(tmpkey);
156 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
157 : QKeySequence::PortableText));
161 bool KeySymbol::isText() const
165 LYXERR(Debug::KEY, "text_ empty, isText() == false");
170 bool KeySymbol::operator==(KeySymbol const & ks) const
172 // we do not have enough info for a fair comparison, so return
173 // false. This works out OK because unknown text from Qt will
174 // get inserted anyway after the isText() check
175 if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
177 return key_ == ks.key_;
181 KeyModifier q_key_state(Qt::KeyboardModifiers state)
183 KeyModifier k = NoModifier;
184 if (state & Qt::ControlModifier)
185 k |= ControlModifier;
186 if (state & Qt::ShiftModifier)
188 if (state & Qt::AltModifier || state & Qt::MetaModifier)