Skip to content
Permalink
Browse files

Also avoid detaching QgsFields and QgsAttributes where possible

  • Loading branch information
nyalldawson committed Oct 20, 2015
1 parent 9a94132 commit 1969e09b6f8379898ea49af861d639245d6d0a92
Showing with 206 additions and 206 deletions.
  1. +2 −2 src/analysis/vector/qgsgeometryanalyzer.cpp
  2. +1 −1 src/analysis/vector/qgsoverlayanalyzer.cpp
  3. +6 −6 src/app/qgisapp.cpp
  4. +1 −1 src/app/qgsattributetabledialog.cpp
  5. +1 −1 src/app/qgsattributetypedialog.cpp
  6. +2 −2 src/app/qgsclipboard.cpp
  7. +2 −2 src/app/qgsdxfexportdialog.cpp
  8. +2 −2 src/app/qgsfeatureaction.cpp
  9. +1 −1 src/app/qgsfieldcalculator.cpp
  10. +6 −6 src/app/qgsidentifyresultsdialog.cpp
  11. +1 −1 src/app/qgslabelpropertydialog.cpp
  12. +4 −4 src/app/qgsmaptoollabel.cpp
  13. +7 −7 src/app/qgsmergeattributesdialog.cpp
  14. +2 −2 src/core/diagram/qgshistogramdiagram.cpp
  15. +2 −2 src/core/qgsexpression.cpp
  16. +2 −2 src/core/qgsfeature.cpp
  17. +1 −1 src/core/qgslabel.cpp
  18. +4 −4 src/core/qgsofflineediting.cpp
  19. +1 −1 src/core/qgsprojectfiletransform.cpp
  20. +2 −2 src/core/qgsrelation.cpp
  21. +1 −1 src/core/qgsvectordataprovider.cpp
  22. +2 −2 src/core/qgsvectorfilewriter.cpp
  23. +15 −15 src/core/qgsvectorlayer.cpp
  24. +5 −5 src/core/qgsvectorlayereditbuffer.cpp
  25. +3 −3 src/core/qgsvectorlayerfeatureiterator.cpp
  26. +2 −2 src/core/qgsvectorlayerimport.cpp
  27. +2 −2 src/core/qgsvectorlayerjoinbuffer.cpp
  28. +1 −1 src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
  29. +1 −1 src/core/symbology-ng/qgspointdisplacementrenderer.cpp
  30. +1 −1 src/gui/attributetable/qgsattributetablemodel.cpp
  31. +1 −1 src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
  32. +1 −1 src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
  33. +2 −2 src/gui/editorwidgets/qgsdefaultsearchwidgetwrapper.cpp
  34. +3 −3 src/gui/editorwidgets/qgsrangeconfigdlg.cpp
  35. +1 −1 src/gui/editorwidgets/qgsrangewidgetfactory.cpp
  36. +4 −4 src/gui/editorwidgets/qgsrangewidgetwrapper.cpp
  37. +1 −1 src/gui/editorwidgets/qgsrelationreferencewidget.cpp
  38. +2 −2 src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.cpp
  39. +12 −12 src/gui/qgsattributeform.cpp
  40. +1 −1 src/gui/qgsformannotationitem.cpp
  41. +1 −1 src/gui/qgsquerybuilder.cpp
  42. +1 −1 src/gui/qgssearchquerybuilder.cpp
  43. +9 −9 src/plugins/evis/eventbrowser/evisgenericeventbrowsergui.cpp
  44. +3 −3 src/plugins/geometry_checker/ui/qgsgeometrycheckerresulttab.cpp
  45. +3 −3 src/plugins/grass/qgsgrassmoduleparam.cpp
  46. +1 −1 src/plugins/roadgraph/speedproperter.cpp
  47. +3 −3 src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.cpp
  48. +1 −1 src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
  49. +10 −10 src/providers/gpx/qgsgpxprovider.cpp
  50. +2 −2 src/providers/grass/qgsgrassprovider.cpp
  51. +4 −4 src/providers/grass/qgsgrassvectormaplayer.cpp
  52. +1 −1 src/providers/mssql/qgsmssqlfeatureiterator.cpp
  53. +19 −19 src/providers/mssql/qgsmssqlprovider.cpp
  54. +2 −2 src/providers/ogr/qgsogrfeatureiterator.cpp
  55. +4 −4 src/providers/ogr/qgsogrprovider.cpp
  56. +5 −5 src/providers/postgres/qgspostgresfeatureiterator.cpp
  57. +14 −14 src/providers/postgres/qgspostgresprovider.cpp
  58. +1 −1 src/providers/spatialite/qgsspatialitefeatureiterator.cpp
  59. +7 −7 src/providers/spatialite/qgsspatialiteprovider.cpp
  60. +2 −2 src/providers/wfs/qgswfsfeatureiterator.cpp
  61. +1 −1 src/providers/wfs/qgswfsprovider.cpp
  62. +1 −1 tests/src/core/testqgsexpressioncontext.cpp
