diff --git a/translatorsinfo/translatorsinfo.cpp b/translatorsinfo/translatorsinfo.cpp index e9fd7c7..ce9b91f 100644 --- a/translatorsinfo/translatorsinfo.cpp +++ b/translatorsinfo/translatorsinfo.cpp @@ -31,10 +31,11 @@ #include #include #include +#include //using namespace LXQt; -void fillLangguages(QMap *languages) +void fillLangguages(QHash *languages) { languages->insert("ach" ,"Acoli"); languages->insert("af" ,"Afrikaans"); @@ -320,6 +321,22 @@ void fillLangguages(QMap *languages) languages->insert("zu_ZA" ,"Zulu (South Africa)"); } +QString languageToString(QString langId) +{ + static QHash 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) { @@ -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 = "
"; + for(const auto& entry : mLangTranslators) { - ret += "
  • " + translator->asHtml() + "
  • "; + const auto& lang = entry.first; + const auto& translators = entry.second; + + ret += "
    " + languageToString(lang) + "
    "; + for(const auto & translator : translators) { + ret += "
    " + translator.asHtml() + "
    "; + } } + ret += "
    "; 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(" %2").arg(contact, mInfo.toHtmlEscaped()); - else if (contact.contains("@") || contact.contains("<")) - mInfo = QString(" %2").arg(contact, mInfo.toHtmlEscaped()); + ret = QString(" %2").arg(mContact, ret.toHtmlEscaped()); + else if (mContact.contains("@") || mContact.contains("<")) + ret = QString(" %2").arg(mContact, ret.toHtmlEscaped()); else - mInfo = QString(" %2").arg(contact, mInfo.toHtmlEscaped()); + ret = QString(" %2").arg(mContact, ret.toHtmlEscaped()); } -} - -void TranslatorPerson::addLanguage(QString langId) -{ - static QMap 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; } diff --git a/translatorsinfo/translatorsinfo.h b/translatorsinfo/translatorsinfo.h index 6a767f1..bbe9591 100644 --- a/translatorsinfo/translatorsinfo.h +++ b/translatorsinfo/translatorsinfo.h @@ -27,9 +27,11 @@ #ifndef TRANSLATORSINFO_H #define TRANSLATORSINFO_H +#include +#include +#include + #include -#include -#include #if 0 namespace LXQt @@ -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 mItems; + std::map > mLangTranslators; //< maps a language to set of it's translators void process(const QString &lang, const QString &englishName, const QString &nativeName, const QString &contact); };