QElectroTech  0.70
nameslist.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "nameslist.h"
19 #include "qetapp.h"
20 
21 // make this class usable with QVariant
22 int NamesList::MetaTypeId = qRegisterMetaType<NamesList>("NamesList");
23 
28 }
29 
34 NamesList::NamesList(const NamesList &other) : hash_names(other.hash_names) {
35 }
36 
41 }
42 
50 void NamesList::addName(const QString &lang, const QString &name) {
51  if (lang.length() != 2) return;
52  hash_names.insert(lang, name);
53 }
54 
59 void NamesList::removeName(const QString &lang) {
60  hash_names.remove(lang);
61 }
62 
67  hash_names.clear();
68 }
69 
73 QList<QString> NamesList::langs() const {
74  return(hash_names.keys());
75 }
76 
80 bool NamesList::isEmpty() const {
81  return(hash_names.isEmpty());
82 }
83 
87 int NamesList::count() const {
88  return(hash_names.count());
89 }
90 
96 QString &NamesList::operator[](const QString &lang) {
97  return(hash_names[lang]);
98 }
99 
105 const QString NamesList::operator[](const QString &lang) const {
106  return(hash_names.value(lang));
107 }
108 
109 
110 
120 void NamesList::fromXml(const QDomElement &xml_element, const QHash<QString, QString> &xml_options) {
121  QHash<QString, QString> xml_opt = getXmlOptions(xml_options);
122  // parcourt les enfants "names" de l'element XML
123  for (QDomNode node = xml_element.firstChild() ; !node.isNull() ; node = node.nextSibling()) {
124  QDomElement names = node.toElement();
125  if (names.isNull() || names.tagName() != xml_opt["ParentTagName"]) continue;
126  // parcourt les petits-enfants "name"
127  for (QDomNode n = names.firstChild() ; !n.isNull() ; n = n.nextSibling()) {
128  QDomElement name = n.toElement();
129  if (name.isNull() || name.tagName() != xml_opt["TagName"]) continue;
130  addName(name.attribute(xml_opt["LanguageAttribute"]), name.text());
131  }
132  }
133 }
134 
143 QDomElement NamesList::toXml(QDomDocument &xml_document, const QHash<QString, QString> &xml_options) const {
144  QHash<QString, QString> xml_opt = getXmlOptions(xml_options);
145  QDomElement names_elmt = xml_document.createElement(xml_opt["ParentTagName"]);
146  QHashIterator<QString, QString> names_iterator(hash_names);
147  while (names_iterator.hasNext()) {
148  names_iterator.next();
149  QDomElement name_elmt = xml_document.createElement(xml_opt["TagName"]);
150  name_elmt.setAttribute(xml_opt["LanguageAttribute"], names_iterator.key());
151  name_elmt.appendChild(xml_document.createTextNode(names_iterator.value()));
152  names_elmt.appendChild(name_elmt);
153  }
154  return(names_elmt);
155 }
156 
164 QHash<QString, QString> NamesList::getXmlOptions(const QHash<QString, QString> &xml_options) const {
165  QHash<QString, QString> new_xml_options = xml_options;
166  if (!xml_options.contains("ParentTagName")) {
167  new_xml_options.insert("ParentTagName", "names");
168  }
169  if (!xml_options.contains("TagName")) {
170  new_xml_options.insert("TagName", "name");
171  }
172  if (!xml_options.contains("LanguageAttribute")) {
173  new_xml_options.insert("LanguageAttribute", "lang");
174  }
175  return new_xml_options;
176 }
177 
182 bool NamesList::operator!=(const NamesList &nl) const {
183  return(hash_names != nl.hash_names);
184 }
185 
190 bool NamesList::operator==(const NamesList &nl) const {
191  return(hash_names == nl.hash_names);
192 }
193 
205 QString NamesList::name(const QString &fallback_name) const {
206  QString system_language = QETApp::langFromSetting();
207  QString returned_name;
208  if (!hash_names[system_language].isEmpty()) {
209  returned_name = hash_names[system_language];
210  } else if (!hash_names["en"].isEmpty()) {
211  returned_name = hash_names["en"];
212  } else if (!fallback_name.isEmpty()) {
213  returned_name = fallback_name;
214  } else if (hash_names.count()) {
215  returned_name = hash_names.value(hash_names.keys().first());
216  }
217  return(returned_name);
218 }
QString & operator[](const QString &)
Definition: nameslist.cpp:96
void fromXml(const QDomElement &, const QHash< QString, QString > &=QHash< QString, QString >())
Definition: nameslist.cpp:120
QHash< QString, QString > hash_names
Definition: nameslist.h:38
QString name(const QString &=QString()) const
Definition: nameslist.cpp:205
QDomElement toXml(QDomDocument &, const QHash< QString, QString > &=QHash< QString, QString >()) const
Definition: nameslist.cpp:143
static QString langFromSetting()
QETApp::langFromSetting.
Definition: qetapp.cpp:184
void addName(const QString &, const QString &)
Definition: nameslist.cpp:50
static int MetaTypeId
Definition: nameslist.h:41
virtual ~NamesList()
Definition: nameslist.cpp:40
QIcon nl
Definition: qeticons.cpp:202
void removeName(const QString &)
Definition: nameslist.cpp:59
bool isEmpty() const
Definition: nameslist.cpp:80
QHash< QString, QString > getXmlOptions(const QHash< QString, QString > &=QHash< QString, QString >()) const
Definition: nameslist.cpp:164
bool operator==(const NamesList &) const
Definition: nameslist.cpp:190
int count() const
Definition: nameslist.cpp:87
bool operator!=(const NamesList &) const
Definition: nameslist.cpp:182
void clearNames()
Definition: nameslist.cpp:66
QList< QString > langs() const
Definition: nameslist.cpp:73