Skip to content
Permalink
Browse files

Unit tests

  • Loading branch information
nyalldawson committed Mar 21, 2021
1 parent 704b280 commit 97f63443de22196dc5da821faca9124f538a1f91
@@ -172,6 +172,9 @@ class TestQgsCallout: public QObject
void curvedClockwise();
void curvedCounterClockwise();
void curvedCurvature();
void balloonCallout();
void balloonCalloutMargin();
void balloonCalloutWedgeWidth();

private:
bool imageCheck( const QString &testName, QImage &image, unsigned int mismatchCount = 0 );
@@ -3886,6 +3889,170 @@ void TestQgsCallout::curvedCurvature()
QVERIFY( imageCheck( "curved_curvature", img, 20 ) );
}

void TestQgsCallout::balloonCallout()
{
QSize size( 640, 480 );
QgsMapSettings mapSettings;
mapSettings.setOutputSize( size );
mapSettings.setExtent( vl->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vl );
mapSettings.setOutputDpi( 96 );

// first render the map and labeling separately

QgsMapRendererSequentialJob job( mapSettings );
job.start();
job.waitForFinished();

QImage img = job.renderedImage();

QPainter p( &img );
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
context.setPainter( &p );

QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Class" );
settings.placement = QgsPalLayerSettings::AroundPoint;
settings.dist = 7;

QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
format.setSize( 12 );
format.setNamedStyle( QStringLiteral( "Bold" ) );
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );

QgsBalloonCallout *callout = new QgsBalloonCallout();
callout->setEnabled( true );
callout->setFillSymbol( QgsFillSymbol::createSimple( QVariantMap( { { "color", "#ffcccc"},
{ "outline-width", "1"}
} ) ) );
settings.setCallout( callout );

vl->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vl->setLabelsEnabled( true );

QgsDefaultLabelingEngine engine;
engine.setMapSettings( mapSettings );
engine.addProvider( new QgsVectorLayerLabelProvider( vl, QString(), true, &settings ) );
//engine.setFlags( QgsLabelingEngine::RenderOutlineLabels | QgsLabelingEngine::DrawLabelRectOnly );
engine.run( context );

p.end();

QVERIFY( imageCheck( "balloon_callout_render", img, 20 ) );
}

void TestQgsCallout::balloonCalloutMargin()
{
QSize size( 640, 480 );
QgsMapSettings mapSettings;
mapSettings.setOutputSize( size );
mapSettings.setExtent( vl->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vl );
mapSettings.setOutputDpi( 96 );

// first render the map and labeling separately

QgsMapRendererSequentialJob job( mapSettings );
job.start();
job.waitForFinished();

QImage img = job.renderedImage();

QPainter p( &img );
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
context.setPainter( &p );

QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Class" );
settings.placement = QgsPalLayerSettings::AroundPoint;
settings.dist = 10;

QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
format.setSize( 12 );
format.setNamedStyle( QStringLiteral( "Bold" ) );
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );

QgsBalloonCallout *callout = new QgsBalloonCallout();
callout->setEnabled( true );
callout->setFillSymbol( QgsFillSymbol::createSimple( QVariantMap( { { "color", "#ffcccc"},
{ "outline-width", "1"}
} ) ) );
callout->setMargins( QgsMargins( 1, 2, 3, 4 ) );
settings.setCallout( callout );

vl->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vl->setLabelsEnabled( true );

QgsDefaultLabelingEngine engine;
engine.setMapSettings( mapSettings );
engine.addProvider( new QgsVectorLayerLabelProvider( vl, QString(), true, &settings ) );
//engine.setFlags( QgsLabelingEngine::RenderOutlineLabels | QgsLabelingEngine::DrawLabelRectOnly );
engine.run( context );

p.end();

QVERIFY( imageCheck( "balloon_callout_margin", img, 20 ) );
}

void TestQgsCallout::balloonCalloutWedgeWidth()
{
QSize size( 640, 480 );
QgsMapSettings mapSettings;
mapSettings.setOutputSize( size );
mapSettings.setExtent( vl->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vl );
mapSettings.setOutputDpi( 96 );

// first render the map and labeling separately

QgsMapRendererSequentialJob job( mapSettings );
job.start();
job.waitForFinished();

QImage img = job.renderedImage();

QPainter p( &img );
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
context.setPainter( &p );

QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Class" );
settings.placement = QgsPalLayerSettings::AroundPoint;
settings.dist = 10;

QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
format.setSize( 12 );
format.setNamedStyle( QStringLiteral( "Bold" ) );
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );

QgsBalloonCallout *callout = new QgsBalloonCallout();
callout->setEnabled( true );
callout->setFillSymbol( QgsFillSymbol::createSimple( QVariantMap( { { "color", "#ffcccc"},
{ "outline-width", "1"}
} ) ) );
callout->setWedgeWidth( 6 );
settings.setCallout( callout );

vl->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vl->setLabelsEnabled( true );

QgsDefaultLabelingEngine engine;
engine.setMapSettings( mapSettings );
engine.addProvider( new QgsVectorLayerLabelProvider( vl, QString(), true, &settings ) );
//engine.setFlags( QgsLabelingEngine::RenderOutlineLabels | QgsLabelingEngine::DrawLabelRectOnly );
engine.run( context );

p.end();

QVERIFY( imageCheck( "balloon_callout_wedge_width", img, 20 ) );
}

//
// Private helper functions not called directly by CTest
//
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 97f6344

Please sign in to comment.