|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see plotpy/LICENSE for details) |
| 5 | + |
| 6 | +"""Test the cosmetic pen used to draw thin curves on screen""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import pytest |
| 12 | +from guidata.qthelpers import qt_app_context |
| 13 | +from qtpy import QtCore as QC |
| 14 | +from qtpy import QtGui as QG |
| 15 | +from qwt import QwtScaleMap |
| 16 | + |
| 17 | +from plotpy.builder import make |
| 18 | +from plotpy.config import CONF |
| 19 | +from plotpy.items.curve import base as curve_base |
| 20 | +from plotpy.items.curve.base import get_screen_pen |
| 21 | + |
| 22 | +WIDTH, HEIGHT = 100, 60 |
| 23 | + |
| 24 | + |
| 25 | +def _scale_maps() -> tuple[QwtScaleMap, QwtScaleMap]: |
| 26 | + """Return X and Y scale maps from [0, 1] to the image area""" |
| 27 | + xmap, ymap = QwtScaleMap(), QwtScaleMap() |
| 28 | + xmap.setPaintInterval(0, WIDTH) |
| 29 | + xmap.setScaleInterval(0.0, 1.0) |
| 30 | + ymap.setPaintInterval(HEIGHT, 0) |
| 31 | + ymap.setScaleInterval(0.0, 1.0) |
| 32 | + return xmap, ymap |
| 33 | + |
| 34 | + |
| 35 | +def _hidpi_image(ratio: float = 2.0) -> QG.QImage: |
| 36 | + """Return a white image with the given device pixel ratio""" |
| 37 | + image = QG.QImage(int(WIDTH * ratio), int(HEIGHT * ratio), QG.QImage.Format_ARGB32) |
| 38 | + image.setDevicePixelRatio(ratio) |
| 39 | + image.fill(QC.Qt.white) |
| 40 | + return image |
| 41 | + |
| 42 | + |
| 43 | +def _painted_rows(image: QG.QImage, column: int) -> int: |
| 44 | + """Return the number of non-white pixels in an image column""" |
| 45 | + white = QG.QColor(QC.Qt.white).rgb() |
| 46 | + return sum(image.pixel(column, row) != white for row in range(image.height())) |
| 47 | + |
| 48 | + |
| 49 | +def _painted_columns(image: QG.QImage, row: int) -> int: |
| 50 | + """Return the number of non-white pixels in an image row""" |
| 51 | + white = QG.QColor(QC.Qt.white).rgb() |
| 52 | + return sum(image.pixel(col, row) != white for col in range(image.width())) |
| 53 | + |
| 54 | + |
| 55 | +def _draw_horizontal_curve(image: QG.QImage, width: float) -> None: |
| 56 | + """Draw a horizontal black curve in the middle of the image""" |
| 57 | + x = np.linspace(0.0, 1.0, 50) |
| 58 | + y = np.full_like(x, 0.5) |
| 59 | + curve = make.curve(x, y, color="#000000", linewidth=width) |
| 60 | + xmap, ymap = _scale_maps() |
| 61 | + painter = QG.QPainter(image) |
| 62 | + curve.drawSeries(painter, xmap, ymap, QC.QRectF(0, 0, WIDTH, HEIGHT), 0, -1) |
| 63 | + painter.end() |
| 64 | + |
| 65 | + |
| 66 | +def _disable_option(monkeypatch: pytest.MonkeyPatch) -> None: |
| 67 | + """Disable the ``curve/cosmetic_hairline`` option""" |
| 68 | + original_get = CONF.get |
| 69 | + |
| 70 | + def get(section, option, *args, **kwargs): |
| 71 | + if (section, option) == ("plot", "curve/cosmetic_hairline"): |
| 72 | + return False |
| 73 | + return original_get(section, option, *args, **kwargs) |
| 74 | + |
| 75 | + monkeypatch.setattr(curve_base.CONF, "get", get) |
| 76 | + |
| 77 | + |
| 78 | +def test_screen_pen_raster() -> None: |
| 79 | + """Thin pens become cosmetic on screen, wide pens are left unchanged""" |
| 80 | + with qt_app_context(exec_loop=False): |
| 81 | + image = _hidpi_image() |
| 82 | + painter = QG.QPainter(image) |
| 83 | + for width in (0.5, 1.0): |
| 84 | + pen = QG.QPen(QG.QColor("red"), width) |
| 85 | + screen_pen = get_screen_pen(painter, pen) |
| 86 | + assert screen_pen.isCosmetic() |
| 87 | + assert screen_pen.widthF() == width |
| 88 | + assert not pen.isCosmetic() # The original pen is not modified |
| 89 | + pen = QG.QPen(QG.QColor("red"), 2.0) |
| 90 | + assert get_screen_pen(painter, pen) is pen |
| 91 | + painter.end() |
| 92 | + |
| 93 | + |
| 94 | +def test_screen_pen_print_and_export() -> None: |
| 95 | + """Pens are left unchanged for other paint engines or scaled painters""" |
| 96 | + with qt_app_context(exec_loop=False): |
| 97 | + pen = QG.QPen(QG.QColor("red"), 1.0) |
| 98 | + picture = QG.QPicture() |
| 99 | + painter = QG.QPainter(picture) |
| 100 | + assert get_screen_pen(painter, pen) is pen |
| 101 | + painter.end() |
| 102 | + image = _hidpi_image() |
| 103 | + painter = QG.QPainter(image) |
| 104 | + painter.scale(3.0, 3.0) # As done by QwtPlotRenderer for printing |
| 105 | + assert get_screen_pen(painter, pen) is pen |
| 106 | + painter.end() |
| 107 | + |
| 108 | + |
| 109 | +def test_screen_pen_option(monkeypatch: pytest.MonkeyPatch) -> None: |
| 110 | + """The ``curve/cosmetic_hairline`` option disables cosmetic pens""" |
| 111 | + _disable_option(monkeypatch) |
| 112 | + with qt_app_context(exec_loop=False): |
| 113 | + image = _hidpi_image() |
| 114 | + painter = QG.QPainter(image) |
| 115 | + pen = QG.QPen(QG.QColor("red"), 1.0) |
| 116 | + assert get_screen_pen(painter, pen) is pen |
| 117 | + painter.end() |
| 118 | + |
| 119 | + |
| 120 | +def test_curve_hidpi_rendering(monkeypatch: pytest.MonkeyPatch) -> None: |
| 121 | + """Curves of width 1 are drawn one device pixel wide on HiDPI screens""" |
| 122 | + with qt_app_context(exec_loop=False): |
| 123 | + image = _hidpi_image() |
| 124 | + _draw_horizontal_curve(image, 1.0) |
| 125 | + assert _painted_rows(image, image.width() // 2) == 1 |
| 126 | + image = _hidpi_image() |
| 127 | + _draw_horizontal_curve(image, 2.0) |
| 128 | + assert _painted_rows(image, image.width() // 2) == 4 |
| 129 | + _disable_option(monkeypatch) |
| 130 | + image = _hidpi_image() |
| 131 | + _draw_horizontal_curve(image, 1.0) |
| 132 | + assert _painted_rows(image, image.width() // 2) == 2 |
| 133 | + |
| 134 | + |
| 135 | +def test_curve_pen_parameters() -> None: |
| 136 | + """The stored curve pen and its parameters are not modified""" |
| 137 | + with qt_app_context(exec_loop=False): |
| 138 | + curve = make.curve([0.0, 1.0], [0.0, 1.0], color="#000000", linewidth=1.0) |
| 139 | + image = _hidpi_image() |
| 140 | + painter = QG.QPainter(image) |
| 141 | + xmap, ymap = _scale_maps() |
| 142 | + curve.drawSeries(painter, xmap, ymap, QC.QRectF(0, 0, WIDTH, HEIGHT), 0, -1) |
| 143 | + painter.end() |
| 144 | + assert not curve.pen().isCosmetic() |
| 145 | + curve.param.update_param(curve) |
| 146 | + assert curve.param.line.width == 1.0 |
| 147 | + |
| 148 | + |
| 149 | +def test_errorbar_hidpi_rendering() -> None: |
| 150 | + """Error bars of width 1 are drawn one device pixel wide on HiDPI screens""" |
| 151 | + with qt_app_context(exec_loop=False): |
| 152 | + x = np.array([0.5]) |
| 153 | + y = np.array([0.5]) |
| 154 | + dy = np.array([0.3]) |
| 155 | + item = make.error(x, y, None, dy, curvestyle="NoCurve", errorbarcap=0) |
| 156 | + image = _hidpi_image() |
| 157 | + painter = QG.QPainter(image) |
| 158 | + xmap, ymap = _scale_maps() |
| 159 | + item.draw(painter, xmap, ymap, QC.QRectF(0, 0, WIDTH, HEIGHT)) |
| 160 | + painter.end() |
| 161 | + assert _painted_columns(image, image.height() // 2) == 1 |
0 commit comments