Skip to content

Commit

Permalink
Comments and API improvements of simplification (no functional changes)
Browse files Browse the repository at this point in the history
- moved QgsVectorLayer::SimplifyHint enum to QgsVectorSimplifyMethod
- use QFlags instead of int for hints
- added few notes about simplification being added in 2.2
  • Loading branch information
wonder-sk committed Feb 21, 2014
1 parent 10f2ac9 commit da1ebc2
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 52 deletions.
3 changes: 3 additions & 0 deletions python/core/qgsfeaturerequest.sip
Expand Up @@ -65,7 +65,10 @@ class QgsFeatureRequest
QgsFeatureRequest& setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields );

//! Set a simplification method for geometries that will be fetched
//! @note added in 2.2

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Feb 22, 2014

Contributor

Thank you very much @wonder-sk

QgsFeatureRequest& setSimplifyMethod( const QgsSimplifyMethod& simplifyMethod );
//! Get simplification method for geometries that will be fetched
//! @note added in 2.2
const QgsSimplifyMethod& simplifyMethod() const;

/**
Expand Down
4 changes: 3 additions & 1 deletion python/core/qgssimplifymethod.sip
@@ -1,5 +1,7 @@

/** This class contains information about how to simplify geometries fetched from a QgsFeatureIterator */
/** This class contains information about how to simplify geometries fetched from a QgsFeatureIterator
* @note added in 2.2
*/
class QgsSimplifyMethod
{
%TypeHeaderCode
Expand Down
23 changes: 11 additions & 12 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -1025,21 +1025,20 @@ class QgsVectorLayer : QgsMapLayer
/** @note not available in python bindings */
// inline QgsGeometryCache* cache();

/** Simplification flags for fast rendering of features */
enum SimplifyHint
{
NoSimplification = 0, //!< No simplification can be applied
GeometrySimplification = 1, //!< The geometries can be simplified using the current map2pixel context state
AntialiasingSimplification = 2, //!< The geometries can be rendered with 'AntiAliasing' disabled because of it is '1-pixel size'
FullSimplification = 3, //!< All simplification hints can be applied ( Geometry + AA-disabling )
};
/** Set the simplification settings for fast rendering of features */
/** Set the simplification settings for fast rendering of features
* @note added in 2.2
*/
void setSimplifyMethod( const QgsVectorSimplifyMethod& simplifyMethod );
/** Returns the simplification settings for fast rendering of features */
/** Returns the simplification settings for fast rendering of features
* @note added in 2.2
*/
const QgsVectorSimplifyMethod& simplifyMethod() const;

/** Returns whether the VectorLayer can apply the specified simplification hint */
bool simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const;
/** Returns whether the VectorLayer can apply the specified simplification hint
* @note Do not use in 3rd party code - may be removed in future version!
* @note added in 2.2
*/
bool simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, QgsVectorSimplifyMethod::SimplifyHint simplifyHint ) const;

public slots:
/**
Expand Down
20 changes: 17 additions & 3 deletions python/core/qgsvectorsimplifymethod.sip
@@ -1,5 +1,7 @@

/** This class contains information how to simplify geometries fetched from a vector layer */
/** This class contains information how to simplify geometries fetched from a vector layer
* @note added in 2.2
*/
class QgsVectorSimplifyMethod
{
%TypeHeaderCode
Expand All @@ -12,10 +14,20 @@ class QgsVectorSimplifyMethod
//! copy constructor
QgsVectorSimplifyMethod( const QgsVectorSimplifyMethod& rh );

/** Simplification flags for fast rendering of features */
enum SimplifyHint
{
NoSimplification = 0, //!< No simplification can be applied
GeometrySimplification = 1, //!< The geometries can be simplified using the current map2pixel context state
AntialiasingSimplification = 2, //!< The geometries can be rendered with 'AntiAliasing' disabled because of it is '1-pixel size'
FullSimplification = 3, //!< All simplification hints can be applied ( Geometry + AA-disabling )
};
typedef QFlags<QgsVectorSimplifyMethod::SimplifyHint> SimplifyHints;

/** Sets the simplification hints of the vector layer managed */
void setSimplifyHints( int simplifyHints );
void setSimplifyHints( QFlags<QgsVectorSimplifyMethod::SimplifyHint> simplifyHints );
/** Gets the simplification hints of the vector layer managed */
int simplifyHints() const;
QFlags<QgsVectorSimplifyMethod::SimplifyHint> simplifyHints() const;

/** Sets the simplification threshold of the vector layer managed */
void setThreshold( float threshold );
Expand All @@ -32,3 +44,5 @@ class QgsVectorSimplifyMethod
/** Gets the maximum scale at which the layer should be simplified */
float maximumScale() const;
};

