]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLyXKeySym.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / qt4 / QLyXKeySym.C
1 /**
2  * \file QLyXKeySym.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger and Jürgen
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QLyXKeySym.h"
15 #include "qlkey.h"
16 #include "qt_helpers.h"
17 //Added by qt3to4:
18 #include <QKeyEvent>
19 #include <Q3CString>
20
21 #include "debug.h"
22
23 #include <q3accel.h>
24 #include <qevent.h>
25 #include <qtextcodec.h>
26
27 #include <map>
28 #include "support/lstrings.h"
29 #include "support/environment.h"
30 #include "encoding.h"
31 #include "language.h"
32
33 using std::endl;
34 using std::string;
35 using std::map;
36 using lyx::support::contains;
37 using lyx::support::getEnv;
38
39
40 namespace {
41
42 typedef map<string, QTextCodec *> EncodingMap;
43 EncodingMap encoding_map;
44
45 char const encode(string const & encoding, QString const & str)
46 {
47         QTextCodec * codec = 0;
48
49         EncodingMap::const_iterator cit = encoding_map.find(encoding);
50         if (cit == encoding_map.end()) {
51                 lyxerr[Debug::KEY] << "Unrecognised encoding '" << encoding
52                                    << "'." << endl;
53                 codec = encoding_map.find("")->second;
54         } else {
55                 codec = cit->second;
56         }
57
58         if (!codec) {
59                 lyxerr[Debug::KEY] << "No codec for encoding '" << encoding
60                                    << "' found." << endl;
61                 return 0;
62         }
63
64         lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
65
66         if (!codec->canEncode(str)) {
67                 lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
68                 return 0;
69         }
70
71         Q3CString tmpstr = codec->fromUnicode(str);
72         char const * tmpcstr = tmpstr;
73         return tmpcstr[0];
74 }
75
76 }
77
78
79 void initEncodings()
80 {
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");
85         if (s.empty()) {
86                 s = getEnv("LC_MESSAGES");
87                 if (s.empty()) {
88                         s = getEnv("LANG");
89                         if (s.empty())
90                                 s = "C";
91                 }
92         }
93
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;
97
98         // strip off any encoding suffix
99         string::size_type i = s.find(".");
100         s = s.substr(0, i);
101
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");
115
116         // FIXME
117         encoding_map["tis620-0"] = 0;
118         encoding_map["pt154"] = 0;
119
120         // There are lots more codecs in Qt too ...
121
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();
129                         break;
130                 }
131         }
132         lyxerr << "Setting new locale for Qt:" << s << endl;
133         QTextCodec * defaultCodec = encoding_map[s];
134         encoding_map[""] = defaultCodec;
135
136         QTextCodec::setCodecForCStrings(defaultCodec);
137
138 }
139
140
141 QLyXKeySym::QLyXKeySym()
142         : LyXKeySym(), key_(0)
143 {
144 }
145
146
147 void QLyXKeySym::set(QKeyEvent * ev)
148 {
149         key_ = ev->key();
150         if (ev->text().isNull()) {
151                 if (lyxerr.debugging())
152                         lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
153                 text_ = "";
154                 return;
155         }
156         text_ = ev->text();
157         if (lyxerr.debugging())
158                 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " <<  fromqstr(text_) << endl;
159 }
160
161
162 void QLyXKeySym::init(string const & symbolname)
163 {
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;
168 }
169
170
171 bool QLyXKeySym::isOK() const
172 {
173         bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
174         if (lyxerr.debugging())
175                 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
176         return ok;
177 }
178
179
180 bool QLyXKeySym::isModifier() const
181 {
182         bool const mod(q_is_modifier(key_));
183         if (lyxerr.debugging())
184                 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
185         return mod;
186 }
187
188
189 string QLyXKeySym::getSymbolName() const
190 {
191         string sym(qkey_to_string(key_));
192
193         // e.g. A-Za-z, and others
194         if (sym.empty())
195                 sym = fromqstr(text_);
196
197         return sym;
198 }
199
200
201 char QLyXKeySym::getISOEncoded(string const & encoding) const
202 {
203         if (lyxerr.debugging())
204                 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
205         unsigned char const c = encode(encoding, text_);
206         if (lyxerr.debugging())
207                 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
208         return c;
209 }
210
211
212 QString const QLyXKeySym::qprint(key_modifier::state mod) const
213 {
214         int tmpkey = key_;
215
216         if (mod & key_modifier::shift)
217                 tmpkey += Qt::SHIFT;
218         if (mod & key_modifier::ctrl)
219                 tmpkey += Qt::CTRL;
220         if (mod & key_modifier::alt)
221                 tmpkey += Qt::ALT;
222
223         return Q3Accel::keyToString(tmpkey);
224 }
225
226
227 string const QLyXKeySym::print(key_modifier::state mod) const
228 {
229         return fromqstr(qprint(mod));
230 }
231
232
233 bool QLyXKeySym::isText() const
234 {
235         if (text_.isEmpty()) {
236                 if (lyxerr.debugging())
237                         lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
238                 return false;
239         }
240
241         QChar const c(text_[0]);
242         if (lyxerr.debugging())
243                 lyxerr[Debug::KEY] << "isText for key " << key_
244                         << " isPrint is " << c.isPrint() << endl;
245         return c.isPrint();
246 }
247
248
249 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
250 {
251         QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
252         QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
253
254         // we do not have enough info for a fair comparison, so return
255         // false. This works out OK because unknown text from Qt will
256         // get inserted anyway after the isText() check
257         if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
258                 return false;
259
260         return q1.key() == q2.key();
261 }