[QGIS Commit] r10990 - in trunk/qgis/src: app app/attributetable core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Jul 1 03:29:10 EDT 2009


Author: mhugent
Date: 2009-07-01 03:29:08 -0400 (Wed, 01 Jul 2009)
New Revision: 10990

Modified:
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
   trunk/qgis/src/app/qgsattributedialog.cpp
   trunk/qgis/src/app/qgsmaptoolidentify.cpp
   trunk/qgis/src/app/qgsvectorlayerproperties.cpp
   trunk/qgis/src/app/qgsvectorlayerproperties.h
   trunk/qgis/src/core/qgsvectorlayer.cpp
   trunk/qgis/src/core/qgsvectorlayer.h
Log:
[FEATURE] Possibility to add attribute aliases for vector layers. The aliases are shown in the infotool and attribute table to have more descriptive names

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-07-01 07:29:08 UTC (rev 10990)
@@ -144,6 +144,7 @@
 
   loadLayer();
   emit modelChanged();
+  emit headerDataChanged ( Qt::Horizontal, 0, columnCount());
 }
 
 void QgsAttributeTableModel::loadLayer()
@@ -272,8 +273,13 @@
     }
     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();

Modified: trunk/qgis/src/app/qgsattributedialog.cpp
===================================================================
--- trunk/qgis/src/app/qgsattributedialog.cpp	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/app/qgsattributedialog.cpp	2009-07-01 07:29:08 UTC (rev 10990)
@@ -101,7 +101,13 @@
         ++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 );

Modified: trunk/qgis/src/app/qgsmaptoolidentify.cpp
===================================================================
--- trunk/qgis/src/app/qgsmaptoolidentify.cpp	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/app/qgsmaptoolidentify.cpp	2009-07-01 07:29:08 UTC (rev 10990)
@@ -337,7 +337,12 @@
       {
         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 @@
       }
     }
   }
-}
\ No newline at end of file
+}

Modified: trunk/qgis/src/app/qgsvectorlayerproperties.cpp
===================================================================
--- trunk/qgis/src/app/qgsvectorlayerproperties.cpp	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/app/qgsvectorlayerproperties.cpp	2009-07-01 07:29:08 UTC (rev 10990)
@@ -146,7 +146,7 @@
 
   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 @@
   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 @@
   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 @@
                           )
     );
   }
+
+  //set the alias for the attribute
+  tblAttributes->setItem( row, 8, new QTableWidgetItem(layer->attributeAlias(idx)));
 }
 
 
@@ -381,6 +385,8 @@
 //! @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 @@
   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_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;

Modified: trunk/qgis/src/app/qgsvectorlayerproperties.h
===================================================================
--- trunk/qgis/src/app/qgsvectorlayerproperties.h	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/app/qgsvectorlayerproperties.h	2009-07-01 07:29:08 UTC (rev 10990)
@@ -96,6 +96,7 @@
     void on_pbnSaveDefaultStyle_clicked();
     void on_pbnLoadStyle_clicked();
     void on_pbnSaveStyleAs_clicked();
+    void on_tblAttributes_cellChanged(int row, int column);
 
     void addAttribute();
     void deleteAttribute();

Modified: trunk/qgis/src/core/qgsvectorlayer.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.cpp	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/core/qgsvectorlayer.cpp	2009-07-01 07:29:08 UTC (rev 10990)
@@ -2401,6 +2401,24 @@
     }
   }
 
+  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 @@
     node.appendChild( classificationElement );
   }
 
+  //edit types
   if ( mEditTypes.size() > 0 )
   {
     QDomElement editTypesElement = doc.createElement( "edittypes" );
@@ -2533,6 +2552,21 @@
     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 @@
   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 @@
   mDeletedAttributeIds.insert( index );
   mAddedAttributeIds.remove( index );
   mUpdatedFields.remove( index );
+  mAttributeAliasMap.remove(index);
 
   setModified( true, false );
 

Modified: trunk/qgis/src/core/qgsvectorlayer.h
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.h	2009-06-30 14:48:27 UTC (rev 10989)
+++ trunk/qgis/src/core/qgsvectorlayer.h	2009-07-01 07:29:08 UTC (rev 10990)
@@ -401,6 +401,14 @@
       @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 @@
     /** 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;
 



More information about the QGIS-commit mailing list