from __future__ import annotations from typing import List, Sequence, Tuple from qgis.PyQt.QtCore import QPointF, QRectF, QSize, Qt, pyqtSignal from qgis.PyQt.QtGui import ( QBrush, QColor, QFont, QLinearGradient, QPainter, QPainterPath, QPen, ) from qgis.PyQt.QtWidgets import QSizePolicy, QWidget from .. import plot from ..i18n import tr from . import theme PAD_LEFT = 52 PAD_RIGHT = 12 PAD_TOP = 12 PAD_BOTTOM = 28 def _metres(value: float) -> str: if abs(value) >= 1000.0: return "%.1f km" % (value / 1000.0) if abs(value) >= 10.0: return "%.0f m" % value return "%.1f m" % value def _distance_axis(span: float): if span >= 2000.0: return lambda value: "%g km" % round(value / 1000.0, 2) return lambda value: "%g m" % round(value, 1) class ProfileChart(QWidget): hovered = pyqtSignal(int) def __init__(self, parent=None) -> None: super().__init__(parent) self.setMinimumHeight(190) self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) self.setMouseTracking(True) self.setCursor(Qt.CursorShape.CrossCursor) self._samples: List[plot.Sample] = [] self._line: List[plot.Sample] = [] self._marks: List[Tuple[float, float]] = [] self._hover = -1 self._empty = tr("Pick two points on the map.") def sizeHint(self): # noqa: N802 return QSize(360, 220) def minimumSizeHint(self): # noqa: N802 return QSize(220, 190) def set_samples(self, samples: Sequence[plot.Sample]) -> None: self._samples = [(float(x), None if y is None else float(y)) for x, y in samples] self._hover = -1 self.update() def set_line(self, line: Sequence[plot.Sample]) -> None: self._line = [(float(x), None if y is None else float(y)) for x, y in line] self.update() def set_marks(self, marks: Sequence[Tuple[float, float]]) -> None: self._marks = [(float(x), float(y)) for x, y in marks] self.update() def set_placeholder(self, text: str) -> None: self._empty = text self.update() def clear(self) -> None: self._samples = [] self._line = [] self._marks = [] self._hover = -1 self.update() def has_data(self) -> bool: return bool(self._samples) def mouseMoveEvent(self, event): # noqa: N802 if not self._samples: return scale_x, _ = self._scales() index = plot.nearest(self._samples, scale_x.to_value(event.position().x() if hasattr(event, "position") else event.x())) if index != self._hover: self._hover = index self.hovered.emit(index) self.update() def leaveEvent(self, event): # noqa: N802 if self._hover != -1: self._hover = -1 self.hovered.emit(-1) self.update() def highlight(self, index: int) -> None: if index != self._hover: self._hover = index self.update() def _area(self) -> QRectF: return QRectF(PAD_LEFT, PAD_TOP, max(1.0, self.width() - PAD_LEFT - PAD_RIGHT), max(1.0, self.height() - PAD_TOP - PAD_BOTTOM)) def _scales(self) -> Tuple[plot.Scale, plot.Scale]: area = self._area() x0, x1, y0, y1 = plot.bounds(self._samples) for x, y in self._line: if y is None: continue y0, y1 = min(y0, y), max(y1, y) low, high = plot.padded(y0, y1) return (plot.Scale(x0, x1, area.left(), area.right()), plot.Scale(low, high, area.bottom(), area.top())) def paintEvent(self, event): # noqa: N802 painter = QPainter(self) painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) colours = theme.colours() area = self._area() painter.fillRect(self.rect(), QColor(colours["chart_bg"])) if not self._samples: painter.setPen(QPen(QColor(colours["chart_muted"]))) painter.drawText(self.rect(), Qt.AlignmentFlag.AlignCenter, self._empty) painter.end() return scale_x, scale_y = self._scales() self._grid(painter, colours, area, scale_x, scale_y) self._terrain(painter, colours, area, scale_x, scale_y) if self._line: self._sight(painter, colours, scale_x, scale_y) self._points_of_interest(painter, colours, scale_x, scale_y) self._cursor(painter, colours, area, scale_x, scale_y) painter.end() def _grid(self, painter, colours, area, scale_x, scale_y) -> None: font = QFont(self.font()) font.setPointSizeF(max(7.0, font.pointSizeF() - 1.5)) painter.setFont(font) grid = QPen(QColor(colours["chart_grid"])) grid.setWidthF(1.0) label = QPen(QColor(colours["chart_muted"])) for value in plot.ticks(scale_y.low, scale_y.high, 5): y = scale_y.to_pixel(value) painter.setPen(grid) painter.drawLine(QPointF(area.left(), y), QPointF(area.right(), y)) painter.setPen(label) painter.drawText(QRectF(0, y - 8, PAD_LEFT - 6, 16), int(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter), "%g" % round(value, 1)) as_distance = _distance_axis(scale_x.high - scale_x.low) for value in plot.ticks(scale_x.low, scale_x.high, 4): x = scale_x.to_pixel(value) painter.setPen(grid) painter.drawLine(QPointF(x, area.top()), QPointF(x, area.bottom())) painter.setPen(label) painter.drawText(QRectF(x - 40, area.bottom() + 4, 80, 16), int(Qt.AlignmentFlag.AlignCenter), as_distance(value)) def _terrain(self, painter, colours, area, scale_x, scale_y) -> None: fill = QLinearGradient(0, area.top(), 0, area.bottom()) top = QColor(colours["chart_terrain"]) top.setAlpha(150) bottom = QColor(colours["chart_terrain"]) bottom.setAlpha(25) fill.setColorAt(0.0, top) fill.setColorAt(1.0, bottom) outline = QPen(QColor(colours["chart_terrain"])) outline.setWidthF(2.0) outline.setJoinStyle(Qt.PenJoinStyle.RoundJoin) for piece in plot.segments(self._samples): path = QPainterPath() path.moveTo(scale_x.to_pixel(piece[0][0]), scale_y.to_pixel(piece[0][1])) for x, y in piece[1:]: path.lineTo(scale_x.to_pixel(x), scale_y.to_pixel(y)) body = QPainterPath(path) body.lineTo(scale_x.to_pixel(piece[-1][0]), area.bottom()) body.lineTo(scale_x.to_pixel(piece[0][0]), area.bottom()) body.closeSubpath() painter.fillPath(body, QBrush(fill)) painter.strokePath(path, outline) def _sight(self, painter, colours, scale_x, scale_y) -> None: pen = QPen(QColor(colours["warn"])) pen.setWidthF(1.6) pen.setStyle(Qt.PenStyle.DashLine) painter.setPen(pen) for piece in plot.segments(self._line): path = QPainterPath() path.moveTo(scale_x.to_pixel(piece[0][0]), scale_y.to_pixel(piece[0][1])) for x, y in piece[1:]: path.lineTo(scale_x.to_pixel(x), scale_y.to_pixel(y)) painter.drawPath(path) def _points_of_interest(self, painter, colours, scale_x, scale_y) -> None: painter.setPen(QPen(QColor(colours["error"]), 2.0)) painter.setBrush(QBrush(QColor(colours["chart_bg"]))) for x, y in self._marks: painter.drawEllipse(QPointF(scale_x.to_pixel(x), scale_y.to_pixel(y)), 4.5, 4.5) def _cursor(self, painter, colours, area, scale_x, scale_y) -> None: if not (0 <= self._hover < len(self._samples)): return distance, height = self._samples[self._hover] x = scale_x.to_pixel(distance) pen = QPen(QColor(colours["chart_text"])) pen.setWidthF(1.0) painter.setPen(pen) painter.drawLine(QPointF(x, area.top()), QPointF(x, area.bottom())) if height is None: text = tr("no elevation data") else: text = "%s ยท %g m" % (_metres(distance), round(height, 1)) painter.setBrush(QBrush(QColor(colours["chart_text"]))) painter.drawEllipse(QPointF(x, scale_y.to_pixel(height)), 3.5, 3.5) self._bubble(painter, colours, area, x, text) def _bubble(self, painter, colours, area, x: float, text: str) -> None: metrics = painter.fontMetrics() width = metrics.horizontalAdvance(text) + 12 height = metrics.height() + 6 left = min(max(area.left(), x - width / 2.0), area.right() - width) box = QRectF(left, area.top(), width, height) back = QColor(colours["surface"]) back.setAlpha(235) painter.setBrush(QBrush(back)) painter.setPen(QPen(QColor(colours["chart_grid"]))) painter.drawRoundedRect(box, 4, 4) painter.setPen(QPen(QColor(colours["chart_text"]))) painter.drawText(box, int(Qt.AlignmentFlag.AlignCenter), text)