Skip to content

Commit 294d7f3

Browse files
author
mhugent
committed
Added a convenience to retrieve the attribute display name from QgsVectorLayer. Use this function in renderer dialogs, info tool, attribute table and composer legend
git-svn-id: http://svn.osgeo.org/qgis/trunk@10992 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 001bfb5 commit 294d7f3

9 files changed

+98
-87
lines changed

src/app/legend/qgslegendlayer.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,14 @@ void QgsLegendLayer::vectorLayerSymbology( const QgsVectorLayer* layer, double w
370370
if ( renderer->needsAttributes() )
371371
{
372372
QgsAttributeList classfieldlist = renderer->classificationAttributes();
373-
const QgsFieldMap& fields = layer->dataProvider()->fields();
373+
const QgsFieldMap& fields = layer->pendingFields();
374374
for ( QgsAttributeList::iterator it = classfieldlist.begin(); it != classfieldlist.end(); ++it )
375375
{
376-
const QgsField& theField = fields[*it];
377-
QString classfieldname = theField.name();
376+
QString classfieldname = layer->attributeAlias(*it);
377+
if(classfieldname.isEmpty())
378+
{
379+
classfieldname = fields[*it].name();
380+
}
378381
itemList.push_front( std::make_pair( classfieldname, QPixmap() ) );
379382
}
380383
}

src/app/qgsattributedialog.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
103103
const QgsField &field = theFieldMap[it.key()];
104104

105105
//show attribute alias if available
106-
QString myFieldName = vl->attributeAlias(it.key());
107-
if(myFieldName.isEmpty())
108-
{
109-
myFieldName = field.name();
110-
}
106+
QString myFieldName = vl->attributeDisplayName(it.key());
111107
int myFieldType = field.type();
112108
QLabel * mypLabel = new QLabel();
113109
mypInnerLayout->addWidget( mypLabel, index, 0 );

src/app/qgscontinuouscolordialog.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ QgsContinuousColorDialog::QgsContinuousColorDialog( QgsVectorLayer * layer )
4141

4242
//find out the numerical fields of mVectorLayer
4343
const QgsFieldMap & fields = mVectorLayer->pendingFields();
44+
QString displayName;
4445

4546
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
4647
{
4748
QVariant::Type type = it->type();
4849
if ( type == QVariant::Int || type == QVariant::Double )
4950
{
50-
classificationComboBox->addItem( it->name(), it.key() );
51+
displayName = mVectorLayer->attributeDisplayName(it.key());
52+
classificationComboBox->addItem( displayName, it.key() );
5153
}
5254
}
5355

@@ -144,7 +146,7 @@ void QgsContinuousColorDialog::apply()
144146
}
145147
else
146148
{
147-
minimumString = QString::number( minimum );
149+
minimumString = QString::number( minimum, 'f', 0 );
148150
}
149151
QgsSymbol* minsymbol = new QgsSymbol( mVectorLayer->geometryType(), minimumString, "", "" );
150152
QPen minPen;
@@ -167,7 +169,7 @@ void QgsContinuousColorDialog::apply()
167169
}
168170
else
169171
{
170-
maximumString = QString::number( maximum );
172+
maximumString = QString::number( maximum, 'f', 0 );
171173
}
172174
QgsSymbol* maxsymbol = new QgsSymbol( mVectorLayer->geometryType(), maximumString, "", "" );
173175
QPen maxPen;

src/app/qgsgraduatedsymboldialog.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ QgsGraduatedSymbolDialog::QgsGraduatedSymbolDialog( QgsVectorLayer * layer ): QD
3737

3838
//find out the numerical fields of mVectorLayer
3939
const QgsFieldMap & fields = layer->pendingFields();
40-
QString str;
40+
QString displayName;
4141

