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];
84 //const char * c = QTextCodec::locale();
85 //string s = c ? c : "";
86 // In this order, see support/filetools.C
87 string s = getEnv("LC_ALL");
89 s = getEnv("LC_MESSAGES");
97 if (s.find("UTF") != string::npos || s.find("utf") != string::npos)
98 //if (contains(c, "UTF") || contains(c, "utf"))
99 lyxerr << "Warning: this system's locale uses Unicode." << endl;
101 // strip off any encoding suffix
102 string::size_type i = s.find(".");
105 encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
106 encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
107 encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
108 encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
109 encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
110 encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
111 encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
112 encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
113 encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
114 encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
115 encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
116 encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
117 encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
120 encoding_map["tis620-0"] = 0;
121 encoding_map["pt154"] = 0;
123 // There are lots more codecs in Qt too ...
125 // when no document open
126 // use the appropriate encoding for the system language
127 lyxerr << "Language code:" << s << endl;
128 for (Languages::const_iterator it=languages.begin(); it != languages.end(); ++it) {
129 //lyxerr << it->second.code() << ":" << it->second.encodingStr() << ":" << it->second.encoding() << endl;
130 if (it->second.code() == s) {
131 s = it->second.encodingStr();
135 lyxerr << "Setting new locale for Qt:" << s << endl;
136 QTextCodec * defaultCodec = encoding_map[s];
137 encoding_map[""] = defaultCodec;
139 QTextCodec::setCodecForCStrings(defaultCodec);
144 QLyXKeySym::QLyXKeySym()
145 : LyXKeySym(), key_(0)
150 void QLyXKeySym::set(QKeyEvent * ev)
153 if (ev->text().isNull()) {
154 if (lyxerr.debugging())
155 lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
160 if (lyxerr.debugging())
161 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
165 void QLyXKeySym::init(string const & symbolname)
167 key_ = string_to_qkey(symbolname);
168 text_ = toqstr(symbolname);
169 if (lyxerr.debugging())
170 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
174 bool QLyXKeySym::isOK() const
176 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
177 if (lyxerr.debugging())
178 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
183 bool QLyXKeySym::isModifier() const
185 bool const mod(q_is_modifier(key_));
186 if (lyxerr.debugging())
187 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
192 string QLyXKeySym::getSymbolName() const
194 string sym(qkey_to_string(key_));
196 // e.g. A-Za-z, and others
198 sym = fromqstr(text_);
204 size_t QLyXKeySym::getUCSEncoded() const
209 BOOST_ASSERT(text_.size() == 1);
210 return qchar_to_ucs4(text_[0]);
214 docstring const QLyXKeySym::print(key_modifier::state mod, bool forgui) const
218 if (mod & key_modifier::shift)
220 if (mod & key_modifier::ctrl)
222 if (mod & key_modifier::alt)
225 QKeySequence seq(tmpkey);
227 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
228 : QKeySequence::PortableText));
232 bool QLyXKeySym::isText() const
234 if (text_.isEmpty()) {
235 if (lyxerr.debugging())
236 lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
244 bool QLyXKeySym::operator==(LyXKeySym const & ks) const
246 QLyXKeySym const & qks = static_cast<QLyXKeySym const &>(ks);
248 // we do not have enough info for a fair comparison, so return
249 // false. This works out OK because unknown text from Qt will
250 // get inserted anyway after the isText() check
251 if (key_ == Qt::Key_unknown || qks.key_ == Qt::Key_unknown)
254 return key_ == qks.key_;