QFlags<QgsVectorSimplifyMethod::SimplifyHint> operator|( QgsVectorSimplifyMethod::SimplifyHint f1, QFlags<QgsVectorSimplifyMethod::SimplifyHint> f2 );
10 changes: 5 additions & 5 deletions src/app/qgsoptions.cpp
Expand Up @@ -569,7 +569,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
chkUseRenderCaching->setChecked( settings.value( "/qgis/enable_render_caching", false ).toBool() );

// Default simplify drawing configuration
mSimplifyDrawingGroupBox->setChecked( settings.value( "/qgis/simplifyDrawingHints", ( int )QgsVectorLayer::GeometrySimplification ).toInt() != QgsVectorLayer::NoSimplification );
mSimplifyDrawingGroupBox->setChecked( settings.value( "/qgis/simplifyDrawingHints", ( int )QgsVectorSimplifyMethod::GeometrySimplification ).toInt() != QgsVectorSimplifyMethod::NoSimplification );
mSimplifyDrawingSpinBox->setValue( settings.value( "/qgis/simplifyDrawingTol", QGis::DEFAULT_MAPTOPIXEL_THRESHOLD ).toFloat() );
mSimplifyDrawingAtProvider->setChecked( !settings.value( "/qgis/simplifyLocal", true ).toBool() );

Expand Down Expand Up @@ -1110,13 +1110,13 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );

// Default simplify drawing configuration
int simplifyHints = QgsVectorLayer::NoSimplification;
QgsVectorSimplifyMethod::SimplifyHints simplifyHints = QgsVectorSimplifyMethod::NoSimplification;
if ( mSimplifyDrawingGroupBox->isChecked() )
{
simplifyHints |= QgsVectorLayer::GeometrySimplification;
if ( mSimplifyDrawingSpinBox->value() > 1 ) simplifyHints |= QgsVectorLayer::AntialiasingSimplification;
simplifyHints |= QgsVectorSimplifyMethod::GeometrySimplification;
if ( mSimplifyDrawingSpinBox->value() > 1 ) simplifyHints |= QgsVectorSimplifyMethod::AntialiasingSimplification;
}
settings.setValue( "/qgis/simplifyDrawingHints", simplifyHints );
settings.setValue( "/qgis/simplifyDrawingHints", ( int ) simplifyHints );
settings.setValue( "/qgis/simplifyDrawingTol", mSimplifyDrawingSpinBox->value() );
settings.setValue( "/qgis/simplifyLocal", !mSimplifyDrawingAtProvider->isChecked() );
settings.setValue( "/qgis/simplifyMaxScale", 1.0 / mSimplifyMaximumScaleComboBox->scale() );
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -393,7 +393,7 @@ void QgsVectorLayerProperties::syncToLayer( void )

// get simplify drawing configuration
const QgsVectorSimplifyMethod& simplifyMethod = layer->simplifyMethod();
mSimplifyDrawingGroupBox->setChecked( simplifyMethod.simplifyHints() != QgsVectorLayer::NoSimplification );
mSimplifyDrawingGroupBox->setChecked( simplifyMethod.simplifyHints() != QgsVectorSimplifyMethod::NoSimplification );
mSimplifyDrawingSpinBox->setValue( simplifyMethod.threshold() );

