Support connect=finite for qt6

This commit is contained in:
Ogi Moore 2021-05-26 18:50:14 -07:00
parent 62165bbe45
commit 78e678c1b9
2 changed files with 34 additions and 14 deletions

View File

@ -1716,7 +1716,12 @@ def arrayToQPath(x, y, connect='all', finiteCheck=True):
# Qt version 5.12.3; these must now be manually cleaned out.
isfinite = None
qtver = [int(x) for x in QtVersion.split('.')]
if qtver >= [5, 12, 3] and finiteCheck:
qt6 = qtver[0] == 6
if eq(connect, 'finite') and qt6:
# we must preserve NaN values here
pass
elif qtver >= [5, 12, 3] and finiteCheck:
isfinite = np.isfinite(x) & np.isfinite(y)
if not np.all(isfinite):
# credit: Divakar https://stackoverflow.com/a/41191127/643629
@ -1731,8 +1736,7 @@ def arrayToQPath(x, y, connect='all', finiteCheck=True):
arr[:] = arr[:][idx]
# decide which points are connected by lines
if eq(connect, 'all') or (eq(connect, 'finite') and QT_LIB in ["PySide6", "PyQt6"]):
# turns out with Qt6 we can give the QPolygonF NaN values and it works out!
if eq(connect, 'all'):
polygon = arrayToQPolygonF(
arr['x'],
arr['y']
@ -1747,9 +1751,20 @@ def arrayToQPath(x, y, connect='all', finiteCheck=True):
# A point will anyway not connect to an invalid point regardless of the
# 'c' value of the invalid point. Therefore, we should set 'c' to 0 for
# the next point of an invalid point.
if isfinite is None:
if qt6:
# look for the first finite element.
isfinite = np.isfinite(x) & np.isfinite(y)
arr[1:]['c'] = isfinite[:-1]
argNaNs = np.argwhere(isfinite)
first = 0 if len(argNaNs) == 0 else argNaNs.item(0)
# if found, use the portion of the array from that point on
polygon = arrayToQPolygonF(x[first:], y[first:])
path.addPolygon(polygon)
return path
else:
if isfinite is None:
isfinite = np.isfinite(x) & np.isfinite(y)
arr[1:]['c'] = isfinite[:-1]
elif isinstance(connect, np.ndarray):
arr[1:]['c'] = connect[:-1]
else:

View File

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from collections import OrderedDict
from copy import deepcopy
from pyqtgraph.functions import arrayToQPath
from contextlib import suppress
from pyqtgraph.functions import arrayToQPath, eq
import numpy as np
import pytest
from numpy.testing import assert_array_almost_equal
@ -286,8 +286,8 @@ LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement
np.arange(5), np.array([0, -1, np.NaN, -3, -4]), 'finite', (
(MoveToElement, 0.0, 0.0),
(LineToElement, 1.0, -1.0),
(LineToElement, 1.0, -1.0),
(MoveToElement if QT_LIB in ["PyQt5", "PySide2"] else LineToElement, 3.0, -3.0),
(LineToElement, 2.0, np.nan) if qt6 else (LineToElement, 1.0, -1.0),
(MoveToElement, 3.0, -3.0),
(LineToElement, 4.0, -4.0)
)
),
@ -295,8 +295,8 @@ LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement
np.array([0, 1, np.NaN, 3, 4]), np.arange(0, -5, step=-1), 'finite', (
(MoveToElement, 0.0, 0.0),
(LineToElement, 1.0, -1.0),
(LineToElement, 1.0, -1.0),
(MoveToElement if QT_LIB in ["PyQt5", "PySide2"] else LineToElement, 3.0, -3.0),
(LineToElement, np.nan, -2.0) if qt6 else (LineToElement, 1.0, -1.0),
(MoveToElement, 3.0, -3.0),
(LineToElement, 4.0, -4.0)
)
),
@ -311,9 +311,14 @@ LineToElement = pg.QtGui.QPainterPath.ElementType.LineToElement
)
]
)
def test_arrayToQPath(x, y, connect, expected):
path = arrayToQPath(x, y, connect=connect)
for i, (x, y) in enumerate(zip(x.tolist(), y.tolist())):
def test_arrayToQPath(xs, ys, connect, expected):
path = arrayToQPath(xs, ys, connect=connect)
for i in range(path.elementCount()):
with suppress(NameError):
# nan elements add two line-segments, for simplicity of test config
# we can ignore the second segment
if (eq(element.x, np.nan) or eq(element.y, np.nan)):
continue
element = path.elementAt(i)
assert expected[i] == (element.type, element.x, element.y)