Skip to content

Commit 93b124e

Browse files
committedAug 30, 2016
Move storage of field alias and default value to QgsField
This is a partial implementation - QMaps are still used internally within QgsVectorLayer to track the alias/default values between attribute edit operations. Sponsored by DB Fahrwegdienste GmbH
1 parent f32b85e commit 93b124e

15 files changed

+314
-79
lines changed
 

‎python/core/qgsfield.sip

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@ class QgsField
4242
bool operator==( const QgsField& other ) const;
4343
bool operator!=( const QgsField& other ) const;
4444

45-
//! Gets the name of the field
45+
/** Returns the name of the field.
46+
* @see setName()
47+
* @see displayName()
48+
*/
4649
QString name() const;
4750

51+
/** Returns the name to use when displaying this field. This will be the
52+
* field alias if set, otherwise the field name.
53+
* @see name()
54+
* @see alias()
55+
* @note added in QGIS 3.0
56+
*/
57+
QString displayName() const;
58+
4859
//! Gets variant type of the field as it will be retrieved from data source
4960
QVariant::Type type() const;
5061

@@ -115,6 +126,36 @@ class QgsField
115126
*/
116127
void setComment( const QString& comment );
117128

129+
/** Returns the expression used when calculating the default value for the field.
130+
* @returns expression evaluated when calculating default values for field, or an
131+
* empty string if no default is set
132+
* @note added in QGIS 3.0
133+
* @see setDefaultValueExpression()
134+
*/
135+
QString defaultValueExpression() const;
136+
137+
/** Sets an expression to use when calculating the default value for the field.
138+
* @param expression expression to evaluate when calculating default values for field. Pass
139+
* an empty expression to clear the default.
140+
* @note added in QGIS 3.0
141+
* @see defaultValueExpression()
142+
*/
143+
void setDefaultValueExpression( const QString& expression );
144+
145+
/** Returns the alias for the field (the friendly displayed name of the field ),
146+
* or an empty string if there is no alias.
147+
* @see setAlias()
148+
* @note added in QGIS 3.0
149+
*/
150+
QString alias() const;
151+
152+
/** Sets the alias for the field (the friendly displayed name of the field ).
153+
* @param alias field alias, or empty string to remove an existing alias
154+
* @see alias()
155+
* @note added in QGIS 3.0
156+
*/
157+
void setAlias( const QString& alias );
158+
118159
/** Formats string for display*/
119160
QString displayString( const QVariant& v ) const;
120161

‎src/app/qgsfieldsproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
343343
setConfigForRow( row, cfg );
344344

345345
//set the alias for the attribute
346-
mFieldsList->setItem( row, attrAliasCol, new QTableWidgetItem( mLayer->attributeAlias( idx ) ) );
346+
mFieldsList->setItem( row, attrAliasCol, new QTableWidgetItem( field.alias() ) );
347347

348348
//published WMS/WFS attributes
349349
QTableWidgetItem* wmsAttrItem = new QTableWidgetItem();

‎src/core/qgsfield.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ QString QgsField::name() const
8686
return d->name;
8787
}
8888

89+
QString QgsField::displayName() const
90+
{
91+
if ( !d->alias.isEmpty() )
92+
return d->alias;
93+
else
94+
return d->name;
95+
}
96+
8997
QVariant::Type QgsField::type() const
9098
{
9199
return d->type;
@@ -151,6 +159,26 @@ void QgsField::setComment( const QString& comment )
151159
d->comment = comment;
152160
}
153161

