Skip to content

Commit 9fda5e4

Browse files
author
mhugent
committed
[FEATURE] Possibility to add attribute aliases for vector layers. The aliases are shown in the infotool and attribute table to have more descriptive names
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10990 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 04e8f61 commit 9fda5e4

7 files changed

+118
-7
lines changed

src/app/attributetable/qgsattributetablemodel.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ void QgsAttributeTableModel::layerModified( bool onlyGeometry )
144144

145145
loadLayer();
146146
emit modelChanged();
147+
emit headerDataChanged ( Qt::Horizontal, 0, columnCount());
147148
}
148149

149150
void QgsAttributeTableModel::loadLayer()
@@ -272,8 +273,13 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
272273
}
273274
else
274275
{
275-
QgsField field = mLayer->pendingFields()[ mAttributes[section] ]; //column
276-
return QVariant( field.name() );
276+
QString attributeName = mLayer->attributeAlias( mAttributes[section] );
277+
if(attributeName.isEmpty())
278+
{
279+
QgsField field = mLayer->pendingFields()[ mAttributes[section] ];
280+
attributeName = field.name();
281+
}
282+
return QVariant(attributeName);
277283
}
278284
}
279285
else return QVariant();

src/app/qgsattributedialog.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
101101
++it )
102102
{
103103
const QgsField &field = theFieldMap[it.key()];
104-
QString myFieldName = field.name();
104+
105+
//show attribute alias if available
106+
QString myFieldName = vl->attributeAlias(it.key());
107+
if(myFieldName.isEmpty())
108+
{
109+
myFieldName = field.name();
110+
}
105111
int myFieldType = field.type();
106112
QLabel * mypLabel = new QLabel();
107113
mypInnerLayout->addWidget( mypLabel, index, 0 );

src/app/qgsmaptoolidentify.cpp

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

343348
// Calculate derived attributes and insert:
@@ -531,4 +536,4 @@ void QgsMapToolIdentify::removeLayer( QString layerID )
531536
}
532537
}
533538
}
534-
}
539+
}

src/app/qgsvectorlayerproperties.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void QgsVectorLayerProperties::loadRows()
146146

147147
tblAttributes->clear();
148148

149-
tblAttributes->setColumnCount( 8 );
149+
tblAttributes->setColumnCount( 9 );
150150
tblAttributes->setRowCount( fields.size() );
151151
tblAttributes->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "id" ) ) );
152152
tblAttributes->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "name" ) ) );
@@ -156,6 +156,7 @@ void QgsVectorLayerProperties::loadRows()
156156
tblAttributes->setHorizontalHeaderItem( 5, new QTableWidgetItem( tr( "comment" ) ) );
157157
tblAttributes->setHorizontalHeaderItem( 6, new QTableWidgetItem( tr( "edit widget" ) ) );
158158
tblAttributes->setHorizontalHeaderItem( 7, new QTableWidgetItem( tr( "values" ) ) );
159+
tblAttributes->setHorizontalHeaderItem( 8, new QTableWidgetItem( tr( "alias" ) ) );
159160

160161
tblAttributes->setSelectionBehavior( QAbstractItemView::SelectRows );
161162
tblAttributes->setSelectionMode( QAbstractItemView::MultiSelection );
@@ -174,7 +175,7 @@ void QgsVectorLayerProperties::setRow( int row, int idx, const QgsField &field )
174175
tblAttributes->setItem( row, 2, new QTableWidgetItem( field.typeName() ) );
175176
tblAttributes->setItem( row, 3, new QTableWidgetItem( QString::number( field.length() ) ) );
176177
tblAttributes->setItem( row, 4, new QTableWidgetItem( QString::number( field.precision() ) ) );
177-
tblAttributes->setItem( row, 5, new QTableWidgetItem( field.comment() ) );
178+
tblAttributes->setItem( row, 5, new QTableWidgetItem( field.comment() ) );
178179

179180
for ( int i = 0; i < 6; i++ )
180181
tblAttributes->item( row, i )->setFlags( tblAttributes->item( row, i )->flags() & ~Qt::ItemIsEditable );
@@ -223,6 +224,9 @@ void QgsVectorLayerProperties::setRow( int row, int idx, const QgsField &field )
223224
)
224225
);
225226
}
227+
228+
//set the alias for the attribute
229+
tblAttributes->setItem( row, 8, new QTableWidgetItem(layer->attributeAlias(idx)));
226230
}
227231

228232

@@ -381,6 +385,8 @@ void QgsVectorLayerProperties::setDisplayField( QString name )
381385
//! @note in raster props, this method is called sync()
382386
void QgsVectorLayerProperties::reset( void )
383387
{
388+
QObject::disconnect(tblAttributes, SIGNAL(cellChanged(int, int)), this, SLOT(on_tblAttributes_cellChanged(int,int)));
389+
384390
// populate the general information
385391
txtDisplayName->setText( layer->name() );
386392
pbnQueryBuilder->setWhatsThis( tr( "This button opens the PostgreSQL query "
@@ -487,6 +493,7 @@ void QgsVectorLayerProperties::reset( void )
487493
sliderTransparency_valueChanged( 255 - layer->getTransparency() );
488494

489495
loadRows();
496+
QObject::connect(tblAttributes, SIGNAL(cellChanged(int, int)), this, SLOT(on_tblAttributes_cellChanged(int,int)));
490497
} // reset()
491498

492499

@@ -1066,6 +1073,27 @@ void QgsVectorLayerProperties::on_pbnSaveStyleAs_clicked()
10661073
}
10671074
}
10681075

