Skip to content

Commit

Permalink
Merge pull request #5556 from m-kuhn/crashOnHighlight
Browse files Browse the repository at this point in the history
No need to crash when a user wants a highlight
  • Loading branch information
m-kuhn committed Nov 8, 2017
2 parents 17c7e2f + fd937c3 commit be5a0c9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gui/qgshighlight.cpp
Expand Up @@ -242,7 +242,7 @@ void QgsHighlight::paintPolygon( QPainter *p, const QgsPolygonXY &polygon )
{
//adding point only if it is more than a pixel apart from the previous one
const QPointF curVertex = toCanvasCoordinates( sourceVertex ) - pos();
if ( std::abs( ring.back().x() - curVertex.x() ) > 1 || std::abs( ring.back().y() - curVertex.y() ) > 1 )
if ( ring.isEmpty() || std::abs( ring.back().x() - curVertex.x() ) > 1 || std::abs( ring.back().y() - curVertex.y() ) > 1 )
{
ring.push_back( curVertex );
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -72,6 +72,7 @@ ADD_PYTHON_TEST(PyQgsGeometryGeneratorSymbolLayer test_qgsgeometrygeneratorsymbo
ADD_PYTHON_TEST(PyQgsGeometryTest test_qgsgeometry.py)
ADD_PYTHON_TEST(PyQgsGeometryValidator test_qgsgeometryvalidator.py)
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRenderer test_qgsgraduatedsymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsHighlight test_qgshighlight.py)
ADD_PYTHON_TEST(PyQgsInterval test_qgsinterval.py)
ADD_PYTHON_TEST(PyQgsJsonUtils test_qgsjsonutils.py)
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)
Expand Down
99 changes: 99 additions & 0 deletions tests/src/python/test_qgshighlight.py
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsHighlight.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Matthias Kuhn'
__date__ = '8.11.2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA
import os
import tempfile
import shutil

from qgis.PyQt.QtCore import (
QSize,
Qt
)
from qgis.PyQt.QtGui import (
QColor,
QImage,
QPainter,
QResizeEvent
)
from qgis.core import (
QgsVectorLayer,
QgsProject,
QgsRectangle,
QgsRenderChecker
)
from qgis.gui import QgsHighlight
from qgis.testing import start_app, unittest
from qgis.testing.mocked import get_iface
from utilities import unitTestDataPath

start_app()
TEST_DATA_DIR = unitTestDataPath()


class TestQgsHighlight(unittest.TestCase):

def setUp(self):
self.iface = get_iface()

self.iface.mapCanvas().viewport().resize(400, 400)
# For some reason the resizeEvent is not delivered, fake it
self.iface.mapCanvas().resizeEvent(QResizeEvent(QSize(400, 400), self.iface.mapCanvas().size()))

def tearDown(self):
QgsProject.instance().removeAllMapLayers()

def runTestForLayer(self, layer, testname):
tempdir = tempfile.mkdtemp()

layer = QgsVectorLayer(layer, 'Layer', 'ogr')
QgsProject.instance().addMapLayer(layer)
self.iface.mapCanvas().setExtent(layer.extent())

geom = next(layer.getFeatures()).geometry()

highlight = QgsHighlight(self.iface.mapCanvas(), geom, layer)
color = QColor(Qt.red)
highlight.setColor(color)
highlight.setWidth(2)
color.setAlpha(50)
highlight.setFillColor(color)
highlight.show()

image = QImage(QSize(400, 400), QImage.Format_ARGB32)
image.fill(Qt.white)
painter = QPainter()
painter.begin(image)
self.iface.mapCanvas().render(painter)
painter.end()
control_image = os.path.join(tempdir, 'highlight_{}.png'.format(testname))
image.save(control_image)
checker = QgsRenderChecker()
checker.setControlPathPrefix("highlight")
checker.setControlName("expected_highlight_{}".format(testname))
checker.setRenderedImage(control_image)
self.assertTrue(checker.compareImages("highlight_{}".format(testname)))
shutil.rmtree(tempdir)

def testLine(self):
lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp')
self.runTestForLayer(lines_shp, 'lines')

def testPolygon(self):
polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp')
self.runTestForLayer(polys_shp, 'polygons')


if __name__ == '__main__':
unittest.main()
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 be5a0c9

Please sign in to comment.