From 7a39e69a32297600507014915cade4161008e854 Mon Sep 17 00:00:00 2001 From: plc-user <74435298+plc-user@users.noreply.github.com> Date: Mon, 3 Feb 2025 13:03:55 +0100 Subject: [PATCH 1/2] element-name: if no name is set, set to "en" / "NoName" (also adjusted comment) --- sources/NameList/nameslist.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sources/NameList/nameslist.cpp b/sources/NameList/nameslist.cpp index 8117651ef..3e227823b 100644 --- a/sources/NameList/nameslist.cpp +++ b/sources/NameList/nameslist.cpp @@ -168,8 +168,13 @@ void NamesList::fromXml(const pugi::xml_node &xml_element, const QHash xml_opt = getXmlOptions(xml_options); QDomElement names_elmt = xml_document.createElement(xml_opt["ParentTagName"]); + if (hash_names.isEmpty()) { + QDomElement name_elmt = xml_document.createElement(xml_opt["TagName"]); + name_elmt.setAttribute(xml_opt["LanguageAttribute"], "en"); + name_elmt.appendChild(xml_document.createTextNode("NoName")); + names_elmt.appendChild(name_elmt); + } QHashIterator names_iterator(hash_names); while (names_iterator.hasNext()) { names_iterator.next(); From 25a81f24fa54c6b278aa97e784f7d746b6afe88e Mon Sep 17 00:00:00 2001 From: plc-user <74435298+plc-user@users.noreply.github.com> Date: Mon, 3 Feb 2025 13:23:32 +0100 Subject: [PATCH 2/2] =?UTF-8?q?NamesList:=20Use=20=E2=80=98Qmap=E2=80=99?= =?UTF-8?q?=20instead=20of=20=E2=80=98Qhash=E2=80=99=20to=20automatically?= =?UTF-8?q?=20sort=20the=20names=20of=20an=20element=20by=20language=20cod?= =?UTF-8?q?e=20before=20saving=20the=20element-file.=20Added=20English=20c?= =?UTF-8?q?omment=20in=20header-file.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sources/NameList/nameslist.cpp | 35 +++++++++++++++++----------------- sources/NameList/nameslist.h | 8 +++++++- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/sources/NameList/nameslist.cpp b/sources/NameList/nameslist.cpp index 3e227823b..c9e104dc7 100644 --- a/sources/NameList/nameslist.cpp +++ b/sources/NameList/nameslist.cpp @@ -46,7 +46,7 @@ NamesList::~NamesList() void NamesList::addName(const QString &lang, const QString &name) { if ((lang.length() != 2) && (lang.length() != 5)) return; if ((lang.length() == 5) && (lang[2] != '_')) return; - hash_names.insert(lang, name); + map_names.insert(lang, name); } /** @@ -54,7 +54,7 @@ void NamesList::addName(const QString &lang, const QString &name) { @param lang la langue pour laquelle il faut supprimer le nom */ void NamesList::removeName(const QString &lang) { - hash_names.remove(lang); + map_names.remove(lang); } /** @@ -62,7 +62,7 @@ void NamesList::removeName(const QString &lang) { */ void NamesList::clearNames() { - hash_names.clear(); + map_names.clear(); } /** @@ -70,7 +70,7 @@ void NamesList::clearNames() */ QList NamesList::langs() const { - return(hash_names.keys()); + return(map_names.keys()); } /** @@ -78,7 +78,7 @@ QList NamesList::langs() const */ bool NamesList::isEmpty() const { - return(hash_names.isEmpty()); + return(map_names.isEmpty()); } /** @@ -86,7 +86,7 @@ bool NamesList::isEmpty() const */ int NamesList::count() const { - return(hash_names.count()); + return(map_names.count()); } /** @@ -95,7 +95,7 @@ int NamesList::count() const defini */ QString &NamesList::operator[](const QString &lang) { - return(hash_names[lang]); + return(map_names[lang]); } /** @@ -105,7 +105,7 @@ QString &NamesList::operator[](const QString &lang) { */ const QString NamesList::operator[](const QString &lang) const { - return(hash_names.value(lang)); + return(map_names.value(lang)); } @@ -184,18 +184,19 @@ QDomElement NamesList::toXml(QDomDocument &xml_document, const QHash xml_opt = getXmlOptions(xml_options); QDomElement names_elmt = xml_document.createElement(xml_opt["ParentTagName"]); - if (hash_names.isEmpty()) { + if (map_names.isEmpty()) { + qInfo() << " NamesList of element is empty - add default: [" << "en" << "] = " << "NoName" << ""; QDomElement name_elmt = xml_document.createElement(xml_opt["TagName"]); name_elmt.setAttribute(xml_opt["LanguageAttribute"], "en"); name_elmt.appendChild(xml_document.createTextNode("NoName")); names_elmt.appendChild(name_elmt); } - QHashIterator names_iterator(hash_names); + QMapIterator names_iterator(map_names); while (names_iterator.hasNext()) { names_iterator.next(); QDomElement name_elmt = xml_document.createElement(xml_opt["TagName"]); name_elmt.setAttribute(xml_opt["LanguageAttribute"], names_iterator.key()); - name_elmt.appendChild(xml_document.createTextNode(names_iterator.value())); + name_elmt.appendChild(xml_document.createTextNode(names_iterator.value().trimmed())); names_elmt.appendChild(name_elmt); } return(names_elmt); @@ -229,7 +230,7 @@ QHash NamesList::getXmlOptions(const QHash & */ bool NamesList::operator!=(const NamesList &nl) const { - return(hash_names != nl.hash_names); + return(map_names != nl.map_names); } /** @@ -238,7 +239,7 @@ bool NamesList::operator!=(const NamesList &nl) const */ bool NamesList::operator==(const NamesList &nl) const { - return(hash_names == nl.hash_names); + return(map_names == nl.map_names); } /** @@ -257,10 +258,10 @@ bool NamesList::operator==(const NamesList &nl) const QString NamesList::name(const QString &fallback_name) const { QString system_language = QETApp::langFromSetting(); - if (! hash_names[system_language].isEmpty()) - return (hash_names[system_language]); - if (! hash_names["en"].isEmpty()) return (hash_names["en"]); + if (! map_names[system_language].isEmpty()) + return (map_names[system_language]); + if (! map_names["en"].isEmpty()) return (map_names["en"]); if (! fallback_name.isEmpty()) return (fallback_name); - if (hash_names.count()) return (hash_names.begin().value()); + if (map_names.count()) return (map_names.begin().value()); return (QString("")); } diff --git a/sources/NameList/nameslist.h b/sources/NameList/nameslist.h index 51ad335e9..c316a79c7 100644 --- a/sources/NameList/nameslist.h +++ b/sources/NameList/nameslist.h @@ -21,6 +21,12 @@ #include /** + This class represents a list of names, used by elements and categories + to embed the same name in several languages. + Languages are represented by two or five letters (typically the first + two of the system locale); examples: en for English, fr for French, + pt_BR for Brazilian Portuguese. + French: Cette classe represente une liste de noms, utilisee par les elements et categories pour embarquer un meme nom en plusieurs langues. @@ -36,7 +42,7 @@ class NamesList { // attributes private: - QHash hash_names; + QMap map_names; public: static int MetaTypeId;