2006-03-05 17:24:44 +00:00
|
|
|
|
/**
|
2007-04-27 08:43:38 +00:00
|
|
|
|
* \file QKeySymbol.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.
|
|
|
|
|
*
|
|
|
|
|
* \author Asger and J<EFBFBD>rgen
|
|
|
|
|
* \author John Levon
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
#include "QKeySymbol.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
#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"
|
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Encoding.h"
|
|
|
|
|
#include "Language.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
QKeySymbol::QKeySymbol()
|
|
|
|
|
: KeySymbol(), key_(0)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
void QKeySymbol::set(QKeyEvent * ev)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
key_ = ev->key();
|
|
|
|
|
if (ev->text().isNull()) {
|
2007-04-01 14:35:42 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "keyevent has isNull() text !" << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
text_ = "";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
text_ = ev->text();
|
2007-04-01 14:35:42 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
void QKeySymbol::init(string const & symbolname)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
key_ = string_to_qkey(symbolname);
|
|
|
|
|
text_ = toqstr(symbolname);
|
2007-04-01 14:35:42 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
bool QKeySymbol::isOK() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
|
2007-04-01 14:35:42 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "isOK is " << ok << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
bool QKeySymbol::isModifier() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
bool const mod(q_is_modifier(key_));
|
2007-04-01 14:35:42 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "isMod is " << mod << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
return mod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
string QKeySymbol::getSymbolName() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
string sym(qkey_to_string(key_));
|
|
|
|
|
|
|
|
|
|
// e.g. A-Za-z, and others
|
|
|
|
|
if (sym.empty())
|
|
|
|
|
sym = fromqstr(text_);
|
|
|
|
|
|
|
|
|
|
return sym;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
char_type QKeySymbol::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-27 08:43:38 +00:00
|
|
|
|
LYXERR(Debug::KEY) << "QKeySymbol::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-04-27 08:43:38 +00:00
|
|
|
|
docstring const QKeySymbol::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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-27 08:43:38 +00:00
|
|
|
|
bool QKeySymbol::isText() const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (text_.isEmpty()) {
|
2007-04-01 14:35:42 +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-04-27 08:43:38 +00:00
|
|
|
|
bool QKeySymbol::operator==(KeySymbol const & ks) const
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2007-04-27 08:43:38 +00:00
|
|
|
|
QKeySymbol const & qks = static_cast<QKeySymbol 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
|