Add an icon setter api to `Selection`

chart_mod_breakup
Tyler Goodlet 2021-09-14 12:25:30 -04:00
parent be5a8e66d8
commit 9e41dfb735
2 changed files with 43 additions and 14 deletions

View File

@ -44,6 +44,7 @@ from PyQt5.QtWidgets import (
) )
from ._event import open_handlers from ._event import open_handlers
from ._icons import mk_icons
from ._style import hcolor, _font, _font_small, DpiAwareFont from ._style import hcolor, _font, _font_small, DpiAwareFont
from ._label import FormatLabel from ._label import FormatLabel
@ -178,7 +179,6 @@ class Selection(QComboBox):
) -> None: ) -> None:
self._items: dict[str, int] = {} self._items: dict[str, int] = {}
super().__init__(parent=parent) super().__init__(parent=parent)
self.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.setSizeAdjustPolicy(QComboBox.AdjustToContents)
# make line edit expand to surrounding frame # make line edit expand to surrounding frame
@ -192,6 +192,13 @@ class Selection(QComboBox):
# TODO: this doesn't seem to work for the currently selected item? # TODO: this doesn't seem to work for the currently selected item?
self.setItemDelegate(FontScaledDelegate(self)) self.setItemDelegate(FontScaledDelegate(self))
self.resize()
self._icons = mk_icons(
self.style(),
self.iconSize()
)
def set_style( def set_style(
self, self,
@ -208,6 +215,25 @@ class Selection(QComboBox):
""" """
) )
def resize(
self,
char: str = 'W',
) -> None:
br = _font.boundingRect(str(char))
_, h = br.width(), br.height()
# TODO: something better then this monkey patch
view = self.view()
# XXX: see size policy settings of line edit
# view._max_item_size = w, h
self.setMinimumHeight(h) # at least one entry in view
view.setMaximumHeight(6*h) # limit to 6 items max in view
icon_size = round(h * 0.75)
self.setIconSize(QSize(icon_size, icon_size))
def set_items( def set_items(
self, self,
keys: list[str], keys: list[str],
@ -231,23 +257,24 @@ class Selection(QComboBox):
# "style item delegate" thing can then specify # "style item delegate" thing can then specify
# that size on each item... # that size on each item...
keys.sort() keys.sort()
br = _font.boundingRect(str(keys[-1])) self.resize(keys[-1])
_, h = br.width(), br.height()
# TODO: something better then this monkey patch def set_icon(
view = self.view() self,
key: str,
icon_name: Optional[str],
# XXX: see size policy settings of line edit ) -> None:
# view._max_item_size = w, h self.setItemIcon(
self._items[key],
self._icons[icon_name],
)
self.setMinimumHeight(h) # at least one entry in view def items(self) -> list[(str, int)]:
view.setMaximumHeight(6*h) # limit to 6 items max in view return list(self._items.items())
icon_size = round(h * 0.75) # NOTE: in theory we can put icons on the RHS side with this hackery:
self.setIconSize(QSize(icon_size, icon_size)) # https://stackoverflow.com/a/64256969
# # NOTE: in theory we can put icons on the RHS side with this hackery:
# # https://stackoverflow.com/a/64256969
# def showPopup(self): # def showPopup(self):
# print('show') # print('show')
# QComboBox.showPopup(self) # QComboBox.showPopup(self)

View File

@ -51,6 +51,8 @@ def mk_icons(
if _icons: if _icons:
return _icons return _icons
_icons[None] = QIcon() # the "null" icon
# load account selection using current style # load account selection using current style
for name, icon_name in _icon_names.items(): for name, icon_name in _icon_names.items():