Skip to content
Permalink
Browse files

[diagrams] save attribute names instead of indices (fix #7914)

  • Loading branch information
m-kuhn committed May 28, 2013
1 parent a7b92fd commit cad339d3cfde3c89e21d67d25222c6e26655d502
@@ -34,8 +34,8 @@ struct QgsDiagramLayerSettings
int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
};

//diagram settings for rendering
@@ -88,8 +88,8 @@ class QgsDiagramSettings
//! Scale diagrams smaller than mMinimumSize to mMinimumSize
double minimumSize;

void readXML( const QDomElement& elem );
void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
};

//additional diagram settings for interpolated size rendering
@@ -134,8 +134,8 @@ class QgsDiagramRendererV2
/**Returns list with all diagram settings in the renderer*/
virtual QList<QgsDiagramSettings> diagramSettings() const = 0;

virtual void readXML( const QDomElement& elem ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0;
virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;

protected:

@@ -156,8 +156,8 @@ class QgsDiagramRendererV2
static int dpiPaintDevice( const QPainter* );

//read / write diagram
void _readXML( const QDomElement& elem );
void _writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
};

/**Renders the diagrams for all features with the same settings*/
@@ -179,8 +179,8 @@ class QgsSingleCategoryDiagramRenderer : QgsDiagramRendererV2

QList<QgsDiagramSettings> diagramSettings() const;

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;

protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
@@ -222,8 +222,8 @@ class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2
int classificationAttribute() const;
void setClassificationAttribute( int index );

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;

protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
@@ -13,6 +13,7 @@
* *
***************************************************************************/
#include "qgsdiagramrendererv2.h"
#include "qgsvectorlayer.h"
#include "diagram/qgstextdiagram.h"
#include "diagram/qgspiediagram.h"
#include "diagram/qgshistogramdiagram.h"
@@ -21,8 +22,10 @@
#include <QPainter>


void QgsDiagramLayerSettings::readXML( const QDomElement& elem )
void QgsDiagramLayerSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{
Q_UNUSED( layer )

placement = ( Placement )elem.attribute( "placement" ).toInt();
placementFlags = ( LinePlacementFlags )elem.attribute( "linePlacementFlags" ).toInt();
priority = elem.attribute( "priority" ).toInt();
@@ -32,8 +35,10 @@ void QgsDiagramLayerSettings::readXML( const QDomElement& elem )
yPosColumn = elem.attribute( "yPosColumn" ).toInt();
}

void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& doc ) const
void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
{
Q_UNUSED( layer )

QDomElement diagramLayerElem = doc.createElement( "DiagramLayerSettings" );
diagramLayerElem.setAttribute( "placement", placement );
diagramLayerElem.setAttribute( "linePlacementFlags", placementFlags );
@@ -45,7 +50,7 @@ void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& do
layerElem.appendChild( diagramLayerElem );
}

void QgsDiagramSettings::readXML( const QDomElement& elem )
void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{
font.fromString( elem.attribute( "font" ) );
backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) );
@@ -117,26 +122,44 @@ void QgsDiagramSettings::readXML( const QDomElement& elem )

//colors
categoryColors.clear();
QStringList colorList = elem.attribute( "colors" ).split( "/" );
QStringList::const_iterator colorIt = colorList.constBegin();
for ( ; colorIt != colorList.constEnd(); ++colorIt )
QDomNodeList attributes = elem.elementsByTagName( "attribute" );

if ( attributes.length() > 0 )
{
QColor newColor( *colorIt );
newColor.setAlpha( 255 - transparency );
categoryColors.append( QColor( newColor ) );
for ( uint i = 0; i < attributes.length(); i++ )
{
QDomElement attrElem = attributes.at( i ).toElement();
QColor newColor( attrElem.attribute( "color" ) );
newColor.setAlpha( 255 - transparency );
categoryColors.append( newColor );
categoryIndices.append( layer->fieldNameIndex( attrElem.attribute( "field" ) ) );
}
}

