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

This commit is contained in:
Matthias Kuhn 2013-05-28 12:03:40 +02:00
parent a7b92fd1c3
commit cad339d3cf
4 changed files with 96 additions and 78 deletions

View File

@ -34,8 +34,8 @@ struct QgsDiagramLayerSettings
int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined) 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) int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const; void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
}; };
//diagram settings for rendering //diagram settings for rendering
@ -88,8 +88,8 @@ class QgsDiagramSettings
//! Scale diagrams smaller than mMinimumSize to mMinimumSize //! Scale diagrams smaller than mMinimumSize to mMinimumSize
double minimumSize; double minimumSize;
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const; void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
}; };
//additional diagram settings for interpolated size rendering //additional diagram settings for interpolated size rendering
@ -134,8 +134,8 @@ class QgsDiagramRendererV2
/**Returns list with all diagram settings in the renderer*/ /**Returns list with all diagram settings in the renderer*/
virtual QList<QgsDiagramSettings> diagramSettings() const = 0; virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
virtual void readXML( const QDomElement& elem ) = 0; virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0; virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
protected: protected:
@ -156,8 +156,8 @@ class QgsDiagramRendererV2
static int dpiPaintDevice( const QPainter* ); static int dpiPaintDevice( const QPainter* );
//read / write diagram //read / write diagram
void _readXML( const QDomElement& elem ); void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void _writeXML( QDomElement& rendererElem, QDomDocument& doc ) const; void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
}; };
/**Renders the diagrams for all features with the same settings*/ /**Renders the diagrams for all features with the same settings*/
@ -179,8 +179,8 @@ class QgsSingleCategoryDiagramRenderer : QgsDiagramRendererV2
QList<QgsDiagramSettings> diagramSettings() const; QList<QgsDiagramSettings> diagramSettings() const;
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const; void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
protected: protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s ); bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
@ -222,8 +222,8 @@ class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2
int classificationAttribute() const; int classificationAttribute() const;
void setClassificationAttribute( int index ); void setClassificationAttribute( int index );
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const; void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
protected: protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s ); bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );

View File

