Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Alias without pending prefix for QgsVectorLayer methods
 * pendingAllAttributesList -> attributeList
 * pendingPkAttributesList -> pkAttributeList
 * pendingFeatureCount -> featureCount

featureCount will now always return the features on the layer and NOT the
committed features count as before.
This changes its behavior but this way it is coherent with the other methods
which work on the layer.
  • Loading branch information
m-kuhn committed Aug 4, 2015
1 parent e832b2a commit cf2f6b1
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 75 deletions.
47 changes: 31 additions & 16 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -568,15 +568,6 @@ class QgsVectorLayer : QgsMapLayer
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
bool readSld( const QDomNode& node, QString& errorMessage );

/**
* Number of features in the layer. This is necessary if features are
* added/deleted or the layer has been subsetted. If the data provider
* chooses not to support this feature, the total number of features
* can be returned.
* @return long containing number of features
*/
virtual long featureCount() const;

/**
* Update the data source of the layer. The layer's renderer and legend will be preserved only
* if the geometry type of the new data source matches the current geometry type of the layer.
Expand Down Expand Up @@ -816,16 +807,40 @@ class QgsVectorLayer : QgsMapLayer
*
* @return A list of fields
*/
const QgsFields pendingFields() const;
QgsFields pendingFields() const;

/**
* Returns list of attribute indexes. i.e. a list from 0 ... fieldCount()
* Alias for {@link attributeList()}
*/
QgsAttributeList pendingAllAttributesList() const;

/**
* Returns list of attribute indexes. i.e. a list from 0 ... fieldCount()
* Alias for {@link attributeList()}
*/
QgsAttributeList attributeList() const;

/**
* Returns list of attributes making up the primary key
* Alias for {@link pkAttributeList()}
*/
QgsAttributeList pendingPkAttributesList() const;

/** Returns list of attributes */
QList<int> pendingAllAttributesList();
/** Returns list of attributes making up the primary key */
QgsAttributeList pkAttributeList() const;

/** Returns list of attribute making up the primary key */
QList<int> pendingPkAttributesList();
/**
* Returns feature count including changes which have not yet been committed
* Alias for {@link featureCount()}
*/
long pendingFeatureCount() const;

/** Returns feature count after commit */
int pendingFeatureCount();
/**
* Returns feature count including changes which have not yet been committed
* If you need only the count of committed features call this method on this layer's provider.
*/
long featureCount() const;

