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