162+
QString QgsField::defaultValueExpression() const
163+
{
164+
return d->defaultValueExpression;
165+
}
166+
167+
void QgsField::setDefaultValueExpression( const QString& expression )
168+
{
169+
d->defaultValueExpression = expression;
170+
}
171+
172+
QString QgsField::alias() const
173+
{
174+
return d->alias;
175+
}
176+
177+
void QgsField::setAlias( const QString& alias )
178+
{
179+
d->alias = alias;
180+
}
181+
154182
/***************************************************************************
155183
* This class is considered CRITICAL and any change MUST be accompanied with
156184
* full unit tests in testqgsfield.cpp.
@@ -252,20 +280,24 @@ QDataStream& operator<<( QDataStream& out, const QgsField& field )
252280
out << field.length();
253281
out << field.precision();
254282
out << field.comment();
283+
out << field.alias();
284+
out << field.defaultValueExpression();
255285
return out;
256286
}
257287

258288
QDataStream& operator>>( QDataStream& in, QgsField& field )
259289
{
260290
quint32 type, length, precision;
261-
QString name, typeName, comment;
262-
in >> name >> type >> typeName >> length >> precision >> comment;
291+
QString name, typeName, comment, alias, defaultValueExpression;
292+
in >> name >> type >> typeName >> length >> precision >> comment >> alias >> defaultValueExpression;
263293
field.setName( name );
264294
field.setType( static_cast< QVariant::Type >( type ) );
265295
field.setTypeName( typeName );
266296
field.setLength( static_cast< int >( length ) );
267297
field.setPrecision( static_cast< int >( precision ) );
268298
field.setComment( comment );
299+
field.setAlias( alias );
300+
field.setDefaultValueExpression( defaultValueExpression );
269301
return in;
270302
}
271303

‎src/core/qgsfield.h

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ class CORE_EXPORT QgsField
4646
Q_GADGET
4747

4848
Q_PROPERTY( bool isNumeric READ isNumeric )
49-
Q_PROPERTY( int length READ length )
50-
Q_PROPERTY( int precision READ precision )
51-
Q_PROPERTY( QString comment READ comment )
52-
Q_PROPERTY( QString name READ name )
49+
Q_PROPERTY( int length READ length WRITE setLength )
50+
Q_PROPERTY( int precision READ precision WRITE setPrecision )
51+
Q_PROPERTY( QString comment READ comment WRITE setComment )
52+
Q_PROPERTY( QString name READ name WRITE setName )
53+
Q_PROPERTY( QString alias READ alias WRITE setAlias )
54+
Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )
5355

5456
public:
5557
/** Constructor. Constructs a new QgsField object.
@@ -84,9 +86,20 @@ class CORE_EXPORT QgsField
8486
bool operator==( const QgsField& other ) const;
8587
bool operator!=( const QgsField& other ) const;
8688

87-
//! Gets the name of the field
89+
/** Returns the name of the field.
90+
* @see setName()
91+
* @see displayName()
92+
*/
8893
QString name() const;
8994

95+
/** Returns the name to use when displaying this field. This will be the
96+
* field alias if set, otherwise the field name.
97+
* @see name()
98+
* @see alias()
99+
* @note added in QGIS 3.0
100+
*/
101+
QString displayName() const;
102+
90103
//! Gets variant type of the field as it will be retrieved from data source
91104
QVariant::Type type() const;
92105

@@ -157,6 +170,36 @@ class CORE_EXPORT QgsField
157170
*/
158171
void setComment( const QString& comment );
159172

173+
/** Returns the expression used when calculating the default value for the field.
174+
* @returns expression evaluated when calculating default values for field, or an
175+
* empty string if no default is set
176+
* @note added in QGIS 3.0
177+
* @see setDefaultValueExpression()
178+
*/
179+
QString defaultValueExpression() const;
180+
181+
/** Sets an expression to use when calculating the default value for the field.
182+
* @param expression expression to evaluate when calculating default values for field. Pass
183+
* an empty expression to clear the default.
184+
* @note added in QGIS 3.0
185+
* @see defaultValueExpression()
186+
*/
187+
void setDefaultValueExpression( const QString& expression );
188+
189+
/** Returns the alias for the field (the friendly displayed name of the field ),
190+
* or an empty string if there is no alias.
191+
* @see setAlias()
192+
* @note added in QGIS 3.0
193+
*/
194+
QString alias() const;
195+
196+
/** Sets the alias for the field (the friendly displayed name of the field ).
197+
* @param alias field alias, or empty string to remove an existing alias
198+
* @see alias()
199+
* @note added in QGIS 3.0
200+
*/
201+
void setAlias( const QString& alias );
202+
160203
/** Formats string for display*/
161204
QString displayString( const QVariant& v ) const;
162205

