Mod doc set style de same + wrap code for better readability

This commit is contained in:
Simon De Backer
2020-07-17 22:32:30 +02:00
parent 7ddae811a5
commit 102629b86f
2 changed files with 229 additions and 134 deletions

View File

@@ -23,12 +23,13 @@
#include <QFont> #include <QFont>
/** /**
* @brief QETXML::penToXml @brief QETXML::penToXml
* Write attribute of a QPen in xml element Write attribute of a QPen in xml element
* @param parent_document : parent document for create the QDomElement @param parent_document : parent document for create the QDomElement
* @param pen : the pen to store @param pen : the pen to store
* @return : A QDomElement with the attribute stored. The tagName of QDomeElement is "pen". @return : A QDomElement with the attribute stored.
*/ The tagName of QDomeElement is "pen".
*/
QDomElement QETXML::penToXml(QDomDocument &parent_document,const QPen& pen) QDomElement QETXML::penToXml(QDomDocument &parent_document,const QPen& pen)
{ {
QDomElement element = parent_document.createElement("pen"); QDomElement element = parent_document.createElement("pen");
@@ -48,19 +49,19 @@ QDomElement QETXML::penToXml(QDomDocument &parent_document,const QPen& pen)
element.setAttribute("style", style); element.setAttribute("style", style);
element.setAttribute("color", pen.color().name()); element.setAttribute("color", pen.color().name());
element.setAttribute("widthF", QString::number(pen.widthF())); element.setAttribute("widthF", QString::number(pen.widthF()));
return element; return element;
} }
/** /**
* @brief QETXML::penFromXml @brief QETXML::penFromXml
* Build a QPen from a xml description Build a QPen from a xml description
* @param element, The QDomElement that describe the pen @param element, The QDomElement that describe the pen
* @return the created pen. If @element is null or tagName isn't "pen" @return the created pen. If @element is null or tagName isn't "pen"
* return a default constructed QPen return a default constructed QPen
*/ */
QPen QETXML::penFromXml(const QDomElement &element) QPen QETXML::penFromXml(const QDomElement &element)
{ {
QPen pen; QPen pen;
if (!(!element.isNull() && element.tagName() == "pen")) if (!(!element.isNull() && element.tagName() == "pen"))
{ {
@@ -82,17 +83,19 @@ QPen QETXML::penFromXml(const QDomElement &element)
pen.setColor(QColor(element.attribute("color", "#000000"))); pen.setColor(QColor(element.attribute("color", "#000000")));
pen.setWidthF(element.attribute("widthF", "1").toDouble()); pen.setWidthF(element.attribute("widthF", "1").toDouble());
return pen; return pen;
} }
/** /**
* @brief QETXML::brushToXml @brief QETXML::brushToXml
* Write attribute of a QBrush in xml element Write attribute of a QBrush in xml element
* @param parent_document : parent document for create the QDomElement @param parent_document : parent document for create the QDomElement
* @param brush : the brush to store @param brush : the brush to store
* @return A QDomElement with the attribute stored. The tagName of QDomeElement is "brush". @return A QDomElement with the attribute stored.
*/ The tagName of QDomeElement is "brush".
QDomElement QETXML::brushToXml(QDomDocument &parent_document, const QBrush& brush) */
QDomElement QETXML::brushToXml(QDomDocument &parent_document,
const QBrush& brush)
{ {
QDomElement element = parent_document.createElement("brush"); QDomElement element = parent_document.createElement("brush");
@@ -114,7 +117,7 @@ QDomElement QETXML::brushToXml(QDomDocument &parent_document, const QBrush& brus
case Qt::BDiagPattern : style = "BDiagPattern"; break; case Qt::BDiagPattern : style = "BDiagPattern"; break;
case Qt::FDiagPattern : style = "FDiagPattern"; break; case Qt::FDiagPattern : style = "FDiagPattern"; break;
case Qt::DiagCrossPattern : style = "DiagCrossPattern"; break; case Qt::DiagCrossPattern : style = "DiagCrossPattern"; break;
default : style = "Unknown"; break; default : style = "Unknown"; break;
} }
element.setAttribute("style", style); element.setAttribute("style", style);
@@ -123,12 +126,12 @@ QDomElement QETXML::brushToXml(QDomDocument &parent_document, const QBrush& brus
} }
/** /**
* @brief QETXML::brushFromXml @brief QETXML::brushFromXml
* Build a QBrush from a xml description Build a QBrush from a xml description
* @param element, the QDomElement that describe the pen @param element, the QDomElement that describe the pen
* @return the created brush. If @element is null or tagName isn't "brush" @return the created brush. If @element is null or tagName isn't "brush"
* return a default constructed QBrush return a default constructed QBrush
*/ */
QBrush QETXML::brushFromXml(const QDomElement &element) QBrush QETXML::brushFromXml(const QDomElement &element)
{ {
QBrush brush; QBrush brush;
@@ -158,15 +161,20 @@ QBrush QETXML::brushFromXml(const QDomElement &element)
} }
/** /**
* @brief QETXML::fileSystemDirToXmlCollectionDir @brief QETXML::fileSystemDirToXmlCollectionDir
* @param document : owner document of returned QDomElement, use to create the QDomElement. @param document : owner document of returned QDomElement,
* @param dir : file system direcory to convert to QDomElement directory use to create the QDomElement.
* @param rename : by default the attribute "name" of the returned QDomElement is the same name of @dir @param dir : file system direcory to convert to QDomElement directory
* but we can override itwith @rename @param rename : by default the attribute "name" of the returned
* @return A file system directory converted to a QDomElement directory ready to be inserted into a XmlElementCollection. QDomElement is the same name of @dir
* If the QDomElement can't be created, return a null QDomElement. but we can override itwith @rename
*/ @return A file system directory converted to a QDomElement directory
QDomElement QETXML::fileSystemDirToXmlCollectionDir(QDomDocument &document, const QDir &dir, const QString& rename) ready to be inserted into a XmlElementCollection.
If the QDomElement can't be created, return a null QDomElement.
*/
QDomElement QETXML::fileSystemDirToXmlCollectionDir(QDomDocument &document,
const QDir &dir,
const QString& rename)
{ {
if (!dir.exists()) return QDomElement(); if (!dir.exists()) return QDomElement();
@@ -175,7 +183,9 @@ QDomElement QETXML::fileSystemDirToXmlCollectionDir(QDomDocument &document, cons
//Get the traduction of this directory //Get the traduction of this directory
QFile qet_dir(dir.filePath("qet_directory")); QFile qet_dir(dir.filePath("qet_directory"));
if (qet_dir.exists() && qet_dir.open(QIODevice::ReadOnly | QIODevice::Text)) if (qet_dir.exists() && qet_dir.open(
QIODevice::ReadOnly
| QIODevice::Text))
{ {
//Get the content of the file //Get the content of the file
QDomDocument trad_document; QDomDocument trad_document;
@@ -196,15 +206,21 @@ QDomElement QETXML::fileSystemDirToXmlCollectionDir(QDomDocument &document, cons
} }
/** /**
* @brief QETXML::fileSystemElementToXmlCollectionElement @brief QETXML::fileSystemElementToXmlCollectionElement
* @param document : owner document of returned QDomElement, use to create the QDomElement. @param document : owner document of returned QDomElement,
* @param file : file system element file to convert to QDomElement; use to create the QDomElement.
* @param rename : by default the attribute "name" of the returned QDomElement is the same name of @file @param file : file system element file to convert to QDomElement;
* but we can override itwith @rename @param rename : by default the attribute "name" of
* @return A file system element converted to a QDomElement ready to be inserted into a XmlElementCollection the returned QDomElement is the same name of @file
* If the QDomElement can't be created, return a null QDomElement but we can override itwith @rename
*/ @return A file system element converted to a QDomElement
QDomElement QETXML::fileSystemElementToXmlCollectionElement(QDomDocument &document, QFile &file, const QString& rename) ready to be inserted into a XmlElementCollection
If the QDomElement can't be created, return a null QDomElement
*/
QDomElement QETXML::fileSystemElementToXmlCollectionElement(
QDomDocument &document,
QFile &file,
const QString& rename)
{ {
if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
@@ -229,26 +245,32 @@ QDomElement QETXML::fileSystemElementToXmlCollectionElement(QDomDocument &docume
} }
/** /**
* @brief QETXML::writeXmlFile @brief QETXML::writeXmlFile
* Export an XML document to an UTF-8 text file indented with 4 spaces, with LF end of lines and no BOM. Export an XML document to an UTF-8 text file indented with 4 spaces,
* @param xml_document : An XML document to be exported with LF end of lines and no BOM.
* @param file_path : Path to the file to be written @param xml_document : An XML document to be exported
* @param error_message : If non-zero, will contain an error message explaining what happened when this function returns false. @param file_path : Path to the file to be written
* @return false if an error occurred, true otherwise @param error_message : If non-zero, will contain an error message
*/ explaining what happened when this function returns false.
bool QETXML::writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message) @return false if an error occurred, true otherwise
*/
bool QETXML::writeXmlFile(const QDomDocument &xml_document,
const QString &file_path,
QString *error_message)
{ {
QFile file(file_path); QFile file(file_path);
// Note: we do not set QIODevice::Text to avoid generating CRLF end of lines // Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
bool file_opening = file.open(QIODevice::WriteOnly); bool file_opening = file.open(QIODevice::WriteOnly);
if (!file_opening) if (!file_opening)
{ {
if (error_message) if (error_message)
{ {
*error_message = QString(QObject::tr("Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.", *error_message = QString(
"error message when attempting to write an XML file") QObject::tr(
).arg(file_path).arg(file.error()); "Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.",
"error message when attempting to write an XML file")
).arg(file_path).arg(file.error());
} }
return(false); return(false);
} }
@@ -263,14 +285,17 @@ bool QETXML::writeXmlFile(const QDomDocument &xml_document, const QString &file_
} }
/** /**
* @brief QETXML::textToDomElement @brief QETXML::textToDomElement
* Return a QDomElement, created from @document, with tag name @tag_name and text @value. Return a QDomElement, created from @document,
* @param document with tag name @tag_name and text @value.
* @param tag_name @param document
* @param value @param tag_name
* @return @param value
*/ @return a QDomElement, created from @document
QDomElement QETXML::textToDomElement(QDomDocument &document, const QString& tag_name, const QString& value) */
QDomElement QETXML::textToDomElement(QDomDocument &document,
const QString& tag_name,
const QString& value)
{ {
QDomElement element = document.createElement(tag_name); QDomElement element = document.createElement(tag_name);
QDomText text = document.createTextNode(value); QDomText text = document.createTextNode(value);
@@ -280,15 +305,19 @@ QDomElement QETXML::textToDomElement(QDomDocument &document, const QString& tag_
} }
/** /**
* @brief QETXML::directChild @brief QETXML::directChild
* @param element @param element
* @param tag_name @param tag_name
* @return All direct child of @element with the tag name @tag_name @return All direct child of @element with the tag name @tag_name
*/ */
QVector<QDomElement> QETXML::directChild(const QDomElement &element, const QString &tag_name) QVector<QDomElement> QETXML::directChild(const QDomElement &element,
const QString &tag_name)
{ {
QVector<QDomElement> return_list; QVector<QDomElement> return_list;
for (QDomNode node = element.firstChild() ; !node.isNull() ; node = node.nextSibling()) for (
QDomNode node = element.firstChild() ;
!node.isNull() ;
node = node.nextSibling())
{ {
if (!node.isElement()) continue; if (!node.isElement()) continue;
QDomElement element = node.toElement(); QDomElement element = node.toElement();
@@ -300,25 +329,34 @@ QVector<QDomElement> QETXML::directChild(const QDomElement &element, const QStri
} }
/** /**
* @brief QETXML::subChild @brief QETXML::subChild
* @param element @param element
* @param parent_tag_name @param parent_tag_name
* @param children_tag_name @param children_tag_name
* @return When given an xml dom element @element, @return When given an xml dom element @element,
* returns a vector of all children dom_elements tagged @children_tag_name returns a vector of all children dom_elements tagged @children_tag_name
* nested in the parent dom elements tagged parent_tag_name, themselves children of the dom element @element. nested in the parent dom elements tagged parent_tag_name,
*/ themselves children of the dom element @element.
QVector<QDomElement> QETXML::subChild(const QDomElement &element, const QString parent_tag_name, const QString &children_tag_name) */
QVector<QDomElement> QETXML::subChild(const QDomElement &element,
const QString parent_tag_name,
const QString &children_tag_name)
{ {
QVector<QDomElement> return_list; QVector<QDomElement> return_list;
for (QDomNode child = element.firstChild() ; !child.isNull() ; child = child.nextSibling()) for (
QDomNode child = element.firstChild() ;
!child.isNull() ;
child = child.nextSibling())
{ {
QDomElement parents = child.toElement(); QDomElement parents = child.toElement();
if (parents.isNull() || parents.tagName() != parent_tag_name) if (parents.isNull() || parents.tagName() != parent_tag_name)
continue; continue;
for (QDomNode node_children = parents.firstChild() ; !node_children.isNull() ; node_children = node_children.nextSibling()) for (
QDomNode node_children = parents.firstChild() ;
!node_children.isNull() ;
node_children = node_children.nextSibling())
{ {
QDomElement n_children = node_children.toElement(); QDomElement n_children = node_children.toElement();
if (!n_children.isNull() && n_children.tagName() == children_tag_name) if (!n_children.isNull() && n_children.tagName() == children_tag_name)
@@ -330,29 +368,34 @@ QVector<QDomElement> QETXML::subChild(const QDomElement &element, const QString
} }
/** /**
* @brief QETXML::marginsToXml @brief QETXML::marginsToXml
* Save a QMargins to xml. the xml tag name is 'margins' Save a QMargins to xml. the xml tag name is 'margins'
* @param parent_document @param parent_document
* @param margins @param margins
* @return @return
*/ */
QDomElement QETXML::marginsToXml(QDomDocument &parent_document, const QMargins &margins) QDomElement QETXML::marginsToXml(QDomDocument &parent_document,
const QMargins &margins)
{ {
auto dom_ = parent_document.createElement("margins"); auto dom_ = parent_document.createElement("margins");
auto text_ = parent_document.createTextNode(QString::number(margins.left()) + QString(";") + auto text_ = parent_document.createTextNode(
QString::number(margins.top()) + QString(";") + QString::number(margins.left())
QString::number(margins.right()) + QString(";") + + QString(";")
QString::number(margins.bottom())); + QString::number(margins.top())
+ QString(";")
+ QString::number(margins.right())
+ QString(";")
+ QString::number(margins.bottom()));
dom_.appendChild(text_); dom_.appendChild(text_);
return dom_; return dom_;
} }
/** /**
* @brief QETXML::marginsFromXml @brief QETXML::marginsFromXml
* @param element @param element
* @return a QMargins from an xml description. @return a QMargins from an xml description.
* The tag name must ne 'margins' The tag name must ne 'margins'
*/ */
QMargins QETXML::marginsFromXml(const QDomElement &element) QMargins QETXML::marginsFromXml(const QDomElement &element)
{ {
if (element.tagName() != "margins") { if (element.tagName() != "margins") {
@@ -361,22 +404,35 @@ QMargins QETXML::marginsFromXml(const QDomElement &element)
auto margins_ = element.text().split(";"); auto margins_ = element.text().split(";");
if (margins_.size() == 4) { if (margins_.size() == 4) {
return QMargins(margins_.at(0).toInt(), margins_.at(1).toInt(), margins_.at(2).toInt(), margins_.at(3).toInt()); return QMargins(
margins_.at(0).toInt(),
margins_.at(1).toInt(),
margins_.at(2).toInt(),
margins_.at(3).toInt());
} else { } else {
return QMargins(); return QMargins();
} }
} }
/** /**
* @brief QETXML::modelHeaderDataToXml @brief QETXML::modelHeaderDataToXml
* Save to xml element all header data specified by @horizontal_section_role and @vertical_section_role Save to xml element all header data specified
* @param parent_document by @horizontal_section_role and @vertical_section_role
* @param model @param parent_document
* @param horizontal_section_role : key as header section and value as list of roles to save in xml @param model
* @param vertical_section_role :key as header section and value as list of roles to save in xml @param horizontal_section_role : key as header section and value
* @return as list of roles to save in xml
*/ @param vertical_section_role :key as header section and value
QDomElement QETXML::modelHeaderDataToXml(QDomDocument &parent_document, const QAbstractItemModel *model, QHash<int, QList<int>> horizontal_section_role, QHash<int, QList<int>> vertical_section_role) as list of roles to save in xml
@return
*/
QDomElement QETXML::modelHeaderDataToXml(
QDomDocument &parent_document,
const QAbstractItemModel *model,
QHash<int,
QList<int>> horizontal_section_role,
QHash<int,
QList<int>> vertical_section_role)
{ {
auto dom_element = parent_document.createElement("header_data"); auto dom_element = parent_document.createElement("header_data");
@@ -401,7 +457,11 @@ QDomElement QETXML::modelHeaderDataToXml(QDomDocument &parent_document, const QA
dom_data.setAttribute("role", meta_enum_role.valueToKey(role)); dom_data.setAttribute("role", meta_enum_role.valueToKey(role));
auto text_node = parent_document.createTextNode(""); auto text_node = parent_document.createTextNode("");
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole || role == Qt::StatusTipRole || role == Qt::WhatsThisRole) if (role == Qt::DisplayRole
|| role == Qt::EditRole
|| role == Qt::ToolTipRole
|| role == Qt::StatusTipRole
|| role == Qt::WhatsThisRole)
{ {
text_node.setData(variant.toString()); text_node.setData(variant.toString());
} }
@@ -433,12 +493,13 @@ QDomElement QETXML::modelHeaderDataToXml(QDomDocument &parent_document, const QA
} }
/** /**
* @brief QETXML::modelHeaderDataFromXml @brief QETXML::modelHeaderDataFromXml
* Restore from xml modele header data Restore from xml modele header data
* @param element @param element
* @param model @param model
*/ */
void QETXML::modelHeaderDataFromXml(const QDomElement &element, QAbstractItemModel *model) void QETXML::modelHeaderDataFromXml(const QDomElement &element,
QAbstractItemModel *model)
{ {
if (element.tagName() != "header_data") if (element.tagName() != "header_data")
return; return;
@@ -449,12 +510,25 @@ void QETXML::modelHeaderDataFromXml(const QDomElement &element, QAbstractItemMod
for (auto child : QETXML::directChild(element, "data")) for (auto child : QETXML::directChild(element, "data"))
{ {
auto section_ = child.attribute("section", "-1").toInt(); auto section_ = child.attribute("section", "-1").toInt();
auto orientation_ = Qt::Orientation(meta_enum_orientation.keyToValue(child.attribute("orientation", "Horizontal").toStdString().data())); auto orientation_ = Qt::Orientation(
auto role_ = meta_enum_role.keyToValue(child.attribute("role", "DisplayRole").toStdString().data()); meta_enum_orientation.keyToValue(
child.attribute(
"orientation",
"Horizontal"
).toStdString().data()));
auto role_ = meta_enum_role.keyToValue(
child.attribute(
"role",
"DisplayRole"
).toStdString().data());
auto text_ = child.text(); auto text_ = child.text();
QVariant data_; QVariant data_;
if (role_ == Qt::DisplayRole || role_ == Qt::EditRole || role_ == Qt::ToolTipRole || role_ == Qt::StatusTipRole || role_ == Qt::WhatsThisRole) { if (role_ == Qt::DisplayRole
|| role_ == Qt::EditRole
|| role_ == Qt::ToolTipRole
|| role_ == Qt::StatusTipRole
|| role_ == Qt::WhatsThisRole) {
data_ = text_; data_ = text_;
} }
else if (role_ == Qt::FontRole) else if (role_ == Qt::FontRole)

View File

@@ -31,27 +31,48 @@ class QAbstractItemModel;
*/ */
namespace QETXML namespace QETXML
{ {
QDomElement penToXml(QDomDocument &parent_document, const QPen& pen); QDomElement penToXml(QDomDocument &parent_document, const QPen& pen);
QPen penFromXml (const QDomElement &element); QPen penFromXml (const QDomElement &element);
QDomElement brushToXml (QDomDocument &parent_document, const QBrush& brush); QDomElement brushToXml (QDomDocument &parent_document,
const QBrush& brush);
QBrush brushFromXml (const QDomElement &element); QBrush brushFromXml (const QDomElement &element);
QDomElement fileSystemDirToXmlCollectionDir (QDomDocument &document, const QDir &dir, const QString& rename = QString()); QDomElement fileSystemDirToXmlCollectionDir (
QDomElement fileSystemElementToXmlCollectionElement (QDomDocument &document, QFile &file, const QString& rename = QString()); QDomDocument &document,
const QDir &dir,
const QString& rename = QString());
QDomElement fileSystemElementToXmlCollectionElement (
QDomDocument &document,
QFile &file,
const QString& rename = QString());
bool writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message = nullptr); bool writeXmlFile(const QDomDocument &xml_document,
const QString &file_path,
QString *error_message = nullptr);
QDomElement textToDomElement (QDomDocument &document, const QString& tag_name, const QString& value); QDomElement textToDomElement (QDomDocument &document,
const QString& tag_name,
const QString& value);
QVector <QDomElement> directChild(const QDomElement &element, const QString &tag_name); QVector <QDomElement> directChild(const QDomElement &element,
QVector <QDomElement> subChild(const QDomElement &element, const QString parent_tag_name, const QString &children_tag_name); const QString &tag_name);
QVector <QDomElement> subChild(const QDomElement &element,
const QString parent_tag_name,
const QString &children_tag_name);
QDomElement marginsToXml (QDomDocument &parent_document, const QMargins &margins); QDomElement marginsToXml (QDomDocument &parent_document,
const QMargins &margins);
QMargins marginsFromXml(const QDomElement &element); QMargins marginsFromXml(const QDomElement &element);
QDomElement modelHeaderDataToXml(QDomDocument &parent_document, const QAbstractItemModel *model, QHash<int, QList<int>> horizontal_section_role, QHash<int, QList<int>> vertical_section_role); QDomElement modelHeaderDataToXml(QDomDocument &parent_document,
void modelHeaderDataFromXml(const QDomElement &element, QAbstractItemModel *model); const QAbstractItemModel *model,
QHash<int,
QList<int>> horizontal_section_role,
QHash<int,
QList<int>> vertical_section_role);
void modelHeaderDataFromXml(const QDomElement &element,
QAbstractItemModel *model);
} }
#endif // QETXML_H #endif // QETXML_H