2002-06-19 05:20:29 +00:00
|
|
|
/**
|
|
|
|
* \file QLyXKeySym.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-06-19 05:20:29 +00:00
|
|
|
*
|
|
|
|
* \author Asger and Juergen
|
2002-10-20 01:48:28 +00:00
|
|
|
* \author John Levon
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-06-19 05:20:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QLyXKeySym.h"
|
|
|
|
#include "qlkey.h"
|
2002-07-30 00:53:57 +00:00
|
|
|
#include "debug.h"
|
2002-12-17 20:37:13 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 05:41:42 +00:00
|
|
|
#include <qevent.h>
|
2002-12-17 20:37:13 +00:00
|
|
|
#include <qtextcodec.h>
|
2002-09-04 09:46:08 +00:00
|
|
|
|
2003-01-05 22:38:42 +00:00
|
|
|
#include <map>
|
|
|
|
|
2002-09-04 09:46:08 +00:00
|
|
|
using std::endl;
|
2003-01-05 22:38:42 +00:00
|
|
|
using std::map;
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
lyxerr[Debug::KEY] << "Unrecognised encoding "
|
|
|
|
<< encoding << endl;
|
|
|
|
codec = QTextCodec::codecForLocale();
|
|
|
|
} else {
|
|
|
|
codec = cit->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!codec) {
|
|
|
|
lyxerr[Debug::KEY] << "No codec exists for encoding "
|
|
|
|
<< encoding << endl;
|
|
|
|
codec = QTextCodec::codecForLocale();
|
|
|
|
}
|
|
|
|
|
|
|
|
lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
|
|
|
|
|
|
|
|
if (!codec->canEncode(str)) {
|
|
|
|
lyxerr[Debug::KEY] << "Oof. Can't encode the text !" << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QCString tmpstr = codec->fromUnicode(str);
|
|
|
|
char const * tmpcstr = tmpstr;
|
|
|
|
return tmpcstr[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initEncodings()
|
|
|
|
{
|
|
|
|
// when no document open
|
|
|
|
encoding_map[""] = QTextCodec::codecForLocale();
|
|
|
|
|
|
|
|
encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
|
|
|
|
encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
|
|
|
|
encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
|
|
|
|
encoding_map["iso8859-4"] = QTextCodec::codecForName("ISO 8859-4");
|
|
|
|
encoding_map["iso8859-5"] = QTextCodec::codecForName("ISO 8859-5");
|
|
|
|
encoding_map["iso8859-6"] = QTextCodec::codecForName("ISO 8859-6");
|
|
|
|
encoding_map["iso8859-7"] = QTextCodec::codecForName("ISO 8859-7");
|
|
|
|
encoding_map["iso8859-9"] = QTextCodec::codecForName("ISO 8859-9");
|
|
|
|
encoding_map["iso8859-15"] = QTextCodec::codecForName("ISO 8859-15");
|
|
|
|
encoding_map["cp1255"] = QTextCodec::codecForName("CP 1255");
|
|
|
|
encoding_map["cp1251"] = QTextCodec::codecForName("CP 1251");
|
|
|
|
encoding_map["koi8"] = QTextCodec::codecForName("KOI8-R");
|
|
|
|
encoding_map["koi8-u"] = QTextCodec::codecForName("KOI8-U");
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
encoding_map["tis620-0"] = 0;
|
|
|
|
encoding_map["pt154"] = 0;
|
|
|
|
|
|
|
|
// There are lots more codecs in Qt too ...
|
|
|
|
}
|
2002-09-04 09:46:08 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 05:20:29 +00:00
|
|
|
QLyXKeySym::QLyXKeySym()
|
2002-08-25 05:41:42 +00:00
|
|
|
: LyXKeySym(), key_(0)
|
2002-06-19 05:20:29 +00:00
|
|
|
{
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
2002-08-25 05:41:42 +00:00
|
|
|
void QLyXKeySym::set(QKeyEvent * ev)
|
2002-06-19 05:20:29 +00:00
|
|
|
{
|
2002-08-25 05:41:42 +00:00
|
|
|
key_ = ev->key();
|
2002-12-12 17:12:20 +00:00
|
|
|
if (ev->text().isNull()) {
|
|
|
|
lyxerr[Debug::KEY] << "keyevent has isNull() text !" << endl;
|
|
|
|
text_ = "";
|
|
|
|
return;
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
text_ = ev->text();
|
2002-12-17 20:37:13 +00:00
|
|
|
lyxerr[Debug::KEY] << "Setting key to " << key_ << ", " << fromqstr(text_) << endl;
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 05:20:29 +00:00
|
|
|
|
|
|
|
void QLyXKeySym::init(string const & symbolname)
|
|
|
|
{
|
|
|
|
key_ = string_to_qkey(symbolname);
|
2002-12-17 20:37:13 +00:00
|
|
|
text_ = toqstr(symbolname);
|
|
|
|
lyxerr[Debug::KEY] << "Init key to " << key_ << ", " << fromqstr(text_) << endl;
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 05:20:29 +00:00
|
|
|
bool QLyXKeySym::isOK() const
|
|
|
|
{
|
2002-12-12 13:46:06 +00:00
|
|
|
bool const ok(!(text_.isEmpty() && key_ == Qt::Key_unknown));
|
|
|
|
lyxerr[Debug::KEY] << "isOK is " << ok << endl;
|
|
|
|
return ok;
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-12-12 13:46:06 +00:00
|
|
|
|
2002-06-19 05:20:29 +00:00
|
|
|
bool QLyXKeySym::isModifier() const
|
|
|
|
{
|
2002-12-12 13:46:06 +00:00
|
|
|
bool const mod(q_is_modifier(key_));
|
|
|
|
lyxerr[Debug::KEY] << "isMod is " << mod << endl;
|
|
|
|
return mod;
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|
|
|
|
|
2002-08-25 05:41:42 +00:00
|
|
|
|
2002-06-19 05:20:29 +00:00
|
|
|
string QLyXKeySym::getSymbolName() const
|
|
|
|
{
|
2002-08-25 05:41:42 +00:00
|
|
|
string sym(qkey_to_string(key_));
|
|
|
|
|
2002-12-19 18:44:31 +00:00
|
|
|
// e.g. A-Za-z, and others
|
|
|
|
if (sym.empty())
|
|
|
|
sym = fromqstr(text_);
|
|
|
|
|
2002-08-25 05:41:42 +00:00
|
|
|
return sym;
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2003-01-05 22:38:42 +00:00
|
|
|
char QLyXKeySym::getISOEncoded(string const & encoding) const
|
2002-06-19 05:20:29 +00:00
|
|
|
{
|
2003-01-23 16:23:43 +00:00
|
|
|
lyxerr[Debug::KEY] << "encoding is " << encoding << endl;
|
2003-01-05 22:38:42 +00:00
|
|
|
unsigned char const c = encode(encoding, text_);
|
2002-12-17 20:37:13 +00:00
|
|
|
lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
|
|
|
|
return c;
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-12-12 13:46:06 +00:00
|
|
|
bool QLyXKeySym::isText() const
|
|
|
|
{
|
|
|
|
if (text_.isEmpty()) {
|
|
|
|
lyxerr[Debug::KEY] << "text_ empty, isText() == false" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QChar const c(text_[0]);
|
|
|
|
lyxerr[Debug::KEY] << "isText for key " << key_
|
|
|
|
<< " isPrint is " << c.isPrint() << endl;
|
|
|
|
return c.isPrint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
|
2002-06-19 05:20:29 +00:00
|
|
|
{
|
2002-12-19 18:44:31 +00:00
|
|
|
QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
|
|
|
|
QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-12-19 18:44:31 +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
|
|
|
|
if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
|
|
|
|
return false;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-12-19 18:44:31 +00:00
|
|
|
return q1.key() == q2.key();
|
2002-06-19 05:20:29 +00:00
|
|
|
}
|