Skip to content

Commit 1563193

Browse files
committedMar 9, 2016
Port more field iterating to Q_FOREACH, avoid some detachments
1 parent 0e5214c commit 1563193

27 files changed

+75
-89
lines changed
 

‎src/core/composer/qgsatlascomposition.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ void QgsAtlasComposition::setSortKeyAttributeIndex( int idx )
147147
{
148148
if ( mCoverageLayer )
149149
{
150-
const QgsFields fields = mCoverageLayer->fields();
150+
QgsFields fields = mCoverageLayer->fields();
151151
if ( idx >= 0 && idx < fields.count() )
152152
{
153-
mSortKeyAttributeName = fields[idx].name();
153+
mSortKeyAttributeName = fields.at( idx ).name();
154154
return;
155155
}
156156
}
@@ -714,10 +714,10 @@ void QgsAtlasComposition::readXML( const QDomElement& atlasElem, const QDomDocum
714714
int idx = mSortKeyAttributeName.toInt( &isIndex );
715715
if ( isIndex && mCoverageLayer )
716716
{
717-
const QgsFields fields = mCoverageLayer->fields();
717+
QgsFields fields = mCoverageLayer->fields();
718718
if ( idx >= 0 && idx < fields.count() )
719719
{
720-
mSortKeyAttributeName = fields[idx].name();
720+
mSortKeyAttributeName = fields.at( idx ).name();
721721
}
722722
}
723723
mSortAscending = atlasElem.attribute( "sortAscending", "true" ) == "true" ? true : false;

‎src/core/composer/qgscomposerattributetable.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,15 @@ void QgsComposerAttributeTable::resetColumns()
181181
mColumns.clear();
182182

183183
//rebuild columns list from vector layer fields
184-
const QgsFields& fields = mVectorLayer->fields();
185-
for ( int idx = 0; idx < fields.count(); ++idx )
184+
int idx = 0;
185+
Q_FOREACH ( const QgsField& field, mVectorLayer->fields() )
186186
{
187187
QString currentAlias = mVectorLayer->attributeDisplayName( idx );
188188
QgsComposerTableColumn* col = new QgsComposerTableColumn;
189-
col->setAttribute( fields[idx].name() );
189+
col->setAttribute( field.name() );
190190
col->setHeading( currentAlias );
191191
mColumns.append( col );
192+
idx++;
192193
}
193194
}
194195

@@ -296,13 +297,15 @@ void QgsComposerAttributeTable::setDisplayAttributes( const QSet<int>& attr, boo
296297
else
297298
{
298299
//resetting, so add all attributes to columns
299-
for ( int idx = 0; idx < fields.count(); ++idx )
300+
int idx = 0;
301+
Q_FOREACH ( const QgsField& field, fields )
300302
{
301303
QString currentAlias = mVectorLayer->attributeDisplayName( idx );
302304
QgsComposerTableColumn* col = new QgsComposerTableColumn;
303-
col->setAttribute( fields[idx].name() );
305+
col->setAttribute( field.name() );
304306
col->setHeading( currentAlias );
305307
mColumns.append( col );
308+
idx++;
306309
}
307310
}
308311

‎src/core/composer/qgscomposerattributetablev2.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,15 @@ void QgsComposerAttributeTableV2::resetColumns()
253253
mColumns.clear();
254254

255255
//rebuild columns list from vector layer fields
256-
const QgsFields& fields = source->fields();
257-
for ( int idx = 0; idx < fields.count(); ++idx )
256+
int idx = 0;
257+
Q_FOREACH ( const QgsField& field, source->fields() )
258258
{
259259
QString currentAlias = source->attributeDisplayName( idx );
260260
QgsComposerTableColumn* col = new QgsComposerTableColumn;
261-
col->setAttribute( fields[idx].name() );
261+
col->setAttribute( field.name() );
262262
col->setHeading( currentAlias );
263263
mColumns.append( col );
264+
idx++;
264265
}
265266
}
266267

@@ -393,13 +394,15 @@ void QgsComposerAttributeTableV2::setDisplayAttributes( const QSet<int>& attr, b
393394
else
394395
{
395396
//resetting, so add all attributes to columns
396-
for ( int idx = 0; idx < fields.count(); ++idx )
397+
int idx = 0;
398+
Q_FOREACH ( const QgsField& field, fields )
397399
{
398400
QString currentAlias = source->attributeDisplayName( idx );
399401
QgsComposerTableColumn* col = new QgsComposerTableColumn;
400-
col->setAttribute( fields[idx].name() );
402+
col->setAttribute( field.name() );
401403
col->setHeading( currentAlias );
402404
mColumns.append( col );
405+
idx++;
403406
}
404407
}
405408

‎src/core/qgseditformconfig.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ QString QgsEditFormConfig::widgetType( int fieldIdx ) const
2929
if ( fieldIdx < 0 || fieldIdx >= mFields.count() )
3030
return "TextEdit";
3131

32-
return mEditorWidgetV2Types.value( mFields[fieldIdx].name(), "TextEdit" );
32+
return mEditorWidgetV2Types.value( mFields.at( fieldIdx ).name(), "TextEdit" );
3333
}
3434

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

45-
return mWidgetConfigs.value( mFields[fieldIdx].name() );
45+
return mWidgetConfigs.value( mFields.at( fieldIdx ).name() );
4646
}
4747

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

