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.
6 * \author Asger & Jürgen
8 * Full author contact details are available in file CREDITS.
13 #include "frontends/KeySymbol.h"
15 #include "GuiKeySymbol.h"
18 #include "qt_helpers.h"
22 #include "support/lstrings.h"
23 #include "support/environment.h"
24 #include "support/unicode.h"
30 #include <QKeySequence>
42 using lyx::support::contains;
43 using lyx::support::getEnv;
46 KeySymbol * createKeySymbol()
48 return new GuiKeySymbol;
52 static char const encode(string const & encoding, QString const & str)
54 typedef map<string, QTextCodec *> EncodingMap;
55 EncodingMap encoding_map;
57 QTextCodec * codec = 0;
59 EncodingMap::const_iterator cit = encoding_map.find(encoding);
60 if (cit == encoding_map.end()) {
61 LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
63 codec = encoding_map.find("")->second;
69 LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
70 << "' found." << endl;
74 LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
76 if (!codec->canEncode(str)) {
77 LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
81 return codec->fromUnicode(str).data()[0];
85 GuiKeySymbol::GuiKeySymbol()
86 : KeySymbol(), key_(0)
91 void GuiKeySymbol::set(QKeyEvent * ev)
94 if (ev->text().isNull()) {
95 LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
100 LYXERR(Debug::KEY) << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
104 void GuiKeySymbol::init(string const & symbolname)
106 key_ = string_to_qkey(symbolname);
107 text_ = toqstr(symbolname);
108 LYXERR(Debug::KEY) << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
112 bool GuiKeySymbol::isOK() const
114 bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
115 LYXERR(Debug::KEY) << "isOK is " << ok << endl;
120 bool GuiKeySymbol::isModifier() const
122 bool const mod(q_is_modifier(key_));
123 LYXERR(Debug::KEY) << "isMod is " << mod << endl;
128 string GuiKeySymbol::getSymbolName() const
130 string sym(qkey_to_string(key_));
132 // e.g. A-Za-z, and others
134 sym = fromqstr(text_);
140 char_type GuiKeySymbol::getUCSEncoded() const
145 // UTF16 has a maximum of two characters.
146 BOOST_ASSERT(text_.size() <= 2);
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;
157 // Only one UCS4 character at the end.
158 docstring ucs4_text = qstring_to_ucs4(text_);
163 docstring const GuiKeySymbol::print(key_modifier::state mod, bool forgui) const
167 if (mod & key_modifier::shift)
169 if (mod & key_modifier::ctrl)
171 if (mod & key_modifier::alt)
174 QKeySequence seq(tmpkey);
176 return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
177 : QKeySequence::PortableText));
181 bool GuiKeySymbol::isText() const
183 if (text_.isEmpty()) {
184 LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
192 bool GuiKeySymbol::operator==(KeySymbol const & ks) const
194 GuiKeySymbol const & qks = static_cast<GuiKeySymbol const &>(ks);
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)
202 return key_ == qks.key_;
206 key_modifier::state q_key_state(Qt::KeyboardModifiers state)
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;