Insert NaN values for connect='pairs' in Qt6

Since the QPainterPath.addPolygon(QPolygonF) does not filter out the NaN
values, we can insert NaN values to create discontinuities in the Lines,
and thus supporting connect='pairs' functionality
This commit is contained in:
Ogi Moore 2021-05-26 18:58:13 -07:00
parent 78e678c1b9
commit 270fc59cab
2 changed files with 22 additions and 9 deletions

View File

@ -1744,6 +1744,18 @@ def arrayToQPath(x, y, connect='all', finiteCheck=True):
path.addPolygon(polygon) path.addPolygon(polygon)
return path return path
elif eq(connect, 'pairs'): elif eq(connect, 'pairs'):
if qt6:
nans = np.broadcast_to(np.nan, (n // 2, 1))
if n % 2 == 0:
xs = np.column_stack([arr['x'].reshape((-1, 2)), nans]).ravel()
ys = np.column_stack([arr['y'].reshape((-1, 2)), nans]).ravel()
else:
xs = np.column_stack([arr['x'][:-1].reshape((-1, 2)), nans]).ravel()
ys = np.column_stack([arr['y'][:-1].reshape((-1, 2)), nans]).ravel()
polygon = arrayToQPolygonF(xs, ys)
path.addPolygon(polygon)
return path
else:
arr[:]['c'][::2] = 0 arr[:]['c'][::2] = 0
arr[:]['c'][1::2] = 1 # connect every 2nd point to every 1st one arr[:]['c'][1::2] = 1 # connect every 2nd point to every 1st one
elif eq(connect, 'finite'): elif eq(connect, 'finite'):

View File

@ -3,6 +3,7 @@ from collections import OrderedDict
from copy import deepcopy from copy import deepcopy
from contextlib import suppress from contextlib import suppress
from pyqtgraph.functions import arrayToQPath, eq from pyqtgraph.functions import arrayToQPath, eq
import numpy as np import numpy as np
import pytest import pytest
from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_almost_equal
@ -248,11 +249,11 @@ def test_siParse(s, suffix, expected):
pg.siParse(s, suffix=suffix) pg.siParse(s, suffix=suffix)
QT_LIB = pg.Qt.QT_LIB qt6 = pg.Qt.QtVersion.startswith("6")
MoveToElement = pg.QtGui.QPainterPath.ElementType.MoveToElement MoveToElement = pg.QtGui.QPainterPath.ElementType.MoveToElement
LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement
@pytest.mark.parametrize( @pytest.mark.parametrize(
"x, y, connect, expected", [ "xs, ys, connect, expected", [
( (
np.arange(6), np.arange(0, -6, step=-1), 'all', ( np.arange(6), np.arange(0, -6, step=-1), 'all', (
(MoveToElement, 0.0, 0.0), (MoveToElement, 0.0, 0.0),
@ -267,9 +268,9 @@ LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement
np.arange(6), np.arange(0, -6, step=-1), 'pairs', ( np.arange(6), np.arange(0, -6, step=-1), 'pairs', (
(MoveToElement, 0.0, 0.0), (MoveToElement, 0.0, 0.0),
(LineToElement, 1.0, -1.0), (LineToElement, 1.0, -1.0),
(MoveToElement, 2.0, -2.0), (LineToElement, np.nan, np.nan) if qt6 else (MoveToElement, 2.0, -2.0),
(LineToElement, 3.0, -3.0), (LineToElement, 3.0, -3.0),
(MoveToElement, 4.0, -4.0), (LineToElement, np.nan, np.nan) if qt6 else (MoveToElement, 4.0, -4.0),
(LineToElement, 5.0, -5.0), (LineToElement, 5.0, -5.0),
) )
), ),
@ -277,9 +278,9 @@ LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement
np.arange(5), np.arange(0, -5, step=-1), 'pairs', ( np.arange(5), np.arange(0, -5, step=-1), 'pairs', (
(MoveToElement, 0.0, 0.0), (MoveToElement, 0.0, 0.0),
(LineToElement, 1.0, -1.0), (LineToElement, 1.0, -1.0),
(MoveToElement, 2.0, -2.0), (LineToElement, np.nan, np.nan) if qt6 else (MoveToElement, 2.0, -2.0),
(LineToElement, 3.0, -3.0), (LineToElement, 3.0, -3.0),
(MoveToElement, 4.0, -4.0) (LineToElement, np.nan, np.nan) if qt6 else (MoveToElement, 4.0, -4.0)
) )
), ),
( (
@ -320,5 +321,5 @@ def test_arrayToQPath(xs, ys, connect, expected):
if (eq(element.x, np.nan) or eq(element.y, np.nan)): if (eq(element.x, np.nan) or eq(element.y, np.nan)):
continue continue
element = path.elementAt(i) element = path.elementAt(i)
assert expected[i] == (element.type, element.x, element.y) assert eq(expected[i], (element.type, element.x, element.y))