]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLyXKeySym.C
namespace grfx -> lyx::graphics
[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 void QLyXKeySym::init(string const & symbolname)
120 {
121         key_ = string_to_qkey(symbolname);
122         text_ = toqstr(symbolname);
123         if (lyxerr.debugging())
124                 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
125 }
126
127
128 bool QLyXKeySym::isOK() const
129 {
130         bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
131         if (lyxerr.debugging())
132                 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
133         return ok;
134 }
135
136
137 bool QLyXKeySym::isModifier() const
138 {
139         bool const mod(q_is_modifier(key_));
140         if (lyxerr.debugging())
141                 lyxerr[Debug::KEY] << "isMod is " << mod << endl;
142         return mod;
143 }
144
145
146 string QLyXKeySym::getSymbolName() const
147 {
148         string sym(qkey_to_string(key_));
149
150         // e.g. A-Za-z, and others
151         if (sym.empty())
152                 sym = fromqstr(text_);
153
154         return sym;
155 }
156
157
158 char QLyXKeySym::getISOEncoded(string const & encoding) const
159 {
160         if (lyxerr.debugging())
161                 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
162         unsigned char const c = encode(encoding, text_);
163         if (lyxerr.debugging())
164                 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
165         return c;
166 }
167
168
169 bool QLyXKeySym::isText() const
170 {
171         if (text_.isEmpty()) {
172                 if (lyxerr.debugging())
173                         lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
174                 return false;
175         }
176
177         QChar const c(text_[0]);
178         if (lyxerr.debugging())
179                 lyxerr[Debug::KEY] << "isText for key " << key_
180                         << " isPrint is " << c.isPrint() << endl;
181         return c.isPrint();
182 }
183
184
185 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
186 {
187         QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
188         QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
189
190         // we do not have enough info for a fair comparison, so return
191         // false. This works out OK because unknown text from Qt will
192         // get inserted anyway after the isText() check
193         if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
194                 return false;
195
196         return q1.key() == q2.key();
197 }