@ -13,6 +13,7 @@
* * * *
***************************************************************************/ ***************************************************************************/
#include "qgsdiagramrendererv2.h" #include "qgsdiagramrendererv2.h"
#include "qgsvectorlayer.h"
#include "diagram/qgstextdiagram.h" #include "diagram/qgstextdiagram.h"
#include "diagram/qgspiediagram.h" #include "diagram/qgspiediagram.h"
#include "diagram/qgshistogramdiagram.h" #include "diagram/qgshistogramdiagram.h"
@ -21,8 +22,10 @@
#include <QPainter> #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(); placement = ( Placement )elem.attribute( "placement" ).toInt();
placementFlags = ( LinePlacementFlags )elem.attribute( "linePlacementFlags" ).toInt(); placementFlags = ( LinePlacementFlags )elem.attribute( "linePlacementFlags" ).toInt();
priority = elem.attribute( "priority" ).toInt(); priority = elem.attribute( "priority" ).toInt();
@ -32,8 +35,10 @@ void QgsDiagramLayerSettings::readXML( const QDomElement& elem )
yPosColumn = elem.attribute( "yPosColumn" ).toInt(); 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" ); QDomElement diagramLayerElem = doc.createElement( "DiagramLayerSettings" );
diagramLayerElem.setAttribute( "placement", placement ); diagramLayerElem.setAttribute( "placement", placement );
diagramLayerElem.setAttribute( "linePlacementFlags", placementFlags ); diagramLayerElem.setAttribute( "linePlacementFlags", placementFlags );
@ -45,7 +50,7 @@ void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& do
layerElem.appendChild( diagramLayerElem ); layerElem.appendChild( diagramLayerElem );
} }
void QgsDiagramSettings::readXML( const QDomElement& elem ) void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{ {
font.fromString( elem.attribute( "font" ) ); font.fromString( elem.attribute( "font" ) );
backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) ); backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) );
@ -117,26 +122,44 @@ void QgsDiagramSettings::readXML( const QDomElement& elem )
//colors //colors
categoryColors.clear(); categoryColors.clear();
QStringList colorList = elem.attribute( "colors" ).split( "/" ); QDomNodeList attributes = elem.elementsByTagName( "attribute" );
QStringList::const_iterator colorIt = colorList.constBegin();
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
QColor newColor( *colorIt );
newColor.setAlpha( 255 - transparency );
categoryColors.append( QColor( newColor ) );
}
//attribute indices if ( attributes.length() > 0 )
categoryIndices.clear();
QStringList catList = elem.attribute( "categories" ).split( "/" );
QStringList::const_iterator catIt = catList.constBegin();
for ( ; catIt != catList.constEnd(); ++catIt )
{ {
categoryIndices.append( catIt->toInt() ); 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" ) ) );
}
}
else
{
// 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" ); QDomElement categoryElem = doc.createElement( "DiagramCategory" );
categoryElem.setAttribute( "font", font.toString() ); categoryElem.setAttribute( "font", font.toString() );
@ -209,26 +232,15 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc
categoryElem.setAttribute( "angleOffset", QString::number( angleOffset ) ); categoryElem.setAttribute( "angleOffset", QString::number( angleOffset ) );
QString colors; 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 ) QDomElement attributeElem = doc.createElement( "attribute" );
{
colors.append( "/" );
}
colors.append( categoryColors.at( i ).name() );
}
categoryElem.setAttribute( "colors", colors );
QString categories; attributeElem.setAttribute( "field", layer->pendingFields().at( categoryIndices.at( i ) ).name() );
for ( int i = 0; i < categoryIndices.size(); ++i ) attributeElem.setAttribute( "color", categoryColors.at( i ).name() );
{ categoryElem.appendChild( attributeElem );
if ( i > 0 )
{
categories.append( "/" );
}
categories.append( QString::number( categoryIndices.at( i ) ) );
} }
categoryElem.setAttribute( "categories", categories );
rendererElem.appendChild( categoryElem ); rendererElem.appendChild( categoryElem );
} }
@ -305,8 +317,10 @@ int QgsDiagramRendererV2::dpiPaintDevice( const QPainter* painter )
return -1; return -1;
} }
void QgsDiagramRendererV2::_readXML( const QDomElement& elem ) void QgsDiagramRendererV2::_readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{ {
Q_UNUSED( layer )
delete mDiagram; delete mDiagram;
QString diagramType = elem.attribute( "diagramType" ); QString diagramType = elem.attribute( "diagramType" );
if ( diagramType == "Pie" ) 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( doc );
Q_UNUSED( layer )
if ( mDiagram ) if ( mDiagram )
{ {
rendererElem.setAttribute( "diagramType", mDiagram->diagramName() ); rendererElem.setAttribute( "diagramType", mDiagram->diagramName() );
@ -363,7 +379,7 @@ QList<QgsDiagramSettings> QgsSingleCategoryDiagramRenderer::diagramSettings() co
return settingsList; return settingsList;
} }
void QgsSingleCategoryDiagramRenderer::readXML( const QDomElement& elem ) void QgsSingleCategoryDiagramRenderer::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
{ {
QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" ); QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" );
if ( categoryElem.isNull() ) if ( categoryElem.isNull() )
@ -371,15 +387,15 @@ void QgsSingleCategoryDiagramRenderer::readXML( const QDomElement& elem )
return; return;
} }
mSettings.readXML( categoryElem ); mSettings.readXML( categoryElem, layer );
_readXML( elem ); _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" ); QDomElement rendererElem = doc.createElement( "SingleCategoryDiagramRenderer" );
mSettings.writeXML( rendererElem, doc ); mSettings.writeXML( rendererElem, doc, layer );
_writeXML( rendererElem, doc ); _writeXML( rendererElem, doc , layer );
layerElem.appendChild( rendererElem ); layerElem.appendChild( rendererElem );
} }
@ -421,7 +437,7 @@ QSizeF QgsLinearlyInterpolatedDiagramRenderer::diagramSize( const QgsAttributes&
return mDiagram->diagramSize( attributes, c, mSettings, mInterpolationSettings ); 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.lowerValue = elem.attribute( "lowerValue" ).toDouble();
mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble(); mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble();
@ -433,12 +449,12 @@ void QgsLinearlyInterpolatedDiagramRenderer::readXML( const QDomElement& elem )
QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" ); QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" );
if ( !settingsElem.isNull() ) 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" ); QDomElement rendererElem = doc.createElement( "LinearlyInterpolatedDiagramRenderer" );
rendererElem.setAttribute( "lowerValue", QString::number( mInterpolationSettings.lowerValue ) ); 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( "upperWidth", QString::number( mInterpolationSettings.upperSize.width() ) );
rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) ); rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) );
rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute ); rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute );
mSettings.writeXML( rendererElem, doc ); mSettings.writeXML( rendererElem, doc, layer );
_writeXML( rendererElem, doc ); _writeXML( rendererElem, doc, layer );
layerElem.appendChild( rendererElem ); layerElem.appendChild( rendererElem );
} }

