]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLyXKeySym.C
2f3665a8170ab3004222603ac6a0984dea8949ae
[lyx.git] / src / frontends / qt2 / 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 Juergen
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14
15 #include "QLyXKeySym.h"
16 #include "qlkey.h"
17 #include "debug.h"
18 #include "qt_helpers.h"
19
20 #include <qevent.h>
21 #include <qtextcodec.h>
22
23 #include <map>
24
25 using std::endl;
26 using std::map;
27
28 namespace {
29
30 typedef map<string, QTextCodec *> EncodingMap;
31 EncodingMap encoding_map;
32
33 char const encode(string const & encoding, QString const & str)
34 {
35         QTextCodec * codec = 0;
36
37         EncodingMap::const_iterator cit = encoding_map.find(encoding);
38         if (cit == encoding_map.end()) {
39                 if (lyxerr.debugging())
40                         lyxerr[Debug::KEY] << "Unrecognised encoding "
41                                 << encoding << endl;
42                 codec = QTextCodec::codecForLocale();
43         } else {
44                 codec = cit->second;
45         }
46
47         if (!codec) {
48                 if (lyxerr.debugging())
49                         lyxerr[Debug::KEY] << "No codec exists for encoding "
50                                 << encoding << endl;
51                 codec = QTextCodec::codecForLocale();
52         }
53
54         if (lyxerr.debugging())
55                 lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
56
57         if (!codec->canEncode(str)) {
58                 if (lyxerr.debugging())
59                         lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
60                 return 0;
61         }
62
63         QCString tmpstr = codec->fromUnicode(str);
64         char const * tmpcstr = tmpstr;
65         return tmpcstr[0];
66 }
67
68 }
69
70
71 void initEncodings()
72 {
73         // when no document open
74         encoding_map[""] = QTextCodec::codecForLocale();
75
76         encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
77         encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
78         encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
79         encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
80         encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
81         encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
82         encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
83         encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
84         encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
85         encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
86         encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
87         encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
88         encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
89
90         // FIXME
91         encoding_map["tis620-0"] = 0;
92         encoding_map["pt154"] = 0;
93
94         // There are lots more codecs in Qt too ...
95 }
96
97
98 QLyXKeySym::QLyXKeySym()
99         : LyXKeySym(), key_(0)
100 {
101 }
102
103
104 void QLyXKeySym::set(QKeyEvent * ev)
105 {
106         key_ = ev->key();
107         if (ev->text().isNull()) {
108                 if (lyxerr.debugging())
109                         lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
110                 text_ = "";
111                 return;
112         }
113         text_ = ev->text();
114         if (lyxerr.debugging()) {
115                 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " <<  fromqstr(text_) << endl;
116         }
117 }
118
119
120 void QLyXKeySym::init(string const & symbolname)
121 {
122         key_ = string_to_qkey(symbolname);
123         text_ = toqstr(symbolname);
124         if (lyxerr.debugging()) {
125                 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
126         }
127 }
128
129
130 bool QLyXKeySym::isOK() const
131 {
132         bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
133         if (lyxerr.debugging()) {
134                 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
135         }
136         return ok;
137 }
138
139
140 bool QLyXKeySym::isModifier() const
141 {
142         bool const mod(q_is_modifier(key_));
143         if (lyxerr.debugging()) {
144                 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
145         }
146         return mod;
147 }
148
149
150 string QLyXKeySym::getSymbolName() const
151 {
152         string sym(qkey_to_string(key_));
153
154         // e.g. A-Za-z, and others
155         if (sym.empty())
156                 sym = fromqstr(text_);
157
158         return sym;
159 }
160
161
162 char QLyXKeySym::getISOEncoded(string const & encoding) const
163 {
164         if (lyxerr.debugging())
165                 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
166         unsigned char const c = encode(encoding, text_);
167         if (lyxerr.debugging())
168                 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
169         return c;
170 }
171
172
173 bool QLyXKeySym::isText() const
174 {
175         if (text_.isEmpty()) {
176                 if (lyxerr.debugging())
177                         lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
178                 return false;
179         }
180
181         QChar const c(text_[0]);
182         if (lyxerr.debugging())
183                 lyxerr[Debug::KEY] << "isText for key " << key_
184                         << " isPrint is " << c.isPrint() << endl;
185         return c.isPrint();
186 }
187
188
189 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
190 {
191         QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
192         QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
193
194         // we do not have enough info for a fair comparison, so return
195         // false. This works out OK because unknown text from Qt will
196         // get inserted anyway after the isText() check
197         if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
198                 return false;
199
200         return q1.key() == q2.key();
201 }