Skip to content

Commit

Permalink
translatorsinfo: sort translators by language
Browse files Browse the repository at this point in the history
Changes are discussed on issue #97
  • Loading branch information
Fat-Zer authored and agaida committed Jan 20, 2019
1 parent 5f583b5 commit 0c19776
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 68 deletions.
94 changes: 37 additions & 57 deletions translatorsinfo/translatorsinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
#include <QStringList>
#include <QTextCodec>
#include <QTextDocument>
#include <QHash>

//using namespace LXQt;

void fillLangguages(QMap<QString, QString> *languages)
void fillLangguages(QHash<QString, QString> *languages)
{
languages->insert("ach" ,"Acoli");
languages->insert("af" ,"Afrikaans");
Expand Down Expand Up @@ -320,6 +321,22 @@ void fillLangguages(QMap<QString, QString> *languages)
languages->insert("zu_ZA" ,"Zulu (South Africa)");
}

QString languageToString(QString langId)
{
static QHash<QString, QString> mLanguagesList;
if (mLanguagesList.isEmpty())
{
fillLangguages(&mLanguagesList);
}

if (mLanguagesList.contains(langId)) {
return mLanguagesList.value(langId);
} else {
return langId;
}
}



QString getValue(const QSettings &src, const QString &key)
{
Expand Down Expand Up @@ -359,88 +376,51 @@ TranslatorsInfo::TranslatorsInfo()
}
src.endGroup();
}

}

TranslatorsInfo::~TranslatorsInfo()
{
qDeleteAll(mItems);
}

QString TranslatorsInfo::asHtml() const
{
QString ret;
for(const TranslatorPerson *translator : qAsConst(mItems))
ret = "<dl>";
for(const auto& entry : mLangTranslators)
{
ret += "<li>" + translator->asHtml() + "</li>";
const auto& lang = entry.first;
const auto& translators = entry.second;

ret += "<dt><strong>" + languageToString(lang) + "</strong></dt>";
for(const auto & translator : translators) {
ret += "<dd>" + translator.asHtml() + "</dd>";
}
}
ret += "</dl>";

return ret;
}



void TranslatorsInfo::process(const QString &lang, const QString &englishName, const QString &nativeName, const QString &contact)
{
QString key = QString("%1:%2:%3").arg(englishName, nativeName, contact);
TranslatorPerson *translator = mItems.value(key);

if (!translator)
{
translator = new TranslatorPerson(englishName, nativeName, contact);
mItems.insert(key, translator);
}

translator->addLanguage(lang);
mLangTranslators[lang].emplace(englishName, nativeName, contact);
}


TranslatorPerson::TranslatorPerson(const QString &englishName, const QString &nativeName, const QString &contact)
QString TranslatorPerson::asHtml() const
{
mEnglishName = englishName;

if (nativeName != englishName)
mNativeName = nativeName;

mContact = contact;

QString ret;
if (mNativeName.isEmpty())
mInfo = QString("%1").arg(mEnglishName);
ret = QString("%1").arg(mEnglishName);
else
mInfo = QString("%1 (%2)").arg(mEnglishName, mNativeName);
ret = QString("%1 (%2)").arg(mEnglishName, mNativeName);

if (!mContact.isEmpty())
{

if (mContact.contains(QRegExp("^(https?|mailto):")))
mInfo = QString(" <a href='%1'>%2</a>").arg(contact, mInfo.toHtmlEscaped());
else if (contact.contains("@") || contact.contains("<"))
mInfo = QString(" <a href='mailto:%1'>%2</a>").arg(contact, mInfo.toHtmlEscaped());
ret = QString(" <a href='%1'>%2</a>").arg(mContact, ret.toHtmlEscaped());
else if (mContact.contains("@") || mContact.contains("<"))
ret = QString(" <a href='mailto:%1'>%2</a>").arg(mContact, ret.toHtmlEscaped());
else
mInfo = QString(" <a href='http://%1'>%2</a>").arg(contact, mInfo.toHtmlEscaped());
ret = QString(" <a href='http://%1'>%2</a>").arg(mContact, ret.toHtmlEscaped());
}
}


void TranslatorPerson::addLanguage(QString langId)
{
static QMap<QString, QString> mLanguagesList;
if (mLanguagesList.isEmpty())
{
fillLangguages(&mLanguagesList);
}

if (mLanguagesList.contains(langId))
mLanguages << mLanguagesList.value(langId);
else
mLanguages << langId;
}


QString TranslatorPerson::asHtml() const
{
QString ret(mInfo);
ret += " - " + mLanguages.join(", ");
return ret;
}

27 changes: 16 additions & 11 deletions translatorsinfo/translatorsinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

#ifndef TRANSLATORSINFO_H
#define TRANSLATORSINFO_H
#include <map>
#include <set>
#include <tuple>

#include <QString>
#include <QMap>
#include <QStringList>

#if 0
namespace LXQt
Expand All @@ -39,36 +41,39 @@ namespace LXQt
class TranslatorPerson
{
public:
TranslatorPerson(const QString &englishName, const QString &nativeName, const QString &contact);

TranslatorPerson(const QString &englishName, const QString &nativeName, const QString &contact)
:mEnglishName (englishName), mNativeName (nativeName != englishName ? nativeName : ""), mContact (contact)
{ }
TranslatorPerson(TranslatorPerson &&other) = default;

QString englishName() const { return mEnglishName; }
QString nativeName() const { return mNativeName; }
QString contact() const { return mContact; }

QString info() const { return mInfo; }

void addLanguage(QString langId);
QString asHtml() const;

// A comparison operator for std::set
bool operator<(const TranslatorPerson & other) const {
return std::tie(this->mEnglishName, this->mNativeName, this->mContact)
< std::tie(other.mEnglishName, other.mNativeName, other.mContact);
}

private:
QString mEnglishName;
QString mNativeName;
QString mContact;

QString mInfo;
QStringList mLanguages;

};

class TranslatorsInfo
{
public:
TranslatorsInfo();
~TranslatorsInfo();
QString asHtml() const;

private:
QMap<QString, TranslatorPerson*> mItems;
std::map<QString, std::set<TranslatorPerson> > mLangTranslators; //< maps a language to set of it's translators
void process(const QString &lang, const QString &englishName, const QString &nativeName, const QString &contact);
};

Expand Down

0 comments on commit 0c19776

Please sign in to comment.