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
29 changes: 21 additions & 8 deletions src/pyqt_reactive/widgets/virtual_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,27 @@ def rows_data_changed(self, first: int, last: int) -> None:
for row in range(first, min(last + 1, len(sizes))):
sizes[row] = None

def kept_size(self, row: int, *, measured: bool = False) -> QSize | None:
"""The row's kept size while a layout runs (only a measured one when `measured`)."""
def kept_row(self, row: int, *, measured: bool = False) -> RowSize | None:
"""The row's kept size and the key it was measured under, while a layout runs (only a
measured one when `measured`)."""
if self._layout_for is None or row >= len(self._row_sizes):
return None
kept = self._row_sizes[row]
if kept is None or kept.size is None or (measured and kept.estimated):
return None
return kept.size
return kept

def kept_size(self, row: int, *, measured: bool = False) -> QSize | None:
kept = self.kept_row(row, measured=measured)
return None if kept is None else kept.size

def hinted(self, key: Hashable, size: QSize) -> QSize:
"""What the view is told a row takes, from its size at the layout width: that size.
A view that scrolls sideways answers the row's extent as the width (`row_extent`).
A layout asks this of every row, so it is answered from the kept key and size with
nothing built: a subclass that overrode `sizeHint` to widen its rows built each
row's key again on every layout, kept size or not."""
return size

def keep_row_size(
self, row: int, key: Hashable, size: QSize, font: QFont, *, estimated: bool = False
Expand Down Expand Up @@ -575,25 +588,25 @@ def painted_document(self, index: QModelIndex, option: QStyleOptionViewItem) ->
return self.final_document(index, option.font, option.rect.width())

def sizeHint(self, option, index) -> QSize: # type: ignore[override]
if (kept := self.kept_size(index.row())) is not None:
return kept
if (kept := self.kept_row(index.row())) is not None:
return self.hinted(kept.key, kept.size)
opt = QStyleOptionViewItem(option)
self.initStyleOption(opt, index)
width = self.available_width(opt)
key = self.preparation_key(index, opt.font, width)
size = self._size_hints.get((key, width))
if size is not None:
self.keep_row_size(index.row(), key, size, opt.font)
return size
return self.hinted(key, size)
if self.view.estimating():
# Away from the reader: an estimate now, measured between frames.
self._unmeasured[QPersistentModelIndex(index)] = None
if not self._measure_timer.isActive():
self._measure_timer.start()
size = self.estimated_size(index, opt.font, width)
self.keep_row_size(index.row(), key, size, opt.font, estimated=True)
return size
return self.measured_size(index, opt.font, width)
return self.hinted(key, size)
return self.hinted(key, self.measured_size(index, opt.font, width))

def measured_size(self, index: QModelIndex, font: QFont, width: int) -> QSize:
"""The row's height laid out at `width`, kept; a pending row's estimate, not kept, so
Expand Down
42 changes: 41 additions & 1 deletion tests/test_virtual_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass
from functools import partial

from PySide6.QtCore import QStringListModel, Qt
from PySide6.QtCore import QSize, QStringListModel, Qt
from PySide6.QtGui import QFont, QGuiApplication, QTextDocument

from pyqt_reactive.widgets.virtual_rows import PendingRow, RowDocumentDelegate, RowFormatting, VirtualRowView
Expand Down Expand Up @@ -180,6 +180,46 @@ def test_a_rows_extent_is_recorded_when_it_is_laid_out(qtbot) -> None:
delegate.cleanup()


class WideRows(BoldRows):
"""Rows of a view that scrolls sideways: a row's hint is as wide as its content."""

def preparation(self, index, font, width):
return partial(UnwrappedRow, self.row_text(index))

def hinted(self, key, size):
return QSize(self.row_extent(key, size.width()), size.height())


def test_a_layout_answers_each_rows_hint_from_its_kept_key(qtbot) -> None:
"""A layout asks every row's size. A row whose size is kept answers from the kept key and
size, widened by the subclass's own `hinted`, without building the row's key again."""
model = QStringListModel(["x" * 200, "short"])
view = VirtualRowView(WideRows())
qtbot.addWidget(view)
view.setModel(model)
view.resize(300, 400)
delegate = view.rows
try:
view.show()
qtbot.waitUntil(lambda: all(
delegate.is_measured(model.index(row, 0), view.font(), view.viewport_width())
for row in range(2)), timeout=3000)
view.doItemsLayout()
width = view.viewport_width()
assert view.sizeHintForIndex(model.index(0, 0)).width() > width
assert view.sizeHintForIndex(model.index(1, 0)).width() == width
built = []
preparation_key = delegate.preparation_key
def counted(*arguments):
built.append(arguments)
return preparation_key(*arguments)
delegate.preparation_key = counted
view.doItemsLayout()
assert built == []
finally:
delegate.cleanup()


def test_a_page_of_rows_arrives_as_one_batch(qtbot) -> None:
"""Rows queued together are worked out by one drain of the pool's queue and reach the
GUI thread together, so the view lays them out once rather than once per row."""
Expand Down
Loading