Skip to content
Permalink
Browse files

move XML flag reading to Xml utils

  • Loading branch information
3nids committed Oct 4, 2018
1 parent 0cd6ea4 commit 819aaee3828830e62649feaeee1a0b410ae90feb
Showing with 27 additions and 13 deletions.
  1. +4 −0 python/core/auto_generated/qgsxmlutils.sip.in
  2. +2 −12 src/core/qgsmaplayer.cpp
  3. +21 −1 src/core/qgsxmlutils.h
@@ -9,6 +9,9 @@






class QgsXmlUtils
{
%Docstring
@@ -67,6 +70,7 @@ Supported types are
%Docstring
Read a QVariant from a QDomElement.
%End

};


@@ -554,7 +554,7 @@ void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &doc
// save categories
QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
layerElement.setAttribute( QStringLiteral( "style_categories" ), categoriesKeys );
layerElement.setAttribute( QStringLiteral( "styleCategories" ), categoriesKeys );

if ( categories.testFlag( Rendering ) )
{
@@ -1060,17 +1060,7 @@ bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMe
}

// Get source categories
QgsMapLayer::StyleCategories sourceCategories = QgsMapLayer::AllStyleCategories;
QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
QString sourceCategoriesStr( myRoot.attribute( "style_categories", metaEnum.valueToKeys( static_cast<int>( QgsMapLayer::AllStyleCategories ) ) ) );
if ( metaEnum.isValid() )
{
bool ok = false;
const char *vs = sourceCategoriesStr.toUtf8().data();
int newValue = metaEnum.keysToValue( vs, &ok );
if ( ok )
sourceCategories = static_cast<QgsMapLayer::StyleCategories>( newValue );
}
QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );

//Test for matching geometry type on vector layers when applying, if geometry type is given in the style
if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) ) &&
@@ -16,14 +16,17 @@
#define QGSXMLUTILS_H

class QDomDocument;
class QDomElement;

class QgsRectangle;

#include <QDomElement>

#include "qgis_core.h"
#include "qgis.h"
#include "qgsunittypes.h"



/**
* \ingroup core
* Assorted helper methods for reading and writing chunks of XML
@@ -75,6 +78,23 @@ class CORE_EXPORT QgsXmlUtils
* Read a QVariant from a QDomElement.
*/
static QVariant readVariant( const QDomElement &element );

template<class T> static T readFlagAttribute( const QDomElement &element, const QString &attributeName, T defaultValue ) SIP_SKIP
{
T value = defaultValue;
// Get source categories
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
QString sourceCategoriesStr( element.attribute( attributeName, metaEnum.valueToKeys( static_cast<int>( defaultValue ) ) ) );
if ( metaEnum.isValid() )
{
bool ok = false;
const char *vs = sourceCategoriesStr.toUtf8().data();
int newValue = metaEnum.keysToValue( vs, &ok );
if ( ok )
value = static_cast<T>( newValue );
}
return value;
}
};


0 comments on commit 819aaee

Please sign in to comment.
You can’t perform that action at this time.