Skip to content
Permalink
Browse files

Port more field iterating to Q_FOREACH, avoid some detachments

  • Loading branch information
nyalldawson committed Mar 9, 2016
1 parent 0e5214c commit 1563193707dea625dffc7ca896d22a25d8679d17
@@ -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;
}
}
@@ -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;
@@ -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++;
}
}

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

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

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

@@ -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
@@ -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
@@ -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 )
@@ -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 )
@@ -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";
@@ -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 += ')';
@@ -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.
@@ -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;
@@ -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;
}

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

@@ -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 );
@@ -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 );
}
@@ -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 );
}
@@ -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 )
{
@@ -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 )
{
@@ -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;
@@ -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() )
{

0 comments on commit 1563193

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