goldendict-ng/src/langcoder.cc

140 lines
2.7 KiB
C++
Raw Normal View History

2009-04-22 21:37:32 +00:00
#include "langcoder.hh"
#include <cctype>
LangCoder langCoder;
LangCoder::LangCoder()
{
LangStruct ls;
for (int i = 0; true; i++) {
const LangCode &lc = LangCodes[i];
if (lc.lang.isEmpty())
break;
//ls.order = i;
//ls.icon = QIcon(":/flags/" + QString(lc.code) + ".png");
codeMap[code2toInt(lc.code)] = i;
}
}
QString LangCoder::decode(quint32 code)
{
// temp!
if (codeMap.contains(code))
return LangCodes[codeMap[code]].lang;
return QString();
}
LangStruct LangCoder::langStruct(quint32 code)
{
LangStruct ls;
ls.code = code;
ls.order = -1;
if (codeMap.contains(code)) {
int order = codeMap[code];
const LangCode &lc = LangCodes[order];
ls.order = order;
ls.lang = lc.lang;
ls.icon = QIcon(":/flags/" + QString(lc.code) + ".png");
}
return ls;
}
quint32 LangCoder::code3toInt(const std::string& code3)
{
if (code3.length() < 2)
return 0;
// this is temporary
2009-04-23 10:29:49 +00:00
char code1 = tolower( code3.at(1) );
char code0 = tolower( code3.at(0) );
2009-04-22 21:37:32 +00:00
2009-04-23 10:29:49 +00:00
return ( ((quint32)code1) << 8 ) + (quint32)code0;
2009-04-22 21:37:32 +00:00
}
/*
LangStruct& LangCoder::CodeToLangStruct(const QString &code)
{
if (codeMap.contains(code)) {
LangStruct &ls = codeMap[code];
if (ls.icon.isNull() && *ls.icon_code) {
ls.icon = QIcon(":/Resources/flags/" + QString(ls.icon_code) + ".png");
}
return ls;
}
return dummyLS;
}
QString LangCoder::CodeToHtml(const QString &code)
{
if (codeMap.contains(code)) {
LangStruct &ls = codeMap[code];
if (*ls.icon_code) {
return "<img src=':/Resources/flags/" + QString(ls.icon_code) + ".png'>&nbsp;" + ls.lang;
}
return ls.lang;
}
return "";
}
bool LangCoder::CheckCode(QString &code)
{
code = code.toUpper();
if (codeMap.contains(code))
return true;
if (code == "DEU") {
code = "GER";
return true;
}
return false;
}
*/
/*
LangModel::LangModel(QObject * parent) : QAbstractItemModel(parent)
{
}
int LangModel::columnCount ( const QModelIndex & parent ) const
{
return 2;
}
int LangModel::rowCount ( const QModelIndex & parent ) const
{
return arraySize(LangCodes);
}
QVariant LangModel::data ( const QModelIndex & index, int role ) const
{
switch (role) {
case Qt::DisplayRole:
return LangCodes[index.row()].lang;
case LangCodeRole:
return LangCodes[index.row()].code;
default:;
}
return QVariant();
}
QModelIndex LangModel::index ( int row, int column, const QModelIndex & parent ) const
{
return createIndex(row, column);
}
QModelIndex LangModel::parent ( const QModelIndex & index ) const
{
return QModelIndex();
}
*/