‎src/core/qgsfield_p.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class QgsFieldPrivate : public QSharedData
6565
, length( other.length )
6666
, precision( other.precision )
6767
, comment( other.comment )
68+
, alias( other.alias )
69+
, defaultValueExpression( other.defaultValueExpression )
6870
{
6971
}
7072

@@ -73,7 +75,8 @@ class QgsFieldPrivate : public QSharedData
7375
bool operator==( const QgsFieldPrivate& other ) const
7476
{
7577
return (( name == other.name ) && ( type == other.type )
76-
&& ( length == other.length ) && ( precision == other.precision ) );
78+
&& ( length == other.length ) && ( precision == other.precision )
79+
&& ( alias == other.alias ) && ( defaultValueExpression == other.defaultValueExpression ) );
7780
}
7881

7982
//! Name
@@ -93,6 +96,12 @@ class QgsFieldPrivate : public QSharedData
9396

9497
//! Comment
9598
QString comment;
99+
100+
//! Alias for field name (friendly name shown to users)
101+
QString alias;
102+
103+
//! Default value expression
104+
QString defaultValueExpression;
96105
};
97106

98107

‎src/core/qgsvectorlayer.cpp

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,13 +1694,10 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )
16941694
if ( field.isEmpty() || expression.isEmpty() )
16951695
continue;
16961696

1697-
int index = mUpdatedFields.fieldNameIndex( field );
1698-
if ( index < 0 )
1699-
continue;
1700-
1701-
mDefaultExpressionMap.insert( index, expression );
1697+
mDefaultExpressionMap.insert( field, expression );
17021698
}
17031699
}
1700+
updateFields();
17041701

17051702
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
17061703

@@ -1903,24 +1900,15 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
19031900
mExpressionFieldBuffer->writeXml( layer_node, document );
19041901

19051902
//default expressions
1906-
if ( !mDefaultExpressionMap.isEmpty() )
1903+
QDomElement defaultsElem = document.createElement( "defaults" );
1904+
Q_FOREACH ( const QgsField& field, mUpdatedFields )
19071905
{
1908-
QDomElement defaultsElem = document.createElement( "defaults" );
1909-
QMap<int, QString>::const_iterator it = mDefaultExpressionMap.constBegin();
1910-
for ( ; it != mDefaultExpressionMap.constEnd(); ++it )
1911-
{
1912-
if ( it.key() >= mUpdatedFields.count() )
1913-
continue;
1914-
1915-
QString fieldName = mUpdatedFields.at( it.key() ).name();
1916-
1917-
QDomElement defaultElem = document.createElement( "default" );
1918-
defaultElem.setAttribute( "field", fieldName );
1919-
defaultElem.setAttribute( "expression", it.value() );
1920-
defaultsElem.appendChild( defaultElem );
1921-
}
1922-
layer_node.appendChild( defaultsElem );
1906+
QDomElement defaultElem = document.createElement( "default" );
1907+
defaultElem.setAttribute( "field", field.name() );
1908+
defaultElem.setAttribute( "expression", field.defaultValueExpression() );
1909+
defaultsElem.appendChild( defaultElem );
19231910
}
1911+
layer_node.appendChild( defaultsElem );
19241912

19251913
writeStyleManager( layer_node, document );
19261914

@@ -1932,6 +1920,8 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
19321920

19331921
bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
19341922
{
1923+
updateFields();
1924+
19351925
readStyle( node, errorMessage );
19361926

19371927
// process the attribute actions
@@ -1973,6 +1963,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
19731963
mAttributeAliasMap.insert( field, aliasElem.attribute( "name" ) );
19741964
}
19751965
}
1966+
updateFields();
19761967

19771968
//Attributes excluded from WMS and WFS
19781969
mExcludeAttributesWMS.clear();
@@ -2159,24 +2150,16 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
21592150
node.appendChild( afField );
21602151