1076+
void QgsVectorLayerProperties::on_tblAttributes_cellChanged(int row, int column)
1077+
{
1078+
if(column == 8 && layer) //only consider attribute aliases in this function
1079+
{
1080+
const QgsFieldMap &fields = layer->pendingFields();
1081+
if(row >= fields.size())
1082+
{
1083+
return; //index must be wrong
1084+
}
1085+
1086+
QgsFieldMap::const_iterator f_it = fields.constBegin();
1087+
f_it += row;
1088+
int index = f_it.key();
1089+
QTableWidgetItem* aliasItem = tblAttributes->item(row, column);
1090+
if(aliasItem)
1091+
{
1092+
layer->addAttributeAlias(index, aliasItem->text());
1093+
}
1094+
}
1095+
}
1096+
10691097
QList<QgsVectorOverlayPlugin*> QgsVectorLayerProperties::overlayPlugins() const
10701098
{
10711099
QList<QgsVectorOverlayPlugin*> pluginList;

src/app/qgsvectorlayerproperties.h

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
9696
void on_pbnSaveDefaultStyle_clicked();
9797
void on_pbnLoadStyle_clicked();
9898
void on_pbnSaveStyleAs_clicked();
99+
void on_tblAttributes_cellChanged(int row, int column);
99100

100101
void addAttribute();
101102
void deleteAttribute();

src/core/qgsvectorlayer.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,24 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
24012401
}
24022402
}
24032403

2404+
mAttributeAliasMap.clear();
2405+
QDomNode aliasesNode = node.namedItem("aliases");
2406+
if(!aliasesNode.isNull())
2407+
{
2408+
QDomElement aliasElem;
2409+
int index;
2410+
QString name;
2411+
2412+
QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName("alias");
2413+
for(int i = 0; i < aliasNodeList.size(); ++i)
2414+
{
2415+
aliasElem = aliasNodeList.at(i).toElement();
2416+
index = aliasElem.attribute("index").toInt();
2417+
name = aliasElem.attribute("name");
2418+
mAttributeAliasMap.insert(index, name);
2419+
}
2420+
}
2421+
24042422
// create and bind a renderer to this layer
24052423

24062424
QDomNode singlenode = node.namedItem( "singlesymbol" );
@@ -2492,6 +2510,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
24922510
node.appendChild( classificationElement );
24932511
}
24942512

2513+
//edit types
24952514
if ( mEditTypes.size() > 0 )
24962515
{
24972516
QDomElement editTypesElement = doc.createElement( "edittypes" );
@@ -2533,6 +2552,21 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
25332552
node.appendChild( editTypesElement );
25342553
}
25352554

2555+
//attribute aliases
2556+
if(mAttributeAliasMap.size() > 0)
2557+
{
2558+
QDomElement aliasElem = doc.createElement("aliases");
2559+
QMap<int, QString>::const_iterator a_it = mAttributeAliasMap.constBegin();
2560+
for(; a_it != mAttributeAliasMap.constEnd(); ++a_it)
2561+
{
2562+
QDomElement aliasEntryElem = doc.createElement("alias");
2563+
aliasEntryElem.setAttribute("index", QString::number(a_it.key()));
2564+
aliasEntryElem.setAttribute("name", a_it.value());
2565+
aliasElem.appendChild(aliasEntryElem);
2566+
}
2567+
node.appendChild(aliasElem);
2568+
}
2569+
25362570
// add the display field
25372571
QDomElement dField = doc.createElement( "displayfield" );
25382572
QDomText dFieldText = doc.createTextNode( displayField() );
@@ -2679,6 +2713,25 @@ bool QgsVectorLayer::addAttribute( QString name, QString type )
26792713
return addAttribute( QgsField( name, map[ type ], type ) );
26802714
}
26812715

2716+
void QgsVectorLayer::addAttributeAlias(int attIndex, QString aliasString)
2717+
{
2718+
mAttributeAliasMap.insert(attIndex, aliasString);
2719+
emit layerModified(false);
2720+
}
2721+
2722+
QString QgsVectorLayer::attributeAlias(int attributeIndex) const
2723+
{
2724+
QMap<int, QString>::const_iterator alias_it = mAttributeAliasMap.find(attributeIndex);
2725+
if(alias_it != mAttributeAliasMap.constEnd())
2726+
{
2727+
return alias_it.value();
2728+
}
2729+
else
2730+
{
2731+
return QString();
2732+
}
2733+
}
2734+
26822735
bool QgsVectorLayer::deleteAttribute( int index )
26832736
{
26842737
if ( !isEditable() )
@@ -2699,6 +2752,7 @@ bool QgsVectorLayer::deleteAttribute( int index )
26992752
mDeletedAttributeIds.insert( index );
27002753
mAddedAttributeIds.remove( index );
27012754
mUpdatedFields.remove( index );
2755+
mAttributeAliasMap.remove(index);
27022756

27032757
setModified( true, false );
27042758

src/core/qgsvectorlayer.h

+11
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,14 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
401401
@note deprecated */
402402
bool addAttribute( QString name, QString type );
403403

404+
/**Sets an alias (a display name) for attributes to display in dialogs
405+
@note added in version 1.2*/
406+
void addAttributeAlias(int attIndex, QString aliasString);
407+
408+
/**Returns the alias of an attribute name or an empty string if there is no alias
409+
@note added in version 1.2*/
410+
QString attributeAlias(int attributeIndex) const;
411+
404412
/** delete an attribute field (but does not commit it) */
405413
bool deleteAttribute( int attr );
406414

@@ -682,6 +690,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
682690
/** field map to commit */
683691
QgsFieldMap mUpdatedFields;
684692

693+
/**Map that stores the aliases for attributes. Key is the attribute index and value the alias for that attribute*/
694+
QMap<int, QString> mAttributeAliasMap;
695+
685696
/** max field index */
686697
int mMaxUpdatedIndex;
687698

0 commit comments

Comments
 (0)