]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLyXKeySym.C
get rid of broken_header.h and some unneeded tests
[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 "QLyXKeySym.h"
15 #include "qlkey.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19
20 #include <qaccel.h>
21 #include <qevent.h>
22 #include <qtextcodec.h>
23
24 #include <map>
25
26 using std::endl;
27 using std::string;
28 using std::map;
29
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                 if (lyxerr.debugging())
43                         lyxerr[Debug::KEY] << "Unrecognised encoding "
44                                 << encoding << endl;
45                 codec = QTextCodec::codecForLocale();
46         } else {
47                 codec = cit->second;
48         }
49
50         if (!codec) {
51                 if (lyxerr.debugging())
52                         lyxerr[Debug::KEY] << "No codec exists for encoding "
53                                 << encoding << endl;
54                 codec = QTextCodec::codecForLocale();
55         }
56
57         if (lyxerr.debugging())
58                 lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
59
60         if (!codec->canEncode(str)) {
61                 if (lyxerr.debugging())
62                         lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
63                 return 0;
64         }
65
66         QCString tmpstr = codec->fromUnicode(str);
67         char const * tmpcstr = tmpstr;
68         return tmpcstr[0];
69 }
70
71 }
72
73
74 void initEncodings()
75 {
76         // when no document open
77         encoding_map[""] = QTextCodec::codecForLocale();
78
79         encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
80         encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
81         encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
82         encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
83         encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
84         encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
85         encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
86         encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
87         encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
88         encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
89         encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
90         encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
91         encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
92
93         // FIXME
94         encoding_map["tis620-0"] = 0;
95         encoding_map["pt154"] = 0;
96
97         // There are lots more codecs in Qt too ...
98 }
99
100
101 QLyXKeySym::QLyXKeySym()
102         : LyXKeySym(), key_(0)
103 {
104 }
105
106
107 void QLyXKeySym::set(QKeyEvent * ev)
108 {
109         key_ = ev->key();
110         if (ev->text().isNull()) {
111                 if (lyxerr.debugging())
112                         lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
113                 text_ = "";
114                 return;
115         }
116         text_ = ev->text();
117         if (lyxerr.debugging())
118                 lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " <<  fromqstr(text_) << endl;
119 }
120
121
122 void QLyXKeySym::init(string const & symbolname)
123 {
124         key_ = string_to_qkey(symbolname);
125         text_ = toqstr(symbolname);
126         if (lyxerr.debugging())
127                 lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
128 }
129
130
131 bool QLyXKeySym::isOK() const
132 {
133         bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
134         if (lyxerr.debugging())
135                 lyxerr[Debug::KEY] << "isOK is " << ok << endl;
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         return mod;
146 }
147
148
149 string QLyXKeySym::getSymbolName() const
150 {
151         string sym(qkey_to_string(key_));
152
153         // e.g. A-Za-z, and others
154         if (sym.empty())
155                 sym = fromqstr(text_);
156
157         return sym;
158 }
159
160
161 char QLyXKeySym::getISOEncoded(string const & encoding) const
162 {
163         if (lyxerr.debugging())
164                 lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
165         unsigned char const c = encode(encoding, text_);
166         if (lyxerr.debugging())
167                 lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
168         return c;
169 }
170
171
172 QString const QLyXKeySym::qprint(key_modifier::state mod) const
173 {
174         int tmpkey = key_;
175
176         if (mod & key_modifier::shift)
177                 tmpkey += Qt::SHIFT;
178         if (mod & key_modifier::ctrl)
179                 tmpkey += Qt::CTRL;
180         if (mod & key_modifier::alt)
181                 tmpkey += Qt::ALT;
182
183         return QAccel::keyToString(tmpkey);
184 }
185
186
187 string const QLyXKeySym::print(key_modifier::state mod) const
188 {
189         return fromqstr(qprint(mod));
190 }
191
192
193 bool QLyXKeySym::isText() const
194 {
195         if (text_.isEmpty()) {
196                 if (lyxerr.debugging())
197                         lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
198                 return false;
199         }
200
201         QChar const c(text_[0]);
202         if (lyxerr.debugging())
203                 lyxerr[Debug::KEY] << "isText for key " << key_
204                         << " isPrint is " << c.isPrint() << endl;
205         return c.isPrint();
206 }
207
208
209 bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
210 {
211         QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
212         QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
213
214         // we do not have enough info for a fair comparison, so return
215         // false. This works out OK because unknown text from Qt will
216         // get inserted anyway after the isText() check
217         if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
218                 return false;
219
220         return q1.key() == q2.key();
221 }