QElectroTech  0.70
qetxml.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 "qetxml.h"
19 #include "nameslist.h"
20 #include <QPen>
21 #include <QDir>
22 
30 QDomElement QETXML::penToXml(QDomDocument &parent_document,const QPen& pen)
31 {
32  QDomElement element = parent_document.createElement("pen");
33 
34  QString style;
35  switch(pen.style())
36  {
37  case Qt::SolidLine : style = "SolidLine"; break;
38  case Qt::DashLine : style = "DashLine"; break;
39  case Qt::DotLine : style = "DotLine"; break;
40  case Qt::DashDotLine : style = "DashDotLine"; break;
41  case Qt::DashDotDotLine : style = "DashDotDotLine"; break;
42  case Qt::CustomDashLine : style = "CustomDashLine"; break;
43  default : style = "Unknown"; break;
44  }
45 
46  element.setAttribute("style", style);
47  element.setAttribute("color", pen.color().name());
48  element.setAttribute("widthF", QString::number(pen.widthF()));
49  return element;
50 }
51 
59 QPen QETXML::penFromXml(const QDomElement &element)
60 {
61  QPen pen;
62 
63  if (!(!element.isNull() && element.tagName() == "pen"))
64  {
65  pen.setStyle(Qt::DashLine);
66  return pen;
67  }
68 
69  QString style = element.attribute("style", "DashLine");
70  if (style == "SolidLine") pen.setStyle(Qt::SolidLine);
71  else if (style == "DashLine") pen.setStyle(Qt::DashLine);
72  else if (style == "DotLine") pen.setStyle(Qt::DotLine);
73  else if (style == "DashDotLine") pen.setStyle(Qt::DashDotLine);
74  else if (style == "DashDotDotLine") pen.setStyle(Qt::DashDotDotLine);
75  else if (style == "CustomDashLine") pen.setStyle(Qt::CustomDashLine),
76  pen.setDashPattern( QVector<qreal>() << 10 << 10 );
77  else pen.setStyle(Qt::DashLine);
78 
79  pen.setColor(QColor(element.attribute("color", "#000000")));
80  pen.setWidthF(element.attribute("widthF", "1").toDouble());
81  return pen;
82 }
83 
91 QDomElement QETXML::brushToXml(QDomDocument &parent_document, const QBrush& brush)
92 {
93  QDomElement element = parent_document.createElement("brush");
94 
95  QString style;
96  switch (brush.style())
97  {
98  case Qt::NoBrush : style = "NoBrush"; break;
99  case Qt::SolidPattern : style = "SolidPattern"; break;
100  case Qt::Dense1Pattern : style = "Dense1Pattern"; break;
101  case Qt::Dense2Pattern : style = "Dense2Pattern"; break;
102  case Qt::Dense3Pattern : style = "Dense3Pattern"; break;
103  case Qt::Dense4Pattern : style = "Dense4Pattern"; break;
104  case Qt::Dense5Pattern : style = "Dense5Pattern"; break;
105  case Qt::Dense6Pattern : style = "Dense6Pattern"; break;
106  case Qt::Dense7Pattern : style = "Dense7Pattern"; break;
107  case Qt::HorPattern : style = "HorPattern"; break;
108  case Qt::VerPattern : style = "VerPattern"; break;
109  case Qt::CrossPattern : style = "CrossPattern"; break;
110  case Qt::BDiagPattern : style = "BDiagPattern"; break;
111  case Qt::FDiagPattern : style = "FDiagPattern"; break;
112  case Qt::DiagCrossPattern : style = "DiagCrossPattern"; break;
113  default : style = "Unknown"; break;
114  }
115 
116  element.setAttribute("style", style);
117  element.setAttribute("color", brush.color().name());
118  return element;
119 }
120 
128 QBrush QETXML::brushFromXml(const QDomElement &element)
129 {
130  QBrush brush;
131 
132  if (!(!element.isNull() && element.tagName() == "brush")) return brush;
133 
134  QString style = element.attribute("style", "NoBrush");
135  if (style == "NoBrush") brush.setStyle(Qt::NoBrush);
136  else if (style == "SolidPattern") brush.setStyle(Qt::SolidPattern);
137  else if (style == "Dense1Pattern") brush.setStyle(Qt::Dense1Pattern);
138  else if (style == "Dense2Pattern") brush.setStyle(Qt::Dense2Pattern);
139  else if (style == "Dense3Pattern") brush.setStyle(Qt::Dense3Pattern);
140  else if (style == "Dense4Pattern") brush.setStyle(Qt::Dense4Pattern);
141  else if (style == "Dense5Pattern") brush.setStyle(Qt::Dense5Pattern);
142  else if (style == "Dense6Pattern") brush.setStyle(Qt::Dense6Pattern);
143  else if (style == "Dense7Pattern") brush.setStyle(Qt::Dense7Pattern);
144  else if (style == "HorPattern") brush.setStyle(Qt::HorPattern);
145  else if (style == "VerPattern") brush.setStyle(Qt::VerPattern);
146  else if (style == "CrossPattern") brush.setStyle(Qt::CrossPattern);
147  else if (style == "BDiagPattern") brush.setStyle(Qt::BDiagPattern);
148  else if (style == "FDiagPattern") brush.setStyle(Qt::FDiagPattern);
149  else if (style == "DiagCrossPattern") brush.setStyle(Qt::DiagCrossPattern);
150  else if (style == "Unknown") brush.setStyle(Qt::NoBrush);
151 
152  brush.setColor(QColor(element.attribute("color", "#000000")));
153  return brush;
154 }
155 
165 QDomElement QETXML::fileSystemDirToXmlCollectionDir(QDomDocument &document, const QDir &dir, const QString& rename)
166 {
167  if (!dir.exists()) return QDomElement();
168 
169  QDomElement dir_element = document.createElement("category");
170  dir_element.setAttribute("name", rename.isNull()? dir.dirName() : rename);
171 
172  //Get the traduction of this directory
173  QFile qet_dir(dir.filePath("qet_directory"));
174  if (qet_dir.exists() && qet_dir.open(QIODevice::ReadOnly | QIODevice::Text))
175  {
176  //Get the content of the file
177  QDomDocument trad_document;
178  if (trad_document.setContent(&qet_dir))
179  {
180  QDomElement root = trad_document.documentElement();
181  if (root.tagName() == "qet-directory")
182  {
183  NamesList nl;
184  nl.fromXml(root);
185  dir_element.appendChild(nl.toXml(document));
186  }
187  }
188  qet_dir.close();
189  }
190 
191  return dir_element;
192 }
193 
203 QDomElement QETXML::fileSystemElementToXmlCollectionElement(QDomDocument &document, QFile &file, const QString& rename)
204 {
205  if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text))
206  {
207  QDomDocument docu;
208  if (docu.setContent(&file))
209  {
210  QFileInfo fi(file);
211  QDomElement dom_element = document.createElement("element");
212  dom_element.setAttribute("name", rename.isEmpty()? fi.fileName() : rename);
213  dom_element.appendChild(docu.documentElement());
214  file.close();
215  return dom_element;
216  }
217  else
218  {
219  file.close();
220  return QDomElement();
221  }
222  }
223  else
224  return QDomElement();
225 }
226 
235 bool QETXML::writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message)
236 {
237  QFile file(file_path);
238 
239  // Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
240  bool file_opening = file.open(QIODevice::WriteOnly);
241  if (!file_opening)
242  {
243  if (error_message)
244  {
245  *error_message = QString(QObject::tr("Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.",
246  "error message when attempting to write an XML file")
247  ).arg(file_path).arg(file.error());
248  }
249  return(false);
250  }
251 
252  QTextStream out(&file);
253  out.setCodec("UTF-8");
254  out.setGenerateByteOrderMark(false);
255  out << xml_document.toString(4);
256  file.close();
257 
258  return(true);
259 }
260 
269 QDomElement QETXML::textToDomElement(QDomDocument &document, const QString& tag_name, const QString& value)
270 {
271  QDomElement element = document.createElement(tag_name);
272  QDomText text = document.createTextNode(value);
273 
274  element.appendChild(text);
275  return element;
276 }
QDomElement penToXml(QDomDocument &parent_document, const QPen &pen)
QETXML::penToXml Write attribute of a QPen in xml element.
Definition: qetxml.cpp:30
bool writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message=nullptr)
QETXML::writeXmlFile Export an XML document to an UTF-8 text file indented with 4 spaces...
Definition: qetxml.cpp:235
QIcon nl
Definition: qeticons.cpp:202
QDomElement brushToXml(QDomDocument &parent_document, const QBrush &brush)
QETXML::brushToXml Write attribute of a QBrush in xml element.
Definition: qetxml.cpp:91
QIcon tr
Definition: qeticons.cpp:204
QPen penFromXml(const QDomElement &element)
QETXML::penFromXml Build a QPen from a xml description.
Definition: qetxml.cpp:59
QDomElement fileSystemDirToXmlCollectionDir(QDomDocument &document, const QDir &dir, const QString &rename=QString())
Definition: qetxml.cpp:165
QDomElement fileSystemElementToXmlCollectionElement(QDomDocument &document, QFile &file, const QString &rename=QString())
Definition: qetxml.cpp:203
QDomElement textToDomElement(QDomDocument &document, const QString &tag_name, const QString &value)
QETXML::textToDomElement Return a QDomElement, created from , with tag name and text ...
Definition: qetxml.cpp:269
QBrush brushFromXml(const QDomElement &element)
QETXML::brushFromXml Build a QBrush from a xml description.
Definition: qetxml.cpp:128