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