]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLyXKeySym.C
enable Font cache only for MacOSX and inline width() for other platform.
[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
18
19 #include "debug.h"
20
21 #include <QKeyEvent>
22 #include <QKeySequence>
23 #include <QEvent>
24 #include <QTextCodec>
25
26 #include <map>
27 #include "support/lstrings.h"
28 #include "support/environment.h"
29 #include "support/unicode.h"
30
31 #include "encoding.h"
32 #include "language.h"
33
34 using std::endl;
35 using std::string;
36 using std::map;
37 using lyx::support::contains;
38 using lyx::support::getEnv;
39
40
41 namespace {
42
43 typedef map<string, QTextCodec *> EncodingMap;
44 EncodingMap encoding_map;
45
46 char const encode(string const & encoding, QString const & str)
47 {
48         QTextCodec * codec = 0;
49
50         EncodingMap::const_iterator cit = encoding_map.find(encoding);
51         if (cit == encoding_map.end()) {
52                 lyxerr[Debug::KEY] << "Unrecognised encoding '" << encoding
53                                    << "'." << endl;
54                 codec = encoding_map.find("")->second;
55         } else {
56                 codec = cit->second;
57         }
58
59         if (!codec) {
60                 lyxerr[Debug::KEY] << "No codec for encoding '" << encoding
61                                    << "' found." << endl;
62                 return 0;
63         }
64
65         lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
66
67         if (!codec->canEncode(str)) {
68                 lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
69                 return 0;
70         }
71
72         return codec->fromUnicode(str).data()[0];
73 }
74
75 }
76
77
78 void initEncodings()
79 {
80 #if 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");
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 #endif
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 size_t QLyXKeySym::getUCSEncoded() const
202 {
203         if (text_.isEmpty())
204                 return 0;
205
206         BOOST_ASSERT(text_.size() == 1);
207         return qchar_to_ucs4(text_[0]);
208 }
209
210
211 QString const QLyXKeySym::qprint(key_modifier::state mod) const
212 {
213         int tmpkey = key_;
214
215         if (mod & key_modifier::shift)
216                 tmpkey += Qt::SHIFT;
217         if (mod & key_modifier::ctrl)
218                 tmpkey += Qt::CTRL;
219         if (mod & key_modifier::alt)
220                 tmpkey += Qt::ALT;
221
222         return QKeySequence(tmpkey).toString();
223 }
224
225
226 string const QLyXKeySym::print(key_modifier::state mod) const
227 {
228         return fromqstr(qprint(mod));
229 }
230
231
232 bool QLyXKeySym::isText() const
233 {
234         if (text_.isEmpty()) {
235                 if (lyxerr.debugging())
236                         lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
237                 return false;
238         }
239
240         return true;
241 }
242
243
244 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
245 {
246         QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
247         QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
248
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)
253                 return false;
254
255         return q1.key() == q2.key();
256 }