2006-03-05 17:24:44 +00:00
|
|
|
|
/**
|
2007-11-27 22:19:36 +00:00
|
|
|
|
* \file qt4/GuiKeySymbol.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
2007-08-31 05:53:55 +00:00
|
|
|
|
* \author Asger & J<EFBFBD>rgen
|
2006-03-05 17:24:44 +00:00
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
#include "KeySymbol.h"
|
2007-08-31 05:53:55 +00:00
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
#include "qlkey.h"
|
|
|
|
|
#include "qt_helpers.h"
|
2006-05-07 10:20:43 +00:00
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
|
#include "support/debug.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Encoding.h"
|
|
|
|
|
#include "Language.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <QKeySequence>
|
|
|
|
|
#include <QEvent>
|
|
|
|
|
#include <QTextCodec>
|
|
|
|
|
|
2007-11-27 22:19:36 +00:00
|
|
|
|
#include "boost/assert.hpp"
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
|
#include <map>
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
using std::map;
|
|
|
|
|
|
2007-11-27 22:19:36 +00:00
|
|
|
|
namespace lyx {
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
|
#if 0
|
2007-10-02 21:51:54 +00:00
|
|
|
|
static char encode(string const & encoding, QString const & str)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-08-31 05:53:55 +00:00
|
|
|
|
typedef map<string, QTextCodec *> EncodingMap;
|
|
|
|
|
EncodingMap encoding_map;
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
QTextCodec * codec = 0;
|
|
|
|
|
|
|
|
|
|
EncodingMap::const_iterator cit = encoding_map.find(encoding);
|
|
|
|
|
if (cit == encoding_map.end()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "Unrecognised encoding '" << encoding << "'.");
|
2006-03-05 17:24:44 +00:00
|
|
|
|
codec = encoding_map.find("")->second;
|
|
|
|
|
} else {
|
|
|
|
|
codec = cit->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!codec) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "No codec for encoding '" << encoding << "' found.");
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "Using codec " << fromqstr(codec->name()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
if (!codec->canEncode(str)) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "Oof. Can't encode the text !");
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-07 10:20:43 +00:00
|
|
|
|
return codec->fromUnicode(str).data()[0];
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
|
#endif
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
void setKeySymbol(KeySymbol * sym, QKeyEvent * ev)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-09-17 18:41:03 +00:00
|
|
|
|
sym->setKey(ev->key());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
if (ev->text().isNull()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "keyevent has isNull() text !");
|
2007-09-17 18:41:03 +00:00
|
|
|
|
sym->setText(docstring());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "Getting key " << ev->key() << ", with text '"
|
|
|
|
|
<< fromqstr(ev->text()) << "'");
|
2007-10-30 16:04:48 +00:00
|
|
|
|
// 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.
|
2007-09-17 18:41:03 +00:00
|
|
|
|
sym->setText(qstring_to_ucs4(ev->text()));
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "Setting key to " << sym->key() << ", "
|
|
|
|
|
<< to_utf8(sym->text()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
void KeySymbol::init(string const & symbolname)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
key_ = string_to_qkey(symbolname);
|
2007-09-17 18:41:03 +00:00
|
|
|
|
text_ = from_utf8(symbolname);
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "Init key to " << key_ << ", " << to_utf8(text_));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
bool KeySymbol::isOK() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-09-17 18:41:03 +00:00
|
|
|
|
bool const ok = !(text_.empty() && key_ == Qt::Key_unknown);
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "isOK is " << ok);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
bool KeySymbol::isModifier() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-09-16 20:58:22 +00:00
|
|
|
|
bool const mod = q_is_modifier(key_);
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "isModifier is " << mod);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return mod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
string KeySymbol::getSymbolName() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-09-17 18:41:03 +00:00
|
|
|
|
string name = qkey_to_string(key_);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-10-30 16:04:48 +00:00
|
|
|
|
// others
|
2007-09-17 18:41:03 +00:00
|
|
|
|
if (name.empty())
|
|
|
|
|
name = to_utf8(text_);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
return name;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
char_type KeySymbol::getUCSEncoded() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-09-17 18:41:03 +00:00
|
|
|
|
if (text_.empty())
|
2006-08-30 14:59:07 +00:00
|
|
|
|
return 0;
|
2006-08-13 22:54:59 +00:00
|
|
|
|
|
2007-02-06 14:13:02 +00:00
|
|
|
|
// UTF16 has a maximum of two characters.
|
|
|
|
|
BOOST_ASSERT(text_.size() <= 2);
|
|
|
|
|
|
|
|
|
|
if (lyxerr.debugging() && text_.size() > 1) {
|
|
|
|
|
// We don't know yet how well support the full ucs4 range.
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "KeySymbol::getUCSEncoded()");
|
|
|
|
|
for (int i = 0; i != int(text_.size()); ++i)
|
|
|
|
|
LYXERR(Debug::KEY, "char " << i << ": " << int(text_[i]));
|
2007-02-06 14:13:02 +00:00
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
return text_[0];
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-02 21:51:54 +00:00
|
|
|
|
docstring const KeySymbol::print(KeyModifier mod, bool forgui) const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
int tmpkey = key_;
|
|
|
|
|
|
2007-10-02 21:51:54 +00:00
|
|
|
|
if (mod & ShiftModifier)
|
|
|
|
|
tmpkey += Qt::ShiftModifier;
|
|
|
|
|
if (mod & ControlModifier)
|
|
|
|
|
tmpkey += Qt::ControlModifier;
|
|
|
|
|
if (mod & AltModifier)
|
|
|
|
|
tmpkey += Qt::AltModifier;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-01-04 17:10:24 +00:00
|
|
|
|
QKeySequence seq(tmpkey);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
|
return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
|
2007-01-04 17:10:24 +00:00
|
|
|
|
: QKeySequence::PortableText));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
bool KeySymbol::isText() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-09-17 18:41:03 +00:00
|
|
|
|
if (!text_.empty())
|
|
|
|
|
return true;
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::KEY, "text_ empty, isText() == false");
|
2007-09-17 18:41:03 +00:00
|
|
|
|
return false;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
|
bool KeySymbol::operator==(KeySymbol const & ks) const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
// 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
|
2007-09-17 18:41:03 +00:00
|
|
|
|
if (key_ == Qt::Key_unknown || ks.key_ == Qt::Key_unknown)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return false;
|
2007-09-17 18:41:03 +00:00
|
|
|
|
return key_ == ks.key_;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
2007-10-02 21:51:54 +00:00
|
|
|
|
KeyModifier q_key_state(Qt::KeyboardModifiers state)
|
2007-08-14 15:00:09 +00:00
|
|
|
|
{
|
2007-10-02 21:51:54 +00:00
|
|
|
|
KeyModifier k = NoModifier;
|
2007-08-14 15:00:09 +00:00
|
|
|
|
if (state & Qt::ControlModifier)
|
2007-10-02 21:51:54 +00:00
|
|
|
|
k |= ControlModifier;
|
2007-08-14 15:00:09 +00:00
|
|
|
|
if (state & Qt::ShiftModifier)
|
2007-10-02 21:51:54 +00:00
|
|
|
|
k |= ShiftModifier;
|
2007-08-14 15:00:09 +00:00
|
|
|
|
if (state & Qt::AltModifier || state & Qt::MetaModifier)
|
2007-10-02 21:51:54 +00:00
|
|
|
|
k |= AltModifier;
|
2007-08-14 15:00:09 +00:00
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
} // namespace lyx
|