85-
return mWidgetConfigs.remove( mFields[fieldIdx].name() );
85+
return mWidgetConfigs.remove( mFields.at( fieldIdx ).name() );
8686
}
8787

8888
void QgsEditFormConfig::setUiForm( const QString& ui )

‎src/core/qgslabel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ QString QgsLabel::labelField( int attr ) const
504504
int fieldIndex = mLabelFieldIdx[attr];
505505
if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
506506
return QString();
507-
return mFields[fieldIndex].name();
507+
return mFields.at( fieldIndex ).name();
508508
}
509509

510510
QgsLabelAttributes *QgsLabel::labelAttributes( void )

‎src/core/qgsofflineediting.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
461461
// create table
462462
QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName );
463463
QString delim = "";
464-
const QgsFields& fields = layer->dataProvider()->fields();
465-
for ( int idx = 0; idx < fields.count(); ++idx )
464+
Q_FOREACH ( const QgsField& field, layer->dataProvider()->fields() )
466465
{
467466
QString dataType = "";
468-
QVariant::Type type = fields[idx].type();
467+
QVariant::Type type = field.type();
469468
if ( type == QVariant::Int || type == QVariant::LongLong )
470469
{
471470
dataType = "INTEGER";
@@ -480,10 +479,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
480479
}
481480
else
482481
{
483-
showWarning( tr( "%1: Unknown data type %2. Not using type affinity for the field." ).arg( fields[idx].name(), QVariant::typeToName( type ) ) );
482+
showWarning( tr( "%1: Unknown data type %2. Not using type affinity for the field." ).arg( field.name(), QVariant::typeToName( type ) ) );
484483
}
485484

486-
sql += delim + QString( "'%1' %2" ).arg( fields[idx].name(), dataType );
485+
sql += delim + QString( "'%1' %2" ).arg( field.name(), dataType );
487486
delim = ',';
488487
}
489488
sql += ')';