//attribute indices
categoryIndices.clear();
QStringList catList = elem.attribute( "categories" ).split( "/" );
QStringList::const_iterator catIt = catList.constBegin();
for ( ; catIt != catList.constEnd(); ++catIt )
else
{
categoryIndices.append( catIt->toInt() );
// Restore old format attributes and colors

QStringList colorList = elem.attribute( "colors" ).split( "/" );
QStringList::const_iterator colorIt = colorList.constBegin();
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
QColor newColor( *colorIt );
newColor.setAlpha( 255 - transparency );
categoryColors.append( QColor( newColor ) );
}

//attribute indices
categoryIndices.clear();
QStringList catList = elem.attribute( "categories" ).split( "/" );
QStringList::const_iterator catIt = catList.constBegin();
for ( ; catIt != catList.constEnd(); ++catIt )
{
categoryIndices.append( catIt->toInt() );
}
}
}

void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc ) const
void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
{
QDomElement categoryElem = doc.createElement( "DiagramCategory" );
categoryElem.setAttribute( "font", font.toString() );
@@ -209,26 +232,15 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc
categoryElem.setAttribute( "angleOffset", QString::number( angleOffset ) );

QString colors;
for ( int i = 0; i < categoryColors.size(); ++i )
int nCats = qMin( categoryColors.size(), categoryIndices.size() );
for ( int i = 0; i < nCats; ++i )
{
if ( i > 0 )
{
colors.append( "/" );
}
colors.append( categoryColors.at( i ).name() );
}
categoryElem.setAttribute( "colors", colors );
QDomElement attributeElem = doc.createElement( "attribute" );

QString categories;
for ( int i = 0; i < categoryIndices.size(); ++i )
{
if ( i > 0 )
{
categories.append( "/" );
}
categories.append( QString::number( categoryIndices.at( i ) ) );
attributeElem.setAttribute( "field", layer->pendingFields().at( categoryIndices.at( i ) ).name() );
attributeElem.setAttribute( "color", categoryColors.at( i ).name() );
categoryElem.appendChild( attributeElem );
}
categoryElem.setAttribute( "categories", categories );

rendererElem.appendChild( categoryElem );
}
@@ -305,8 +317,10 @@ int QgsDiagramRendererV2::dpiPaintDevice( const QPainter* painter )
return -1;
}

void QgsDiagramRendererV2::_readXML( const QDomElement& elem )
void QgsDiagramRendererV2::_readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{
Q_UNUSED( layer )

delete mDiagram;
QString diagramType = elem.attribute( "diagramType" );
if ( diagramType == "Pie" )
@@ -327,9 +341,11 @@ void QgsDiagramRendererV2::_readXML( const QDomElement& elem )
}
}

