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