Skip to content

Commit 3c50546

Browse files
author
jef
committed
fix #3057
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15687 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 7b497d2 commit 3c50546

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

src/core/qgsvectorlayer.cpp

+36-19
Original file line numberDiff line numberDiff line change
@@ -2679,14 +2679,14 @@ bool QgsVectorLayer::readXml( QDomNode & layer_node )
26792679
}
26802680
mJoinBuffer->readXml( layer_node );
26812681

2682+
updateFieldMap();
2683+
26822684
QString errorMsg;
26832685
if ( !readSymbology( layer_node, errorMsg ) )
26842686
{
26852687
return false;
26862688
}
26872689

2688-
updateFieldMap();
2689-
26902690
return mValid; // should be true if read successfully
26912691

26922692
} // void QgsVectorLayer::readXml
@@ -3039,16 +3039,27 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
30393039
if ( !aliasesNode.isNull() )
30403040
{
30413041
QDomElement aliasElem;
3042-
int index;
30433042
QString name;
30443043

30453044
QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName( "alias" );
30463045
for ( int i = 0; i < aliasNodeList.size(); ++i )
30473046
{
30483047
aliasElem = aliasNodeList.at( i ).toElement();
3049-
index = aliasElem.attribute( "index" ).toInt();
3050-
name = aliasElem.attribute( "name" );
3051-
mAttributeAliasMap.insert( index, name );
3048+
3049+
QString field;
3050+
if ( aliasElem.hasAttribute( "field" ) )
3051+
{
3052+
field = aliasElem.attribute( "field" );
3053+
}
3054+
else
3055+
{
3056+
int index = aliasElem.attribute( "index" ).toInt();
3057+
3058+
if ( pendingFields().contains( index ) )
3059+
field = pendingFields()[ index ].name();
3060+
}
3061+
3062+
mAttributeAliasMap.insert( field, aliasElem.attribute( "name" ) );
30523063
}
30533064
}
30543065

@@ -3217,11 +3228,16 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
32173228
if ( mAttributeAliasMap.size() > 0 )
32183229
{
32193230
QDomElement aliasElem = doc.createElement( "aliases" );
3220-
QMap<int, QString>::const_iterator a_it = mAttributeAliasMap.constBegin();
3231+
QMap<QString, QString>::const_iterator a_it = mAttributeAliasMap.constBegin();
32213232
for ( ; a_it != mAttributeAliasMap.constEnd(); ++a_it )
32223233
{
3234+
int idx = fieldNameIndex( a_it.key() );
3235+
if( idx < 0 )
3236+
continue;
3237+
32233238
QDomElement aliasEntryElem = doc.createElement( "alias" );
3224-
aliasEntryElem.setAttribute( "index", QString::number( a_it.key() ) );
3239+
aliasEntryElem.setAttribute( "field", a_it.key() );
3240+
aliasEntryElem.setAttribute( "index", idx );
32253241
aliasEntryElem.setAttribute( "name", a_it.value() );
32263242
aliasElem.appendChild( aliasEntryElem );
32273243
}
@@ -3317,21 +3333,23 @@ bool QgsVectorLayer::addAttribute( QString name, QString type )
33173333

33183334
void QgsVectorLayer::addAttributeAlias( int attIndex, QString aliasString )
33193335
{
3320-
mAttributeAliasMap.insert( attIndex, aliasString );
3336+
if ( !pendingFields().contains( attIndex ) )
3337+
return;
3338+
3339+
QString name = pendingFields()[ attIndex ].name();
3340+
3341+
mAttributeAliasMap.insert( name, aliasString );
33213342
emit layerModified( false );
33223343
}
33233344

33243345
QString QgsVectorLayer::attributeAlias( int attributeIndex ) const
33253346
{
3326-
QMap<int, QString>::const_iterator alias_it = mAttributeAliasMap.find( attributeIndex );
3327-
if ( alias_it != mAttributeAliasMap.constEnd() )
3328-
{
3329-
return alias_it.value();
3330-
}
3331-
else
3332-
{
3333-
return QString();
3334-
}
3347+
if ( !pendingFields().contains( attributeIndex ) )
3348+
return "";
3349+
3350+
QString name = pendingFields()[ attributeIndex ].name();
3351+
3352+
return mAttributeAliasMap.value( name, "" );
33353353
}
33363354

33373355
QString QgsVectorLayer::attributeDisplayName( int attributeIndex ) const
@@ -3369,7 +3387,6 @@ bool QgsVectorLayer::deleteAttribute( int index )
33693387
mDeletedAttributeIds.insert( index );
33703388
mAddedAttributeIds.remove( index );
33713389
mUpdatedFields.remove( index );
3372-
mAttributeAliasMap.remove( index );
33733390

33743391
setModified( true, false );
33753392

src/core/qgsvectorlayer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
884884
/** field map to commit */
885885
QgsFieldMap mUpdatedFields;
886886

887-
/**Map that stores the aliases for attributes. Key is the attribute index and value the alias for that attribute*/
888-
QMap<int, QString> mAttributeAliasMap;
887+
/**Map that stores the aliases for attributes. Key is the attribute name and value the alias for that attribute*/
888+
QMap< QString, QString > mAttributeAliasMap;
889889

890890
/** max field index */
891891
int mMaxUpdatedIndex;

0 commit comments

Comments
 (0)