Add QgsMapLayer::isSpatial() method

The current approach of testing !vl || vl->geometryType() != NoGeometry
is not intuitive and has been the source of 2 recent bugs.

Replacing these tests with the new isSpatial() function makes it
immediately obvious what is being tested. It also allows for
non-spatial plugin layers to be correctly handled by overriding
this method.
This commit is contained in:
Nyall Dawson 2016-03-16 12:35:45 +11:00
parent 7de0757f56
commit 5e08626051
11 changed files with 40 additions and 3 deletions

View File

@ -273,6 +273,11 @@ class QgsMapLayer : QObject
/** True if the layer can be edited */
virtual bool isEditable() const;
/** Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated with it.
* @note added in QGIS 2.16
*/
virtual bool isSpatial() const;
/** Sets state from Dom document
@param layerElement The Dom element corresponding to ``maplayer'' tag
@note

View File

@ -975,6 +975,8 @@ class QgsVectorLayer : QgsMapLayer
/** Returns true if the provider is in editing mode */
virtual bool isEditable() const;
virtual bool isSpatial() const;
/** Returns true if the provider is in read-only mode */
virtual bool isReadOnly() const;

View File

@ -144,6 +144,8 @@ class QgsRasterLayer : QgsMapLayer
/** Returns a list with classification items (Text and color) */
QList< QPair< QString, QColor > > legendSymbologyItems() const;
virtual bool isSpatial() const;
/** \brief Obtain GDAL Metadata for this layer */
QString metadata();

View File

@ -101,7 +101,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
// duplicate layer
QAction* duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );
if ( !vlayer || vlayer->geometryType() != QGis::NoGeometry )
if ( layer->isSpatial() )
{
// set layer scale visibility
menu->addAction( tr( "&Set Layer Scale Visibility" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) );

View File

@ -289,6 +289,11 @@ class CORE_EXPORT QgsMapLayer : public QObject
/** True if the layer can be edited */
virtual bool isEditable() const;
/** Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated with it.
* @note added in QGIS 2.16
*/
virtual bool isSpatial() const { return true; }
/** Sets state from Dom document
@param layerElement The Dom element corresponding to ``maplayer'' tag
@note

View File

@ -2618,6 +2618,11 @@ bool QgsVectorLayer::isEditable() const
return ( mEditBuffer && mDataProvider );
}
bool QgsVectorLayer::isSpatial() const
{
return geometryType() != QGis::NoGeometry;
}
bool QgsVectorLayer::isReadOnly() const
{
return mReadOnly;

View File

@ -1088,6 +1088,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Returns true if the provider is in editing mode */
virtual bool isEditable() const override;
virtual bool isSpatial() const override;
/** Returns true if the provider is in read-only mode */
virtual bool isReadOnly() const;

View File

@ -280,6 +280,8 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** Returns a list with classification items (Text and color) */
QgsLegendColorList legendSymbologyItems() const;
virtual bool isSpatial() const override { return true; }
/** \brief Obtain GDAL Metadata for this layer */
QString metadata() override;

View File

@ -143,8 +143,7 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
if ( !layerNode->layer() )
continue;
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerNode->layer() );
if ( !vl || vl->geometryType() != QGis::NoGeometry )
if ( layerNode->layer()->isSpatial() )
{
mCanvas->setDestinationCrs( layerNode->layer()->crs() );
mCanvas->setMapUnits( layerNode->layer()->crs().mapUnits() );

View File

@ -69,6 +69,7 @@ class TestQgsRasterLayer : public QObject
void cleanup() {} // will be called after every testfunction.
void isValid();
void isSpatial();
void pseudoColor();
void colorRamp1();
void colorRamp2();
@ -205,6 +206,11 @@ void TestQgsRasterLayer::isValid()
QVERIFY( render( "raster" ) );
}
void TestQgsRasterLayer::isSpatial()
{
QVERIFY( mpRasterLayer->isSpatial() );
}
void TestQgsRasterLayer::pseudoColor()
{
QgsRasterShader* rasterShader = new QgsRasterShader();

View File

@ -104,6 +104,7 @@ class TestQgsVectorLayer : public QObject
void uniqueValues();
void minimumValue();
void maximumValue();
void isSpatial();
};
void TestQgsVectorLayer::initTestCase()
@ -341,5 +342,13 @@ void TestQgsVectorLayer::maximumValue()
QCOMPARE( vLayer->maximumValue( 1000 ), QVariant() );
}
void TestQgsVectorLayer::isSpatial()
{
QVERIFY( mpPointsLayer->isSpatial() );
QVERIFY( mpPolysLayer->isSpatial() );
QVERIFY( mpLinesLayer->isSpatial() );
QVERIFY( !mpNonSpatialLayer->isSpatial() );
}
QTEST_MAIN( TestQgsVectorLayer )
#include "testqgsvectorlayer.moc"