2006-03-05 17:24:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file QLyXKeySym.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Asger and J<EFBFBD>rgen
|
|
|
|
|
* \author John Levon
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "QLyXKeySym.h"
|
|
|
|
|
#include "qlkey.h"
|
|
|
|
|
#include "qt_helpers.h"
|
2006-05-07 10:20:43 +00:00
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
2006-05-07 10:20:43 +00:00
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <QKeySequence>
|
|
|
|
|
#include <QEvent>
|
|
|
|
|
#include <QTextCodec>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/environment.h"
|
2006-08-13 22:54:59 +00:00
|
|
|
|
#include "support/unicode.h"
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
#include "encoding.h"
|
|
|
|
|
#include "language.h"
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
using std::endl;
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::map;
|
|
|
|
|
using lyx::support::contains;
|
|
|
|
|
using lyx::support::getEnv;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
typedef map<string, QTextCodec *> EncodingMap;
|
|
|
|
|
EncodingMap encoding_map;
|
|
|
|
|
|
|
|
|
|
char const encode(string const & encoding, QString const & str)
|
|
|
|
|
{
|
|
|
|
|
QTextCodec * codec = 0;
|
|
|
|
|
|
|
|
|
|
EncodingMap::const_iterator cit = encoding_map.find(encoding);
|
|
|
|
|
if (cit == encoding_map.end()) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Unrecognised encoding '" << encoding
|
2006-04-05 23:56:29 +00:00
|
|
|
|
<< "'." << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
codec = encoding_map.find("")->second;
|
|
|
|
|
} else {
|
|
|
|
|
codec = cit->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!codec) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "No codec for encoding '" << encoding
|
2006-04-05 23:56:29 +00:00
|
|
|
|
<< "' found." << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Using codec " << fromqstr(codec->name()) << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
if (!codec->canEncode(str)) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Oof. Can't encode the text !" << endl;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QLyXKeySym::QLyXKeySym()
|
|
|
|
|
: LyXKeySym(), key_(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void QLyXKeySym::set(QKeyEvent * ev)
|
|
|
|
|
{
|
|
|
|
|
key_ = ev->key();
|
|
|
|
|
if (ev->text().isNull()) {
|
|
|
|
|
if (lyxerr.debugging())
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
text_ = "";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
text_ = ev->text();
|
|
|
|
|
if (lyxerr.debugging())
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void QLyXKeySym::init(string const & symbolname)
|
|
|
|
|
{
|
|
|
|
|
key_ = string_to_qkey(symbolname);
|
|
|
|
|
text_ = toqstr(symbolname);
|
|
|
|
|
if (lyxerr.debugging())
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool QLyXKeySym::isOK() const
|
|
|
|
|
{
|
|
|
|
|
bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
|
|
|
|
|
if (lyxerr.debugging())
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "isOK is " << ok << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool QLyXKeySym::isModifier() const
|
|
|
|
|
{
|
|
|
|
|
bool const mod(q_is_modifier(key_));
|
|
|
|
|
if (lyxerr.debugging())
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "isMod is " << mod << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return mod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string QLyXKeySym::getSymbolName() const
|
|
|
|
|
{
|
|
|
|
|
string sym(qkey_to_string(key_));
|
|
|
|
|
|
|
|
|
|
// e.g. A-Za-z, and others
|
|
|
|
|
if (sym.empty())
|
|
|
|
|
sym = fromqstr(text_);
|
|
|
|
|
|
|
|
|
|
return sym;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-01-24 14:01:34 +00:00
|
|
|
|
char_type QLyXKeySym::getUCSEncoded() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2006-08-30 14:59:07 +00:00
|
|
|
|
if (text_.isEmpty())
|
|
|
|
|
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-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "QLyXKeySym::getUCSEncoded()" << endl;
|
2007-02-06 14:13:02 +00:00
|
|
|
|
for (int i = 0; i < text_.size(); ++i) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "char " << i << ": "
|
2007-02-06 14:13:02 +00:00
|
|
|
|
<< text_[i].unicode() << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only one UCS4 character at the end.
|
|
|
|
|
docstring ucs4_text = qstring_to_ucs4(text_);
|
|
|
|
|
return ucs4_text[0];
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-01-04 17:10:24 +00:00
|
|
|
|
docstring const QLyXKeySym::print(key_modifier::state mod, bool forgui) const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
2007-01-04 17:10:24 +00:00
|
|
|
|
|
|
|
|
|
QKeySequence seq(tmpkey);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2007-01-04 17:10:24 +00:00
|
|
|
|
return qstring_to_ucs4(seq.toString(forgui ? QKeySequence::NativeText
|
|
|
|
|
: QKeySequence::PortableText));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool QLyXKeySym::isText() const
|
|
|
|
|
{
|
|
|
|
|
if (text_.isEmpty()) {
|
|
|
|
|
if (lyxerr.debugging())
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "text_ empty, isText() == false" << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
|
return true;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-01-08 11:01:26 +00:00
|
|
|
|
bool QLyXKeySym::operator==(LyXKeySym const & ks) const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-01-08 11:01:26 +00:00
|
|
|
|
QLyXKeySym const & qks = static_cast<QLyXKeySym const &>(ks);
|
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-01-08 11:01:26 +00:00
|
|
|
|
if (key_ == Qt::Key_unknown || qks.key_ == Qt::Key_unknown)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2007-01-08 11:01:26 +00:00
|
|
|
|
return key_ == qks.key_;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|