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"
35 using lyx::support::contains;
36 using lyx::support::getEnv;
41 typedef map<string, QTextCodec *> EncodingMap;
42 EncodingMap encoding_map;
44 char const encode(string const & encoding, QString const & str)
46 QTextCodec * codec = 0;
48 EncodingMap::const_iterator cit = encoding_map.find(encoding);
49 if (cit == encoding_map.end()) {
50 lyxerr[Debug::KEY] << "Unrecognised encoding '" << encoding
52 codec = encoding_map.find("")->second;
58 lyxerr[Debug::KEY] << "No codec for encoding '" << encoding
59 << "' found." << endl;
63 lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
65 if (!codec->canEncode(str)) {
66 lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
70 return codec->fromUnicode(str).data()[0];
78 //const char * c = QTextCodec::locale();
79 //string s = c ? c : "";
80 // In this order, see support/filetools.C
81 string s = getEnv("LC_ALL");
83 s = getEnv("LC_MESSAGES");
91 if (s.find("UTF") != string::npos || s.find("utf") != string::npos)
92 //if (contains(c, "UTF") || contains(c, "utf"))
93 lyxerr << "Warning: this system's locale uses Unicode." << endl;
95 // strip off any encoding suffix
96 string::size_type i = s.find(".");
99 encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
100 encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
101 encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
102 encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
103 encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
104 encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
105 encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
106 encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
107 encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
108 encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
109 encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
110 encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
111 encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
114 encoding_map["tis620-0"] = 0;
115 encoding_map["pt154"] = 0;
117 // There are lots more codecs in Qt too ...
119 // when no document open
120 // use the appropriate encoding for the system language
121 lyxerr << "Language code:" << s << endl;
122 for (Languages::const_iterator it=languages.begin(); it != languages.end(); ++it) {
123 //lyxerr << it->second.code() << ":" << it->second.encodingStr() << ":" << it->second.encoding() << endl;
124 if (it->second.code() == s) {
125 s = it->second.encodingStr();
129 lyxerr << "Setting new locale for Qt:" << s << endl;
130 QTextCodec * defaultCodec = encoding_map[s];
131 encoding_map[""] = defaultCodec;
133 QTextCodec::setCodecForCStrings(defaultCodec);
138 QLyXKeySym::QLyXKeySym()
139 : LyXKeySym(), key_(0)
144 void QLyXKeySym::set(QKeyEvent * ev)
147 if (ev->text().isNull()) {
148 if (lyxerr.debugging())
149 lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
154 if (lyxerr.debugging())
155 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
159 void QLyXKeySym::init(string const & symbolname)
161 key_ = string_to_qkey(symbolname);
162 text_ = toqstr(symbolname);
163 if (lyxerr.debugging())
164 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
168 bool QLyXKeySym::isOK() const
170 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
171 if (lyxerr.debugging())
172 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
177 bool QLyXKeySym::isModifier() const
179 bool const mod(q_is_modifier(key_));
180 if (lyxerr.debugging())
181 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
186 string QLyXKeySym::getSymbolName() const
188 string sym(qkey_to_string(key_));
190 // e.g. A-Za-z, and others
192 sym = fromqstr(text_);
198 char QLyXKeySym::getISOEncoded(string const & encoding) const
200 if (lyxerr.debugging())
201 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
202 unsigned char const c = encode(encoding, text_);
203 if (lyxerr.debugging())
204 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
209 QString const QLyXKeySym::qprint(key_modifier::state mod) const
213 if (mod & key_modifier::shift)
215 if (mod & key_modifier::ctrl)
217 if (mod & key_modifier::alt)
220 return QKeySequence(tmpkey).toString();
224 string const QLyXKeySym::print(key_modifier::state mod) const
226 return fromqstr(qprint(mod));
230 bool QLyXKeySym::isText() const
232 if (text_.isEmpty()) {
233 if (lyxerr.debugging())
234 lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
238 QChar const c(text_[0]);
239 if (lyxerr.debugging())
240 lyxerr[Debug::KEY] << "isText for key " << key_
241 << " isPrint is " << c.isPrint() << endl;
246 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
248 QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
249 QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
251 // we do not have enough info for a fair comparison, so return
252 // false. This works out OK because unknown text from Qt will
253 // get inserted anyway after the isText() check
254 if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
257 return q1.key() == q2.key();