View File

@ -32,6 +32,8 @@ class QDomElement;
class QgsPalGeometry; class QgsPalGeometry;
class QgsCoordinateTransform; class QgsCoordinateTransform;
class QgsMapToPixel; class QgsMapToPixel;
class QgsVectorLayer;
namespace pal { class Layer; } namespace pal { class Layer; }
class CORE_EXPORT QgsDiagramLayerSettings 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 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) int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const; void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
}; };
//diagram settings for rendering //diagram settings for rendering
@ -141,8 +143,8 @@ class CORE_EXPORT QgsDiagramSettings
//! Scale diagrams smaller than mMinimumSize to mMinimumSize //! Scale diagrams smaller than mMinimumSize to mMinimumSize
double minimumSize; double minimumSize;
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const; void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
}; };
//additional diagram settings for interpolated size rendering //additional diagram settings for interpolated size rendering
@ -181,8 +183,8 @@ class CORE_EXPORT QgsDiagramRendererV2
/**Returns list with all diagram settings in the renderer*/ /**Returns list with all diagram settings in the renderer*/
virtual QList<QgsDiagramSettings> diagramSettings() const = 0; virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
virtual void readXML( const QDomElement& elem ) = 0; virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0; virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
protected: protected:
@ -203,8 +205,8 @@ class CORE_EXPORT QgsDiagramRendererV2
static int dpiPaintDevice( const QPainter* ); static int dpiPaintDevice( const QPainter* );
//read / write diagram //read / write diagram
void _readXML( const QDomElement& elem ); void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void _writeXML( QDomElement& rendererElem, QDomDocument& doc ) const; void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
/**Reference to the object that does the real diagram rendering*/ /**Reference to the object that does the real diagram rendering*/
QgsDiagram* mDiagram; QgsDiagram* mDiagram;
@ -225,8 +227,8 @@ class CORE_EXPORT QgsSingleCategoryDiagramRenderer : public QgsDiagramRendererV2
QList<QgsDiagramSettings> diagramSettings() const; QList<QgsDiagramSettings> diagramSettings() const;
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const; void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
protected: protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s ); 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; } int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; } void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
void readXML( const QDomElement& elem ); void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const; void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
protected: protected:
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s ); bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );

View File

@ -1830,13 +1830,13 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
if ( !singleCatDiagramElem.isNull() ) if ( !singleCatDiagramElem.isNull() )
{ {
mDiagramRenderer = new QgsSingleCategoryDiagramRenderer(); mDiagramRenderer = new QgsSingleCategoryDiagramRenderer();
mDiagramRenderer->readXML( singleCatDiagramElem ); mDiagramRenderer->readXML( singleCatDiagramElem, this );
} }
QDomElement linearDiagramElem = node.firstChildElement( "LinearlyInterpolatedDiagramRenderer" ); QDomElement linearDiagramElem = node.firstChildElement( "LinearlyInterpolatedDiagramRenderer" );
if ( !linearDiagramElem.isNull() ) if ( !linearDiagramElem.isNull() )
{ {
mDiagramRenderer = new QgsLinearlyInterpolatedDiagramRenderer(); mDiagramRenderer = new QgsLinearlyInterpolatedDiagramRenderer();
mDiagramRenderer->readXML( linearDiagramElem ); mDiagramRenderer->readXML( linearDiagramElem, this );
} }
if ( mDiagramRenderer ) if ( mDiagramRenderer )
@ -1845,7 +1845,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
if ( !diagramSettingsElem.isNull() ) if ( !diagramSettingsElem.isNull() )
{ {
mDiagramLayerSettings = new QgsDiagramLayerSettings(); mDiagramLayerSettings = new QgsDiagramLayerSettings();
mDiagramLayerSettings->readXML( diagramSettingsElem ); mDiagramLayerSettings->readXML( diagramSettingsElem, this );
} }
} }
} }
@ -2168,9 +2168,9 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
if ( mDiagramRenderer ) if ( mDiagramRenderer )
{ {
mDiagramRenderer->writeXML( mapLayerNode, doc ); mDiagramRenderer->writeXML( mapLayerNode, doc, this );
if ( mDiagramLayerSettings ) if ( mDiagramLayerSettings )
mDiagramLayerSettings->writeXML( mapLayerNode, doc ); mDiagramLayerSettings->writeXML( mapLayerNode, doc, this );
} }
} }