Skip to content
Permalink
Browse files

Added a convenience to retrieve the attribute display name from QgsVe…

…ctorLayer. Use this function in renderer dialogs, info tool, attribute table and composer legend

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10992 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent
mhugent committed Jul 1, 2009
1 parent 023c6db commit b2d1d0de0b582a1e429b0e539bd59207b5dfd508
@@ -370,11 +370,14 @@ void QgsLegendLayer::vectorLayerSymbology( const QgsVectorLayer* layer, double w
if ( renderer->needsAttributes() )
{
QgsAttributeList classfieldlist = renderer->classificationAttributes();
const QgsFieldMap& fields = layer->dataProvider()->fields();
const QgsFieldMap& fields = layer->pendingFields();
for ( QgsAttributeList::iterator it = classfieldlist.begin(); it != classfieldlist.end(); ++it )
{
const QgsField& theField = fields[*it];
QString classfieldname = theField.name();
QString classfieldname = layer->attributeAlias(*it);
if(classfieldname.isEmpty())
{
classfieldname = fields[*it].name();
}
itemList.push_front( std::make_pair( classfieldname, QPixmap() ) );
}
}
@@ -103,11 +103,7 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
const QgsField &field = theFieldMap[it.key()];

//show attribute alias if available
QString myFieldName = vl->attributeAlias(it.key());
if(myFieldName.isEmpty())
{
myFieldName = field.name();
}
QString myFieldName = vl->attributeDisplayName(it.key());
int myFieldType = field.type();
QLabel * mypLabel = new QLabel();
mypInnerLayout->addWidget( mypLabel, index, 0 );
@@ -41,13 +41,15 @@ QgsContinuousColorDialog::QgsContinuousColorDialog( QgsVectorLayer * layer )

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

for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
{
QVariant::Type type = it->type();
if ( type == QVariant::Int || type == QVariant::Double )
{
classificationComboBox->addItem( it->name(), it.key() );
displayName = mVectorLayer->attributeDisplayName(it.key());
classificationComboBox->addItem( displayName, it.key() );
}
}

@@ -144,7 +146,7 @@ void QgsContinuousColorDialog::apply()
}
else
{
minimumString = QString::number( minimum );
minimumString = QString::number( minimum, 'f', 0 );
}
QgsSymbol* minsymbol = new QgsSymbol( mVectorLayer->geometryType(), minimumString, "", "" );
QPen minPen;
@@ -167,7 +169,7 @@ void QgsContinuousColorDialog::apply()
}
else
{
maximumString = QString::number( maximum );
maximumString = QString::number( maximum, 'f', 0 );
}
QgsSymbol* maxsymbol = new QgsSymbol( mVectorLayer->geometryType(), maximumString, "", "" );
QPen maxPen;
@@ -37,15 +37,16 @@ QgsGraduatedSymbolDialog::QgsGraduatedSymbolDialog( QgsVectorLayer * layer ): QD

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

for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
{
QVariant::Type type = ( *it ).type();
if ( type == QVariant::Int || type == QVariant::Double )
{
classificationComboBox->addItem( it->name() );
mFieldMap.insert( std::make_pair( it->name(), it.key() ) );
displayName = layer->attributeDisplayName(it.key());
classificationComboBox->addItem( displayName );
mFieldMap.insert( std::make_pair( displayName, it.key() ) );
}
}

@@ -350,7 +351,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
}
else
{
lowerString = QString::number(*last_it);
lowerString = QString::number(*last_it, 'f', 0);
}
( *symbol_it )->setLowerValue(lowerString);

@@ -360,7 +361,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
}
else
{
upperString = QString::number(*it);
upperString = QString::number(*it, 'f', 0);
}
( *symbol_it )->setUpperValue(upperString);

@@ -391,7 +392,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
}
else
{
lowerString = QString::number(lower);
lowerString = QString::number(lower, 'f', 0);
}

( *symbol_it )->setLowerValue(lowerString);
@@ -403,7 +404,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
}
else
{
upperString = QString::number(upper);
upperString = QString::number(upper, 'f', 0);
}

