fixed edge display for volume cells
[engrid-github.git] / src / libengrid / xmlhandler.cpp
blob808b010561b1eb1fad1f2dd56cef4aa710232a32
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "xmlhandler.h"
23 #include <QFile>
24 #include <QtDebug>
25 #include <QDir>
26 #include <QString>
27 #include <QMessageBox>
29 #include "engrid.h"
31 #include <iostream>
32 using namespace std;
34 XmlHandler::XmlHandler(QString tagName, QObject *parent)
35 : QObject(parent) {
36 m_parent = parent;
38 // initialise XML document
39 m_TagName = tagName;
40 QDomElement root = m_XmlDoc.createElement(m_TagName);
41 m_XmlDoc.appendChild(root);
44 bool XmlHandler::openXml(QString file_name) {
45 QFile xml_file(file_name);
46 if (!xml_file.open(QIODevice::ReadOnly)) {
47 QString error_message = "Failed to open xml_file " + xml_file.fileName();
48 error_message += QString("\n") + "QDir::current()=" + QDir::current().absolutePath();
49 error_message += QString("\n") + "QDir::currentPath()=" + QDir::currentPath();
51 qWarning() << error_message;
52 return(false);
54 if (!m_XmlDoc.setContent(&xml_file)) {
55 qWarning() << tr("Error reading XML file:\n").arg(file_name) << "\n setContent failed.";
56 return(false);
58 xml_file.close();
60 // initialize m_DomNode
61 resetToTopNode();
63 return(true);
66 bool XmlHandler::saveXml(QString file_name) {
67 QString buffer = m_XmlDoc.toString(2);
68 // QString buffer = m_XmlDoc.toString(0);
69 QFile xml_file(file_name);
70 xml_file.open(QIODevice::WriteOnly | QIODevice::Text);
71 QTextStream f(&xml_file);
72 f << buffer << endl;
73 xml_file.close();
74 return(true);
77 QString XmlHandler::getXmlSection(QString name) {
78 QStringList tags = name.toLower().split(tr("/"), QString::SkipEmptyParts);
79 QDomElement element = m_XmlDoc.documentElement();
80 bool found = true;
81 QString section_text = tr("");
82 try {
83 foreach(QString tag, tags) {
84 QDomNodeList nodes = element.elementsByTagName(tag);
85 if (nodes.size() > 1) {
86 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
88 if (nodes.size() == 0) {
89 found = false;
90 break;
92 if (!nodes.at(0).isElement()) {
93 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
95 element = nodes.at(0).toElement();
97 } catch (Error err) {
98 err.display();
100 if (found) {
101 section_text = element.text();
103 return section_text;
106 void XmlHandler::setXmlSection(QString name, QString contents) {
107 QStringList tags = name.toLower().split(tr("/"), QString::SkipEmptyParts);
108 QDomElement element = m_XmlDoc.documentElement();
109 try {
110 foreach(QString tag, tags) {
111 QDomNodeList nodes = element.elementsByTagName(tag);
112 if (nodes.size() > 1) {
113 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
115 if (nodes.size() == 0) {
116 QDomElement new_element = m_XmlDoc.createElement(tag);
117 element.appendChild(new_element);
118 element = new_element;
119 } else if (!nodes.at(0).isElement()) {
120 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
121 } else {
122 element = nodes.at(0).toElement();
125 while (element.hasChildNodes()) {
126 element.removeChild(element.firstChild());
128 QDomText text_node = m_XmlDoc.createTextNode(contents);
129 element.appendChild(text_node);
130 } catch (Error err) {
131 err.display();
135 QStringList XmlHandler::allKeys() {
136 QStringList ret;
137 QDomNode dom_node;
138 dom_node = m_XmlDoc.firstChild();
139 parseNode(dom_node, ret, dom_node.nodeName());
140 return(ret);
143 QStringList XmlHandler::childGroups() {
144 QStringList ret;
146 for (QDomNode sub_node = m_DomNode.firstChild(); !sub_node.isNull(); sub_node = sub_node.nextSibling()) {
147 if (sub_node.nodeType() == QDomNode::ElementNode) {
148 ret << sub_node.nodeName();
152 return(ret);
155 QStringList XmlHandler::childKeys() {
156 QStringList ret;
157 qDebug() << "m_DomNode.nodeName()=" << m_DomNode.nodeName();
158 QString output = parseNode(m_DomNode, ret, m_DomNode.nodeName());
159 qDebug() << "output=" << output;
160 qDebug() << "ret=" << ret;
161 return(ret);
164 QString XmlHandler::group(bool absolute) {
165 if (!absolute) {
166 return m_DomNode.nodeName();
167 } else {
168 QString section;
169 QString stop_node = m_XmlDoc.firstChild().nodeName();
170 QDomNode parent_node = m_DomNode;
171 while (!parent_node.isNull() && parent_node.nodeName() != stop_node) {
172 section = parent_node.nodeName() + tr("/") + section;
173 parent_node = parent_node.parentNode();
175 return section;
179 void XmlHandler::setGroup(const QString & prefix) {
180 resetToTopNode();
181 beginGroup(prefix);
184 void XmlHandler::beginGroup(const QString & prefix) {
185 QStringList tag_list = prefix.split(tr("/"));
186 foreach(QString tag, tag_list) {
187 if (!tag.isEmpty()) {
188 QDomElement dom_element = m_DomNode.toElement();
189 QDomNodeList dom_node_list = dom_element.elementsByTagName(tag);
190 m_DomNode = dom_node_list.at(0);
195 void XmlHandler::endGroup() {
196 if (!m_DomNode.parentNode().isNull()) m_DomNode = m_DomNode.parentNode();
199 QString XmlHandler::parseNode(const QDomNode& dom_node, QStringList& string_list, QString stop_node) {
200 QString section;
202 if (dom_node.nodeType() == QDomNode::TextNode) {
203 QDomNode parent_node = dom_node.parentNode();
204 while (!parent_node.isNull() && parent_node.nodeName() != stop_node) {
205 section = parent_node.nodeName() + tr("/") + section;
206 parent_node = parent_node.parentNode();
208 string_list << section;
211 for (QDomNode sub_node = dom_node.firstChild(); !sub_node.isNull(); sub_node = sub_node.nextSibling()) {
212 parseNode(sub_node, string_list, stop_node);
215 // qDebug() << "section=" << section;
216 return(section);
219 void XmlHandler::resetToTopNode() {
220 m_DomNode = m_XmlDoc.firstChild();
223 void XmlHandler::resetXmlDoc() {
224 m_XmlDoc.clear();
225 QDomElement root = m_XmlDoc.createElement(m_TagName);
226 m_XmlDoc.appendChild(root);