OMG Qt view item sizing is sooo dumb..

fsp_feeds
Tyler Goodlet 2021-07-25 12:14:03 -04:00
parent 29ea91553d
commit 940aafe1be
1 changed files with 58 additions and 3 deletions

View File

@ -22,12 +22,15 @@ from typing import Optional
# import trio # import trio
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QSize, QModelIndex
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QWidget, QWidget,
QComboBox, QComboBox,
QLineEdit, QLineEdit,
QProgressBar, QProgressBar,
QSizePolicy, QSizePolicy,
QStyledItemDelegate,
QStyleOptionViewItem,
) )
from ._style import hcolor, _font, DpiAwareFont from ._style import hcolor, _font, DpiAwareFont
@ -72,7 +75,7 @@ class FontAndChartAwareLineEdit(QLineEdit):
# witty bit of margin # witty bit of margin
self.setTextMargins(2, 2, 2, 2) self.setTextMargins(2, 2, 2, 2)
def sizeHint(self) -> QtCore.QSize: def sizeHint(self) -> QSize:
""" """
Scale edit box to size of dpi aware font. Scale edit box to size of dpi aware font.
@ -104,6 +107,39 @@ class FontAndChartAwareLineEdit(QLineEdit):
self.setFocus() self.setFocus()
class FontScaledDelegate(QStyledItemDelegate):
"""
Super simple view delegate to render text in the same
font size as the search widget.
"""
def __init__(
self,
parent=None,
font: DpiAwareFont = _font,
) -> None:
super().__init__(parent)
self.dpi_font = font
def sizeHint(
self,
option: QStyleOptionViewItem,
index: QModelIndex,
) -> QSize:
# value = index.data()
# br = self.dpi_font.boundingRect(value)
# w, h = br.width(), br.height()
w, h = self.parent()._max_item_size
return QSize(w, h)
class FieldsForm(QtGui.QWidget): class FieldsForm(QtGui.QWidget):
def __init__( def __init__(
@ -190,13 +226,32 @@ class FieldsForm(QtGui.QWidget):
select = QComboBox(self) select = QComboBox(self)
for i, value in enumerate(values): for i, value in enumerate(values):
select.insertItem(0, str(value)) select.insertItem(i, str(value))
select.setStyleSheet( select.setStyleSheet(
f"QComboBox {{ color : {hcolor('gunmetal')}; }}" f"QComboBox {{ color : {hcolor('gunmetal')}; }}"
) )
select.show() select.setSizeAdjustPolicy(QComboBox.AdjustToContents)
select.setIconSize(QSize(0, 0))
self.setSizePolicy(
QSizePolicy.Fixed,
QSizePolicy.Fixed,
)
view = select.view()
view.setItemDelegate(FontScaledDelegate(view))
# compute maximum item size so that the weird
# "style item delegate" thing can then specify
# that size on each item...
values.sort()
br = _font.boundingRect(str(values[-1]))
w, h = br.width(), br.height()
view._max_item_size = w, h
view.setUniformItemSizes(True)
# limit to 6 items?
view.setMaximumHeight(6*h)
select.show()
self.hbox.addWidget(select) self.hbox.addWidget(select)
return select return select