Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port more field iterating to Q_FOREACH, avoid some detachments
  • Loading branch information
nyalldawson committed Mar 9, 2016
1 parent 0e5214c commit 1563193
Show file tree
Hide file tree
Showing 27 changed files with 75 additions and 89 deletions.
8 changes: 4 additions & 4 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -147,10 +147,10 @@ void QgsAtlasComposition::setSortKeyAttributeIndex( int idx )
{
if ( mCoverageLayer )
{
const QgsFields fields = mCoverageLayer->fields();
QgsFields fields = mCoverageLayer->fields();
if ( idx >= 0 && idx < fields.count() )
{
mSortKeyAttributeName = fields[idx].name();
mSortKeyAttributeName = fields.at( idx ).name();
return;
}
}
Expand Down Expand Up @@ -714,10 +714,10 @@ void QgsAtlasComposition::readXML( const QDomElement& atlasElem, const QDomDocum
int idx = mSortKeyAttributeName.toInt( &isIndex );
if ( isIndex && mCoverageLayer )
{
const QgsFields fields = mCoverageLayer->fields();
QgsFields fields = mCoverageLayer->fields();
if ( idx >= 0 && idx < fields.count() )
{
mSortKeyAttributeName = fields[idx].name();
mSortKeyAttributeName = fields.at( idx ).name();
}
}
mSortAscending = atlasElem.attribute( "sortAscending", "true" ) == "true" ? true : false;
Expand Down
13 changes: 8 additions & 5 deletions src/core/composer/qgscomposerattributetable.cpp
Expand Up @@ -181,14 +181,15 @@ void QgsComposerAttributeTable::resetColumns()
mColumns.clear();

//rebuild columns list from vector layer fields
const QgsFields& fields = mVectorLayer->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
int idx = 0;
Q_FOREACH ( const QgsField& field, mVectorLayer->fields() )
{
QString currentAlias = mVectorLayer->attributeDisplayName( idx );
QgsComposerTableColumn* col = new QgsComposerTableColumn;
col->setAttribute( fields[idx].name() );
col->setAttribute( field.name() );
col->setHeading( currentAlias );
mColumns.append( col );
idx++;
}
}

Expand Down Expand Up @@ -296,13 +297,15 @@ void QgsComposerAttributeTable::setDisplayAttributes( const QSet<int>& attr, boo
else
{
//resetting, so add all attributes to columns
for ( int idx = 0; idx < fields.count(); ++idx )
int idx = 0;
Q_FOREACH ( const QgsField& field, fields )
{
QString currentAlias = mVectorLayer->attributeDisplayName( idx );
QgsComposerTableColumn* col = new QgsComposerTableColumn;
col->setAttribute( fields[idx].name() );
col->setAttribute( field.name() );
col->setHeading( currentAlias );
mColumns.append( col );
idx++;
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/core/composer/qgscomposerattributetablev2.cpp
Expand Up @@ -253,14 +253,15 @@ void QgsComposerAttributeTableV2::resetColumns()
mColumns.clear();

//rebuild columns list from vector layer fields
const QgsFields& fields = source->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
int idx = 0;
Q_FOREACH ( const QgsField& field, source->fields() )
{
QString currentAlias = source->attributeDisplayName( idx );
QgsComposerTableColumn* col = new QgsComposerTableColumn;
col->setAttribute( fields[idx].name() );
col->setAttribute( field.name() );
col->setHeading( currentAlias );
mColumns.append( col );
idx++;
}
}

Expand Down Expand Up @@ -393,13 +394,15 @@ void QgsComposerAttributeTableV2::setDisplayAttributes( const QSet<int>& attr, b
else
{
//resetting, so add all attributes to columns
for ( int idx = 0; idx < fields.count(); ++idx )
int idx = 0;
Q_FOREACH ( const QgsField& field, fields )
{
QString currentAlias = source->attributeDisplayName( idx );
QgsComposerTableColumn* col = new QgsComposerTableColumn;
col->setAttribute( fields[idx].name() );
col->setAttribute( field.name() );
col->setHeading( currentAlias );
mColumns.append( col );
idx++;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -29,7 +29,7 @@ QString QgsEditFormConfig::widgetType( int fieldIdx ) const
if ( fieldIdx < 0 || fieldIdx >= mFields.count() )
return "TextEdit";

return mEditorWidgetV2Types.value( mFields[fieldIdx].name(), "TextEdit" );
return mEditorWidgetV2Types.value( mFields.at( fieldIdx ).name(), "TextEdit" );
}

QString QgsEditFormConfig::widgetType( const QString& fieldName ) const
Expand All @@ -42,7 +42,7 @@ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( int fieldIdx ) const
if ( fieldIdx < 0 || fieldIdx >= mFields.count() )
return QgsEditorWidgetConfig();

return mWidgetConfigs.value( mFields[fieldIdx].name() );
return mWidgetConfigs.value( mFields.at( fieldIdx ).name() );
}

QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( const QString& widgetName ) const
Expand Down Expand Up @@ -82,7 +82,7 @@ bool QgsEditFormConfig::removeWidgetConfig( int fieldIdx )
if ( fieldIdx < 0 || fieldIdx >= mFields.count() )
return false;

return mWidgetConfigs.remove( mFields[fieldIdx].name() );
return mWidgetConfigs.remove( mFields.at( fieldIdx ).name() );
}

void QgsEditFormConfig::setUiForm( const QString& ui )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgslabel.cpp
Expand Up @@ -504,7 +504,7 @@ QString QgsLabel::labelField( int attr ) const
int fieldIndex = mLabelFieldIdx[attr];
if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
return QString();
return mFields[fieldIndex].name();
return mFields.at( fieldIndex ).name();
}

QgsLabelAttributes *QgsLabel::labelAttributes( void )
Expand Down
9 changes: 4 additions & 5 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -461,11 +461,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
// create table
QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName );
QString delim = "";
const QgsFields& fields = layer->dataProvider()->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
Q_FOREACH ( const QgsField& field, layer->dataProvider()->fields() )
{
QString dataType = "";
QVariant::Type type = fields[idx].type();
QVariant::Type type = field.type();
if ( type == QVariant::Int || type == QVariant::LongLong )
{
dataType = "INTEGER";
Expand All @@ -480,10 +479,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
}
else
{
showWarning( tr( "%1: Unknown data type %2. Not using type affinity for the field." ).arg( fields[idx].name(), QVariant::typeToName( type ) ) );
showWarning( tr( "%1: Unknown data type %2. Not using type affinity for the field." ).arg( field.name(), QVariant::typeToName( type ) ) );
}

sql += delim + QString( "'%1' %2" ).arg( fields[idx].name(), dataType );
sql += delim + QString( "'%1' %2" ).arg( field.name(), dataType );
delim = ',';
}
sql += ')';
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -254,9 +254,9 @@ void QgsVectorLayer::setDisplayField( const QString& fldName )
{
int fieldsSize = mUpdatedFields.size();

for ( int idx = 0; idx < mUpdatedFields.count(); ++idx )
Q_FOREACH ( const QgsField& field, mUpdatedFields )
{
QString fldName = mUpdatedFields.at( idx ).name();
QString fldName = field.name();
QgsDebugMsg( "Checking field " + fldName + " of " + QString::number( fieldsSize ) + " total" );

// Check the fields and keep the first one that matches.
Expand Down Expand Up @@ -2158,7 +2158,7 @@ QString QgsVectorLayer::attributeDisplayName( int attributeIndex ) const
{
if ( attributeIndex >= 0 && attributeIndex < mUpdatedFields.count() )
{
displayName = mUpdatedFields[attributeIndex].name();
displayName = mUpdatedFields.at( attributeIndex ).name();
}
}
return displayName;
Expand Down
5 changes: 2 additions & 3 deletions src/core/qgsvectorlayereditbuffer.cpp
Expand Up @@ -225,10 +225,9 @@ bool QgsVectorLayerEditBuffer::addAttribute( const QgsField &field )
if ( field.name().isEmpty() )
return false;

const QgsFields& updatedFields = L->fields();
for ( int idx = 0; idx < updatedFields.count(); ++idx )
Q_FOREACH ( const QgsField& updatedField, L->fields() )
{
if ( updatedFields[idx].name() == field.name() )
if ( updatedField.name() == field.name() )
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -240,14 +240,14 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
for ( int idx = 0; idx < joinFields.count(); ++idx )
{
// if using just a subset of fields, filter some of them out
if ( hasSubset && !subset.contains( joinFields[idx].name() ) )
if ( hasSubset && !subset.contains( joinFields.at( idx ).name() ) )
continue;

//skip the join field to avoid double field names (fields often have the same name)
// when using subset of field, use all the selected fields
if ( hasSubset || joinFields[idx].name() != joinFieldName )
if ( hasSubset || joinFields.at( idx ).name() != joinFieldName )
{
QgsField f = joinFields[idx];
QgsField f = joinFields.at( idx );
f.setName( prefix + f.name() );
fields.append( f, QgsFields::OriginJoin, idx + ( joinIdx*1000 ) );
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgsvirtuallayerdefinition.cpp
Expand Up @@ -201,9 +201,8 @@ QUrl QgsVirtualLayerDefinition::toUrl() const
url.addQueryItem( "geometry", geometryField() );
}

for ( int i = 0; i < fields().count(); i++ )
Q_FOREACH ( const QgsField& f, fields() )
{
const QgsField& f = fields()[i];
if ( f.type() == QVariant::Int )
url.addQueryItem( "field", f.name() + ":int" );
else if ( f.type() == QVariant::Double )
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgsvirtuallayerdefinitionutils.cpp
Expand Up @@ -64,10 +64,8 @@ QgsVirtualLayerDefinition QgsVirtualLayerDefinitionUtils::fromJoinedLayer( QgsVe
}
else
{
const QgsFields& joinedFields = layer->dataProvider()->fields();
for ( int i = 0; i < joinedFields.count(); i++ )
Q_FOREACH ( const QgsField& f, layer->dataProvider()->fields() )
{
const QgsField& f = joinedFields.field( i );
columns << joinName + "." + f.name() + " AS " + prefix + f.name();
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/gui/qgsdatadefinedbutton.cpp
Expand Up @@ -166,10 +166,8 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
if ( mVectorLayer )
{
// store just a list of fields of unknown type or those that match the expected type
const QgsFields& fields = mVectorLayer->fields();
for ( int i = 0; i < fields.count(); ++i )
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
{
const QgsField& f = fields.at( i );
bool fieldMatch = false;
// NOTE: these are the only QVariant enums supported at this time (see QgsField)
QString fieldType;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -301,7 +301,7 @@ void QgsExpressionBuilderWidget::loadFieldNames( const QgsFields& fields )
fieldNames.reserve( fields.count() );
for ( int i = 0; i < fields.count(); ++i )
{
QString fieldName = fields[i].name();
QString fieldName = fields.at( i ).name();
fieldNames << fieldName;
registerItem( "Fields and Values", fieldName, " \"" + fieldName + "\" ", "", QgsExpressionItem::Field, false, i );
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsformannotationitem.cpp
Expand Up @@ -99,7 +99,7 @@ QWidget* QgsFormAnnotationItem::createDesignerWidget( const QString& filePath )
{
if ( i < fields.count() )
{
QWidget* attWidget = widget->findChild<QWidget*>( fields[i].name() );
QWidget* attWidget = widget->findChild<QWidget*>( fields.at( i ).name() );
if ( attWidget )
{
QgsAttributeEditor::createAttributeEditor( widget, attWidget, mVectorLayer, i, attrs.at( i ) );
Expand Down
5 changes: 2 additions & 3 deletions src/gui/symbology-ng/qgspointdisplacementrendererwidget.cpp
Expand Up @@ -68,10 +68,9 @@ QgsPointDisplacementRendererWidget::QgsPointDisplacementRendererWidget( QgsVecto
//insert attributes into combo box
if ( layer )
{
const QgsFields& layerAttributes = layer->fields();
for ( int idx = 0; idx < layerAttributes.count(); ++idx )
Q_FOREACH ( const QgsField& f, layer->fields() )
{
mLabelFieldComboBox->addItem( layerAttributes[idx].name() );
mLabelFieldComboBox->addItem( f.name() );
}
mLabelFieldComboBox->addItem( tr( "None" ) );

Expand Down
4 changes: 1 addition & 3 deletions src/gui/symbology-ng/qgsrendererv2widget.cpp
Expand Up @@ -340,10 +340,8 @@ void QgsRendererV2DataDefinedMenus::populateMenu( QMenu* menu, const QString& fi
menu->addSeparator();

bool hasField = false;
const QgsFields & flds = mLayer->fields();
for ( int idx = 0; idx < flds.count(); ++idx )
Q_FOREACH ( const QgsField& fld, mLayer->fields() )
{
const QgsField& fld = flds[idx];
if ( fld.type() == QVariant::Int || fld.type() == QVariant::Double )
{
QAction* a = new QAction( fld.name(), actionGroup );
Expand Down
5 changes: 2 additions & 3 deletions src/gui/symbology-ng/qgsvectorfieldsymbollayerwidget.cpp
Expand Up @@ -24,12 +24,11 @@ QgsVectorFieldSymbolLayerWidget::QgsVectorFieldSymbolLayerWidget( const QgsVecto

if ( mVectorLayer )
{
const QgsFields& fm = mVectorLayer->fields();
mXAttributeComboBox->addItem( "" );
mYAttributeComboBox->addItem( "" );
for ( int idx = 0; idx < fm.count(); ++idx )
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
{
QString fieldName = fm[idx].name();
QString fieldName = f.name();
mXAttributeComboBox->addItem( fieldName );
mYAttributeComboBox->addItem( fieldName );
}
Expand Down
11 changes: 5 additions & 6 deletions src/plugins/grass/qgsgrassmoduleparam.cpp
Expand Up @@ -1192,16 +1192,15 @@ void QgsGrassModuleVectorField::updateFields()
{
continue;
}
QgsFields fields = mLayerInput->currentFields();

int index = 0;
for ( int i = 0; i < fields.size(); i++ )
Q_FOREACH ( const QgsField& field, mLayerInput->currentFields() )
{
if ( mType.contains( fields.at( i ).typeName() ) )
if ( mType.contains( field.typeName() ) )
{
comboBox->addItem( fields.at( i ).name() );
QgsDebugMsg( "current = " + current + " field = " + fields.at( i ).name() );
if ( fields.at( i ).name() == current )
comboBox->addItem( field.name() );
QgsDebugMsg( "current = " + current + " field = " + field.name() );
if ( field.name() == current )
{
comboBox->setCurrentIndex( index );
}
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/interpolation/qgsinterpolationdialog.cpp
Expand Up @@ -245,10 +245,8 @@ void QgsInterpolationDialog::on_mInputLayerComboBox_currentIndexChanged( const Q
}

//insert numeric attributes of layer into mInterpolationAttributesComboBox
const QgsFields& fields = provider->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
Q_FOREACH ( const QgsField& currentField, provider->fields() )
{
const QgsField& currentField = fields[idx];
QVariant::Type currentType = currentField.type();
if ( currentType == QVariant::Int || currentType == QVariant::Double )
{
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/roadgraph/linevectorlayerwidget.cpp
Expand Up @@ -209,10 +209,8 @@ void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
if ( !provider )
return;

const QgsFields& fields = provider->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
Q_FOREACH ( const QgsField& currentField, provider->fields() )
{
const QgsField& currentField = fields[idx];
QVariant currentType = currentField.type();
if ( currentType == QVariant::Int || currentType == QVariant::String )
{
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/zonal_statistics/qgszonalstatisticsdialog.cpp
Expand Up @@ -218,12 +218,11 @@ bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
return false;
}

const QgsFields& providerFields = dp->fields();
QString currentFieldName;

for ( int idx = 0; idx < providerFields.count(); ++idx )
Q_FOREACH ( const QgsField& field, dp->fields() )
{
currentFieldName = providerFields[idx].name();
currentFieldName = field.name();
if ( currentFieldName == ( prefix + "mean" ) || currentFieldName == ( prefix + "sum" ) || currentFieldName == ( prefix + "count" ) )
{
return false;
Expand Down
3 changes: 1 addition & 2 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -2480,9 +2480,8 @@ void QgsGrass::createTable( dbDriver *driver, const QString tableName, const Qgs
}

QStringList fieldsStringList;
for ( int i = 0; i < fields.size(); i++ )
Q_FOREACH ( const QgsField& field, fields )
{
QgsField field = fields.field( i );
QString name = field.name().toLower().replace( " ", "_" );
if ( name.at( 0 ).isDigit() )
{
Expand Down

0 comments on commit 1563193

Please sign in to comment.