Skip to content

Commit 4d9ac32

Browse files
committed
Add non-linear scale warning to image drawing methods
This is related to #36
1 parent c968cd1 commit 4d9ac32

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

plotpy/items/image/base.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from guidata.utils.misc import assert_interfaces_valid
1515
from qtpy import QtCore as QC
1616
from qtpy import QtGui as QG
17-
from qwt import QwtPlotItem
17+
from qwt import QwtLinearScaleEngine, QwtPlotItem
1818

1919
from plotpy import io
2020
from plotpy._scaler import (
@@ -55,6 +55,7 @@
5555
from qtpy.QtGui import QColor, QPainter
5656

5757
from plotpy.interfaces import IItemType
58+
from plotpy.plot import BasePlot
5859
from plotpy.styles.base import ItemParameters
5960
from plotpy.widgets.colormap.widget import EditableColormap
6061

@@ -582,6 +583,29 @@ def draw_border(
582583
"""
583584
self.border_rect.draw(painter, xMap, yMap, canvasRect)
584585

586+
def warn_if_non_linear_scale(self, painter: QPainter, canvasRect: QRectF) -> bool:
587+
"""Warn if non-linear scale
588+
589+
Args:
590+
painter: Painter
591+
canvasRect: Canvas rectangle
592+
593+
Returns:
594+
True if non-linear scale
595+
"""
596+
plot: BasePlot = self.plot()
597+
if not isinstance(
598+
plot.axisScaleEngine(plot.X_BOTTOM), QwtLinearScaleEngine
599+
) or not isinstance(plot.axisScaleEngine(plot.Y_LEFT), QwtLinearScaleEngine):
600+
painter.setPen(QC.Qt.red)
601+
painter.drawText(
602+
canvasRect,
603+
QC.Qt.AlignCenter,
604+
_("Non-linear scales are not supported for image items"),
605+
)
606+
return True
607+
return False
608+
585609
def draw_image(
586610
self,
587611
painter: QPainter,
@@ -601,6 +625,8 @@ def draw_image(
601625
xMap: X axis scale map
602626
yMap: Y axis scale map
603627
"""
628+
if self.warn_if_non_linear_scale(painter, canvasRect):
629+
return
604630
dest = _scale_rect(
605631
self.data, src_rect, self._offscreen, dst_rect, self.lut, self.interpolate
606632
)

plotpy/items/image/image_items.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ def draw_image(
376376
"""
377377
if self.data is None:
378378
return
379+
if self.warn_if_non_linear_scale(painter, canvasRect):
380+
return
381+
379382
src2 = self._rescale_src_rect(src_rect)
380383
dst_rect = tuple([int(i) for i in dst_rect])
381384

@@ -651,6 +654,8 @@ def draw_image(
651654
xMap: X axis scale map
652655
yMap: Y axis scale map
653656
"""
657+
if self.warn_if_non_linear_scale(painter, canvasRect):
658+
return
654659
xytr = self.x, self.y, src_rect
655660
dst_rect = tuple([int(i) for i in dst_rect])
656661
dest = _scale_xy(

plotpy/items/image/misc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def draw_image(
169169
xMap: X axis scale map
170170
yMap: Y axis scale map
171171
"""
172+
if self.warn_if_non_linear_scale(painter, canvasRect):
173+
return
172174
self._offscreen[...] = np.uint32(0)
173175
dest = _scale_quads(
174176
self.X,
@@ -294,6 +296,8 @@ def draw_image(
294296
xMap: X axis scale map
295297
yMap: Y axis scale map
296298
"""
299+
if self.warn_if_non_linear_scale(painter, canvasRect):
300+
return
297301
computation = self.histparam.computation
298302
i1, j1, i2, j2 = src_rect
299303

plotpy/items/image/transform.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ def draw_image(
257257
xMap: X axis scale map
258258
yMap: Y axis scale map
259259
"""
260+
if self.warn_if_non_linear_scale(painter, canvasRect):
261+
return
260262
W = canvasRect.width()
261263
H = canvasRect.height()
262264
if W <= 1 or H <= 1:

plotpy/tests/items/test_mandelbrot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def __init__(self, xmin, xmax, ymin, ymax):
4747

4848
# ---- QwtPlotItem API ------------------------------------------------------
4949
def draw_image(self, painter, canvasRect, srcRect, dstRect, xMap, yMap):
50+
if self.warn_if_non_linear_scale(painter, canvasRect):
51+
return
5052
x1, y1, x2, y2 = canvasRect.toAlignedRect().getCoords()
5153
i1, j1, i2, j2 = srcRect
5254

plotpy/tests/widgets/test_dotarraydemo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def types(self) -> tuple[type[IItemType], ...]:
102102

103103
def draw_image(self, painter, canvasRect, srcRect, dstRect, xMap, yMap):
104104
"""Draw image"""
105+
if self.warn_if_non_linear_scale(painter, canvasRect):
106+
return
105107
painter.setRenderHint(QG.QPainter.Antialiasing, True)
106108
param = self.param
107109
xcoords = vmap(xMap, np.arange(0, param.dim_h + 1, param.step_x))

0 commit comments

Comments
 (0)