Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 21, 2021
1 parent 704b280 commit 97f6344
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions tests/src/core/testqgscallout.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
//
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 97f6344

Please sign in to comment.