Skip to content

Commit be5a0c9

Browse files
authoredNov 8, 2017
Merge pull request #5556 from m-kuhn/crashOnHighlight
No need to crash when a user wants a highlight
2 parents 17c7e2f + fd937c3 commit be5a0c9

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed
 

‎src/gui/qgshighlight.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void QgsHighlight::paintPolygon( QPainter *p, const QgsPolygonXY &polygon )
242242
{
243243
//adding point only if it is more than a pixel apart from the previous one
244244
const QPointF curVertex = toCanvasCoordinates( sourceVertex ) - pos();
245-
if ( std::abs( ring.back().x() - curVertex.x() ) > 1 || std::abs( ring.back().y() - curVertex.y() ) > 1 )
245+
if ( ring.isEmpty() || std::abs( ring.back().x() - curVertex.x() ) > 1 || std::abs( ring.back().y() - curVertex.y() ) > 1 )
246246
{
247247
ring.push_back( curVertex );
248248
}

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ ADD_PYTHON_TEST(PyQgsGeometryGeneratorSymbolLayer test_qgsgeometrygeneratorsymbo
7272
ADD_PYTHON_TEST(PyQgsGeometryTest test_qgsgeometry.py)
7373
ADD_PYTHON_TEST(PyQgsGeometryValidator test_qgsgeometryvalidator.py)
7474
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRenderer test_qgsgraduatedsymbolrenderer.py)
75+
ADD_PYTHON_TEST(PyQgsHighlight test_qgshighlight.py)
7576
ADD_PYTHON_TEST(PyQgsInterval test_qgsinterval.py)
7677
ADD_PYTHON_TEST(PyQgsJsonUtils test_qgsjsonutils.py)
7778
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)

‎tests/src/python/test_qgshighlight.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsHighlight.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Matthias Kuhn'
10+
__date__ = '8.11.2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
import os
17+
import tempfile
18+
import shutil
19+
20+
from qgis.PyQt.QtCore import (
21+
QSize,
22+
Qt
23+
)
24+
from qgis.PyQt.QtGui import (
25+
QColor,
26+
QImage,
27+
QPainter,
28+
QResizeEvent
29+
)
30+
from qgis.core import (
31+
QgsVectorLayer,
32+
QgsProject,
33+
QgsRectangle,
34+
QgsRenderChecker
35+
)
36+
from qgis.gui import QgsHighlight
37+
from qgis.testing import start_app, unittest
38+
from qgis.testing.mocked import get_iface
39+
from utilities import unitTestDataPath
40+
41+
start_app()
42+
TEST_DATA_DIR = unitTestDataPath()
43+
44+
45+
class TestQgsHighlight(unittest.TestCase):
46+
47+
def setUp(self):
48+
self.iface = get_iface()
49+
50+
self.iface.mapCanvas().viewport().resize(400, 400)
51+
# For some reason the resizeEvent is not delivered, fake it
52+
self.iface.mapCanvas().resizeEvent(QResizeEvent(QSize(400, 400), self.iface.mapCanvas().size()))
53+
54+
def tearDown(self):
55+
QgsProject.instance().removeAllMapLayers()
56+
57+
def runTestForLayer(self, layer, testname):
58+
tempdir = tempfile.mkdtemp()
59+
60+
layer = QgsVectorLayer(layer, 'Layer', 'ogr')
61+
QgsProject.instance().addMapLayer(layer)
62+
self.iface.mapCanvas().setExtent(layer.extent())
63+
64+
geom = next(layer.getFeatures()).geometry()
65+
66+
highlight = QgsHighlight(self.iface.mapCanvas(), geom, layer)
67+
color = QColor(Qt.red)
68+
highlight.setColor(color)
69+
highlight.setWidth(2)
70+
color.setAlpha(50)
71+
highlight.setFillColor(color)
72+
highlight.show()
73+
74+
image = QImage(QSize(400, 400), QImage.Format_ARGB32)
75+
image.fill(Qt.white)
76+
painter = QPainter()
77+
painter.begin(image)
78+
self.iface.mapCanvas().render(painter)
79+
painter.end()
80+
control_image = os.path.join(tempdir, 'highlight_{}.png'.format(testname))
81+
image.save(control_image)
82+
checker = QgsRenderChecker()
83+
checker.setControlPathPrefix("highlight")
84+
checker.setControlName("expected_highlight_{}".format(testname))
85+
checker.setRenderedImage(control_image)
86+
self.assertTrue(checker.compareImages("highlight_{}".format(testname)))
87+
shutil.rmtree(tempdir)
88+
89+
def testLine(self):
90+
lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp')
91+
self.runTestForLayer(lines_shp, 'lines')
92+
93+
def testPolygon(self):
94+
polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp')
95+
self.runTestForLayer(polys_shp, 'polygons')
96+
97+
98+
if __name__ == '__main__':
99+
unittest.main()
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.