if ( !( layer->dataProvider()->capabilities() & QgsVectorDataProvider::SimplifyGeometries ) )
Expand Down Expand Up @@ -558,11 +558,11 @@ void QgsVectorLayerProperties::apply()
layer->setMetadataUrlFormat( mLayerMetadataUrlFormatComboBox->currentText() );

//layer simplify drawing configuration
int simplifyHints = QgsVectorLayer::NoSimplification;
QgsVectorSimplifyMethod::SimplifyHints simplifyHints = QgsVectorSimplifyMethod::NoSimplification;
if ( mSimplifyDrawingGroupBox->isChecked() )
{
simplifyHints |= QgsVectorLayer::GeometrySimplification;
if ( mSimplifyDrawingSpinBox->value() > 1 ) simplifyHints |= QgsVectorLayer::AntialiasingSimplification;
simplifyHints |= QgsVectorSimplifyMethod::GeometrySimplification;
if ( mSimplifyDrawingSpinBox->value() > 1 ) simplifyHints |= QgsVectorSimplifyMethod::AntialiasingSimplification;
}
QgsVectorSimplifyMethod simplifyMethod = layer->simplifyMethod();
simplifyMethod.setSimplifyHints( simplifyHints );
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsfeaturerequest.h
Expand Up @@ -123,7 +123,10 @@ class CORE_EXPORT QgsFeatureRequest
QgsFeatureRequest& setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields );

//! Set a simplification method for geometries that will be fetched
//! @note added in 2.2
QgsFeatureRequest& setSimplifyMethod( const QgsSimplifyMethod& simplifyMethod );
//! Get simplification method for geometries that will be fetched
//! @note added in 2.2
const QgsSimplifyMethod& simplifyMethod() const { return mSimplifyMethod; }