( *symbol_it )->setUpperValue(upperString);
@@ -337,11 +337,7 @@ void QgsMapToolIdentify::identifyVectorLayer( const QgsPoint& point )
{
featureNode->setText( 1, it->toString() );
}
QString attributeName = layer->attributeAlias(it.key());
if(attributeName.isEmpty())
{
attributeName = fields[it.key()].name();
}
QString attributeName = layer->attributeDisplayName(it.key());
mResults->addAttribute( featureNode, attributeName, it->isNull() ? "NULL" : it->toString() );
}

@@ -32,66 +32,65 @@ QgsUniqueValueDialog::QgsUniqueValueDialog( QgsVectorLayer* vl ): QDialog(), mVe
{
setupUi( this );
setOrientation( Qt::Vertical );

//find out the fields of mVectorLayer
QgsVectorDataProvider *provider;
if (( provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() ) ) )
if(mVectorLayer)
{
const QgsFieldMap & fields = provider->fields();
QString str;

for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
//we cannot use unique values for not-commited fields because QgsVectorLayer has no 'unique values' method...
QgsVectorDataProvider* provider = mVectorLayer->dataProvider();
if(provider)
{
str = ( *it ).name();
mClassificationComboBox->addItem( str );
const QgsFieldMap & fields = provider->fields();
QString str;

for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
{
str = ( *it ).name();
str = mVectorLayer->attributeDisplayName(it.key());
mClassificationComboBox->addItem( str, it.key() );
}
}
}
else
{
QgsDebugMsg( "data provider is null" );
return;
}


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

const QgsUniqueValueRenderer* renderer = dynamic_cast < const QgsUniqueValueRenderer * >( mVectorLayer->renderer() );

if ( renderer )
if(mVectorLayer)
{
mClassListWidget->clear();

// XXX - mloskot - fix for Ticket #31 (bug)
//QgsAttributeList attributes = renderer->classificationAttributes();
//QgsAttributeList::iterator iter = attributes.begin();
//int classattr = *iter;
//QString field = provider->fields()[ classattr ].name();
QString field = provider->fields()[ renderer->classificationField()].name();
mOldClassificationAttribute = field;
mClassificationComboBox->setCurrentIndex( mClassificationComboBox->findText( field ) );

const QList<QgsSymbol*> list = renderer->symbols();
//fill the items of the renderer into mValues
for ( QList<QgsSymbol*>::const_iterator iter = list.begin(); iter != list.end(); ++iter )
const QgsUniqueValueRenderer* renderer = dynamic_cast < const QgsUniqueValueRenderer * >( mVectorLayer->renderer() );

if ( renderer )
{
QgsSymbol* symbol = ( *iter );
QString symbolvalue = symbol->lowerValue();
QgsSymbol* sym = new QgsSymbol( mVectorLayer->geometryType(), symbol->lowerValue(), symbol->upperValue(), symbol->label() );
sym->setPen( symbol->pen() );
sym->setCustomTexture( symbol->customTexture() );
sym->setBrush( symbol->brush() );
sym->setNamedPointSymbol( symbol->pointSymbolName() );
sym->setPointSize( symbol->pointSize() );
sym->setScaleClassificationField( symbol->scaleClassificationField() );
sym->setRotationClassificationField( symbol->rotationClassificationField() );
mValues.insert( symbolvalue, sym );

QListWidgetItem *item = new QListWidgetItem( symbolvalue );
mClassListWidget->addItem( item );
updateEntryIcon( symbol, item );
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
item->setData( Qt::UserRole, symbol->lowerValue() );
item->setToolTip( symbol->label() );
mClassListWidget->clear();
QString field = mVectorLayer->attributeDisplayName(renderer->classificationField());
mOldClassificationAttribute = field;
mClassificationComboBox->setCurrentIndex( mClassificationComboBox->findText( field ) );

const QList<QgsSymbol*> list = renderer->symbols();
//fill the items of the renderer into mValues
for ( QList<QgsSymbol*>::const_iterator iter = list.begin(); iter != list.end(); ++iter )
{
QgsSymbol* symbol = ( *iter );
QString symbolvalue = symbol->lowerValue();
QgsSymbol* sym = new QgsSymbol( mVectorLayer->geometryType(), symbol->lowerValue(), symbol->upperValue(), symbol->label() );
sym->setPen( symbol->pen() );
sym->setCustomTexture( symbol->customTexture() );
sym->setBrush( symbol->brush() );
sym->setNamedPointSymbol( symbol->pointSymbolName() );
sym->setPointSize( symbol->pointSize() );
sym->setScaleClassificationField( symbol->scaleClassificationField() );
sym->setRotationClassificationField( symbol->rotationClassificationField() );
mValues.insert( symbolvalue, sym );

QListWidgetItem *item = new QListWidgetItem( symbolvalue );
mClassListWidget->addItem( item );
updateEntryIcon( symbol, item );
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
item->setData( Qt::UserRole, symbol->lowerValue() );
item->setToolTip( symbol->label() );
}
}
}

