]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiKeySymbol.cpp
GuiWrap.cpp: disconnect the widgets that are by default disabled from readonly
[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 "KeySymbol.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 namespace {
47
48 char encode(string const & encoding, QString const & str)
49 {
50         typedef map<string, QTextCodec *> EncodingMap;
51         EncodingMap encoding_map;
52
53         QTextCodec * codec = 0;
54
55         EncodingMap::const_iterator cit = encoding_map.find(encoding);
56         if (cit == encoding_map.end()) {
57                 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
58                                    << "'." << endl;
59                 codec = encoding_map.find("")->second;
60         } else {
61                 codec = cit->second;
62         }
63
64         if (!codec) {
65                 LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
66                                    << "' found." << endl;
67                 return 0;
68         }
69
70         LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
71
72         if (!codec->canEncode(str)) {
73                 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
74                 return 0;
75         }
76
77         return codec->fromUnicode(str).data()[0];
78 }
79
80 } // anon namespace
81
82
83 void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
84 {
85         sym->setKey(ev->key());
86         if (ev->text().isNull()) {
87                 LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
88                 sym->setText(docstring());
89                 return;
90         }
91         sym->setText(qstring_to_ucs4(ev->text()));
92         LYXERR(Debug::KEY) << "Setting key to " << sym->key() << ", "
93                 << to_utf8(sym->text()) << endl;
94 }
95
96
97 void KeySymbol::init(string const & symbolname)
98 {
99         key_ = string_to_qkey(symbolname);
100         text_ = from_utf8(symbolname);
101         LYXERR(Debug::KEY) << "Init key to " << key_ << ", "
102                 << to_utf8(text_) << endl;
103 }
104
105
106 bool KeySymbol::isOK() const
107 {
108         bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
109         LYXERR(Debug::KEY) << "isOK is " << ok << endl;
110         return ok;
111 }
112
113
114 bool KeySymbol::isModifier() const
115 {
116         bool const mod = q_is_modifier(key_);
117         LYXERR(Debug::KEY) << "isMod is " << mod << endl;
118         return mod;
119 }
120
121
122 string KeySymbol::getSymbolName() const
123 {
124         string name = qkey_to_string(key_);
125
126         // e.g. A-Za-z, and others
127         if (name.empty())
128                 name = to_utf8(text_);
129
130         return name;
131 }
132
133
134 char_type KeySymbol::getUCSEncoded() const
135 {
136         if (text_.empty())
137                 return 0;
138
139         // UTF16 has a maximum of two characters.
140         BOOST_ASSERT(text_.size() <= 2);
141
142         if (lyxerr.debugging() && text_.size() > 1) {
143                 // We don't know yet how well support the full ucs4 range.
144                 LYXERR(Debug::KEY) << "KeySymbol::getUCSEncoded()" << endl;
145                 for (int i = 0; i != int(text_.size()); ++i) {
146                         LYXERR(Debug::KEY) << "char " << i << ": "
147                                 << int(text_[i]) << endl;
148                 }
149         }
150
151         return text_[0];
152 }
153
154
155 docstring const KeySymbol::print(key_modifier::state mod, bool forgui) const
156 {
157         int tmpkey = key_;
158
159         if (mod & key_modifier::shift)
160                 tmpkey += Qt::SHIFT;
161         if (mod & key_modifier::ctrl)
162                 tmpkey += Qt::CTRL;
163         if (mod & key_modifier::alt)
164                 tmpkey += Qt::ALT;
165
166         QKeySequence seq(tmpkey);
167
168         return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
169                                             : QKeySequence::PortableText));
170 }
171
172
173 bool KeySymbol::isText() const
174 {
175         if (!text_.empty())
176                 return true;
177         LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
178         return false;
179 }
180
181
182 bool KeySymbol::operator==(KeySymbol const & ks) const
183 {
184         // we do not have enough info for a fair comparison, so return
185         // false. This works out OK because unknown text from Qt will
186         // get inserted anyway after the isText() check
187         if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
188                 return false;
189         return key_ == ks.key_;
190 }
191
192
193 key_modifier::state q_key_state(Qt::KeyboardModifiers state)
194 {
195         key_modifier::state k = key_modifier::none;
196         if (state & Qt::ControlModifier)
197                 k |= key_modifier::ctrl;
198         if (state & Qt::ShiftModifier)
199                 k |= key_modifier::shift;
200         if (state & Qt::AltModifier || state & Qt::MetaModifier)
201                 k |= key_modifier::alt;
202         return k;
203 }
204
205 } // namespace lyx