/**
Expand Down
1 change: 1 addition & 0 deletions src/core/qgssimplifymethod.h
Expand Up @@ -20,6 +20,7 @@ class QgsAbstractGeometrySimplifier;

/**
* This class contains information about how to simplify geometries fetched from a QgsFeatureIterator
* @note added in 2.2
*/
class CORE_EXPORT QgsSimplifyMethod
{
Expand Down
8 changes: 4 additions & 4 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -186,7 +186,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,

// Default simplify drawing settings
QSettings settings;
mSimplifyMethod.setSimplifyHints( settings.value( "/qgis/simplifyDrawingHints", mSimplifyMethod.simplifyHints() ).toInt() );
mSimplifyMethod.setSimplifyHints( ( QgsVectorSimplifyMethod::SimplifyHints ) settings.value( "/qgis/simplifyDrawingHints", ( int ) mSimplifyMethod.simplifyHints() ).toInt() );
mSimplifyMethod.setThreshold( settings.value( "/qgis/simplifyDrawingTol", mSimplifyMethod.threshold() ).toFloat() );
mSimplifyMethod.setForceLocalOptimization( settings.value( "/qgis/simplifyLocal", mSimplifyMethod.forceLocalOptimization() ).toBool() );
mSimplifyMethod.setMaximumScale( settings.value( "/qgis/simplifyMaxScale", mSimplifyMethod.maximumScale() ).toFloat() );
Expand Down Expand Up @@ -704,7 +704,7 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
.setSubsetOfAttributes( attributes );

// enable the simplification of the geometries (Using the current map2pixel context) before send it to renderer engine.
if ( simplifyDrawingCanbeApplied( rendererContext, QgsVectorLayer::GeometrySimplification ) )
if ( simplifyDrawingCanbeApplied( rendererContext, QgsVectorSimplifyMethod::GeometrySimplification ) )
{
QPainter* p = rendererContext.painter();
double dpi = ( p->device()->logicalDpiX() + p->device()->logicalDpiY() ) / 2;
Expand Down Expand Up @@ -1290,7 +1290,7 @@ bool QgsVectorLayer::setSubsetString( QString subset )
return res;
}

bool QgsVectorLayer::simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const
bool QgsVectorLayer::simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, QgsVectorSimplifyMethod::SimplifyHint simplifyHint ) const
{
if ( mDataProvider && !mEditBuffer && ( hasGeometryType() && geometryType() != QGis::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
{
Expand Down Expand Up @@ -1907,7 +1907,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}

// get the simplification drawing settings
mSimplifyMethod.setSimplifyHints( e.attribute( "simplifyDrawingHints", "1" ).toInt() );
mSimplifyMethod.setSimplifyHints( ( QgsVectorSimplifyMethod::SimplifyHints ) e.attribute( "simplifyDrawingHints", "1" ).toInt() );
mSimplifyMethod.setThreshold( e.attribute( "simplifyDrawingTol", "1" ).toFloat() );
mSimplifyMethod.setForceLocalOptimization( e.attribute( "simplifyLocal", "1" ).toInt() );
mSimplifyMethod.setMaximumScale( e.attribute( "simplifyMaxScale", "1" ).toFloat() );
Expand Down
23 changes: 11 additions & 12 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1376,21 +1376,20 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** @note not available in python bindings */
inline QgsGeometryCache* cache() { return mCache; }

/** Simplification flags for fast rendering of features */
enum SimplifyHint
{
NoSimplification = 0, //!< No simplification can be applied
GeometrySimplification = 1, //!< The geometries can be simplified using the current map2pixel context state
AntialiasingSimplification = 2, //!< The geometries can be rendered with 'AntiAliasing' disabled because of it is '1-pixel size'
FullSimplification = 3, //!< All simplification hints can be applied ( Geometry + AA-disabling )
};
/** Set the simplification settings for fast rendering of features */
/** Set the simplification settings for fast rendering of features
* @note added in 2.2
*/
void setSimplifyMethod( const QgsVectorSimplifyMethod& simplifyMethod ) { mSimplifyMethod = simplifyMethod; }
/** Returns the simplification settings for fast rendering of features */
/** Returns the simplification settings for fast rendering of features
* @note added in 2.2
*/
inline const QgsVectorSimplifyMethod& simplifyMethod() const { return mSimplifyMethod; }

/** Returns whether the VectorLayer can apply the specified simplification hint */
bool simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const;
/** Returns whether the VectorLayer can apply the specified simplification hint
* @note Do not use in 3rd party code - may be removed in future version!
* @note added in 2.2
*/
bool simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, QgsVectorSimplifyMethod::SimplifyHint simplifyHint ) const;

public slots:
/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorsimplifymethod.cpp
Expand Up @@ -18,7 +18,7 @@
#include "qgsvectorlayer.h"

QgsVectorSimplifyMethod::QgsVectorSimplifyMethod()
: mSimplifyHints( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD > 1 ? QgsVectorLayer::FullSimplification : QgsVectorLayer::GeometrySimplification )
: mSimplifyHints( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD > 1 ? QgsVectorSimplifyMethod::FullSimplification : QgsVectorSimplifyMethod::GeometrySimplification )
, mThreshold( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD )
, mLocalOptimization( true )
, mMaximumScale( 1 )
Expand Down
24 changes: 20 additions & 4 deletions src/core/qgsvectorsimplifymethod.h
Expand Up @@ -16,7 +16,11 @@
#ifndef QGSVECTORSIMPLIFYMETHOD_H
#define QGSVECTORSIMPLIFYMETHOD_H

/** This class contains information how to simplify geometries fetched from a vector layer */
#include <QFlags>

/** This class contains information how to simplify geometries fetched from a vector layer
* @note added in 2.2
*/
class CORE_EXPORT QgsVectorSimplifyMethod
{
public:
Expand All @@ -27,10 +31,20 @@ class CORE_EXPORT QgsVectorSimplifyMethod
//! assignment operator
QgsVectorSimplifyMethod& operator=( const QgsVectorSimplifyMethod& rh );

/** Simplification flags for fast rendering of features */
enum SimplifyHint
{
NoSimplification = 0, //!< No simplification can be applied
GeometrySimplification = 1, //!< The geometries can be simplified using the current map2pixel context state
AntialiasingSimplification = 2, //!< The geometries can be rendered with 'AntiAliasing' disabled because of it is '1-pixel size'
FullSimplification = 3, //!< All simplification hints can be applied ( Geometry + AA-disabling )
};
Q_DECLARE_FLAGS( SimplifyHints, SimplifyHint )

/** Sets the simplification hints of the vector layer managed */
void setSimplifyHints( int simplifyHints ) { mSimplifyHints = simplifyHints; }
void setSimplifyHints( SimplifyHints simplifyHints ) { mSimplifyHints = simplifyHints; }
/** Gets the simplification hints of the vector layer managed */
inline int simplifyHints() const { return mSimplifyHints; }
inline SimplifyHints simplifyHints() const { return mSimplifyHints; }

/** Sets the simplification threshold of the vector layer managed */
void setThreshold( float threshold ) { mThreshold = threshold; }
Expand All @@ -49,7 +63,7 @@ class CORE_EXPORT QgsVectorSimplifyMethod

private:
/** Simplification hints for fast rendering of features of the vector layer managed */
int mSimplifyHints;
SimplifyHints mSimplifyHints;
/** Simplification threshold */
float mThreshold;
/** Simplification executes after fetch the geometries from provider, otherwise it executes, when supported, in provider before fetch the geometries */
Expand All @@ -58,4 +72,6 @@ class CORE_EXPORT QgsVectorSimplifyMethod
float mMaximumScale;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorSimplifyMethod::SimplifyHints )

#endif // QGSVECTORSIMPLIFYMETHOD_H
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgslinesymbollayerv2.cpp
Expand Up @@ -188,7 +188,7 @@ void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym
p->setPen( context.selected() ? mSelPen : mPen );

// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #2 points).
if ( points.size() <= 2 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
if ( points.size() <= 2 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorSimplifyMethod::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
{
p->setRenderHint( QPainter::Antialiasing, false );
p->drawPolyline( points );
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbollayerv2.cpp
Expand Up @@ -388,7 +388,7 @@ void QgsFillSymbolLayerV2::_renderPolygon( QPainter* p, const QPolygonF& points,
}

// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #5 points).
if ( points.size() <= 5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
if ( points.size() <= 5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorSimplifyMethod::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
{
p->setRenderHint( QPainter::Antialiasing, false );
p->drawRect( points.boundingRect() );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsatlascomposition.cpp
Expand Up @@ -81,7 +81,7 @@ void TestQgsAtlasComposition::initTestCase()
"ogr" );

QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorLayer::NoSimplification );
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
mVectorLayer->setSimplifyMethod( simplifyMethod );

QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << mVectorLayer );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsblendmodes.cpp
Expand Up @@ -89,7 +89,7 @@ void TestQgsBlendModes::initTestCase()
myPolyFileInfo.completeBaseName(), "ogr" );

QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorLayer::NoSimplification );
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );

mpPolysLayer->setSimplifyMethod( simplifyMethod );
QgsMapLayerRegistry::instance()->addMapLayers(
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsgradients.cpp
Expand Up @@ -96,7 +96,7 @@ void TestQgsGradients::initTestCase()
myPolyFileInfo.completeBaseName(), "ogr" );

QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorLayer::NoSimplification );
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
mpPolysLayer->setSimplifyMethod( simplifyMethod );

// Register the layer with the registry
Expand Down Expand Up @@ -250,7 +250,7 @@ void TestQgsGradients::gradientSymbolFromQml()
mReport += "<h2>Gradient symbol from QML test</h2>\n";
QVERIFY( setQml( "gradient" ) );
QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorLayer::NoSimplification );
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
mpPolysLayer->setSimplifyMethod( simplifyMethod );
QVERIFY( imageCheck( "gradient_from_qml" ) );
}
Expand Down

0 comments on commit da1ebc2

Please sign in to comment.