# HG changeset patch # User Chris Cannam # Date 1557476540 -3600 # Node ID 9e846af73f3e6c140c7a15efc6c355a155da982b # Parent 4a6ca97ce783a4b06c554ccbbf7bdfa88d64ff2a Add setLayerProperties (from XML string) diff -r 4a6ca97ce783 -r 9e846af73f3e layer/LayerFactory.cpp --- a/layer/LayerFactory.cpp Fri May 10 08:44:32 2019 +0100 +++ b/layer/LayerFactory.cpp Fri May 10 09:22:20 2019 +0100 @@ -497,46 +497,54 @@ settings.beginGroup("LayerDefaults"); QString defaults = settings.value(getLayerTypeName(type), "").toString(); if (defaults == "") return; + setLayerProperties(layer, defaults); + settings.endGroup(); +} -// cerr << "defaults=\"" << defaults << "\"" << endl; +void +LayerFactory::setLayerProperties(Layer *layer, QString newXml) +{ + QDomDocument docOld, docNew; + QString oldXml = layer->toXmlString(); - QString xml = layer->toXmlString(); - QDomDocument docOld, docNew; - - if (docOld.setContent(xml, false) && - docNew.setContent(defaults, false)) { + if (!docOld.setContent(oldXml, false)) { + SVCERR << "LayerFactory::setLayerProperties: Failed to parse XML for existing layer properties! XML string is: " << oldXml << endl; + return; + } + + if (!docNew.setContent(newXml, false)) { + SVCERR << "LayerFactory::setLayerProperties: Failed to parse XML: " << newXml << endl; + return; + } - QXmlAttributes attrs; + QXmlAttributes attrs; - QDomElement layerElt = docNew.firstChildElement("layer"); - QDomNamedNodeMap attrNodes = layerElt.attributes(); + QDomElement layerElt = docNew.firstChildElement("layer"); + QDomNamedNodeMap attrNodes = layerElt.attributes(); - for (int i = 0; i < attrNodes.length(); ++i) { - QDomAttr attr = attrNodes.item(i).toAttr(); - if (attr.isNull()) continue; + for (int i = 0; i < attrNodes.length(); ++i) { + QDomAttr attr = attrNodes.item(i).toAttr(); + if (attr.isNull()) continue; // cerr << "append \"" << attr.name() // << "\" -> \"" << attr.value() << "\"" // << endl; - attrs.append(attr.name(), "", "", attr.value()); - } - - layerElt = docOld.firstChildElement("layer"); - attrNodes = layerElt.attributes(); - for (int i = 0; i < attrNodes.length(); ++i) { - QDomAttr attr = attrNodes.item(i).toAttr(); - if (attr.isNull()) continue; - if (attrs.value(attr.name()) == "") { + attrs.append(attr.name(), "", "", attr.value()); + } + + layerElt = docOld.firstChildElement("layer"); + attrNodes = layerElt.attributes(); + for (int i = 0; i < attrNodes.length(); ++i) { + QDomAttr attr = attrNodes.item(i).toAttr(); + if (attr.isNull()) continue; + if (attrs.value(attr.name()) == "") { // cerr << "append \"" << attr.name() // << "\" -> \"" << attr.value() << "\"" // << endl; - attrs.append(attr.name(), "", "", attr.value()); - } + attrs.append(attr.name(), "", "", attr.value()); } - - layer->setProperties(attrs); } - - settings.endGroup(); + + layer->setProperties(attrs); } LayerFactory::LayerType diff -r 4a6ca97ce783 -r 9e846af73f3e layer/LayerFactory.h --- a/layer/LayerFactory.h Fri May 10 08:44:32 2019 +0100 +++ b/layer/LayerFactory.h Fri May 10 09:22:20 2019 +0100 @@ -68,8 +68,20 @@ Layer *createLayer(LayerType type); + /** + * Set the default properties of a layer, from the XML string + * contained in the LayerDefaults settings group for the given + * layer type. Leave unchanged any properties not mentioned in the + * settings. + */ void setLayerDefaultProperties(LayerType type, Layer *layer); + /** + * Set the properties of a layer, from the XML string + * provided. Leave unchanged any properties not mentioned. + */ + void setLayerProperties(Layer *layer, QString xmlString); + QString getLayerPresentationName(LayerType type); bool isLayerSliceable(const Layer *);