@@ -149,7 +148,7 @@ void QgsUniqueValueDialog::apply()
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() );
if ( provider )
{
int fieldIndex = provider->fieldNameIndex( mClassificationComboBox->currentText() );
int fieldIndex = mClassificationComboBox->itemData( mClassificationComboBox->currentIndex() ).toInt();
if ( fieldIndex != -1 )
{
renderer->setClassificationField( fieldIndex );
@@ -292,7 +291,7 @@ void QgsUniqueValueDialog::changeClassificationAttribute()
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() );
if ( provider )
{
int nr = provider->fieldNameIndex( attributeName );
int nr = mClassificationComboBox->itemData(mClassificationComboBox->currentIndex()).toInt();
if ( nr == -1 )
{
return;
@@ -102,22 +102,17 @@ int QgsLegendModel::addVectorLayerItems( QStandardItem* layerItem, QgsMapLayer*
QSettings settings;
if ( settings.value( "/qgis/showLegendClassifiers", false ).toBool() )
{
QgsVectorDataProvider* provider = vectorLayer->dataProvider();

if ( provider )
QgsFieldMap layerFields = vectorLayer->pendingFields();
QgsAttributeList attributes = vectorRenderer->classificationAttributes();
QgsAttributeList::const_iterator att_it = attributes.constBegin();
for ( ; att_it != attributes.constEnd(); ++att_it )
{
QgsFieldMap providerFields = provider->fields();
QgsAttributeList attributes = vectorRenderer->classificationAttributes();
QgsAttributeList::const_iterator att_it = attributes.constBegin();
for ( ; att_it != attributes.constEnd(); ++att_it )
QgsFieldMap::const_iterator fieldIt = layerFields.find( *att_it );
if ( fieldIt != layerFields.constEnd() )
{
QgsFieldMap::const_iterator fieldIt = providerFields.find( *att_it );
if ( fieldIt != providerFields.constEnd() )
{
QString attributeName = fieldIt.value().name();
QStandardItem* attributeItem = new QStandardItem( attributeName );
layerItem->setChild( layerItem->rowCount(), 0, attributeItem );
}
QString attributeName = vectorLayer->attributeDisplayName(fieldIt.key());
QStandardItem* attributeItem = new QStandardItem( attributeName );
layerItem->setChild( layerItem->rowCount(), 0, attributeItem );
}
}
}
@@ -2732,6 +2732,21 @@ QString QgsVectorLayer::attributeAlias(int attributeIndex) const
}
}

QString QgsVectorLayer::attributeDisplayName(int attributeIndex) const
{
QString displayName = attributeAlias(attributeIndex);
if(displayName.isEmpty())
{
const QgsFieldMap& fields = pendingFields();
QgsFieldMap::const_iterator fieldIt = fields.find( attributeIndex );
if ( fieldIt != fields.constEnd() )
{
displayName = fieldIt->name();
}
}
return displayName;
}

bool QgsVectorLayer::deleteAttribute( int index )
{
if ( !isEditable() )
@@ -409,6 +409,10 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
@note added in version 1.2*/
QString attributeAlias(int attributeIndex) const;

/**Convenience function that returns the attribute alias if defined or the field name else
@note added in version 1.2*/
QString attributeDisplayName(int attributeIndex) const;

/** delete an attribute field (but does not commit it) */
bool deleteAttribute( int attr );

0 comments on commit b2d1d0d

Please sign in to comment.
You can’t perform that action at this time.