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 <qtextcodec.h>
33 typedef map<string, QTextCodec *> EncodingMap;
34 EncodingMap encoding_map;
36 char const encode(string const & encoding, QString const & str)
38 QTextCodec * codec = 0;
40 EncodingMap::const_iterator cit = encoding_map.find(encoding);
41 if (cit == encoding_map.end()) {
42 if (lyxerr.debugging())
43 lyxerr[Debug::KEY] << "Unrecognised encoding "
45 codec = QTextCodec::codecForLocale();
51 if (lyxerr.debugging())
52 lyxerr[Debug::KEY] << "No codec exists for encoding "
54 codec = QTextCodec::codecForLocale();
57 if (lyxerr.debugging())
58 lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
60 if (!codec->canEncode(str)) {
61 if (lyxerr.debugging())
62 lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
66 QCString tmpstr = codec->fromUnicode(str);
67 char const * tmpcstr = tmpstr;
76 // when no document open
77 encoding_map[""] = QTextCodec::codecForLocale();
79 encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
80 encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
81 encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
82 encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
83 encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
84 encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
85 encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
86 encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
87 encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
88 encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
89 encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
90 encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
91 encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
94 encoding_map["tis620-0"] = 0;
95 encoding_map["pt154"] = 0;
97 // There are lots more codecs in Qt too ...
101 QLyXKeySym::QLyXKeySym()
102 : LyXKeySym(), key_(0)
107 void QLyXKeySym::set(QKeyEvent * ev)
110 if (ev->text().isNull()) {
111 if (lyxerr.debugging())
112 lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
117 if (lyxerr.debugging())
118 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
122 void QLyXKeySym::init(string const & symbolname)
124 key_ = string_to_qkey(symbolname);
125 text_ = toqstr(symbolname);
126 if (lyxerr.debugging())
127 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
131 bool QLyXKeySym::isOK() const
133 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
134 if (lyxerr.debugging())
135 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
140 bool QLyXKeySym::isModifier() const
142 bool const mod(q_is_modifier(key_));
143 if (lyxerr.debugging())
144 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
149 string QLyXKeySym::getSymbolName() const
151 string sym(qkey_to_string(key_));
153 // e.g. A-Za-z, and others
155 sym = fromqstr(text_);
161 char QLyXKeySym::getISOEncoded(string const & encoding) const
163 if (lyxerr.debugging())
164 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
165 unsigned char const c = encode(encoding, text_);
166 if (lyxerr.debugging())
167 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
172 QString const QLyXKeySym::qprint(key_modifier::state mod) const
176 if (mod & key_modifier::shift)
178 if (mod & key_modifier::ctrl)
180 if (mod & key_modifier::alt)
183 return QAccel::keyToString(tmpkey);
187 string const QLyXKeySym::print(key_modifier::state mod) const
189 return fromqstr(qprint(mod));
193 bool QLyXKeySym::isText() const
195 if (text_.isEmpty()) {
196 if (lyxerr.debugging())
197 lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
201 QChar const c(text_[0]);
202 if (lyxerr.debugging())
203 lyxerr[Debug::KEY] << "isText for key " << key_
204 << " isPrint is " << c.isPrint() << endl;
209 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
211 QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
212 QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
214 // we do not have enough info for a fair comparison, so return
215 // false. This works out OK because unknown text from Qt will
216 // get inserted anyway after the isText() check
217 if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
220 return q1.key() == q2.key();