4242
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
4343
{
4444
QVariant::Type type = ( *it ).type();
4545
if ( type == QVariant::Int || type == QVariant::Double )
4646
{
47-
classificationComboBox->addItem( it->name() );
48-
mFieldMap.insert( std::make_pair( it->name(), it.key() ) );
47+
displayName = layer->attributeDisplayName(it.key());
48+
classificationComboBox->addItem( displayName );
49+
mFieldMap.insert( std::make_pair( displayName, it.key() ) );
4950
}
5051
}
5152

@@ -350,7 +351,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
350351
}
351352
else
352353
{
353-
lowerString = QString::number(*last_it);
354+
lowerString = QString::number(*last_it, 'f', 0);
354355
}
355356
( *symbol_it )->setLowerValue(lowerString);
356357

@@ -360,7 +361,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
360361
}
361362
else
362363
{
363-
upperString = QString::number(*it);
364+
upperString = QString::number(*it, 'f', 0);
364365
}
365366
( *symbol_it )->setUpperValue(upperString);
366367

@@ -391,7 +392,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
391392
}
392393
else
393394
{
394-
lowerString = QString::number(lower);
395+
lowerString = QString::number(lower, 'f', 0);
395396
}
396397

397398
( *symbol_it )->setLowerValue(lowerString);
@@ -403,7 +404,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
403404
}
404405
else
405406
{
406-
upperString = QString::number(upper);
407+
upperString = QString::number(upper, 'f', 0);
407408
}
408409

409410
( *symbol_it )->setUpperValue(upperString);

src/app/qgsmaptoolidentify.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,7 @@ void QgsMapToolIdentify::identifyVectorLayer( const QgsPoint& point )
337337
{
338338
featureNode->setText( 1, it->toString() );
339339
}
340-
QString attributeName = layer->attributeAlias(it.key());
341-
if(attributeName.isEmpty())
342-
{
343-
attributeName = fields[it.key()].name();
344-
}
340+
QString attributeName = layer->attributeDisplayName(it.key());
345341
mResults->addAttribute( featureNode, attributeName, it->isNull() ? "NULL" : it->toString() );
346342
}
347343

src/app/qgsuniquevaluedialog.cpp

+49-50
Original file line numberDiff line numberDiff line change
@@ -32,66 +32,65 @@ QgsUniqueValueDialog::QgsUniqueValueDialog( QgsVectorLayer* vl ): QDialog(), mVe
3232
{
3333
setupUi( this );
3434
setOrientation( Qt::Vertical );
35+
3536
//find out the fields of mVectorLayer
36-
QgsVectorDataProvider *provider;
37-
if (( provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() ) ) )
37+
if(mVectorLayer)
3838
{
39-
const QgsFieldMap & fields = provider->fields();
40-
QString str;
41-
42-
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
39+
//we cannot use unique values for not-commited fields because QgsVectorLayer has no 'unique values' method...
40+
QgsVectorDataProvider* provider = mVectorLayer->dataProvider();
41+
if(provider)
4342
{
44-
str = ( *it ).name();
45-
mClassificationComboBox->addItem( str );
43+
const QgsFieldMap & fields = provider->fields();
44+
QString str;
45+
46+
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
47+
{
48+
str = ( *it ).name();
49+
str = mVectorLayer->attributeDisplayName(it.key());
50+
mClassificationComboBox->addItem( str, it.key() );
51+
}
4652
}
4753
}
48-
else
49-
{
50-
QgsDebugMsg( "data provider is null" );
51-
return;
52-
}
54+
5355

5456
mClassListWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
5557
mClassListWidget->setEditTriggers( QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed | QAbstractItemView::AnyKeyPressed );
5658
mClassListWidget->setSortingEnabled( true );
5759

