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