aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-04-16 10:19:17 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-04-21 13:20:15 +0200
commit6389561fd75e80c69d7c83f6a87d988450a2e569 (patch)
tree5cbb981cfc1f9f3f3124fbd06a81fd6944498e35
parentdb52b6e6ec926f48679d091f7afa42f514b5e71c (diff)
Add a numpy overload for some functions taking lists of QPointF
Add functions taking x,y data in the form of numpy-vectors (as common for matplotlib, etc) for: QPainter::drawPoints() QXYSeries::append(), QXYSeries::replace() They are added as separate functions instead of overloads since there is a restriction in that numpy arrays cause an error: FIXME Subscripted generics cannot be used with class and instance checks when passed to other type check macros. Task-number: PYSIDE-1540 Task-number: PYSIDE-1503 Change-Id: Ie1521ce19127bb0641fbdb4f7320a8707682724e Reviewed-by: Christian Tismer <tismer@stackless.com> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/pyside6/PySide6/QtCharts/typesystem_charts.xml13
-rw-r--r--sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml8
-rw-r--r--sources/pyside6/PySide6/glue/qtcharts.cpp10
-rw-r--r--sources/pyside6/PySide6/glue/qtgui.cpp5
-rw-r--r--sources/pyside6/tests/QtGui/qpainter_test.py12
5 files changed, 47 insertions, 1 deletions
diff --git a/sources/pyside6/PySide6/QtCharts/typesystem_charts.xml b/sources/pyside6/PySide6/QtCharts/typesystem_charts.xml
index 643201f6d..6a325431b 100644
--- a/sources/pyside6/PySide6/QtCharts/typesystem_charts.xml
+++ b/sources/pyside6/PySide6/QtCharts/typesystem_charts.xml
@@ -266,5 +266,16 @@
<object-type name="QVXYModelMapper"/>
<object-type name="QXYLegendMarker"/>
<object-type name="QXYModelMapper"/>
- <object-type name="QXYSeries"/>
+ <!-- Add numpy versions as separate functions since passing ndarrays to other
+ typecheck macros causes:
+ FIXME Subscripted generics cannot be used with class and instance checks -->
+ <object-type name="QXYSeries">
+ <include file-name="pyside_numpy.h" location="global"/>
+ <add-function signature="appendNp(PyArrayObject *, PyArrayObject *)">
+ <inject-code file="../glue/qtcharts.cpp" snippet="qxyseries-appendnp-numpy-x-y"/>
+ </add-function>
+ <add-function signature="replaceNp(PyArrayObject *, PyArrayObject *)">
+ <inject-code file="../glue/qtcharts.cpp" snippet="qxyseries-replacenp-numpy-x-y"/>
+ </add-function>
+ </object-type>
</typesystem>
diff --git a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml
index 3db662a8f..4a230d5df 100644
--- a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml
+++ b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml
@@ -45,6 +45,7 @@
<load-typesystem name="templates/gui_common.xml" generate="no"/>
<load-typesystem name="templates/opengl_common.xml" generate="no"/>
+ <custom-type name="PyArrayObject"/>
<rejection class="^Q.*$" argument-type="^QPlatform.*$"/>
<function signature="qAlpha(uint)"/>
@@ -1745,6 +1746,7 @@
<extra-includes>
<include file-name="QPainterPath" location="global"/>
<include file-name="QPixmap" location="global"/>
+ <include file-name="pyside_numpy.h" location="global"/>
</extra-includes>
<enum-type name="CompositionMode"/>
<enum-type name="PixmapFragmentHint" flags="PixmapFragmentHints" since="4.7"/>
@@ -1793,6 +1795,12 @@
</add-function>
<modify-function signature="drawPoints(const QPolygon&amp;)" overload-number="0"/>
<modify-function signature="drawPoints(const QPolygonF&amp;)" overload-number="1"/>
+ <!-- Add numpy versions as separate functions since passing ndarrays to other
+ typecheck macros causes:
+ FIXME Subscripted generics cannot be used with class and instance checks -->
+ <add-function signature="drawPointsNp(PyArrayObject *, PyArrayObject *)">
+ <inject-code file="../glue/qtgui.cpp" snippet="qpainter-drawpointsnp-numpy-x-y"/>
+ </add-function>
<modify-function signature="drawPolygon(const QPoint*,int,Qt::FillRule)" remove="all"/>
<add-function signature="drawPolygon(QList&lt;QPoint>,Qt::FillRule)">
diff --git a/sources/pyside6/PySide6/glue/qtcharts.cpp b/sources/pyside6/PySide6/glue/qtcharts.cpp
index 1828fecc0..7171f19df 100644
--- a/sources/pyside6/PySide6/glue/qtcharts.cpp
+++ b/sources/pyside6/PySide6/glue/qtcharts.cpp
@@ -40,3 +40,13 @@
// @snippet qchart-releaseownership
Shiboken::Object::releaseOwnership(%PYARG_1);
// @snippet qchart-releaseownership
+
+// @snippet qxyseries-appendnp-numpy-x-y
+const auto points = PySide::Numpy::xyDataToQPointFList(%PYARG_1, %PYARG_2);
+%CPPSELF.append(points);
+// @snippet qxyseries-appendnp-numpy-x-y
+
+// @snippet qxyseries-replacenp-numpy-x-y
+const auto points = PySide::Numpy::xyDataToQPointFList(%PYARG_1, %PYARG_2);
+%CPPSELF.replace(points);
+// @snippet qxyseries-replacenp-numpy-x-y
diff --git a/sources/pyside6/PySide6/glue/qtgui.cpp b/sources/pyside6/PySide6/glue/qtgui.cpp
index c0323ad0f..28d7c468e 100644
--- a/sources/pyside6/PySide6/glue/qtgui.cpp
+++ b/sources/pyside6/PySide6/glue/qtgui.cpp
@@ -458,6 +458,11 @@ PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[%RETURN_TYPE](retval_));
PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[%ARG1_TYPE](%1));
// @snippet qclipboard-text
+// @snippet qpainter-drawpointsnp-numpy-x-y
+const auto points = PySide::Numpy::xyDataToQPointFList(%PYARG_1, %PYARG_2);
+%CPPSELF.drawPoints(points);
+// @snippet qpainter-drawpointsnp-numpy-x-y
+
// @snippet qpainter-drawpolygon
%CPPSELF.%FUNCTION_NAME(%1.data(), %1.size(), %2);
// @snippet qpainter-drawpolygon
diff --git a/sources/pyside6/tests/QtGui/qpainter_test.py b/sources/pyside6/tests/QtGui/qpainter_test.py
index b05189d01..752475494 100644
--- a/sources/pyside6/tests/QtGui/qpainter_test.py
+++ b/sources/pyside6/tests/QtGui/qpainter_test.py
@@ -40,6 +40,13 @@ from PySide6.QtGui import QPainter, QLinearGradient, QImage
from PySide6.QtCore import QLine, QLineF, QPoint, QPointF, QRect, QRectF, Qt
+try:
+ import numpy as np
+ HAVE_NUMPY = True
+except ModuleNotFoundError:
+ HAVE_NUMPY = False
+
+
class QPainterDrawText(UsesQGuiApplication):
def setUp(self):
super(QPainterDrawText, self).setUp()
@@ -109,6 +116,11 @@ class QPainterDrawText(UsesQGuiApplication):
QPoint(20.0, 10.0),
QPoint(80.0, 30.0),
QPoint(90.0, 70.0)])
+ if HAVE_NUMPY:
+ x = np.array([10.0, 20.0, 80.0, 90.0])
+ y = np.array([80.0, 10.0, 30.0, 70.0])
+ self.painter.drawPointsNp(x, y)
+
class SetBrushWithOtherArgs(UsesQGuiApplication):