@@ -2679,14 +2679,14 @@ bool QgsVectorLayer::readXml( QDomNode & layer_node )
2679
2679
}
2680
2680
mJoinBuffer ->readXml ( layer_node );
2681
2681
2682
+ updateFieldMap ();
2683
+
2682
2684
QString errorMsg;
2683
2685
if ( !readSymbology ( layer_node, errorMsg ) )
2684
2686
{
2685
2687
return false ;
2686
2688
}
2687
2689
2688
- updateFieldMap ();
2689
-
2690
2690
return mValid ; // should be true if read successfully
2691
2691
2692
2692
} // void QgsVectorLayer::readXml
@@ -3039,16 +3039,27 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
3039
3039
if ( !aliasesNode.isNull () )
3040
3040
{
3041
3041
QDomElement aliasElem;
3042
- int index ;
3043
3042
QString name;
3044
3043
3045
3044
QDomNodeList aliasNodeList = aliasesNode.toElement ().elementsByTagName ( " alias" );
3046
3045
for ( int i = 0 ; i < aliasNodeList.size (); ++i )
3047
3046
{
3048
3047
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" ) );
3052
3063
}
3053
3064
}
3054
3065
@@ -3217,11 +3228,16 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
3217
3228
if ( mAttributeAliasMap .size () > 0 )
3218
3229
{
3219
3230
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 ();
3221
3232
for ( ; a_it != mAttributeAliasMap .constEnd (); ++a_it )
3222
3233
{
3234
+ int idx = fieldNameIndex ( a_it.key () );
3235
+ if ( idx < 0 )
3236
+ continue ;
3237
+
3223
3238
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 );
3225
3241
aliasEntryElem.setAttribute ( " name" , a_it.value () );
3226
3242
aliasElem.appendChild ( aliasEntryElem );
3227
3243
}
@@ -3317,21 +3333,23 @@ bool QgsVectorLayer::addAttribute( QString name, QString type )
3317
3333
3318
3334
void QgsVectorLayer::addAttributeAlias ( int attIndex, QString aliasString )
3319
3335
{
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 );
3321
3342
emit layerModified ( false );
3322
3343
}
3323
3344
3324
3345
QString QgsVectorLayer::attributeAlias ( int attributeIndex ) const
3325
3346
{
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, " " );
3335
3353
}
3336
3354
3337
3355
QString QgsVectorLayer::attributeDisplayName ( int attributeIndex ) const
@@ -3369,7 +3387,6 @@ bool QgsVectorLayer::deleteAttribute( int index )
3369
3387
mDeletedAttributeIds .insert ( index );
3370
3388
mAddedAttributeIds .remove ( index );
3371
3389
mUpdatedFields .remove ( index );
3372
- mAttributeAliasMap .remove ( index );
3373
3390
3374
3391
setModified ( true , false );
3375
3392
0 commit comments