mirror of
https://github.com/altlinux/admc.git
synced 2025-03-17 18:50:13 +03:00
load/apply attributes via QDomElement
\instead of using attribute node directly this also adds creating attribute if it doesn't exist delete find_attribute_node()
This commit is contained in:
parent
e5d1560a80
commit
42f60de1b2
@ -18,38 +18,3 @@
|
||||
*/
|
||||
|
||||
#include "xml_edit.h"
|
||||
|
||||
#include <QStack>
|
||||
|
||||
// Look for attribute's node in the document by iterating
|
||||
// through all nodes and looking for and attribute with same name
|
||||
QDomNode find_attribute_node(const QDomDocument &doc, const QString &attribute_name) {
|
||||
QStack<QDomElement> elements_to_explore;
|
||||
const QDomElement top_element = doc.documentElement();
|
||||
elements_to_explore.push(top_element);
|
||||
|
||||
while (!elements_to_explore.isEmpty()) {
|
||||
const QDomElement element = elements_to_explore.pop();
|
||||
|
||||
QDomNode child = element.firstChild();
|
||||
while (!child.isNull()) {
|
||||
QDomElement child_as_element = child.toElement();
|
||||
const bool is_element = !child_as_element.isNull();
|
||||
|
||||
if (is_element) {
|
||||
elements_to_explore.push(child_as_element);
|
||||
}
|
||||
|
||||
// Check node's attributes
|
||||
const QDomNamedNodeMap attributes = child.attributes();
|
||||
const QDomNode attribute_node = attributes.namedItem(attribute_name);
|
||||
if (!attribute_node.isNull()) {
|
||||
return attribute_node;
|
||||
}
|
||||
|
||||
child = child.nextSibling();
|
||||
}
|
||||
}
|
||||
|
||||
return QDomNode();
|
||||
}
|
||||
|
@ -46,6 +46,4 @@ bool changed() const;\
|
||||
bool verify_input(QWidget *parent);\
|
||||
bool apply(QDomDocument *doc);
|
||||
|
||||
QDomNode find_attribute_node(const QDomDocument &doc, const QString &attribute_name);
|
||||
|
||||
#endif /* XML_EDIT_H */
|
||||
|
@ -24,6 +24,19 @@
|
||||
#include <QMessageBox>
|
||||
#include <QLabel>
|
||||
|
||||
QDomElement get_element_by_tag_name(const QDomDocument &doc, const QString &tag_name) {
|
||||
const QDomNodeList parents = doc.elementsByTagName(tag_name);
|
||||
const QDomNode parent_node = parents.at(0);
|
||||
const QDomElement parent_element = parent_node.toElement();
|
||||
|
||||
// NOTE: should never happen? as long as xml files are validated on load at least
|
||||
if (parent_element.isNull()) {
|
||||
printf("get_element_by_tag_name() failed to find element \"%s\"\n", qPrintable(tag_name));
|
||||
}
|
||||
|
||||
return parent_element;
|
||||
}
|
||||
|
||||
XmlStringEdit::XmlStringEdit(const XmlAttribute &attribute_arg)
|
||||
: attribute(attribute_arg) {
|
||||
edit = new QLineEdit();
|
||||
@ -36,9 +49,10 @@ XmlStringEdit::XmlStringEdit(const XmlAttribute &attribute_arg)
|
||||
}
|
||||
|
||||
void XmlStringEdit::load(const QDomDocument &doc) {
|
||||
const QDomNode attribute_node = find_attribute_node(doc, attribute.name());
|
||||
const QDomElement parent_element = get_element_by_tag_name(doc, attribute.parent_name());
|
||||
const QString value = parent_element.attribute(attribute.name(), QString());
|
||||
|
||||
original_value = attribute_node.nodeValue();
|
||||
original_value = value;
|
||||
|
||||
edit->blockSignals(true);
|
||||
edit->setText(original_value);
|
||||
@ -74,9 +88,10 @@ bool XmlStringEdit::apply(QDomDocument *doc) {
|
||||
return true;
|
||||
}
|
||||
|
||||
QDomNode attribute_node = find_attribute_node(*doc, attribute.name());
|
||||
const QString new_value = edit->text();
|
||||
attribute_node.setNodeValue(new_value);
|
||||
|
||||
QDomElement parent_element = get_element_by_tag_name(*doc, attribute.parent_name());
|
||||
parent_element.setAttribute(attribute.name(), new_value);
|
||||
|
||||
printf("apply %s: [%s]=>[%s]\n", qPrintable(attribute.name()), qPrintable(original_value), qPrintable(new_value));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user