21612152
//attribute aliases
2162-
if ( !mAttributeAliasMap.isEmpty() )
2153+
QDomElement aliasElem = doc.createElement( "aliases" );
2154+
Q_FOREACH ( const QgsField& field, mUpdatedFields )
21632155
{
2164-
QDomElement aliasElem = doc.createElement( "aliases" );
2165-
QMap<QString, QString>::const_iterator a_it = mAttributeAliasMap.constBegin();
2166-
for ( ; a_it != mAttributeAliasMap.constEnd(); ++a_it )
2167-
{
2168-
int idx = fieldNameIndex( a_it.key() );
2169-
if ( idx < 0 )
2170-
continue;
2171-
2172-
QDomElement aliasEntryElem = doc.createElement( "alias" );
2173-
aliasEntryElem.setAttribute( "field", a_it.key() );
2174-
aliasEntryElem.setAttribute( "index", idx );
2175-
aliasEntryElem.setAttribute( "name", a_it.value() );
2176-
aliasElem.appendChild( aliasEntryElem );
2177-
}
2178-
node.appendChild( aliasElem );
2156+
QDomElement aliasEntryElem = doc.createElement( "alias" );
2157+
aliasEntryElem.setAttribute( "field", field.name() );
2158+
aliasEntryElem.setAttribute( "index", mUpdatedFields.indexFromName( field.name() ) );
2159+
aliasEntryElem.setAttribute( "name", field.alias() );
2160+
aliasElem.appendChild( aliasEntryElem );
21792161
}
2162+
node.appendChild( aliasElem );
21802163

21812164
//exclude attributes WMS
21822165
QDomElement excludeWMSElem = doc.createElement( "excludeAttributesWMS" );
@@ -2403,9 +2386,11 @@ void QgsVectorLayer::remAttributeAlias( int attIndex )
24032386
return;
24042387

24052388
QString name = fields().at( attIndex ).name();
2389+
mUpdatedFields[ attIndex ].setAlias( QString() );
24062390
if ( mAttributeAliasMap.contains( name ) )
24072391
{
24082392
mAttributeAliasMap.remove( name );
2393+
updateFields();
24092394
emit layerModified();
24102395
}
24112396
}
@@ -2426,6 +2411,7 @@ void QgsVectorLayer::addAttributeAlias( int attIndex, const QString& aliasString
24262411
QString name = fields().at( attIndex ).name();
24272412

24282413
mAttributeAliasMap.insert( name, aliasString );
2414+
mUpdatedFields[ attIndex ].setAlias( aliasString );
24292415
emit layerModified(); // TODO[MD]: should have a different signal?
24302416
}
24312417

@@ -2434,22 +2420,26 @@ QString QgsVectorLayer::attributeAlias( int attributeIndex ) const
24342420
if ( attributeIndex < 0 || attributeIndex >= fields().count() )
24352421
return QString();
24362422

2437-
QString name = fields().at( attributeIndex ).name();
2438-
2439-
return mAttributeAliasMap.value( name, QString() );
2423+
return fields().at( attributeIndex ).alias();
24402424
}
24412425

24422426
QString QgsVectorLayer::attributeDisplayName( int attributeIndex ) const
24432427
{
2444-
QString displayName = attributeAlias( attributeIndex );
2445-
if ( displayName.isEmpty() )
2428+
if ( attributeIndex >= 0 && attributeIndex < mUpdatedFields.count() )
2429+
return mUpdatedFields.at( attributeIndex ).displayName();
2430+
else
2431+
return QString();
2432+
}
2433+
2434+
QgsStringMap QgsVectorLayer::attributeAliases() const
2435+
{
2436+
QgsStringMap map;
2437+
Q_FOREACH ( const QgsField& field, fields() )
24462438
{
2447-
if ( attributeIndex >= 0 && attributeIndex < mUpdatedFields.count() )
2448-
{
2449-
displayName = mUpdatedFields.at( attributeIndex ).name();
2450-
}
2439+
if ( !field.alias().isEmpty() )
2440+
map.insert( field.name(), field.alias() );
24512441
}
2452-
return displayName;
2442+
return map;
24532443
}
24542444

24552445
bool QgsVectorLayer::deleteAttribute( int index )
@@ -3159,6 +3149,25 @@ void QgsVectorLayer::updateFields()
31593149
if ( mExpressionFieldBuffer )
31603150
mExpressionFieldBuffer->updateFields( mUpdatedFields );
31613151