‎src/core/qgsvectorlayer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ void QgsVectorLayer::setDisplayField( const QString& fldName )
254254
{
255255
int fieldsSize = mUpdatedFields.size();
256256

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

262262
// Check the fields and keep the first one that matches.
@@ -2158,7 +2158,7 @@ QString QgsVectorLayer::attributeDisplayName( int attributeIndex ) const
21582158
{
21592159
if ( attributeIndex >= 0 && attributeIndex < mUpdatedFields.count() )
21602160
{
2161-
displayName = mUpdatedFields[attributeIndex].name();
2161+
displayName = mUpdatedFields.at( attributeIndex ).name();
21622162
}
21632163
}
21642164
return displayName;

‎src/core/qgsvectorlayereditbuffer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,9 @@ bool QgsVectorLayerEditBuffer::addAttribute( const QgsField &field )
225225
if ( field.name().isEmpty() )
226226
return false;
227227

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

‎src/core/qgsvectorlayerjoinbuffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
240240
for ( int idx = 0; idx < joinFields.count(); ++idx )
241241
{
242242
// if using just a subset of fields, filter some of them out
243-
if ( hasSubset && !subset.contains( joinFields[idx].name() ) )
243+
if ( hasSubset && !subset.contains( joinFields.at( idx ).name() ) )
244244
continue;
245245

246246
//skip the join field to avoid double field names (fields often have the same name)
247247
// when using subset of field, use all the selected fields
248-
if ( hasSubset || joinFields[idx].name() != joinFieldName )
248+
if ( hasSubset || joinFields.at( idx ).name() != joinFieldName )
249249
{
250-
QgsField f = joinFields[idx];
250+
QgsField f = joinFields.at( idx );
251251
f.setName( prefix + f.name() );
252252
fields.append( f, QgsFields::OriginJoin, idx + ( joinIdx*1000 ) );
253253
}

‎src/core/qgsvirtuallayerdefinition.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ QUrl QgsVirtualLayerDefinition::toUrl() const
201201
url.addQueryItem( "geometry", geometryField() );
202202
}
203203

204-
for ( int i = 0; i < fields().count(); i++ )
204+
Q_FOREACH ( const QgsField& f, fields() )
205205
{
206-
const QgsField& f = fields()[i];
207206
if ( f.type() == QVariant::Int )
208207
url.addQueryItem( "field", f.name() + ":int" );
209208
else if ( f.type() == QVariant::Double )

‎src/core/qgsvirtuallayerdefinitionutils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ QgsVirtualLayerDefinition QgsVirtualLayerDefinitionUtils::fromJoinedLayer( QgsVe
6464
}
6565
else
6666
{
67-
const QgsFields& joinedFields = layer->dataProvider()->fields();
68-
for ( int i = 0; i < joinedFields.count(); i++ )
67+
Q_FOREACH ( const QgsField& f, layer->dataProvider()->fields() )
6968
{
70-
const QgsField& f = joinedFields.field( i );
7169
columns << joinName + "." + f.name() + " AS " + prefix + f.name();
7270
}
7371
}

‎src/gui/qgsdatadefinedbutton.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
166166
if ( mVectorLayer )
167167
{
168168
// store just a list of fields of unknown type or those that match the expected type
169-
const QgsFields& fields = mVectorLayer->fields();
170-
for ( int i = 0; i < fields.count(); ++i )
169+
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
171170
{
172-
const QgsField& f = fields.at( i );
173171
bool fieldMatch = false;
174172
// NOTE: these are the only QVariant enums supported at this time (see QgsField)
175173
QString fieldType;

‎src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void QgsExpressionBuilderWidget::loadFieldNames( const QgsFields& fields )
301301
fieldNames.reserve( fields.count() );
302302
for ( int i = 0; i < fields.count(); ++i )
303303
{
304-
QString fieldName = fields[i].name();
304+
QString fieldName = fields.at( i ).name();
305305
fieldNames << fieldName;
306306
registerItem( "Fields and Values", fieldName, " \"" + fieldName + "\" ", "", QgsExpressionItem::Field, false, i );
307307
}

‎src/gui/qgsformannotationitem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ QWidget* QgsFormAnnotationItem::createDesignerWidget( const QString& filePath )
9999
{
100100
if ( i < fields.count() )
101101
{
102-
QWidget* attWidget = widget->findChild<QWidget*>( fields[i].name() );
102+
QWidget* attWidget = widget->findChild<QWidget*>( fields.at( i ).name() );
103103
if ( attWidget )
104104
{
105105
QgsAttributeEditor::createAttributeEditor( widget, attWidget, mVectorLayer, i, attrs.at( i ) );

‎src/gui/symbology-ng/qgspointdisplacementrendererwidget.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ QgsPointDisplacementRendererWidget::QgsPointDisplacementRendererWidget( QgsVecto
6868
//insert attributes into combo box
6969
if ( layer )
7070
{
71-
const QgsFields& layerAttributes = layer->fields();
72-
for ( int idx = 0; idx < layerAttributes.count(); ++idx )
71+
Q_FOREACH ( const QgsField& f, layer->fields() )
7372
{
74-
mLabelFieldComboBox->addItem( layerAttributes[idx].name() );
73+
mLabelFieldComboBox->addItem( f.name() );
7574
}
7675
mLabelFieldComboBox->addItem( tr( "None" ) );
7776

‎src/gui/symbology-ng/qgsrendererv2widget.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,8 @@ void QgsRendererV2DataDefinedMenus::populateMenu( QMenu* menu, const QString& fi
340340
menu->addSeparator();
341341

342342
bool hasField = false;
343-
const QgsFields & flds = mLayer->fields();
344-
for ( int idx = 0; idx < flds.count(); ++idx )
343+
Q_FOREACH ( const QgsField& fld, mLayer->fields() )
345344
{
346-
const QgsField& fld = flds[idx];
347345
if ( fld.type() == QVariant::Int || fld.type() == QVariant::Double )
348346
{
349347
QAction* a = new QAction( fld.name(), actionGroup );

‎src/gui/symbology-ng/qgsvectorfieldsymbollayerwidget.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ QgsVectorFieldSymbolLayerWidget::QgsVectorFieldSymbolLayerWidget( const QgsVecto
2424

2525
if ( mVectorLayer )
2626
{
27-
const QgsFields& fm = mVectorLayer->fields();
2827
mXAttributeComboBox->addItem( "" );
2928
mYAttributeComboBox->addItem( "" );
30-
for ( int idx = 0; idx < fm.count(); ++idx )
29+
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
3130
{
32-
QString fieldName = fm[idx].name();
31+
QString fieldName = f.name();
3332
mXAttributeComboBox->addItem( fieldName );
3433
mYAttributeComboBox->addItem( fieldName );
3534
}

‎src/plugins/grass/qgsgrassmoduleparam.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,16 +1192,15 @@ void QgsGrassModuleVectorField::updateFields()
11921192
{
11931193
continue;
11941194
}
1195-
QgsFields fields = mLayerInput->currentFields();
11961195

11971196
int index = 0;
1198-
for ( int i = 0; i < fields.size(); i++ )
1197+
Q_FOREACH ( const QgsField& field, mLayerInput->currentFields() )
11991198
{
1200-
if ( mType.contains( fields.at( i ).typeName() ) )
1199+
if ( mType.contains( field.typeName() ) )
12011200
{
1202-
comboBox->addItem( fields.at( i ).name() );
1203-
QgsDebugMsg( "current = " + current + " field = " + fields.at( i ).name() );
1204-
if ( fields.at( i ).name() == current )
1201+
comboBox->addItem( field.name() );
1202+
QgsDebugMsg( "current = " + current + " field = " + field.name() );
1203+
if ( field.name() == current )
12051204
{
12061205
comboBox->setCurrentIndex( index );
12071206
}

‎src/plugins/interpolation/qgsinterpolationdialog.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ void QgsInterpolationDialog::on_mInputLayerComboBox_currentIndexChanged( const Q
245245
}
246246

247247
//insert numeric attributes of layer into mInterpolationAttributesComboBox
248-
const QgsFields& fields = provider->fields();
249-
for ( int idx = 0; idx < fields.count(); ++idx )
248+
Q_FOREACH ( const QgsField& currentField, provider->fields() )
250249
{
251-
const QgsField& currentField = fields[idx];
252250
QVariant::Type currentType = currentField.type();
253251
if ( currentType == QVariant::Int || currentType == QVariant::Double )
254252
{

‎src/plugins/roadgraph/linevectorlayerwidget.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,8 @@ void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
209209
if ( !provider )
210210
return;
211211

212-
const QgsFields& fields = provider->fields();
213-
for ( int idx = 0; idx < fields.count(); ++idx )
212+
Q_FOREACH ( const QgsField& currentField, provider->fields() )
214213
{
215-
const QgsField& currentField = fields[idx];
216214
QVariant currentType = currentField.type();
217215
if ( currentType == QVariant::Int || currentType == QVariant::String )
218216
{

‎src/plugins/zonal_statistics/qgszonalstatisticsdialog.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,11 @@ bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
218218
return false;
219219
}
220220

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

224-
for ( int idx = 0; idx < providerFields.count(); ++idx )
223+
Q_FOREACH ( const QgsField& field, dp->fields() )
225224
{
226-
currentFieldName = providerFields[idx].name();
225+
currentFieldName = field.name();
227226
if ( currentFieldName == ( prefix + "mean" ) || currentFieldName == ( prefix + "sum" ) || currentFieldName == ( prefix + "count" ) )
228227
{
229228
return false;

‎src/providers/grass/qgsgrass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,9 +2480,8 @@ void QgsGrass::createTable( dbDriver *driver, const QString tableName, const Qgs
24802480
}
24812481

24822482
QStringList fieldsStringList;
2483-
for ( int i = 0; i < fields.size(); i++ )
2483+
Q_FOREACH ( const QgsField& field, fields )
24842484
{
2485-
QgsField field = fields.field( i );
24862485
QString name = field.name().toLower().replace( " ", "_" );
24872486
if ( name.at( 0 ).isDigit() )
24882487
{

0 commit comments

Comments
 (0)
Please sign in to comment.