@@ -485,8 +485,8 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
values = simpleMeasure( dissolveGeometry );
QgsAttributes attributes( 3 );
attributes[0] = QVariant( currentKey );
attributes[1] = values[ 0 ];
attributes[2] = values[ 1 ];
attributes[1] = values.at( 0 );
attributes[2] = values.at( 1 );
QgsFeature dissolveFeature;
dissolveFeature.setAttributes( attributes );
dissolveFeature.setGeometry( dissolveGeometry );
@@ -185,7 +185,7 @@ void QgsOverlayAnalyzer::combineFieldLists( QgsFields& fieldListA, const QgsFiel
{
QList<QString> names;
for ( int idx = 0; idx < fieldListA.count(); ++idx )
names.append( fieldListA[idx].name() );
names.append( fieldListA.at( idx ).name() );

for ( int idx = 0; idx < fieldListB.count(); ++idx )
{
@@ -6164,14 +6164,14 @@ void QgisApp::mergeAttributesOfSelectedFeatures()

QVariant val = merged.at( i );
// convert to destination data type
if ( ! vl->fields()[i].convertCompatible( val ) )
if ( ! vl->fields().at( i ).convertCompatible( val ) )
{
if ( firstFeature )
{
//only warn on first feature
messageBar()->pushMessage(
tr( "Invalid result" ),
tr( "Could not store value '%1' in field of type %2" ).arg( merged.at( i ).toString(), vl->fields()[i].typeName() ),
tr( "Could not store value '%1' in field of type %2" ).arg( merged.at( i ).toString(), vl->fields().at( i ).typeName() ),
QgsMessageBar::WARNING );
}
}
@@ -6301,11 +6301,11 @@ void QgisApp::mergeSelectedFeatures()
{
QVariant val = attrs.at( i );
// convert to destination data type
if ( ! vl->fields()[i].convertCompatible( val ) )
if ( ! vl->fields().at( i ).convertCompatible( val ) )
{
messageBar()->pushMessage(
tr( "Invalid result" ),
tr( "Could not store value '%1' in field of type %2" ).arg( attrs.at( i ).toString(), vl->fields()[i].typeName() ),
tr( "Could not store value '%1' in field of type %2" ).arg( attrs.at( i ).toString(), vl->fields().at( i ).typeName() ),
QgsMessageBar::WARNING );
}
attrs[i] = val;
@@ -6527,13 +6527,13 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
if ( pkAttrList.contains( dst ) )
{
dstAttr[ dst ] = pasteVectorLayer->dataProvider()->defaultValue( dst );
if ( !dstAttr[ dst ].isNull() )
if ( !dstAttr.at( dst ).isNull() )
continue;
else if ( pasteVectorLayer->providerType() == "spatialite" )
continue;
}

dstAttr[ dst ] = srcAttr[ src ];
dstAttr[ dst ] = srcAttr.at( src );
}

featureIt->setAttributes( dstAttr );
@@ -389,7 +389,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, const
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( layer );

QgsField fld = layer->fields()[ fieldindex ];
QgsField fld = layer->fields().at( fieldindex );

//go through all the features and change the new attributes
QgsFeatureIterator fit = layer->getFeatures( request );
@@ -43,7 +43,7 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
, mFieldIdx( fieldIdx )
{
setupUi( this );
setWindowTitle( tr( "Edit Widget Properties - %1 (%2)" ).arg( vl->fields()[fieldIdx].name(), vl->name() ) );
setWindowTitle( tr( "Edit Widget Properties - %1 (%2)" ).arg( vl->fields().at( fieldIdx ).name(), vl->name() ) );

connect( selectionListWidget, SIGNAL( currentRowChanged( int ) ), this, SLOT( setStackPage( int ) ) );

@@ -91,7 +91,7 @@ void QgsClipboard::setSystemClipboard()

for ( int idx = 0; idx < mFeatureFields.count(); ++idx )
{
textFields += mFeatureFields[idx].name();
textFields += mFeatureFields.at( idx ).name();
}
textLines += textFields.join( "\t" );
textFields.clear();
@@ -116,7 +116,7 @@ void QgsClipboard::setSystemClipboard()
for ( int idx = 0; idx < attributes.count(); ++idx )
{
// QgsDebugMsg(QString("inspecting field '%1'.").arg(it2->toString()));
textFields += attributes[idx].toString();
textFields += attributes.at( idx ).toString();
}

textLines += textFields.join( "\t" );
@@ -72,7 +72,7 @@ void FieldSelectorDelegate::setEditorData( QWidget *editor, const QModelIndex &i

int idx = m->attributeIndex( vl );
if ( vl->fields().exists( idx ) )
fcb->setField( vl->fields()[ idx ].name() );
fcb->setField( vl->fields().at( idx ).name() );
}

void FieldSelectorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
@@ -217,7 +217,7 @@ QVariant QgsVectorLayerAndAttributeModel::data( const QModelIndex& idx, int role
if ( role == Qt::DisplayRole )
{
if ( vl->fields().exists( idx ) )
return vl->fields()[ idx ].name();
return vl->fields().at( idx ).name();
else
return vl->name();
}
@@ -231,10 +231,10 @@ void QgsFeatureAction::onFeatureSaved( const QgsFeature& feature )
{
QgsAttributes newValues = feature.attributes();
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
if ( origValues[idx] != newValues[idx] )
if ( origValues[idx] != newValues.at( idx ) )
{
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
sLastUsedValues[ mLayer ][idx] = newValues[idx];
sLastUsedValues[ mLayer ][idx] = newValues.at( idx );
}
}
}
@@ -252,7 +252,7 @@ void QgsFieldCalculator::accept()
bool useGeometry = exp.needsGeometry();
int rownum = 1;

QgsField field = mVectorLayer->fields()[mAttributeId];
QgsField field = mVectorLayer->fields().at( mAttributeId );

bool newField = !mUpdateExistingGroupBox->isChecked();
QVariant emptyAttribute;
@@ -471,7 +471,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
if ( i >= fields.count() )
continue;

QString value = fields[i].displayString( attrs[i] );
QString value = fields.at( i ).displayString( attrs.at( i ) );
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << value );

attrItem->setData( 0, Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
@@ -486,7 +486,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
continue;
}

value = representValue( vlayer, fields[i].name(), attrs[i] );
value = representValue( vlayer, fields[i].name(), attrs.at( i ) );

attrItem->setData( 1, Qt::DisplayRole, value );

@@ -513,8 +513,8 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
if ( i >= fields.count() )
continue;

QString value = fields[i].displayString( attrs[i] );
QString value2 = representValue( vlayer, fields[i].name(), value );
QString value = fields.at( i ).displayString( attrs.at( i ) );
QString value2 = representValue( vlayer, fields.at( i ).name(), value );

tblResults->setRowCount( j + 1 );

@@ -734,11 +734,11 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
if ( i >= fields.count() )
continue;

QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << attrs[i].toString() );
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << attrs.at( i ).toString() );

attrItem->setData( 0, Qt::DisplayRole, fields[i].name() );

QVariant value = attrs[i];
QVariant value = attrs.at( i );
attrItem->setData( 1, Qt::DisplayRole, value );
featItem->addChild( attrItem );
}
@@ -96,7 +96,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
mCurLabelField = vlayer->fieldNameIndex( labelFieldName );
if ( mCurLabelField >= 0 )
{
mLabelTextLineEdit->setText( attributeValues[mCurLabelField].toString() );
mLabelTextLineEdit->setText( attributeValues.at( mCurLabelField ).toString() );
const QgsFields& layerFields = vlayer->fields();
switch ( layerFields[mCurLabelField].type() )
{
@@ -484,10 +484,10 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, const QgsFeat
}

QgsAttributes attributes = f.attributes();
if ( !attributes[xCol].isNull() )
x = attributes[xCol].toDouble( &xSuccess );
if ( !attributes[yCol].isNull() )
y = attributes[yCol].toDouble( &ySuccess );
if ( !attributes.at( xCol ).isNull() )
x = attributes.at( xCol ).toDouble( &xSuccess );
if ( !attributes.at( yCol ).isNull() )
y = attributes.at( yCol ).toDouble( &ySuccess );

return true;
}
@@ -127,10 +127,10 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
{
int idx = mTableWidget->horizontalHeaderItem( j )->data( Qt::UserRole ).toInt();

QTableWidgetItem* attributeValItem = new QTableWidgetItem( attrs[idx].toString() );
QTableWidgetItem* attributeValItem = new QTableWidgetItem( attrs.at( idx ).toString() );
attributeValItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mTableWidget->setItem( i + 1, j, attributeValItem );
mTableWidget->setCellWidget( i + 1, j, QgsAttributeEditor::createAttributeEditor( mTableWidget, NULL, mVectorLayer, idx, attrs[idx] ) );
mTableWidget->setCellWidget( i + 1, j, QgsAttributeEditor::createAttributeEditor( mTableWidget, NULL, mVectorLayer, idx, attrs.at( idx ) ) );
}
}