3152+
// set aliases and default values
3153+
QMap< QString, QString >::const_iterator aliasIt = mAttributeAliasMap.constBegin();
3154+
for ( ; aliasIt != mAttributeAliasMap.constEnd(); ++aliasIt )
3155+
{
3156+
int index = mUpdatedFields.fieldNameIndex( aliasIt.key() );
3157+
if ( index < 0 )
3158+
continue;
3159+
3160+
mUpdatedFields[ index ].setAlias( aliasIt.value() );
3161+
}
3162+
QMap< QString, QString >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
3163+
for ( ; defaultIt != mDefaultExpressionMap.constEnd(); ++defaultIt )
3164+
{
3165+
int index = mUpdatedFields.fieldNameIndex( defaultIt.key() );
3166+
if ( index < 0 )
3167+
continue;
3168+
3169+
mUpdatedFields[ index ].setDefaultValueExpression( defaultIt.value() );
3170+
}
31623171
if ( oldFields != mUpdatedFields )
31633172
{
31643173
emit updatedFields();
@@ -3177,7 +3186,10 @@ void QgsVectorLayer::createJoinCaches()
31773186

31783187
QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature& feature, QgsExpressionContext* context ) const
31793188
{
3180-
QString expression = mDefaultExpressionMap.value( index, QString() );
3189+
if ( index < 0 || index >= mUpdatedFields.count() )
3190+
return QVariant();
3191+
3192+
QString expression = mUpdatedFields.at( index ).defaultValueExpression();
31813193
if ( expression.isEmpty() )
31823194
return mDataProvider->defaultValue( index );
31833195

@@ -3223,19 +3235,26 @@ QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature& feature, Qgs
32233235

32243236
void QgsVectorLayer::setDefaultValueExpression( int index, const QString& expression )
32253237
{
3238+
if ( index < 0 || index >= mUpdatedFields.count() )
3239+
return;
3240+
32263241
if ( expression.isEmpty() )
32273242
{
3228-
mDefaultExpressionMap.remove( index );
3243+
mDefaultExpressionMap.remove( mUpdatedFields.at( index ).name() );
32293244
}
32303245
else
32313246
{
3232-
mDefaultExpressionMap.insert( index, expression );
3247+
mDefaultExpressionMap.insert( mUpdatedFields.at( index ).name(), expression );
32333248
}
3249+
updateFields();
32343250
}
32353251

32363252
QString QgsVectorLayer::defaultValueExpression( int index ) const
32373253
{
3238-
return mDefaultExpressionMap.value( index, QString() );
3254+
if ( index < 0 || index >= mUpdatedFields.count() )
3255+
return QString();
3256+
else
3257+
return mUpdatedFields.at( index ).defaultValueExpression();
32393258
}
32403259

32413260
void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int limit )

‎src/core/qgsvectorlayer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
14221422
/** Convenience function that returns the attribute alias if defined or the field name else */
14231423
QString attributeDisplayName( int attributeIndex ) const;
14241424

1425-
const QMap< QString, QString >& attributeAliases() const { return mAttributeAliasMap; }
1425+
//! Returns a map of field name to attribute alias
1426+
QgsStringMap attributeAliases() const;
14261427

14271428
const QSet<QString>& excludeAttributesWMS() const { return mExcludeAttributesWMS; }
14281429
void setExcludeAttributesWMS( const QSet<QString>& att ) { mExcludeAttributesWMS = att; }
@@ -2207,10 +2208,10 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
22072208
QgsFields mUpdatedFields;
22082209

22092210
/** Map that stores the aliases for attributes. Key is the attribute name and value the alias for that attribute*/
2210-
QMap< QString, QString > mAttributeAliasMap;
2211+
QgsStringMap mAttributeAliasMap;
22112212

22122213
//! Map which stores default value expressions for fields
2213-
QMap< int, QString > mDefaultExpressionMap;
2214+
QgsStringMap mDefaultExpressionMap;
22142215

