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"
24 #include <QKeySequence>
28 #include "boost/assert.hpp"
39 static char encode(string const & encoding, QString const & str)
41 typedef map<string, QTextCodec *> EncodingMap;
42 EncodingMap encoding_map;
44 QTextCodec * codec = 0;
46 EncodingMap::const_iterator cit = encoding_map.find(encoding);
47 if (cit == encoding_map.end()) {
48 LYXERR(Debug::KEY, "Unrecognised encoding '" << encoding << "'.");
49 codec = encoding_map.find("")->second;
55 LYXERR(Debug::KEY, "No codec for encoding '" << encoding << "' found.");
59 LYXERR(Debug::KEY, "Using codec " << fromqstr(codec->name()));
61 if (!codec->canEncode(str)) {
62 LYXERR(Debug::KEY, "Oof. Can't encode the text !");
66 return codec->fromUnicode(str).data()[0];
71 void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
73 sym->setKey(ev->key());
74 if (ev->text().isNull()) {
75 LYXERR(Debug::KEY, "keyevent has isNull() text !");
76 sym->setText(docstring());
79 LYXERR(Debug::KEY, "Getting key " << ev->key() << ", with text '"
80 << fromqstr(ev->text()) << "'");
81 // This is unsafe because ev->text() is the unicode representation of the
82 // key, not the name of the key. For example, Ctrl-x and Alt-x produce
84 sym->setText(qstring_to_ucs4(ev->text()));
85 LYXERR(Debug::KEY, "Setting key to " << sym->key() << ", "
86 << to_utf8(sym->text()));
90 void KeySymbol::init(string const & symbolname)
92 key_ = string_to_qkey(symbolname);
93 text_ = from_utf8(symbolname);
94 LYXERR(Debug::KEY, "Init key to " << key_ << ", " << to_utf8(text_));
98 bool KeySymbol::isOK() const
100 bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
101 LYXERR(Debug::KEY, "isOK is " << ok);
106 bool KeySymbol::isModifier() const
108 bool const mod = q_is_modifier(key_);
109 LYXERR(Debug::KEY, "isModifier is " << mod);
114 string KeySymbol::getSymbolName() const
116 string name = qkey_to_string(key_);
120 name = to_utf8(text_);
126 char_type KeySymbol::getUCSEncoded() const
131 // UTF16 has a maximum of two characters.
132 BOOST_ASSERT(text_.size() <= 2);
134 if (lyxerr.debugging() && text_.size() > 1) {
135 // We don't know yet how well support the full ucs4 range.
136 LYXERR(Debug::KEY, "KeySymbol::getUCSEncoded()");
137 for (int i = 0; i != int(text_.size()); ++i)
138 LYXERR(Debug::KEY, "char " << i << ": " << int(text_[i]));
145 docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
149 if (mod & ShiftModifier)
150 tmpkey += Qt::ShiftModifier;
151 if (mod & ControlModifier)
152 tmpkey += Qt::ControlModifier;
153 if (mod & AltModifier)
154 tmpkey += Qt::AltModifier;
156 QKeySequence seq(tmpkey);
158 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
159 : QKeySequence::PortableText));
163 bool KeySymbol::isText() const
167 LYXERR(Debug::KEY, "text_ empty, isText() == false");
172 bool KeySymbol::operator==(KeySymbol const & ks) const
174 // we do not have enough info for a fair comparison, so return
175 // false. This works out OK because unknown text from Qt will
176 // get inserted anyway after the isText() check
177 if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
179 return key_ == ks.key_;
183 KeyModifier q_key_state(Qt::KeyboardModifiers state)
185 KeyModifier k = NoModifier;
186 if (state & Qt::ControlModifier)
187 k |= ControlModifier;
188 if (state & Qt::ShiftModifier)
190 if (state & Qt::AltModifier || state & Qt::MetaModifier)