@@ -307,7 +307,7 @@ QVariant QgsMergeAttributesDialog::featureAttribute( int featureId, int col )
}
else
{
return QVariant( mVectorLayer->fields()[col].type() );
return QVariant( mVectorLayer->fields().at( col ).type() );
}
}

@@ -333,7 +333,7 @@ QVariant QgsMergeAttributesDialog::minimumAttribute( int col )

if ( numberOfConsideredFeatures < 1 )
{
return QVariant( mVectorLayer->fields()[col].type() );
return QVariant( mVectorLayer->fields().at( col ).type() );
}

return QVariant( minimumValue );
@@ -361,7 +361,7 @@ QVariant QgsMergeAttributesDialog::maximumAttribute( int col )

if ( numberOfConsideredFeatures < 1 )
{
return QVariant( mVectorLayer->fields()[col].type() );
return QVariant( mVectorLayer->fields().at( col ).type() );
}

return QVariant( maximumValue );
@@ -386,7 +386,7 @@ QVariant QgsMergeAttributesDialog::meanAttribute( int col )

if ( numberOfConsideredFeatures < 1 )
{
return QVariant( mVectorLayer->fields()[col].type() );
return QVariant( mVectorLayer->fields().at( col ).type() );
}

double mean = sum / numberOfConsideredFeatures;
@@ -417,7 +417,7 @@ QVariant QgsMergeAttributesDialog::medianAttribute( int col )

