Skip to content
Permalink
Browse files

[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
  • Loading branch information
mhugent
mhugent committed Jul 1, 2009
1 parent 04e8f61 commit 9fda5e4b33a446bd520753e06dd79fd96d851fe2
@@ -144,6 +144,7 @@ void QgsAttributeTableModel::layerModified( bool onlyGeometry )

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

void QgsAttributeTableModel::loadLayer()
@@ -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();
@@ -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 );
@@ -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:
@@ -531,4 +536,4 @@ void QgsMapToolIdentify::removeLayer( QString layerID )
}
}
}
}
}
@@ -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" ) ) );
@@ -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 );
@@ -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 );
@@ -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)));
}


@@ -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 "
@@ -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()


@@ -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;
@@ -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();
@@ -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" );
@@ -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" );
@@ -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() );
@@ -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() )
@@ -2699,6 +2752,7 @@ bool QgsVectorLayer::deleteAttribute( int index )
mDeletedAttributeIds.insert( index );
mAddedAttributeIds.remove( index );
mUpdatedFields.remove( index );
mAttributeAliasMap.remove(index);

setModified( true, false );

@@ -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 );

@@ -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;

0 comments on commit 9fda5e4

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