Scale view to measured results row count

In other words instead of some static view size previously determined by
the accompanying (slow) chart's height, (recursively) calculate the
number of displayed rows and compute the minimal height needed. This
still caps the view at the height of the chart such that the view will
switch to scroll bar mode when too many results are shown and can't all
be fit in the vertical space.

Deats:
- add a ``CompleterView.iter_df_rows()`` which recursively iterates all
  rows in depth-first order making it simple to compute the absolute
  number of result rows in view and thus the minimal number of pixels to
  show all results.
- always pass the height in the `.on_resize()` handler to ensure
  triggering the height logic when new results are generated in the
  search loop.
history_view
Tyler Goodlet 2022-09-12 09:58:11 -04:00
parent 73a02d54b7
commit 3fd7107e08
1 changed files with 46 additions and 20 deletions

View File

@ -35,9 +35,13 @@ from collections import defaultdict
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from functools import partial from functools import partial
from typing import ( from typing import (
Optional, Callable, Optional,
Awaitable, Sequence, Callable,
Any, AsyncIterator Awaitable,
Sequence,
Any,
AsyncIterator,
Iterator,
) )
import time import time
# from pprint import pformat # from pprint import pformat
@ -119,7 +123,7 @@ class CompleterView(QTreeView):
# TODO: size this based on DPI font # TODO: size this based on DPI font
self.setIndentation(_font.px_size) self.setIndentation(_font.px_size)
# self.setUniformRowHeights(True) self.setUniformRowHeights(True)
# self.setColumnWidth(0, 3) # self.setColumnWidth(0, 3)
# self.setVerticalBarPolicy(Qt.ScrollBarAlwaysOff) # self.setVerticalBarPolicy(Qt.ScrollBarAlwaysOff)
# self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustIgnored) # self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustIgnored)
@ -166,11 +170,15 @@ class CompleterView(QTreeView):
) -> None: ) -> None:
model = self.model() model = self.model()
cols = model.columnCount() cols = model.columnCount()
# rows = model.rowCount()
cidx = self.selectionModel().currentIndex() cidx = self.selectionModel().currentIndex()
row_h = self.rowHeight(cidx) rows = model.rowCount()
# print(f'row_h: {row_h}') self.expandAll()
row_h = rows_h = self.rowHeight(cidx) * rows
for idx, item in self.iter_df_rows():
row_h = self.rowHeight(idx)
rows_h += row_h
# print(f'row_h: {row_h}\nrows_h: {rows_h}')
col_w_tot = 0 col_w_tot = 0
for i in range(cols): for i in range(cols):
@ -195,16 +203,16 @@ class CompleterView(QTreeView):
if h: if h:
h: int = round(h) h: int = round(h)
# mn = row_h * 2 abs_mx = round(0.91 * h)
# self.setMinimumHeight(mn) self.setMaximumHeight(abs_mx)
# abs_mx = h - row_h
# print(f'set min {abs_mx}')
# self.setFixedHeight(round(0.9 * h))
self.setMinimumHeight(round(0.91 * h))
# self.setMaximumHeight(0.9 * abs_mx) if rows_h <= abs_mx:
# 6 result row slots and 3 rows for sections and headers # self.setMinimumHeight(rows_h)
# mn = (6 + 3) * row_h self.setMinimumHeight(rows_h)
# self.setFixedHeight(rows_h)
else:
self.setMinimumHeight(abs_mx)
# dyncamically size to width of longest result seen # dyncamically size to width of longest result seen
curr_w = self.width() curr_w = self.width()
@ -335,6 +343,23 @@ class CompleterView(QTreeView):
item = model.itemFromIndex(idx) item = model.itemFromIndex(idx)
yield idx, item yield idx, item
def iter_df_rows(
self,
iparent: QModelIndex = QModelIndex(),
) -> Iterator[tuple[QModelIndex, QStandardItem]]:
model = self.model()
isections = model.rowCount(iparent)
for i in range(isections):
idx = model.index(i, 0, iparent)
item = model.itemFromIndex(idx)
yield idx, item
if model.hasChildren(idx):
# recursively yield child items depth-first
yield from self.iter_df_rows(idx)
def find_section( def find_section(
self, self,
section: str, section: str,
@ -358,7 +383,8 @@ class CompleterView(QTreeView):
status_field: str = None, status_field: str = None,
) -> None: ) -> None:
'''Clear all result-rows from under the depth = 1 section. '''
Clear all result-rows from under the depth = 1 section.
''' '''
idx = self.find_section(section) idx = self.find_section(section)
@ -459,7 +485,7 @@ class CompleterView(QTreeView):
# a resize of some higher level parent-container widget. # a resize of some higher level parent-container widget.
search = self.parent() search = self.parent()
w, h = search.space_dims() w, h = search.space_dims()
self.resize_to_results(w=w) self.resize_to_results(w=w, h=h)
self.show() self.show()
@ -942,8 +968,8 @@ async def handle_keyboard_input(
if key in (Qt.Key_Enter, Qt.Key_Return): if key in (Qt.Key_Enter, Qt.Key_Return):
_search_enabled = False _search_enabled = False
await search.chart_current_item(clear_to_cache=True) await search.chart_current_item(clear_to_cache=True)
view.show_matches()
search.show_only_cache_entries() search.show_only_cache_entries()
view.show_matches()
search.focus() search.focus()
elif not ctl and not bar.text(): elif not ctl and not bar.text():