if ( size < 1 )
{
return QVariant( mVectorLayer->fields()[col].type() );
return QVariant( mVectorLayer->fields().at( col ).type() );
}

bool even = ( size % 2 ) < 1;
@@ -93,11 +93,11 @@ QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const
return QSizeF(); //zero size if no attributes
}

double maxValue = attributes[0].toDouble();
double maxValue = attributes.at( 0 ).toDouble();

for ( int i = 0; i < attributes.count(); ++i )
{
maxValue = qMax( attributes[i].toDouble(), maxValue );
maxValue = qMax( attributes.at( i ).toDouble(), maxValue );
}

switch ( s.diagramOrientation )
@@ -2637,7 +2637,7 @@ QString QgsExpression::replaceExpressionText( const QString &action, const QgsFe
QgsVectorLayer *layer,
const QMap<QString, QVariant> *substitutionMap, const QgsDistanceArea *distanceArea )
{
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( feat ? *feat : QgsFeature(), layer ? layer->pendingFields() : QgsFields() );
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( feat ? *feat : QgsFeature(), layer ? layer->fields() : QgsFields() );
return replaceExpressionText( action, &context, substitutionMap, distanceArea );
}

@@ -3353,7 +3353,7 @@ bool QgsExpression::NodeColumnRef::prepare( QgsExpression *parent, const QgsExpr

for ( int i = 0; i < fields.count(); ++i )
{
if ( QString::compare( fields[i].name(), mName, Qt::CaseInsensitive ) == 0 )
if ( QString::compare( fields.at( i ).name(), mName, Qt::CaseInsensitive ) == 0 )
{
mIndex = i;
return true;
@@ -231,7 +231,7 @@ QVariant QgsFeature::attribute( int fieldIdx ) const
if ( fieldIdx < 0 || fieldIdx >= d->attributes.count() )
return QVariant();

return d->attributes[fieldIdx];
return d->attributes.at( fieldIdx );
}


@@ -241,7 +241,7 @@ QVariant QgsFeature::attribute( const QString& name ) const
if ( fieldIdx == -1 )
return QVariant();

return d->attributes[fieldIdx];
return d->attributes.at( fieldIdx );
}

int QgsFeature::fieldNameIndex( const QString& fieldName ) const
@@ -716,7 +716,7 @@ bool QgsLabel::readLabelField( QDomElement &el, int attr, const QString& prefix
int idx = 0;
for ( ; idx < mFields.count(); ++idx )
{
if ( mFields[idx].name() == name )
if ( mFields.at( idx ).name() == name )
{
break;
}
@@ -622,7 +622,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
QgsAttributes newAttrs( attrs.count() );
for ( int it = 0; it < attrs.count(); ++it )
{
newAttrs[column++] = attrs[it];
newAttrs[column++] = attrs.at( it );
}
f.setAttributes( newAttrs );

@@ -747,15 +747,15 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
QgsAttributes attrs = f.attributes();
for ( int it = 0; it < attrs.count(); ++it )
{
newAttrs[ attrLookup[ it ] ] = attrs[ it ];
newAttrs[ attrLookup[ it ] ] = attrs.at( it );
}

// try to use default value from the provider
// (important especially e.g. for postgis primary key generated from a sequence)
for ( int k = 0; k < newAttrs.count(); ++k )
{
if ( newAttrs[k].isNull() && !defaultValues[k].isNull() )
newAttrs[k] = defaultValues[k];
if ( newAttrs.at( k ).isNull() && !defaultValues.at( k ).isNull() )
newAttrs[k] = defaultValues.at( k );
}

f.setAttributes( newAttrs );
@@ -355,7 +355,7 @@ void QgsProjectFileTransform::transform0110to1000()
int fieldNumber = classificationFieldElem.text().toInt();
if ( fieldNumber >= 0 && fieldNumber < theFields.count() )
{
QDomText fieldName = mDom.createTextNode( theFields[fieldNumber].name() );
QDomText fieldName = mDom.createTextNode( theFields.at( fieldNumber ).name() );
QDomNode nameNode = classificationFieldElem.firstChild();
classificationFieldElem.replaceChild( fieldName, nameNode );
}
@@ -193,12 +193,12 @@ QgsFeatureRequest QgsRelation::getReferencedFeatureRequest( const QgsAttributes&
if ( referencedField.type() == QVariant::String )
{
// Use quotes
conditions << QString( "\"%1\" = '%2'" ).arg( fieldPair.referencedField(), attributes[referencingIdx].toString() );
conditions << QString( "\"%1\" = '%2'" ).arg( fieldPair.referencedField(), attributes.at( referencingIdx ).toString() );
}
else
{
// No quotes
conditions << QString( "\"%1\" = %2" ).arg( fieldPair.referencedField(), attributes[referencingIdx].toString() );
conditions << QString( "\"%1\" = %2" ).arg( fieldPair.referencedField(), attributes.at( referencingIdx ).toString() );
}
}

@@ -410,7 +410,7 @@ void QgsVectorDataProvider::fillMinMaxCache()
QgsAttributes attrs = f.attributes();
for ( QgsAttributeList::const_iterator it = keys.begin(); it != keys.end(); ++it )
{
const QVariant& varValue = attrs[*it];
const QVariant& varValue = attrs.at( *it );

if ( varValue.isNull() )
continue;

6 comments on commit 1969e09

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied Oct 20, 2015

You probably already know, but just to be sure: at() requires an index to exist, so it can only be used in places where this precondition is guaranteed. Other methods (like .value() and [] will return a default constructed value instead).
(Just for your information, I haven't seen any such cases in this commit nor have I been looking for it ;) )

@nyalldawson

This comment has been minimized.

Copy link
Collaborator Author

@nyalldawson nyalldawson replied Oct 20, 2015

@m-kuhn are you sure? That's not the case for QgsFields, and QVector docs also doesn't state that (which QgsAttributes is derived from).

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied Oct 20, 2015

Maybe not for QgsFields (since that's our own implementation, but I think we may change that to better match the Qt implementation at any time)

QVector:

http://doc.qt.io/qt-4.8/qvector.html#at

i must be a valid index position in the vector (i.e., 0 <= i < size()).

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied Oct 20, 2015

... realized that [] has the same preconditions like at() as opposed to value()

@nyalldawson

This comment has been minimized.

Copy link
Collaborator Author

@nyalldawson nyalldawson replied Oct 20, 2015

@m-kuhn yeah, so by my understanding [] should be harmlessly swappable to at() to force use of const.

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied Oct 20, 2015

looks like, yeah

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