/** Make layer read-only (editing disabled) or not
* @return false if the layer is in editing yet
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/vector/qgsgeometryanalyzer.cpp
Expand Up @@ -961,7 +961,7 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
QgsGeometry* lrsGeom = 0;
double measure1, measure2 = 0.0;

int nEventFeatures = eventLayer->pendingFeatureCount();
int nEventFeatures = eventLayer->featureCount();
int featureCounter = 0;
int nOutputFeatures = 0; //number of output features for the current event feature
if ( p )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -6285,7 +6285,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )

QHash<int, int> remap;
const QgsFields &fields = clipboard()->fields();
QgsAttributeList pkAttrList = pasteVectorLayer->pendingPkAttributesList();
QgsAttributeList pkAttrList = pasteVectorLayer->pkAttributeList();
for ( int idx = 0; idx < fields.count(); ++idx )
{
int dst = pasteVectorLayer->fieldNameIndex( fields[idx].name() );
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -88,7 +88,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()

//create combo boxes and insert attribute names
const QgsFields& fields = mVectorLayer->fields();
QSet<int> pkAttrList = mVectorLayer->pendingPkAttributesList().toSet();
QSet<int> pkAttrList = mVectorLayer->pkAttributeList().toSet();

int col = 0;
for ( int idx = 0; idx < fields.count(); ++idx )
Expand Down Expand Up @@ -487,7 +487,7 @@ void QgsMergeAttributesDialog::on_mFromSelectedPushButton_clicked()
return;
}

QSet<int> pkAttributes = mVectorLayer->pendingPkAttributesList().toSet();
QSet<int> pkAttributes = mVectorLayer->pkAttributeList().toSet();
for ( int i = 0; i < mTableWidget->columnCount(); ++i )
{
if ( pkAttributes.contains( i ) )
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -602,8 +602,8 @@ QVariant QgsLegendModelV2::data( const QModelIndex& index, int role ) const
if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )
{
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() );
if ( vlayer && vlayer->pendingFeatureCount() >= 0 )
name += QString( " [%1]" ).arg( vlayer->pendingFeatureCount() );
if ( vlayer && vlayer->featureCount() >= 0 )
name += QString( " [%1]" ).arg( vlayer->featureCount() );
}
return name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -168,8 +168,8 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )
{
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() );
if ( vlayer && vlayer->pendingFeatureCount() >= 0 )
name += QString( " [%1]" ).arg( vlayer->pendingFeatureCount() );
if ( vlayer && vlayer->featureCount() >= 0 )
name += QString( " [%1]" ).arg( vlayer->featureCount() );
}
return name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -413,8 +413,8 @@ void QgsSymbolV2LegendNode::updateLabel()
layerName = mLayerNode->customProperty( "legend/title-label" ).toString();

mLabel = mUserLabel.isEmpty() ? layerName : mUserLabel;
if ( showFeatureCount && vl && vl->pendingFeatureCount() >= 0 )
mLabel += QString( " [%1]" ).arg( vl->pendingFeatureCount() );
if ( showFeatureCount && vl && vl->featureCount() >= 0 )
mLabel += QString( " [%1]" ).arg( vl->featureCount() );
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -883,8 +883,8 @@ void QgsOfflineEditing::copySymbology( QgsVectorLayer* sourceLayer, QgsVectorLay
// NOTE: use this to map column indices in case the remote geometry column is not last
QMap<int, int> QgsOfflineEditing::attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer )
{
const QgsAttributeList& offlineAttrs = offlineLayer->pendingAllAttributesList();
const QgsAttributeList& remoteAttrs = remoteLayer->pendingAllAttributesList();
const QgsAttributeList& offlineAttrs = offlineLayer->attributeList();
const QgsAttributeList& remoteAttrs = remoteLayer->attributeList();

QMap < int /*offline attr*/, int /*remote attr*/ > attrLookup;
// NOTE: use size of remoteAttrs, as offlineAttrs can have new attributes not yet synced
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -1945,7 +1945,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
errorMessage->clear();
}

QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList() : layer->pendingAllAttributesList();
QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList() : layer->attributeList();
QgsFeature fet;

//add possible attributes needed by renderer
Expand Down
24 changes: 4 additions & 20 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -675,17 +675,6 @@ bool QgsVectorLayer::diagramsEnabled() const
return false;
}

long QgsVectorLayer::featureCount() const
{
if ( !mDataProvider )
{
QgsDebugMsg( "invoked with null mDataProvider" );
return 0;
}

return mDataProvider->featureCount();
}

long QgsVectorLayer::featureCount( QgsSymbolV2* symbol )
{
if ( !mSymbolFeatureCounted ) return -1;
Expand Down Expand Up @@ -716,7 +705,7 @@ bool QgsVectorLayer::countSymbolFeatures( bool showProgress )
mSymbolFeatureCountMap.insert( symbolIt->second, 0 );
}

long nFeatures = pendingFeatureCount();
long nFeatures = featureCount();
QProgressDialog progressDialog( tr( "Updating feature count for layer %1" ).arg( name() ), tr( "Abort" ), 0, nFeatures );
progressDialog.setWindowTitle( tr( "QGIS" ) );
progressDialog.setWindowModality( Qt::WindowModal );
Expand Down Expand Up @@ -2218,12 +2207,7 @@ bool QgsVectorLayer::deleteFeature( QgsFeatureId fid )
return res;
}

QgsAttributeList QgsVectorLayer::pendingAllAttributesList()
{
return mUpdatedFields.allAttributesList();
}

