Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/pyqt_reactive/widgets/fitted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
asks for its text's width but needs only its margins; each member decides how text that
does not fit is shown, and the whole text stays the tooltip."""

import math

from PySide6.QtCore import QEvent, QPointF, QRect, QSize, Qt
from PySide6.QtGui import QColor, QPainter, QPalette, QTextLayout, QTextOption
from PySide6.QtWidgets import QLabel
Expand Down Expand Up @@ -79,16 +81,12 @@
self._min_size_hint = hint
return QSize(hint)

def _prepare_text_layouts(self, rect: QRect) -> tuple[QRect, tuple[QTextLayout, ...], QPointF, bool]:
flags, text = self.drawn(rect)
if not text or rect.width() <= 0 or rect.height() <= 0:
cached = (QRect(rect), (), QPointF(), False)
self._text_layouts = cached
return cached
def _layout_text(self, text: str, width: int) -> tuple[tuple[QTextLayout, ...], float]:
"""Lay out `text` into `width` pixels with the single QTextLayout engine used for both
painting and wrapped height measurement."""
font = self.font()
option = QTextOption(self.alignment())
option.setWrapMode(self.WRAP_MODE)
width = rect.width()
layouts: list[QTextLayout] = []
y = 0.0
for paragraph in text.split("\n"):
Expand All @@ -105,14 +103,23 @@
y += line.height()
layout.endLayout()
layouts.append(layout)
return tuple(layouts), y

def _prepare_text_layouts(self, rect: QRect) -> tuple[QRect, tuple[QTextLayout, ...], QPointF, bool]:

Check failure on line 108 in src/pyqt_reactive/widgets/fitted_text.py

View workflow job for this annotation

GitHub Actions / code-quality

Ruff E501

Line too long (105 > 100)
flags, text = self.drawn(rect)
if not text or rect.width() <= 0 or rect.height() <= 0:
cached = (QRect(rect), (), QPointF(), False)
self._text_layouts = cached
return cached
layouts, y = self._layout_text(text, rect.width())
if flags & self.ALIGN_VCENTER:
top = rect.top() + (rect.height() - y) / 2.0
elif flags & self.ALIGN_BOTTOM:
top = rect.bottom() + 1.0 - y
else:
top = float(rect.top())
needs_clip = y > rect.height() or rect != self.rect()
cached = (QRect(rect), tuple(layouts), QPointF(float(rect.left()), top), needs_clip)
cached = (QRect(rect), layouts, QPointF(float(rect.left()), top), needs_clip)
self._text_layouts = cached
return cached

Expand Down Expand Up @@ -180,7 +187,8 @@
def text_rect(self, width: int) -> QRect:
margins = self.contentsMargins()
inner = max(width - margins.left() - margins.right(), 1)
return self.fontMetrics().boundingRect(QRect(0, 0, inner, 1 << 20), self.WRAP_FLAGS, self.text())
_, y = self._layout_text(self.text(), inner)
return QRect(0, 0, inner, math.ceil(y))

def heightForWidth(self, width: int) -> int: # noqa: N802 - Qt virtual method name
cached = self._height_for_width
Expand Down
21 changes: 21 additions & 0 deletions tests/test_fitted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,24 @@ def counting_text_rect(width: int):
assert text_rect_calls == 3
assert shorter < bigger
assert widget.sizeHint().height() <= bigger


def test_wrapped_text_height_matches_painted_qtextlayout_without_clipping(qtbot):
"""heightForWidth and paintEvent share QTextLayout so narrow punctuation-heavy text
never wraps into more lines during paint than heightForWidth reserved."""
text = (
"Objective: Ship Android/Qt Toad mobile UX fixes (IME adjustResize/safe-area, "
"tab close, sidebar bands), profile & fix Pixel 8a scroll FPS with FrameProbe, "
"and keep Qt/Textual shared-frontend hoists & NRA Toad bench moving"
)
widget = WrappedText(text)
qtbot.addWidget(widget)
for width in (140, 180, 207, 260, 360):
height = widget.heightForWidth(width)
widget.resize(width, height)
widget.grab()
cached = widget._text_layouts
assert cached is not None
_rect, _layouts, origin, needs_clip = cached
assert not needs_clip, f"WrappedText clipped at width={width} height={height}"
assert origin.y() >= 0.0
Loading