X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiKeySymbol.cpp;h=d3ba7f29650687201d08b67f632513982e9a05ee;hb=ba76bf5eb85db5a10839fccee3430d906d3f7b70;hp=79baf39fff98f6b22a95bc3c2af1b3740ee12f86;hpb=a1cec91afaca91968b46e695533c10ad2a3f73d3;p=lyx.git diff --git a/src/frontends/qt4/GuiKeySymbol.cpp b/src/frontends/qt4/GuiKeySymbol.cpp index 79baf39fff..d3ba7f2965 100644 --- a/src/frontends/qt4/GuiKeySymbol.cpp +++ b/src/frontends/qt4/GuiKeySymbol.cpp @@ -1,5 +1,5 @@ /** - * \file qt4/KeySymbolFactory.cpp + * \file qt4/GuiKeySymbol.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * @@ -10,18 +10,12 @@ #include -#include "frontends/KeySymbol.h" - -#include "GuiKeySymbol.h" +#include "KeySymbol.h" #include "qlkey.h" #include "qt_helpers.h" -#include "debug.h" - -#include "support/lstrings.h" -#include "support/environment.h" -#include "support/unicode.h" +#include "support/debug.h" #include "Encoding.h" #include "Language.h" @@ -31,25 +25,16 @@ #include #include +#include "boost/assert.hpp" + #include +using namespace std; namespace lyx { -using std::endl; -using std::string; -using std::map; -using lyx::support::contains; -using lyx::support::getEnv; - - -KeySymbol * createKeySymbol() -{ - return new GuiKeySymbol; -} - - -static char const encode(string const & encoding, QString const & str) +#if 0 +static char encode(string const & encoding, QString const & str) { typedef map EncodingMap; EncodingMap encoding_map; @@ -58,88 +43,87 @@ static char const encode(string const & encoding, QString const & str) EncodingMap::const_iterator cit = encoding_map.find(encoding); if (cit == encoding_map.end()) { - LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding - << "'." << endl; + LYXERR(Debug::KEY, "Unrecognised encoding '" << encoding << "'."); codec = encoding_map.find("")->second; } else { codec = cit->second; } if (!codec) { - LYXERR(Debug::KEY) << "No codec for encoding '" << encoding - << "' found." << endl; + LYXERR(Debug::KEY, "No codec for encoding '" << encoding << "' found."); return 0; } - LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl; + LYXERR(Debug::KEY, "Using codec " << fromqstr(codec->name())); if (!codec->canEncode(str)) { - LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl; + LYXERR(Debug::KEY, "Oof. Can't encode the text !"); return 0; } return codec->fromUnicode(str).data()[0]; } +#endif -GuiKeySymbol::GuiKeySymbol() - : KeySymbol(), key_(0) +void setKeySymbol(KeySymbol * sym, QKeyEvent * ev) { -} - - -void GuiKeySymbol::set(QKeyEvent * ev) -{ - key_ = ev->key(); + sym->setKey(ev->key()); if (ev->text().isNull()) { - LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl; - text_ = ""; + LYXERR(Debug::KEY, "keyevent has isNull() text !"); + sym->setText(docstring()); return; } - text_ = ev->text(); - LYXERR(Debug::KEY) << "Setting key to " << key_ << ", " << fromqstr(text_) << endl; + LYXERR(Debug::KEY, "Getting key " << ev->key() << ", with text '" + << fromqstr(ev->text()) << "'"); + // This is unsafe because ev->text() is the unicode representation of the + // key, not the name of the key. For example, Ctrl-x and Alt-x produce + // different texts. + sym->setText(qstring_to_ucs4(ev->text())); + LYXERR(Debug::KEY, "Setting key to " << sym->key() << ", " + << to_utf8(sym->text())); } -void GuiKeySymbol::init(string const & symbolname) +void KeySymbol::init(string const & symbolname) { key_ = string_to_qkey(symbolname); - text_ = toqstr(symbolname); - LYXERR(Debug::KEY) << "Init key to " << key_ << ", " << fromqstr(text_) << endl; + text_ = from_utf8(symbolname); + LYXERR(Debug::KEY, "Init key to " << key_ << ", " << to_utf8(text_)); } -bool GuiKeySymbol::isOK() const +bool KeySymbol::isOK() const { - bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown)); - LYXERR(Debug::KEY) << "isOK is " << ok << endl; + bool const ok = !(text_.empty() && key_ == Qt::Key_unknown); + LYXERR(Debug::KEY, "isOK is " << ok); return ok; } -bool GuiKeySymbol::isModifier() const +bool KeySymbol::isModifier() const { - bool const mod(q_is_modifier(key_)); - LYXERR(Debug::KEY) << "isMod is " << mod << endl; + bool const mod = q_is_modifier(key_); + LYXERR(Debug::KEY, "isModifier is " << mod); return mod; } -string GuiKeySymbol::getSymbolName() const +string KeySymbol::getSymbolName() const { - string sym(qkey_to_string(key_)); + string name = qkey_to_string(key_); - // e.g. A-Za-z, and others - if (sym.empty()) - sym = fromqstr(text_); + // others + if (name.empty()) + name = to_utf8(text_); - return sym; + return name; } -char_type GuiKeySymbol::getUCSEncoded() const +char_type KeySymbol::getUCSEncoded() const { - if (text_.isEmpty()) + if (text_.empty()) return 0; // UTF16 has a maximum of two characters. @@ -147,29 +131,25 @@ char_type GuiKeySymbol::getUCSEncoded() const if (lyxerr.debugging() && text_.size() > 1) { // We don't know yet how well support the full ucs4 range. - LYXERR(Debug::KEY) << "GuiKeySymbol::getUCSEncoded()" << endl; - for (int i = 0; i < text_.size(); ++i) { - LYXERR(Debug::KEY) << "char " << i << ": " - << text_[i].unicode() << endl; - } + LYXERR(Debug::KEY, "KeySymbol::getUCSEncoded()"); + for (int i = 0; i != int(text_.size()); ++i) + LYXERR(Debug::KEY, "char " << i << ": " << int(text_[i])); } - // Only one UCS4 character at the end. - docstring ucs4_text = qstring_to_ucs4(text_); - return ucs4_text[0]; + return text_[0]; } -docstring const GuiKeySymbol::print(key_modifier::state mod, bool forgui) const +docstring const KeySymbol::print(KeyModifier mod, bool forgui) const { int tmpkey = key_; - if (mod & key_modifier::shift) - tmpkey += Qt::SHIFT; - if (mod & key_modifier::ctrl) - tmpkey += Qt::CTRL; - if (mod & key_modifier::alt) - tmpkey += Qt::ALT; + if (mod & ShiftModifier) + tmpkey += Qt::ShiftModifier; + if (mod & ControlModifier) + tmpkey += Qt::ControlModifier; + if (mod & AltModifier) + tmpkey += Qt::AltModifier; QKeySequence seq(tmpkey); @@ -178,40 +158,35 @@ docstring const GuiKeySymbol::print(key_modifier::state mod, bool forgui) const } -bool GuiKeySymbol::isText() const +bool KeySymbol::isText() const { - if (text_.isEmpty()) { - LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl; - return false; - } - - return true; + if (!text_.empty()) + return true; + LYXERR(Debug::KEY, "text_ empty, isText() == false"); + return false; } -bool GuiKeySymbol::operator==(KeySymbol const & ks) const +bool KeySymbol::operator==(KeySymbol const & ks) const { - GuiKeySymbol const & qks = static_cast(ks); - // we do not have enough info for a fair comparison, so return // false. This works out OK because unknown text from Qt will // get inserted anyway after the isText() check - if (key_ == Qt::Key_unknown || qks.key_ == Qt::Key_unknown) + if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown) return false; - - return key_ == qks.key_; + return key_ == ks.key_; } -key_modifier::state q_key_state(Qt::KeyboardModifiers state) +KeyModifier q_key_state(Qt::KeyboardModifiers state) { - key_modifier::state k = key_modifier::none; + KeyModifier k = NoModifier; if (state & Qt::ControlModifier) - k |= key_modifier::ctrl; + k |= ControlModifier; if (state & Qt::ShiftModifier) - k |= key_modifier::shift; + k |= ShiftModifier; if (state & Qt::AltModifier || state & Qt::MetaModifier) - k |= key_modifier::alt; + k |= AltModifier; return k; }