22152216
/** Holds the configuration for the edit form */
22162217
QgsEditFormConfig* mEditFormConfig;

‎src/core/qgsvectorlayereditbuffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ void QgsVectorLayerEditBuffer::updateFields( QgsFields& fields )
6565
// delete attributes from the higher indices to lower indices
6666
for ( int i = mDeletedAttributeIds.count() - 1; i >= 0; --i )
6767
{
68-
fields.remove( mDeletedAttributeIds[i] );
68+
fields.remove( mDeletedAttributeIds.at( i ) );
6969
}
7070
// add new fields
7171
for ( int i = 0; i < mAddedAttributes.count(); ++i )
7272
{
73-
fields.append( mAddedAttributes[i], QgsFields::OriginEdit, i );
73+
fields.append( mAddedAttributes.at( i ), QgsFields::OriginEdit, i );
7474
}
7575
// rename fields
7676
QgsFieldNameMap::const_iterator renameIt = mRenamedAttributes.constBegin();

‎src/gui/attributetable/qgsattributetablemodel.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
540540
}
541541
else if ( section >= 0 && section < mFieldCount )
542542
{
543-
QString attributeName = layer()->attributeAlias( mAttributes.at( section ) );
544-
if ( attributeName.isEmpty() )
545-
{
546-
QgsField field = layer()->fields().at( mAttributes.at( section ) );
547-
attributeName = field.name();
548-
}
543+
QString attributeName = layer()->fields().at( mAttributes.at( section ) ).displayName();
549544
return QVariant( attributeName );
550545
}
551546
else

‎src/gui/editorwidgets/qgsrelationreferenceconfigdlg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void QgsRelationReferenceConfigDlg::loadFields()
191191
const QgsFields& flds = l->fields();
192192
for ( int i = 0; i < flds.count(); i++ )
193193
{
194-
mAvailableFieldsList->addItem( l->attributeAlias( i ).isEmpty() ? flds.at( i ).name() : l->attributeAlias( i ) );
194+
mAvailableFieldsList->addItem( flds.at( i ).displayName() );
195195
mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
196196
}
197197
}

