Skip to content

Commit

Permalink
[FEATURE] add QgsVectorLayer::fieldNameIndex()
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12344 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 6, 2009
1 parent 8006e3c commit 60d9cca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -329,7 +329,7 @@ void QgsVectorLayer::drawLabels( QgsRenderContext& rendererContext )
{
foreach( QString attrName, mRendererV2->usedAttributes() )
{
int attrNum = QgsFeatureRendererV2::fieldNameIndex( pendingFields(), attrName );
int attrNum = fieldNameIndex( attrName );
attributes.append( attrNum );
}
// make sure the renderer is ready for classification ("symbolForFeature")
Expand Down Expand Up @@ -831,7 +831,7 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
QgsAttributeList attributes;
foreach( QString attrName, mRendererV2->usedAttributes() )
{
int attrNum = QgsFeatureRendererV2::fieldNameIndex( pendingFields(), attrName );
int attrNum = fieldNameIndex( attrName );
attributes.append( attrNum );
QgsDebugMsg( "attrs: " + attrName + " - " + QString::number( attrNum ) );
}
Expand Down Expand Up @@ -4468,3 +4468,17 @@ QPair<QString, QString> QgsVectorLayer::checkedState( int idx )
else
return QPair<QString, QString>( "1", "0" );
}

int QgsVectorLayer::fieldNameIndex( const QString& fieldName ) const
{
const QgsFieldMap &theFields = pendingFields();

for ( QgsFieldMap::const_iterator it = theFields.constBegin(); it != theFields.constEnd(); ++it )
{
if ( it->name() == fieldName )
{
return it.key();
}
}
return -1;
}
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -528,6 +528,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Execute redo operation. To be called only from QgsVectorLayerUndoCommand. */
void redoEditCommand( QgsUndoCommand* cmd );

/** Returns the index of a field name or -1 if the field does not exist
@note this method was added in version 1.4
*/
int fieldNameIndex( const QString& fieldName ) const;

public slots:
/** Select feature by its ID, optionally emit signal selectionChanged() */
void select( int featureId, bool emitSignal = TRUE );
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Expand Up @@ -267,7 +267,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
{
QgsVectorDataProvider* provider = vlayer->dataProvider();

int attrNum = fieldNameIndex( vlayer->pendingFields(), attrName );
int attrNum = vlayer->fieldNameIndex( attrName );

double minimum = provider->minimumValue( attrNum ).toDouble();
double maximum = provider->maximumValue( attrNum ).toDouble();
Expand Down
Expand Up @@ -242,7 +242,7 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q
void QgsCategorizedSymbolRendererV2Widget::addCategories()
{
QString attrName = cboCategorizedColumn->currentText();
int idx = QgsFeatureRendererV2::fieldNameIndex( mLayer->pendingFields(), attrName );
int idx = mLayer->fieldNameIndex( attrName );
QList<QVariant> unique_vals;
mLayer->dataProvider()->uniqueValues( idx, unique_vals );

Expand Down

0 comments on commit 60d9cca

Please sign in to comment.