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"
37 using lyx::support::contains;
38 using lyx::support::getEnv;
43 typedef map<string, QTextCodec *> EncodingMap;
44 EncodingMap encoding_map;
46 char const encode(string const & encoding, QString const & str)
48 QTextCodec * codec = 0;
50 EncodingMap::const_iterator cit = encoding_map.find(encoding);
51 if (cit == encoding_map.end()) {
52 lyxerr[Debug::KEY] << "Unrecognised encoding '" << encoding
54 codec = encoding_map.find("")->second;
60 lyxerr[Debug::KEY] << "No codec for encoding '" << encoding
61 << "' found." << endl;
65 lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
67 if (!codec->canEncode(str)) {
68 lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
72 return codec->fromUnicode(str).data()[0];
81 //const char * c = QTextCodec::locale();
82 //string s = c ? c : "";
83 // In this order, see support/filetools.C
84 string s = getEnv("LC_ALL");
86 s = getEnv("LC_MESSAGES");
94 if (s.find("UTF") != string::npos || s.find("utf") != string::npos)
95 //if (contains(c, "UTF") || contains(c, "utf"))
96 lyxerr << "Warning: this system's locale uses Unicode." << endl;
98 // strip off any encoding suffix
99 string::size_type i = s.find(".");
102 encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
103 encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
104 encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
105 encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
106 encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
107 encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
108 encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
109 encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
110 encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
111 encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
112 encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
113 encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
114 encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
117 encoding_map["tis620-0"] = 0;
118 encoding_map["pt154"] = 0;
120 // There are lots more codecs in Qt too ...
122 // when no document open
123 // use the appropriate encoding for the system language
124 lyxerr << "Language code:" << s << endl;
125 for (Languages::const_iterator it=languages.begin(); it != languages.end(); ++it) {
126 //lyxerr << it->second.code() << ":" << it->second.encodingStr() << ":" << it->second.encoding() << endl;
127 if (it->second.code() == s) {
128 s = it->second.encodingStr();
132 lyxerr << "Setting new locale for Qt:" << s << endl;
133 QTextCodec * defaultCodec = encoding_map[s];
134 encoding_map[""] = defaultCodec;
136 QTextCodec::setCodecForCStrings(defaultCodec);
141 QLyXKeySym::QLyXKeySym()
142 : LyXKeySym(), key_(0)
147 void QLyXKeySym::set(QKeyEvent * ev)
150 if (ev->text().isNull()) {
151 if (lyxerr.debugging())
152 lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
157 if (lyxerr.debugging())
158 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
162 void QLyXKeySym::init(string const & symbolname)
164 key_ = string_to_qkey(symbolname);
165 text_ = toqstr(symbolname);
166 if (lyxerr.debugging())
167 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
171 bool QLyXKeySym::isOK() const
173 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
174 if (lyxerr.debugging())
175 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
180 bool QLyXKeySym::isModifier() const
182 bool const mod(q_is_modifier(key_));
183 if (lyxerr.debugging())
184 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
189 string QLyXKeySym::getSymbolName() const
191 string sym(qkey_to_string(key_));
193 // e.g. A-Za-z, and others
195 sym = fromqstr(text_);
201 size_t QLyXKeySym::getUCSEncoded() const
206 BOOST_ASSERT(text_.size() == 1);
207 return qchar_to_ucs4(text_[0]);
211 QString const QLyXKeySym::qprint(key_modifier::state mod) const
215 if (mod & key_modifier::shift)
217 if (mod & key_modifier::ctrl)
219 if (mod & key_modifier::alt)
222 return QKeySequence(tmpkey).toString();
226 string const QLyXKeySym::print(key_modifier::state mod) const
228 return fromqstr(qprint(mod));
232 bool QLyXKeySym::isText() const
234 if (text_.isEmpty()) {
235 if (lyxerr.debugging())
236 lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
244 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
246 QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
247 QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
249 // we do not have enough info for a fair comparison, so return
250 // false. This works out OK because unknown text from Qt will
251 // get inserted anyway after the isText() check
252 if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
255 return q1.key() == q2.key();