58-
const QgsUniqueValueRenderer* renderer = dynamic_cast < const QgsUniqueValueRenderer * >( mVectorLayer->renderer() );
59-
60-
if ( renderer )
60+
if(mVectorLayer)
6161
{
62-
mClassListWidget->clear();
63-
64-
// XXX - mloskot - fix for Ticket #31 (bug)
65-
//QgsAttributeList attributes = renderer->classificationAttributes();
66-
//QgsAttributeList::iterator iter = attributes.begin();
67-
//int classattr = *iter;
68-
//QString field = provider->fields()[ classattr ].name();
69-
QString field = provider->fields()[ renderer->classificationField()].name();
70-
mOldClassificationAttribute = field;
71-
mClassificationComboBox->setCurrentIndex( mClassificationComboBox->findText( field ) );
72-
73-
const QList<QgsSymbol*> list = renderer->symbols();
74-
//fill the items of the renderer into mValues
75-
for ( QList<QgsSymbol*>::const_iterator iter = list.begin(); iter != list.end(); ++iter )
62+
const QgsUniqueValueRenderer* renderer = dynamic_cast < const QgsUniqueValueRenderer * >( mVectorLayer->renderer() );
63+
64+
if ( renderer )
7665
{
77-
QgsSymbol* symbol = ( *iter );
78-
QString symbolvalue = symbol->lowerValue();
79-
QgsSymbol* sym = new QgsSymbol( mVectorLayer->geometryType(), symbol->lowerValue(), symbol->upperValue(), symbol->label() );
80-
sym->setPen( symbol->pen() );
81-
sym->setCustomTexture( symbol->customTexture() );
82-
sym->setBrush( symbol->brush() );
83-
sym->setNamedPointSymbol( symbol->pointSymbolName() );
84-
sym->setPointSize( symbol->pointSize() );
85-
sym->setScaleClassificationField( symbol->scaleClassificationField() );
86-
sym->setRotationClassificationField( symbol->rotationClassificationField() );
87-
mValues.insert( symbolvalue, sym );
88-
89-
QListWidgetItem *item = new QListWidgetItem( symbolvalue );
90-
mClassListWidget->addItem( item );
91-
updateEntryIcon( symbol, item );
92-
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
93-
item->setData( Qt::UserRole, symbol->lowerValue() );
94-
item->setToolTip( symbol->label() );
66+
mClassListWidget->clear();
67+
QString field = mVectorLayer->attributeDisplayName(renderer->classificationField());
68+
mOldClassificationAttribute = field;
69+
mClassificationComboBox->setCurrentIndex( mClassificationComboBox->findText( field ) );
70+
71+
const QList<QgsSymbol*> list = renderer->symbols();
72+
//fill the items of the renderer into mValues
73+
for ( QList<QgsSymbol*>::const_iterator iter = list.begin(); iter != list.end(); ++iter )
74+
{
75+
QgsSymbol* symbol = ( *iter );
76+
QString symbolvalue = symbol->lowerValue();
77+
QgsSymbol* sym = new QgsSymbol( mVectorLayer->geometryType(), symbol->lowerValue(), symbol->upperValue(), symbol->label() );
78+
sym->setPen( symbol->pen() );
79+
sym->setCustomTexture( symbol->customTexture() );
80+
sym->setBrush( symbol->brush() );
81+
sym->setNamedPointSymbol( symbol->pointSymbolName() );
82+
sym->setPointSize( symbol->pointSize() );
83+
sym->setScaleClassificationField( symbol->scaleClassificationField() );
84+
sym->setRotationClassificationField( symbol->rotationClassificationField() );
85+
mValues.insert( symbolvalue, sym );
86+
87+
QListWidgetItem *item = new QListWidgetItem( symbolvalue );
88+
mClassListWidget->addItem( item );
89+
updateEntryIcon( symbol, item );
90+
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
91+
item->setData( Qt::UserRole, symbol->lowerValue() );
92+
item->setToolTip( symbol->label() );
93+
}
9594
}
9695
}
9796

