Factor font-size-based labeled-line edit into generics widget

fsp_feeds
Tyler Goodlet 2021-07-23 12:00:48 -04:00
parent 5fb00f726e
commit 3fb0e02788
1 changed files with 51 additions and 18 deletions

View File

@ -71,7 +71,6 @@ from ..log import get_logger
from ._style import ( from ._style import (
_font, _font,
DpiAwareFont, DpiAwareFont,
# hcolor,
) )
@ -425,27 +424,22 @@ class CompleterView(QTreeView):
self.resize() self.resize()
class SearchBar(QtWidgets.QLineEdit): class FontAndChartAwareLineEdit(QtWidgets.QLineEdit):
mode_name: str = 'mode: search'
def __init__( def __init__(
self, self,
parent: QWidget, parent: QWidget,
parent_chart: QWidget, # noqa parent_chart: QWidget, # noqa
view: Optional[CompleterView] = None,
font: DpiAwareFont = _font, font: DpiAwareFont = _font,
) -> None: ) -> None:
super().__init__(parent) super().__init__(parent)
# self.setContextMenuPolicy(Qt.CustomContextMenu) # self.setContextMenuPolicy(Qt.CustomContextMenu)
# self.customContextMenuRequested.connect(self.show_menu) # self.customContextMenuRequested.connect(self.show_menu)
# self.setStyleSheet(f"font: 18px") # self.setStyleSheet(f"font: 18px")
self.view: CompleterView = view
self.dpi_font = font self.dpi_font = font
self.godwidget = parent_chart self.godwidget = parent_chart
@ -460,14 +454,9 @@ class SearchBar(QtWidgets.QLineEdit):
# witty bit of margin # witty bit of margin
self.setTextMargins(2, 2, 2, 2) self.setTextMargins(2, 2, 2, 2)
def focus(self) -> None: # chart count which will be used to calculate
self.selectAll() # width of input field.
self.show() self._chars: int = 9
self.setFocus()
def show(self) -> None:
super().show()
self.view.show_matches()
def sizeHint(self) -> QtCore.QSize: def sizeHint(self) -> QtCore.QSize:
""" """
@ -475,9 +464,52 @@ class SearchBar(QtWidgets.QLineEdit):
""" """
psh = super().sizeHint() psh = super().sizeHint()
psh.setHeight(self.dpi_font.px_size + 2)
dpi_font = self.dpi_font
char_w_pxs = dpi_font.boundingRect('A').width()
# space for ``._chars: int``
chars_w = self._chars * char_w_pxs * dpi_font.scale()
psh.setHeight(dpi_font.px_size + 2)
psh.setWidth(chars_w)
return psh return psh
def set_width_in_chars(
self,
chars: int,
) -> None:
self._chars = chars
self.sizeHint()
self.update()
def focus(self) -> None:
self.selectAll()
self.show()
self.setFocus()
class SearchBar(FontAndChartAwareLineEdit):
mode_name: str = 'mode: search'
def __init__(
self,
parent: QWidget,
view: Optional[CompleterView] = None,
**kwargs,
) -> None:
super().__init__(parent, **kwargs)
self.view: CompleterView = view
def show(self) -> None:
super().show()
self.view.show_matches()
def unfocus(self) -> None: def unfocus(self) -> None:
self.parent().hide() self.parent().hide()
self.clearFocus() self.clearFocus()
@ -540,8 +572,8 @@ class SearchWidget(QtWidgets.QWidget):
) )
self.bar = SearchBar( self.bar = SearchBar(
parent=self, parent=self,
parent_chart=godwidget,
view=self.view, view=self.view,
parent_chart=godwidget,
) )
self.bar_hbox.addWidget(self.bar) self.bar_hbox.addWidget(self.bar)
@ -599,11 +631,12 @@ class SearchWidget(QtWidgets.QWidget):
def chart_current_item( def chart_current_item(
self, self,
clear_to_cache: bool = True, clear_to_cache: bool = True,
) -> Optional[str]: ) -> Optional[str]:
'''Attempt to load and switch the current selected '''Attempt to load and switch the current selected
completion result to the affiliated chart app. completion result to the affiliated chart app.
Return any loaded symbol Return any loaded symbol.
''' '''
value = self.get_current_item() value = self.get_current_item()