Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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@10990 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jul 1, 2009
1 parent 6a9ae8f commit 047eb9d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/app/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -144,6 +144,7 @@ void QgsAttributeTableModel::layerModified( bool onlyGeometry )

loadLayer();
emit modelChanged();
emit headerDataChanged ( Qt::Horizontal, 0, columnCount());
}

void QgsAttributeTableModel::loadLayer()
Expand Down Expand Up @@ -272,8 +273,13 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
}
else
{
QgsField field = mLayer->pendingFields()[ mAttributes[section] ]; //column
return QVariant( field.name() );
QString attributeName = mLayer->attributeAlias( mAttributes[section] );
if(attributeName.isEmpty())
{
QgsField field = mLayer->pendingFields()[ mAttributes[section] ];
attributeName = field.name();
}
return QVariant(attributeName);
}
}
else return QVariant();
Expand Down
8 changes: 7 additions & 1 deletion src/app/qgsattributedialog.cpp
Expand Up @@ -101,7 +101,13 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
++it )
{
const QgsField &field = theFieldMap[it.key()];
QString myFieldName = field.name();

//show attribute alias if available
QString myFieldName = vl->attributeAlias(it.key());
if(myFieldName.isEmpty())
{
myFieldName = field.name();
}
int myFieldType = field.type();
QLabel * mypLabel = new QLabel();
mypInnerLayout->addWidget( mypLabel, index, 0 );
Expand Down
9 changes: 7 additions & 2 deletions src/app/qgsmaptoolidentify.cpp
Expand Up @@ -337,7 +337,12 @@ void QgsMapToolIdentify::identifyVectorLayer( const QgsPoint& point )
{
featureNode->setText( 1, it->toString() );
}
mResults->addAttribute( featureNode, fields[it.key()].name(), it->isNull() ? "NULL" : it->toString() );
QString attributeName = layer->attributeAlias(it.key());
if(attributeName.isEmpty())
{
attributeName = fields[it.key()].name();
}
mResults->addAttribute( featureNode, attributeName, it->isNull() ? "NULL" : it->toString() );
}

// Calculate derived attributes and insert:
Expand Down Expand Up @@ -531,4 +536,4 @@ void QgsMapToolIdentify::removeLayer( QString layerID )
}
}
}
}
}
32 changes: 30 additions & 2 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -146,7 +146,7 @@ void QgsVectorLayerProperties::loadRows()

tblAttributes->clear();

tblAttributes->setColumnCount( 8 );
tblAttributes->setColumnCount( 9 );
tblAttributes->setRowCount( fields.size() );
tblAttributes->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "id" ) ) );
tblAttributes->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "name" ) ) );
Expand All @@ -156,6 +156,7 @@ void QgsVectorLayerProperties::loadRows()
tblAttributes->setHorizontalHeaderItem( 5, new QTableWidgetItem( tr( "comment" ) ) );
tblAttributes->setHorizontalHeaderItem( 6, new QTableWidgetItem( tr( "edit widget" ) ) );
tblAttributes->setHorizontalHeaderItem( 7, new QTableWidgetItem( tr( "values" ) ) );
tblAttributes->setHorizontalHeaderItem( 8, new QTableWidgetItem( tr( "alias" ) ) );

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

for ( int i = 0; i < 6; i++ )
tblAttributes->item( row, i )->setFlags( tblAttributes->item( row, i )->flags() & ~Qt::ItemIsEditable );
Expand Down Expand Up @@ -223,6 +224,9 @@ void QgsVectorLayerProperties::setRow( int row, int idx, const QgsField &field )
)
);
}

//set the alias for the attribute
tblAttributes->setItem( row, 8, new QTableWidgetItem(layer->attributeAlias(idx)));
}


Expand Down Expand Up @@ -381,6 +385,8 @@ void QgsVectorLayerProperties::setDisplayField( QString name )
//! @note in raster props, this method is called sync()
void QgsVectorLayerProperties::reset( void )
{
QObject::disconnect(tblAttributes, SIGNAL(cellChanged(int, int)), this, SLOT(on_tblAttributes_cellChanged(int,int)));

// populate the general information
txtDisplayName->setText( layer->name() );
pbnQueryBuilder->setWhatsThis( tr( "This button opens the PostgreSQL query "
Expand Down Expand Up @@ -487,6 +493,7 @@ void QgsVectorLayerProperties::reset( void )
sliderTransparency_valueChanged( 255 - layer->getTransparency() );

loadRows();
QObject::connect(tblAttributes, SIGNAL(cellChanged(int, int)), this, SLOT(on_tblAttributes_cellChanged(int,int)));
} // reset()


Expand Down Expand Up @@ -1066,6 +1073,27 @@ void QgsVectorLayerProperties::on_pbnSaveStyleAs_clicked()
}
}