@@ -149,7 +148,7 @@ void QgsUniqueValueDialog::apply()
149148
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() );
150149
if ( provider )
151150
{
152-
int fieldIndex = provider->fieldNameIndex( mClassificationComboBox->currentText() );
151+
int fieldIndex = mClassificationComboBox->itemData( mClassificationComboBox->currentIndex() ).toInt();
153152
if ( fieldIndex != -1 )
154153
{
155154
renderer->setClassificationField( fieldIndex );
@@ -292,7 +291,7 @@ void QgsUniqueValueDialog::changeClassificationAttribute()
292291
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() );
293292
if ( provider )
294293
{
295-
int nr = provider->fieldNameIndex( attributeName );
294+
int nr = mClassificationComboBox->itemData(mClassificationComboBox->currentIndex()).toInt();
296295
if ( nr == -1 )
297296
{
298297
return;

src/core/composer/qgslegendmodel.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,17 @@ int QgsLegendModel::addVectorLayerItems( QStandardItem* layerItem, QgsMapLayer*
102102
QSettings settings;
103103
if ( settings.value( "/qgis/showLegendClassifiers", false ).toBool() )
104104
{
105-
QgsVectorDataProvider* provider = vectorLayer->dataProvider();
106-
107-
if ( provider )
105+
QgsFieldMap layerFields = vectorLayer->pendingFields();
106+
QgsAttributeList attributes = vectorRenderer->classificationAttributes();
107+
QgsAttributeList::const_iterator att_it = attributes.constBegin();
108+
for ( ; att_it != attributes.constEnd(); ++att_it )
108109
{
109-
QgsFieldMap providerFields = provider->fields();
110-
QgsAttributeList attributes = vectorRenderer->classificationAttributes();
111-
QgsAttributeList::const_iterator att_it = attributes.constBegin();
112-
for ( ; att_it != attributes.constEnd(); ++att_it )
110+
QgsFieldMap::const_iterator fieldIt = layerFields.find( *att_it );
111+
if ( fieldIt != layerFields.constEnd() )
113112
{
114-
QgsFieldMap::const_iterator fieldIt = providerFields.find( *att_it );
115-
if ( fieldIt != providerFields.constEnd() )
116-
{
117-
QString attributeName = fieldIt.value().name();
118-
QStandardItem* attributeItem = new QStandardItem( attributeName );
119-
layerItem->setChild( layerItem->rowCount(), 0, attributeItem );
120-
}
113+
QString attributeName = vectorLayer->attributeDisplayName(fieldIt.key());
114+
QStandardItem* attributeItem = new QStandardItem( attributeName );
115+
layerItem->setChild( layerItem->rowCount(), 0, attributeItem );
121116
}
122117
}
123118
}

src/core/qgsvectorlayer.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,21 @@ QString QgsVectorLayer::attributeAlias(int attributeIndex) const
27322732
}
27332733
}
27342734

2735+
QString QgsVectorLayer::attributeDisplayName(int attributeIndex) const
2736+
{
2737+
QString displayName = attributeAlias(attributeIndex);
2738+
if(displayName.isEmpty())
2739+
{
2740+
const QgsFieldMap& fields = pendingFields();
2741+
QgsFieldMap::const_iterator fieldIt = fields.find( attributeIndex );
2742+
if ( fieldIt != fields.constEnd() )
2743+
{
2744+
displayName = fieldIt->name();
2745+
}
2746+
}
2747+
return displayName;
2748+
}
2749+
27352750
bool QgsVectorLayer::deleteAttribute( int index )
27362751
{
27372752
if ( !isEditable() )

src/core/qgsvectorlayer.h

+4
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
409409
@note added in version 1.2*/
410410
QString attributeAlias(int attributeIndex) const;
411411

412+
/**Convenience function that returns the attribute alias if defined or the field name else
413+
@note added in version 1.2*/
414+
QString attributeDisplayName(int attributeIndex) const;
415+
412416
/** delete an attribute field (but does not commit it) */
413417
bool deleteAttribute( int attr );
414418

0 commit comments

Comments
 (0)