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