‎src/server/qgsserverprojectparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ void QgsServerProjectParser::addLayerProjectSettings( QDomElement& layerElem, QD
797797
attributeElem.setAttribute( "name", field.name() );
798798
attributeElem.setAttribute( "type", QVariant::typeToName( field.type() ) );
799799
attributeElem.setAttribute( "typeName", field.typeName() );
800-
QString alias = vLayer->attributeAlias( idx );
800+
QString alias = field.alias();
801801
if ( !alias.isEmpty() )
802802
{
803803
attributeElem.setAttribute( "alias", alias );

‎src/server/qgswfsprojectparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ void QgsWFSProjectParser::describeFeatureType( const QString& aTypeName, QDomEle
485485

486486
sequenceElem.appendChild( attElem );
487487

488-
QString alias = layer->attributeAlias( idx );
488+
QString alias = fields.at( idx ).alias();
489489
if ( !alias.isEmpty() )
490490
{
491491
attElem.setAttribute( "alias", alias );

‎src/server/qgswfsserver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
485485

486486
//is there alias info for this vector layer?
487487
QMap< int, QString > layerAliasInfo;
488-
const QMap< QString, QString >& aliasMap = layer->attributeAliases();
489-
QMap< QString, QString >::const_iterator aliasIt = aliasMap.constBegin();
488+
QgsStringMap aliasMap = layer->attributeAliases();
489+
QgsStringMap::const_iterator aliasIt = aliasMap.constBegin();
490490
for ( ; aliasIt != aliasMap.constEnd(); ++aliasIt )
491491
{
492492
int attrIndex = layer->fieldNameIndex( aliasIt.key() );
@@ -872,8 +872,8 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
872872

873873
//is there alias info for this vector layer?
874874
QMap< int, QString > layerAliasInfo;
875-
const QMap< QString, QString >& aliasMap = layer->attributeAliases();
876-
QMap< QString, QString >::const_iterator aliasIt = aliasMap.constBegin();
875+
QgsStringMap aliasMap = layer->attributeAliases();
876+
QgsStringMap::const_iterator aliasIt = aliasMap.constBegin();
877877
for ( ; aliasIt != aliasMap.constEnd(); ++aliasIt )
878878
{
879879
int attrIndex = layer->fieldNameIndex( aliasIt.key() );

‎tests/src/core/testqgsfield.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class TestQgsField: public QObject
4040
void displayString();
4141
void convertCompatible();
4242
void dataStream();
43+
void displayName();
4344

4445
private:
4546
};
@@ -116,6 +117,10 @@ void TestQgsField::gettersSetters()
116117
QCOMPARE( field.precision(), 2 );
117118
field.setComment( "comment" );
118119
QCOMPARE( field.comment(), QString( "comment" ) );
120+
field.setAlias( "alias" );
121+
QCOMPARE( field.alias(), QString( "alias" ) );
122+
field.setDefaultValueExpression( "1+2" );
123+
QCOMPARE( field.defaultValueExpression(), QString( "1+2" ) );
119124
}
120125

121126
void TestQgsField::isNumeric()
@@ -177,6 +182,14 @@ void TestQgsField::equality()
177182
QVERIFY( !( field1 == field2 ) );
178183
QVERIFY( field1 != field2 );
179184
field2.setPrecision( 2 );
185+
field2.setAlias( "alias " );
186+
QVERIFY( !( field1 == field2 ) );
187+
QVERIFY( field1 != field2 );
188+
field2.setAlias( QString() );
189+
field2.setDefaultValueExpression( "1+2" );
190+
QVERIFY( !( field1 == field2 ) );
191+
QVERIFY( field1 != field2 );
192+
field2.setDefaultValueExpression( QString() );
180193
}
181194

182195
void TestQgsField::asVariant()
@@ -358,6 +371,8 @@ void TestQgsField::dataStream()
358371
original.setPrecision( 2 );
359372
original.setTypeName( "typename1" );
360373
original.setComment( "comment1" );
374+
original.setAlias( "alias" );
375+
original.setDefaultValueExpression( "default" );
361376

362377
QByteArray ba;
363378
QDataStream ds( &ba, QIODevice::ReadWrite );
@@ -372,5 +387,16 @@ void TestQgsField::dataStream()
372387
QCOMPARE( result.comment(), original.comment() ); //comment is NOT required for equality
373388
}
374389

390+
void TestQgsField::displayName()
391+
{
392+
QgsField field;
393+
field.setName( "name" );
394+
QCOMPARE( field.displayName(), QString( "name" ) );
395+
field.setAlias( "alias" );
396+
QCOMPARE( field.displayName(), QString( "alias" ) );
397+
field.setAlias( QString() );
398+
QCOMPARE( field.displayName(), QString( "name" ) );
399+
}
400+
375401
QTEST_MAIN( TestQgsField )
376402
#include "testqgsfield.moc"

‎tests/src/python/test_qgsvectorlayer.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,70 @@ def test_setRendererV2(self):
14601460
self.assertTrue(self.rendererChanged)
14611461
self.assertEqual(layer.rendererV2(), r)
14621462

1463+
def testGetSetAliases(self):
1464+
""" test getting and setting aliases """
1465+
layer = createLayerWithOnePoint()
1466+
1467+
self.assertFalse(layer.attributeAlias(0))
1468+
self.assertFalse(layer.attributeAlias(1))
1469+
self.assertFalse(layer.attributeAlias(2))
1470+
1471+
layer.addAttributeAlias(0, "test")
1472+
self.assertEqual(layer.attributeAlias(0), "test")
1473+
self.assertFalse(layer.attributeAlias(1))
1474+
self.assertFalse(layer.attributeAlias(2))
1475+
self.assertEqual(layer.fields().at(0).alias(), "test")
1476+
1477+
layer.addAttributeAlias(1, "test2")
1478+
self.assertEqual(layer.attributeAlias(0), "test")
1479+
self.assertEqual(layer.attributeAlias(1), "test2")
1480+
self.assertFalse(layer.attributeAlias(2))
1481+
self.assertEqual(layer.fields().at(0).alias(), "test")
1482+
self.assertEqual(layer.fields().at(1).alias(), "test2")
1483+
1484+
layer.addAttributeAlias(1, None)
1485+
self.assertEqual(layer.attributeAlias(0), "test")
1486+
self.assertFalse(layer.attributeAlias(1))
1487+
self.assertFalse(layer.attributeAlias(2))
1488+
self.assertEqual(layer.fields().at(0).alias(), "test")
1489+
self.assertFalse(layer.fields().at(1).alias())
1490+
1491+
layer.remAttributeAlias(0)
1492+
self.assertFalse(layer.attributeAlias(0))
1493+
self.assertFalse(layer.attributeAlias(1))
1494+
self.assertFalse(layer.attributeAlias(2))
1495+
self.assertFalse(layer.fields().at(0).alias())
1496+
self.assertFalse(layer.fields().at(1).alias())
1497+
1498+
def testSaveRestoreAliases(self):
1499+
""" test saving and restoring aliases from xml"""
1500+
layer = createLayerWithOnePoint()
1501+
1502+
# no default expressions
1503+
doc = QDomDocument("testdoc")
1504+
elem = doc.createElement("maplayer")
1505+
self.assertTrue(layer.writeXml(elem, doc))
1506+
1507+
layer2 = createLayerWithOnePoint()
1508+
self.assertTrue(layer2.readXml(elem))
1509+
self.assertFalse(layer2.attributeAlias(0))
1510+
self.assertFalse(layer2.attributeAlias(1))
1511+
1512+
# set some aliases
1513+
layer.addAttributeAlias(0, "test")
1514+
layer.addAttributeAlias(1, "test2")
1515+
1516+
doc = QDomDocument("testdoc")
1517+
elem = doc.createElement("maplayer")
1518+
self.assertTrue(layer.writeXml(elem, doc))
1519+
1520+
layer3 = createLayerWithOnePoint()
1521+
self.assertTrue(layer3.readXml(elem))
1522+
self.assertEqual(layer3.attributeAlias(0), "test")
1523+
self.assertEqual(layer3.attributeAlias(1), "test2")
1524+
self.assertEqual(layer3.fields().at(0).alias(), "test")
1525+
self.assertEqual(layer3.fields().at(1).alias(), "test2")
1526+
14631527
def testGetSetDefaults(self):
14641528
""" test getting and setting default expressions """
14651529
layer = createLayerWithOnePoint()
@@ -1472,11 +1536,14 @@ def testGetSetDefaults(self):
14721536
self.assertEqual(layer.defaultValueExpression(0), "'test'")
14731537
self.assertFalse(layer.defaultValueExpression(1))
14741538
self.assertFalse(layer.defaultValueExpression(2))
1539+
self.assertEqual(layer.fields().at(0).defaultValueExpression(), "'test'")
14751540

14761541
layer.setDefaultValueExpression(1, "2+2")
14771542
self.assertEqual(layer.defaultValueExpression(0), "'test'")
14781543
self.assertEqual(layer.defaultValueExpression(1), "2+2")
14791544
self.assertFalse(layer.defaultValueExpression(2))
1545+
self.assertEqual(layer.fields().at(0).defaultValueExpression(), "'test'")
1546+
self.assertEqual(layer.fields().at(1).defaultValueExpression(), "2+2")
14801547

14811548
def testSaveRestoreDefaults(self):
14821549
""" test saving and restoring default expressions from xml"""
@@ -1504,6 +1571,8 @@ def testSaveRestoreDefaults(self):
15041571
self.assertTrue(layer3.readXml(elem))
15051572
self.assertEqual(layer3.defaultValueExpression(0), "'test'")
15061573
self.assertEqual(layer3.defaultValueExpression(1), "2+2")
1574+
self.assertEqual(layer3.fields().at(0).defaultValueExpression(), "'test'")
1575+
self.assertEqual(layer3.fields().at(1).defaultValueExpression(), "2+2")
15071576

15081577
def testEvaluatingDefaultExpressions(self):
15091578
""" tests calculation of default values"""

0 commit comments

Comments
 (0)
Please sign in to comment.