3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger and Jürgen
9 * Full author contact details are available in file CREDITS.
14 #include "QKeySymbol.h"
16 #include "qt_helpers.h"
22 #include <QKeySequence>
27 #include "support/lstrings.h"
28 #include "support/environment.h"
29 #include "support/unicode.h"
40 using lyx::support::contains;
41 using lyx::support::getEnv;
46 typedef map<string, QTextCodec *> EncodingMap;
47 EncodingMap encoding_map;
49 char const encode(string const & encoding, QString const & str)
51 QTextCodec * codec = 0;
53 EncodingMap::const_iterator cit = encoding_map.find(encoding);
54 if (cit == encoding_map.end()) {
55 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
57 codec = encoding_map.find("")->second;
63 LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
64 << "' found." << endl;
68 LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
70 if (!codec->canEncode(str)) {
71 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
75 return codec->fromUnicode(str).data()[0];
81 QKeySymbol::QKeySymbol()
82 : KeySymbol(), key_(0)
87 void QKeySymbol::set(QKeyEvent * ev)
90 if (ev->text().isNull()) {
91 LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
96 LYXERR(Debug::KEY) << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
100 void QKeySymbol::init(string const & symbolname)
102 key_ = string_to_qkey(symbolname);
103 text_ = toqstr(symbolname);
104 LYXERR(Debug::KEY) << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
108 bool QKeySymbol::isOK() const
110 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
111 LYXERR(Debug::KEY) << "isOK is " << ok << endl;
116 bool QKeySymbol::isModifier() const
118 bool const mod(q_is_modifier(key_));
119 LYXERR(Debug::KEY) << "isMod is " << mod << endl;
124 string QKeySymbol::getSymbolName() const
126 string sym(qkey_to_string(key_));
128 // e.g. A-Za-z, and others
130 sym = fromqstr(text_);
136 char_type QKeySymbol::getUCSEncoded() const
141 // UTF16 has a maximum of two characters.
142 BOOST_ASSERT(text_.size() <= 2);
144 if (lyxerr.debugging() && text_.size() > 1) {
145 // We don't know yet how well support the full ucs4 range.
146 LYXERR(Debug::KEY) << "QKeySymbol::getUCSEncoded()" << endl;
147 for (int i = 0; i < text_.size(); ++i) {
148 LYXERR(Debug::KEY) << "char " << i << ": "
149 << text_[i].unicode() << endl;
153 // Only one UCS4 character at the end.
154 docstring ucs4_text = qstring_to_ucs4(text_);
159 docstring const QKeySymbol::print(key_modifier::state mod, bool forgui) const
163 if (mod & key_modifier::shift)
165 if (mod & key_modifier::ctrl)
167 if (mod & key_modifier::alt)
170 QKeySequence seq(tmpkey);
172 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
173 : QKeySequence::PortableText));
177 bool QKeySymbol::isText() const
179 if (text_.isEmpty()) {
180 LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
188 bool QKeySymbol::operator==(KeySymbol const & ks) const
190 QKeySymbol const & qks = static_cast<QKeySymbol const &>(ks);
192 // we do not have enough info for a fair comparison, so return
193 // false. This works out OK because unknown text from Qt will
194 // get inserted anyway after the isText() check
195 if (key_ == Qt::Key_unknown || qks.key_ == Qt::Key_unknown)
198 return key_ == qks.key_;