void QgsDiagramRendererV2::_writeXML( QDomElement& rendererElem, QDomDocument& doc ) const
void QgsDiagramRendererV2::_writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
{
Q_UNUSED( doc );
Q_UNUSED( layer )

if ( mDiagram )
{
rendererElem.setAttribute( "diagramType", mDiagram->diagramName() );
@@ -363,23 +379,23 @@ QList<QgsDiagramSettings> QgsSingleCategoryDiagramRenderer::diagramSettings() co
return settingsList;
}

void QgsSingleCategoryDiagramRenderer::readXML( const QDomElement& elem )
void QgsSingleCategoryDiagramRenderer::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{
QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" );
if ( categoryElem.isNull() )
{
return;
}

mSettings.readXML( categoryElem );
_readXML( elem );
mSettings.readXML( categoryElem, layer );
_readXML( elem, layer );
}

void QgsSingleCategoryDiagramRenderer::writeXML( QDomElement& layerElem, QDomDocument& doc ) const
void QgsSingleCategoryDiagramRenderer::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
{
QDomElement rendererElem = doc.createElement( "SingleCategoryDiagramRenderer" );
mSettings.writeXML( rendererElem, doc );
_writeXML( rendererElem, doc );
mSettings.writeXML( rendererElem, doc, layer );
_writeXML( rendererElem, doc , layer );
layerElem.appendChild( rendererElem );
}

@@ -421,7 +437,7 @@ QSizeF QgsLinearlyInterpolatedDiagramRenderer::diagramSize( const QgsAttributes&
return mDiagram->diagramSize( attributes, c, mSettings, mInterpolationSettings );
}

void QgsLinearlyInterpolatedDiagramRenderer::readXML( const QDomElement& elem )
void QgsLinearlyInterpolatedDiagramRenderer::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{
mInterpolationSettings.lowerValue = elem.attribute( "lowerValue" ).toDouble();
mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble();
@@ -433,12 +449,12 @@ void QgsLinearlyInterpolatedDiagramRenderer::readXML( const QDomElement& elem )
QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" );
if ( !settingsElem.isNull() )
{
mSettings.readXML( settingsElem );
mSettings.readXML( settingsElem, layer );
}
_readXML( elem );
_readXML( elem, layer );
}

void QgsLinearlyInterpolatedDiagramRenderer::writeXML( QDomElement& layerElem, QDomDocument& doc ) const
void QgsLinearlyInterpolatedDiagramRenderer::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
{
QDomElement rendererElem = doc.createElement( "LinearlyInterpolatedDiagramRenderer" );
rendererElem.setAttribute( "lowerValue", QString::number( mInterpolationSettings.lowerValue ) );
@@ -448,7 +464,7 @@ void QgsLinearlyInterpolatedDiagramRenderer::writeXML( QDomElement& layerElem, Q
rendererElem.setAttribute( "upperWidth", QString::number( mInterpolationSettings.upperSize.width() ) );
rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) );
rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute );
mSettings.writeXML( rendererElem, doc );
_writeXML( rendererElem, doc );
mSettings.writeXML( rendererElem, doc, layer );
_writeXML( rendererElem, doc, layer );
layerElem.appendChild( rendererElem );
}
@@ -32,6 +32,8 @@ class QDomElement;
class QgsPalGeometry;
class QgsCoordinateTransform;
class QgsMapToPixel;
class QgsVectorLayer;

namespace pal { class Layer; }

class CORE_EXPORT QgsDiagramLayerSettings
@@ -88,8 +90,8 @@ class CORE_EXPORT QgsDiagramLayerSettings
int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
};

//diagram settings for rendering
@@ -141,8 +143,8 @@ class CORE_EXPORT QgsDiagramSettings
//! Scale diagrams smaller than mMinimumSize to mMinimumSize
double minimumSize;

void readXML( const QDomElement& elem );
void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
};

//additional diagram settings for interpolated size rendering
@@ -181,8 +183,8 @@ class CORE_EXPORT QgsDiagramRendererV2
/**Returns list with all diagram settings in the renderer*/
virtual QList<QgsDiagramSettings> diagramSettings() const = 0;

virtual void readXML( const QDomElement& elem ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0;
virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;

protected:

@@ -203,8 +205,8 @@ class CORE_EXPORT QgsDiagramRendererV2
static int dpiPaintDevice( const QPainter* );

//read / write diagram
void _readXML( const QDomElement& elem );
void _writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;

/**Reference to the object that does the real diagram rendering*/
QgsDiagram* mDiagram;
@@ -225,8 +227,8 @@ class CORE_EXPORT QgsSingleCategoryDiagramRenderer : public QgsDiagramRendererV2

QList<QgsDiagramSettings> diagramSettings() const;

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;

protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
@@ -267,8 +269,8 @@ class CORE_EXPORT QgsLinearlyInterpolatedDiagramRenderer : public QgsDiagramRend
int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;

protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );

0 comments on commit cad339d

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