QgsAttributeList QgsVectorLayer::pendingPkAttributesList()
QgsAttributeList QgsVectorLayer::pkAttributeList() const
{
QgsAttributeList pkAttributesList;

Expand All @@ -2238,7 +2222,7 @@ QgsAttributeList QgsVectorLayer::pendingPkAttributesList()
return pkAttributesList;
}

int QgsVectorLayer::pendingFeatureCount()
long QgsVectorLayer::featureCount() const
{
return mDataProvider->featureCount() +
( mEditBuffer ? mEditBuffer->mAddedFeatures.size() - mEditBuffer->mDeletedFeatureIds.size() : 0 );
Expand Down Expand Up @@ -3563,7 +3547,7 @@ QString QgsVectorLayer::metadata()
myMetadata += "</p>\n";
}

QgsAttributeList pkAttrList = pendingPkAttributesList();
QgsAttributeList pkAttrList = pkAttributeList();
if ( !pkAttrList.isEmpty() )
{
myMetadata += "<p class=\"glossy\">" + tr( "Primary key attributes" ) + "</p>\n";
Expand Down
49 changes: 32 additions & 17 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1061,15 +1061,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
bool readSld( const QDomNode& node, QString& errorMessage ) override;

/**
* Number of features in the layer. This is necessary if features are
* added/deleted or the layer has been subsetted. If the data provider
* chooses not to support this feature, the total number of features
* can be returned.
* @return long containing number of features
*/
virtual long featureCount() const;

/**
* Number of features rendered with specified symbol. Features must be first
* calculated by countSymbolFeatures()
Expand Down Expand Up @@ -1306,7 +1297,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*
* @return A list of fields
*/
const inline QgsFields fields() const { return mUpdatedFields; }
inline QgsFields fields() const { return mUpdatedFields; }

/**
* Returns the list of fields of this layer.
Expand All @@ -1315,16 +1306,40 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*
* @return A list of fields
*/
const inline QgsFields pendingFields() const { return mUpdatedFields; }
inline QgsFields pendingFields() const { return mUpdatedFields; }

/**
* Returns list of attribute indexes. i.e. a list from 0 ... fieldCount()
* Alias for {@link attributeList()}
*/
inline QgsAttributeList pendingAllAttributesList() const { return mUpdatedFields.allAttributesList(); }

/**
* Returns list of attribute indexes. i.e. a list from 0 ... fieldCount()
* Alias for {@link attributeList()}
*/
inline QgsAttributeList attributeList() const { return mUpdatedFields.allAttributesList(); }

/**
* Returns list of attributes making up the primary key
* Alias for {@link pkAttributeList()}
*/
inline QgsAttributeList pendingPkAttributesList() const { return pkAttributeList(); }

/** Returns list of attributes */
QgsAttributeList pendingAllAttributesList();
/** Returns list of attributes making up the primary key */
QgsAttributeList pkAttributeList() const;

/** Returns list of attribute making up the primary key */
QgsAttributeList pendingPkAttributesList();
/**
* Returns feature count including changes which have not yet been committed
* Alias for {@link featureCount()}
*/
inline long pendingFeatureCount() const { return featureCount(); }

/** Returns feature count after commit */
int pendingFeatureCount();
/**
* Returns feature count including changes which have not yet been committed
* If you need only the count of committed features call this method on this layer's provider.
*/
long featureCount() const;

/** Make layer read-only (editing disabled) or not
* @return false if the layer is in editing yet
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayercache.cpp
Expand Up @@ -31,7 +31,7 @@ QgsVectorLayerCache::QgsVectorLayerCache( QgsVectorLayer* layer, int cacheSize,
connect( mLayer, SIGNAL( layerDeleted() ), SLOT( layerDeleted() ) );

setCacheGeometry( true );
setCacheSubsetOfAttributes( mLayer->pendingAllAttributesList() );
setCacheSubsetOfAttributes( mLayer->attributeList() );
setCacheAddedAttributes( true );

connect( mLayer, SIGNAL( attributeDeleted( int ) ), SLOT( attributeDeleted( int ) ) );
Expand Down Expand Up @@ -330,7 +330,7 @@ bool QgsVectorLayerCache::checkInformationCovered( const QgsFeatureRequest& feat

if ( !featureRequest.flags().testFlag( QgsFeatureRequest::SubsetOfAttributes ) )
{
requestedAttributes = mLayer->pendingAllAttributesList();
requestedAttributes = mLayer->attributeList();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerimport.cpp
Expand Up @@ -302,7 +302,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
errorMessage->clear();
}

QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList() : layer->pendingAllAttributesList();
QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList() : layer->attributeList();
QgsFeature fet;

QgsFeatureRequest req;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -111,7 +111,7 @@ void QgsDualView::columnBoxInit()
// if neither diaplay expression nor display field is saved...
if ( displayExpression == "" )
{
QgsAttributeList pkAttrs = mLayerCache->layer()->pendingPkAttributesList();
QgsAttributeList pkAttrs = mLayerCache->layer()->pkAttributeList();

if ( pkAttrs.size() > 0 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgsrulebasedrendererv2widget.cpp
Expand Up @@ -506,7 +506,7 @@ void QgsRuleBasedRendererV2Widget::countFeatures()
renderContext.setRendererScale( 0 ); // ignore scale
mRenderer->startRender( renderContext, mLayer->fields() );

int nFeatures = mLayer->pendingFeatureCount();
int nFeatures = mLayer->featureCount();
QProgressDialog p( tr( "Calculating feature count." ), tr( "Abort" ), 0, nFeatures );
p.setWindowModality( Qt::WindowModal );
int featuresCounted = 0;
Expand Down
8 changes: 4 additions & 4 deletions tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -77,7 +77,7 @@ void TestVectorLayerJoinBuffer::initTestCase()
QgsFeature fA2( mLayerA->dataProvider()->fields(), 2 );
fA2.setAttribute( "id_a", 2 );
mLayerA->dataProvider()->addFeatures( QgsFeatureList() << fA1 << fA2 );
QVERIFY( mLayerA->pendingFeatureCount() == 2 );
QVERIFY( mLayerA->featureCount() == 2 );

// LAYER B //

Expand All @@ -92,7 +92,7 @@ void TestVectorLayerJoinBuffer::initTestCase()
fB2.setAttribute( "id_b", 2 );
fB2.setAttribute( "value_b", 12 );
mLayerB->dataProvider()->addFeatures( QgsFeatureList() << fB1 << fB2 );
QVERIFY( mLayerB->pendingFeatureCount() == 2 );
QVERIFY( mLayerB->featureCount() == 2 );

// LAYER C //

Expand All @@ -104,7 +104,7 @@ void TestVectorLayerJoinBuffer::initTestCase()
fC1.setAttribute( "id_c", 1 );
fC1.setAttribute( "value_c", 101 );
mLayerC->dataProvider()->addFeatures( QgsFeatureList() << fC1 );
QVERIFY( mLayerC->pendingFeatureCount() == 1 );
QVERIFY( mLayerC->featureCount() == 1 );

QgsMapLayerRegistry::instance()->addMapLayer( mLayerA );
QgsMapLayerRegistry::instance()->addMapLayer( mLayerB );
Expand Down Expand Up @@ -259,7 +259,7 @@ void TestVectorLayerJoinBuffer::testJoinSubset()
fX1.setAttribute( "value_x1", 111 );
fX1.setAttribute( "value_x2", 222 );
layerX->dataProvider()->addFeatures( QgsFeatureList() << fX1 );
QVERIFY( layerX->pendingFeatureCount() == 1 );
QVERIFY( layerX->featureCount() == 1 );

QgsMapLayerRegistry::instance()->addMapLayer( layerX );

Expand Down

0 comments on commit cf2f6b1

Please sign in to comment.