]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QKeySymbol.cpp
Whitespace cleanup
[features.git] / src / frontends / qt4 / QKeySymbol.cpp
1 /**
2  * \file QKeySymbol.cpp
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 "QKeySymbol.h"
15 #include "qlkey.h"
16 #include "qt_helpers.h"
17
18
19 #include "debug.h"
20
21 #include <QKeyEvent>
22 #include <QKeySequence>
23 #include <QEvent>
24 #include <QTextCodec>
25
26 #include <map>
27 #include "support/lstrings.h"
28 #include "support/environment.h"
29 #include "support/unicode.h"
30
31 #include "Encoding.h"
32 #include "Language.h"
33
34
35 namespace lyx {
36
37 using std::endl;
38 using std::string;
39 using std::map;
40 using lyx::support::contains;
41 using lyx::support::getEnv;
42
43
44 namespace {
45
46 typedef map<string, QTextCodec *> EncodingMap;
47 EncodingMap encoding_map;
48
49 char const encode(string const & encoding, QString const & str)
50 {
51         QTextCodec * codec = 0;
52
53         EncodingMap::const_iterator cit = encoding_map.find(encoding);
54         if (cit == encoding_map.end()) {
55                 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
56                                    << "'." << endl;
57                 codec = encoding_map.find("")->second;
58         } else {
59                 codec = cit->second;
60         }
61
62         if (!codec) {
63                 LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
64                                    << "' found." << endl;
65                 return 0;
66         }
67
68         LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
69
70         if (!codec->canEncode(str)) {
71                 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
72                 return 0;
73         }
74
75         return codec->fromUnicode(str).data()[0];
76 }
77
78 }
79
80
81 QKeySymbol::QKeySymbol()
82         : KeySymbol(), key_(0)
83 {
84 }
85
86
87 void QKeySymbol::set(QKeyEvent * ev)
88 {
89         key_ = ev->key();
90         if (ev->text().isNull()) {
91                 LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
92                 text_ = "";
93                 return;
94         }
95         text_ = ev->text();
96         LYXERR(Debug::KEY) << "Setting key to " << key_ << ", " <<  fromqstr(text_) << endl;
97 }
98
99
100 void QKeySymbol::init(string const & symbolname)
101 {
102         key_ = string_to_qkey(symbolname);
103         text_ = toqstr(symbolname);
104         LYXERR(Debug::KEY) << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
105 }
106
107
108 bool QKeySymbol::isOK() const
109 {
110         bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
111         LYXERR(Debug::KEY) << "isOK is " << ok << endl;
112         return ok;
113 }
114
115
116 bool QKeySymbol::isModifier() const
117 {
118         bool const mod(q_is_modifier(key_));
119         LYXERR(Debug::KEY) << "isMod is " << mod << endl;
120         return mod;
121 }
122
123
124 string QKeySymbol::getSymbolName() const
125 {
126         string sym(qkey_to_string(key_));
127
128         // e.g. A-Za-z, and others
129         if (sym.empty())
130                 sym = fromqstr(text_);
131
132         return sym;
133 }
134
135
136 char_type QKeySymbol::getUCSEncoded() const
137 {
138         if (text_.isEmpty())
139                 return 0;
140
141         // UTF16 has a maximum of two characters.
142         BOOST_ASSERT(text_.size() <= 2);
143
144         if (lyxerr.debugging() && text_.size() > 1) {
145                 // We don't know yet how well support the full ucs4 range.
146                 LYXERR(Debug::KEY) << "QKeySymbol::getUCSEncoded()" << endl;
147                 for (int i = 0; i < text_.size(); ++i) {
148                         LYXERR(Debug::KEY) << "char " << i << ": "
149                                 << text_[i].unicode() << endl;
150                 }
151         }
152
153         // Only one UCS4 character at the end.
154         docstring ucs4_text = qstring_to_ucs4(text_);
155         return ucs4_text[0];
156 }
157
158
159 docstring const QKeySymbol::print(key_modifier::state mod, bool forgui) const
160 {
161         int tmpkey = key_;
162
163         if (mod & key_modifier::shift)
164                 tmpkey += Qt::SHIFT;
165         if (mod & key_modifier::ctrl)
166                 tmpkey += Qt::CTRL;
167         if (mod & key_modifier::alt)
168                 tmpkey += Qt::ALT;
169
170         QKeySequence seq(tmpkey);
171
172         return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
173                                             : QKeySequence::PortableText));
174 }
175
176
177 bool QKeySymbol::isText() const
178 {
179         if (text_.isEmpty()) {
180                 LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
181                 return false;
182         }
183
184         return true;
185 }
186
187
188 bool QKeySymbol::operator==(KeySymbol const & ks) const
189 {
190         QKeySymbol const & qks = static_cast<QKeySymbol const &>(ks);
191
192         // we do not have enough info for a fair comparison, so return
193         // false. This works out OK because unknown text from Qt will
194         // get inserted anyway after the isText() check
195         if (key_ == Qt::Key_unknown || qks.key_ == Qt::Key_unknown)
196                 return false;
197
198         return key_ == qks.key_;
199 }
200
201
202 } // namespace lyx