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 "QLyXKeySym.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 QLyXKeySym::QLyXKeySym()
82 : LyXKeySym(), key_(0)
87 void QLyXKeySym::set(QKeyEvent * ev)
90 if (ev->text().isNull()) {
91 if (lyxerr.debugging())
92 lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
97 if (lyxerr.debugging())
98 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
102 void QLyXKeySym::init(string const & symbolname)
104 key_ = string_to_qkey(symbolname);
105 text_ = toqstr(symbolname);
106 if (lyxerr.debugging())
107 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
111 bool QLyXKeySym::isOK() const
113 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
114 if (lyxerr.debugging())
115 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
120 bool QLyXKeySym::isModifier() const
122 bool const mod(q_is_modifier(key_));
123 if (lyxerr.debugging())
124 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
129 string QLyXKeySym::getSymbolName() const
131 string sym(qkey_to_string(key_));
133 // e.g. A-Za-z, and others
135 sym = fromqstr(text_);
141 char_type QLyXKeySym::getUCSEncoded() const
146 // UTF16 has a maximum of two characters.
147 BOOST_ASSERT(text_.size() <= 2);
149 if (lyxerr.debugging() && text_.size() > 1) {
150 // We don't know yet how well support the full ucs4 range.
151 lyxerr[Debug::KEY] << "QLyXKeySym::getUCSEncoded()" << endl;
152 for (int i = 0; i < text_.size(); ++i) {
153 lyxerr[Debug::KEY] << "char " << i << ": "
154 << text_[i].unicode() << endl;
158 // Only one UCS4 character at the end.
159 docstring ucs4_text = qstring_to_ucs4(text_);
164 docstring const QLyXKeySym::print(key_modifier::state mod, bool forgui) const
168 if (mod & key_modifier::shift)
170 if (mod & key_modifier::ctrl)
172 if (mod & key_modifier::alt)
175 QKeySequence seq(tmpkey);
177 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
178 : QKeySequence::PortableText));
182 bool QLyXKeySym::isText() const
184 if (text_.isEmpty()) {
185 if (lyxerr.debugging())
186 lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
194 bool QLyXKeySym::operator==(LyXKeySym const & ks) const
196 QLyXKeySym const & qks = static_cast<QLyXKeySym const &>(ks);
198 // we do not have enough info for a fair comparison, so return
199 // false. This works out OK because unknown text from Qt will
200 // get inserted anyway after the isText() check
201 if (key_ == Qt::Key_unknown || qks.key_ == Qt::Key_unknown)
204 return key_ == qks.key_;