3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger and Juergen
9 * Full author contact details are available in file CREDITS
15 #include "QLyXKeySym.h"
18 #include "qt_helpers.h"
21 #include <qtextcodec.h>
30 typedef map<string, QTextCodec *> EncodingMap;
31 EncodingMap encoding_map;
33 char const encode(string const & encoding, QString const & str)
35 QTextCodec * codec = 0;
37 EncodingMap::const_iterator cit = encoding_map.find(encoding);
38 if (cit == encoding_map.end()) {
39 if (lyxerr.debugging())
40 lyxerr[Debug::KEY] << "Unrecognised encoding "
42 codec = QTextCodec::codecForLocale();
48 if (lyxerr.debugging())
49 lyxerr[Debug::KEY] << "No codec exists for encoding "
51 codec = QTextCodec::codecForLocale();
54 if (lyxerr.debugging())
55 lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
57 if (!codec->canEncode(str)) {
58 if (lyxerr.debugging())
59 lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
63 QCString tmpstr = codec->fromUnicode(str);
64 char const * tmpcstr = tmpstr;
73 // when no document open
74 encoding_map[""] = QTextCodec::codecForLocale();
76 encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
77 encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
78 encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
79 encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
80 encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
81 encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
82 encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
83 encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
84 encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
85 encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
86 encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
87 encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
88 encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
91 encoding_map["tis620-0"] = 0;
92 encoding_map["pt154"] = 0;
94 // There are lots more codecs in Qt too ...
98 QLyXKeySym::QLyXKeySym()
99 : LyXKeySym(), key_(0)
104 void QLyXKeySym::set(QKeyEvent * ev)
107 if (ev->text().isNull()) {
108 if (lyxerr.debugging())
109 lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
114 if (lyxerr.debugging()) {
115 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
120 void QLyXKeySym::init(string const & symbolname)
122 key_ = string_to_qkey(symbolname);
123 text_ = toqstr(symbolname);
124 if (lyxerr.debugging()) {
125 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
130 bool QLyXKeySym::isOK() const
132 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
133 if (lyxerr.debugging()) {
134 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;
150 string QLyXKeySym::getSymbolName() const
152 string sym(qkey_to_string(key_));
154 // e.g. A-Za-z, and others
156 sym = fromqstr(text_);
162 char QLyXKeySym::getISOEncoded(string const & encoding) const
164 if (lyxerr.debugging())
165 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
166 unsigned char const c = encode(encoding, text_);
167 if (lyxerr.debugging())
168 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
173 bool QLyXKeySym::isText() const
175 if (text_.isEmpty()) {
176 if (lyxerr.debugging())
177 lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
181 QChar const c(text_[0]);
182 if (lyxerr.debugging())
183 lyxerr[Debug::KEY] << "isText for key " << key_
184 << " isPrint is " << c.isPrint() << endl;
189 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
191 QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
192 QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
194 // we do not have enough info for a fair comparison, so return
195 // false. This works out OK because unknown text from Qt will
196 // get inserted anyway after the isText() check
197 if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
200 return q1.key() == q2.key();