Skip to content
Permalink
Browse files

Add new tests

  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent 787f62f commit c5dd120860b166ae7cdc3e5671f490add6ee21aa
@@ -188,8 +188,8 @@ QSizeF QgsColorRampLegendNode::drawSymbol( const QgsLegendSettings &settings, It
const double gradientTop = rampTopMm * dotsPerMM;
const double gradientBottom = gradientTop + height * dotsPerMM;

QLinearGradient gradient( 0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientBottom : gradientTop,
0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientTop : gradientBottom );
QLinearGradient gradient( 0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientTop : gradientBottom,
0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientBottom : gradientTop );
if ( mRamp->type() == QgsGradientColorRamp::typeString() || mRamp->type() == QgsCptCityColorRamp::typeString() )
{
//color ramp gradient
@@ -29,7 +29,8 @@
QgsMapSettings,
QgsColorRampLegendNodeSettings,
QgsBearingNumericFormat,
QgsReadWriteContext)
QgsReadWriteContext,
QgsBasicNumericFormat)
from qgis.testing import start_app, unittest

start_app()
@@ -127,6 +128,32 @@ def test_icon(self):

self.assertTrue(self.imageCheck('color_ramp_legend_node_icon', 'color_ramp_legend_node_icon', im, 10))

def test_icon_with_settings(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))

# need a layer in order to make legend nodes
layer = QgsVectorLayer('dummy', 'test', 'memory')
layer_tree_layer = QgsLayerTreeLayer(layer)

settings = QgsColorRampLegendNodeSettings()
format = QgsBasicNumericFormat()
format.setShowTrailingZeros(True)
format.setNumberDecimalPlaces(3)
settings.setNumericFormat(format)
settings.setDirection(QgsColorRampLegendNodeSettings.MaximumToMinimum)

node = TestColorRampLegend(layer_tree_layer, r, settings, 5, 10)

pixmap = node.data(Qt.DecorationRole)

im = QImage(pixmap.size(), QImage.Format_ARGB32)
im.fill(QColor(255, 255, 255))
p = QPainter(im)
p.drawPixmap(0, 0, pixmap)
p.end()

self.assertTrue(self.imageCheck('color_ramp_legend_node_settings_icon', 'color_ramp_legend_node_settings_icon', im, 10))

def test_draw(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))

@@ -169,6 +196,55 @@ def test_draw(self):

self.assertTrue(self.imageCheck('color_ramp_legend_node_draw', 'color_ramp_legend_node_draw', image))

def test_draw_settings(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))

# need a layer in order to make legend nodes
layer = QgsVectorLayer('dummy', 'test', 'memory')
layer_tree_layer = QgsLayerTreeLayer(layer)

settings = QgsColorRampLegendNodeSettings()
format = QgsBasicNumericFormat()
format.setShowTrailingZeros(True)
format.setNumberDecimalPlaces(3)
settings.setNumericFormat(format)
settings.setDirection(QgsColorRampLegendNodeSettings.MaximumToMinimum)

node = QgsColorRampLegendNode(layer_tree_layer, r, settings, 5, 10)

ls = QgsLegendSettings()
item_style = ls.style(QgsLegendStyle.SymbolLabel)
item_style.setFont(QgsFontUtils.getStandardTestFont('Bold', 18))
ls.setStyle(QgsLegendStyle.SymbolLabel, item_style)

item_context = QgsLayerTreeModelLegendNode.ItemContext()

image = QImage(400, 250, QImage.Format_ARGB32)
image.fill(QColor(255, 255, 255))

p = QPainter(image)

ms = QgsMapSettings()
ms.setExtent(QgsRectangle(1, 10, 1, 10))
ms.setOutputSize(image.size())
context = QgsRenderContext.fromMapSettings(ms)
context.setPainter(p)
context.setScaleFactor(150 / 25.4) # 150 DPI

p.scale(context.scaleFactor(), context.scaleFactor())

item_context.context = context
item_context.painter = p
item_context.top = 1
item_context.columnLeft = 3
item_context.columnRight = 30
item_context.patchSize = QSizeF(12, 40)

node.drawSymbol(ls, item_context, 0)
p.end()

self.assertTrue(self.imageCheck('color_ramp_legend_node_settings_draw', 'color_ramp_legend_node_settings_draw', image))

def imageCheck(self, name, reference_image, image, size_tolerance=0):
TestQgsColorRampLegendNode.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
Binary file not shown.
Binary file not shown.

0 comments on commit c5dd120

Please sign in to comment.
You can’t perform that action at this time.