void QgsVectorLayerProperties::on_tblAttributes_cellChanged(int row, int column)
{
if(column == 8 && layer) //only consider attribute aliases in this function
{
const QgsFieldMap &fields = layer->pendingFields();
if(row >= fields.size())
{
return; //index must be wrong
}

QgsFieldMap::const_iterator f_it = fields.constBegin();
f_it += row;
int index = f_it.key();
QTableWidgetItem* aliasItem = tblAttributes->item(row, column);
if(aliasItem)
{
layer->addAttributeAlias(index, aliasItem->text());
}
}
}

QList<QgsVectorOverlayPlugin*> QgsVectorLayerProperties::overlayPlugins() const
{
QList<QgsVectorOverlayPlugin*> pluginList;
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -96,6 +96,7 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
void on_pbnSaveDefaultStyle_clicked();
void on_pbnLoadStyle_clicked();
void on_pbnSaveStyleAs_clicked();
void on_tblAttributes_cellChanged(int row, int column);

void addAttribute();
void deleteAttribute();
Expand Down
54 changes: 54 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2401,6 +2401,24 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
}

mAttributeAliasMap.clear();
QDomNode aliasesNode = node.namedItem("aliases");
if(!aliasesNode.isNull())
{
QDomElement aliasElem;
int index;
QString name;

QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName("alias");
for(int i = 0; i < aliasNodeList.size(); ++i)
{
aliasElem = aliasNodeList.at(i).toElement();
index = aliasElem.attribute("index").toInt();
name = aliasElem.attribute("name");
mAttributeAliasMap.insert(index, name);
}
}

// create and bind a renderer to this layer

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

//edit types
if ( mEditTypes.size() > 0 )
{
QDomElement editTypesElement = doc.createElement( "edittypes" );
Expand Down Expand Up @@ -2533,6 +2552,21 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
node.appendChild( editTypesElement );
}

//attribute aliases
if(mAttributeAliasMap.size() > 0)
{
QDomElement aliasElem = doc.createElement("aliases");
QMap<int, QString>::const_iterator a_it = mAttributeAliasMap.constBegin();
for(; a_it != mAttributeAliasMap.constEnd(); ++a_it)
{
QDomElement aliasEntryElem = doc.createElement("alias");
aliasEntryElem.setAttribute("index", QString::number(a_it.key()));
aliasEntryElem.setAttribute("name", a_it.value());
aliasElem.appendChild(aliasEntryElem);
}
node.appendChild(aliasElem);
}

// add the display field
QDomElement dField = doc.createElement( "displayfield" );
QDomText dFieldText = doc.createTextNode( displayField() );
Expand Down Expand Up @@ -2679,6 +2713,25 @@ bool QgsVectorLayer::addAttribute( QString name, QString type )
return addAttribute( QgsField( name, map[ type ], type ) );
}

void QgsVectorLayer::addAttributeAlias(int attIndex, QString aliasString)
{
mAttributeAliasMap.insert(attIndex, aliasString);
emit layerModified(false);
}

QString QgsVectorLayer::attributeAlias(int attributeIndex) const
{
QMap<int, QString>::const_iterator alias_it = mAttributeAliasMap.find(attributeIndex);
if(alias_it != mAttributeAliasMap.constEnd())
{
return alias_it.value();
}
else
{
return QString();
}
}

bool QgsVectorLayer::deleteAttribute( int index )
{
if ( !isEditable() )
Expand All @@ -2699,6 +2752,7 @@ bool QgsVectorLayer::deleteAttribute( int index )
mDeletedAttributeIds.insert( index );
mAddedAttributeIds.remove( index );
mUpdatedFields.remove( index );
mAttributeAliasMap.remove(index);

setModified( true, false );

Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -401,6 +401,14 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
@note deprecated */
bool addAttribute( QString name, QString type );

/**Sets an alias (a display name) for attributes to display in dialogs
@note added in version 1.2*/
void addAttributeAlias(int attIndex, QString aliasString);

/**Returns the alias of an attribute name or an empty string if there is no alias
@note added in version 1.2*/
QString attributeAlias(int attributeIndex) const;

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

Expand Down Expand Up @@ -682,6 +690,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** field map to commit */
QgsFieldMap mUpdatedFields;

/**Map that stores the aliases for attributes. Key is the attribute index and value the alias for that attribute*/
QMap<int, QString> mAttributeAliasMap;

/** max field index */
int mMaxUpdatedIndex;

Expand Down

0 comments on commit 047eb9d

Please sign in to comment.