Move font-aware line edit to "text entry" mod

ordermodepps_backup
Tyler Goodlet 2021-07-24 16:04:58 -04:00
parent 39edbc126a
commit df9e3654f0
2 changed files with 154 additions and 89 deletions

View File

@ -72,6 +72,7 @@ from ._style import (
_font, _font,
DpiAwareFont, DpiAwareFont,
) )
from ._text_entry import FontAndChartAwareLineEdit
log = get_logger(__name__) log = get_logger(__name__)
@ -95,7 +96,7 @@ class SimpleDelegate(QStyledItemDelegate):
class CompleterView(QTreeView): class CompleterView(QTreeView):
mode_name: str = 'mode: search-nav' mode_name: str = 'search-nav'
# XXX: relevant docs links: # XXX: relevant docs links:
# - simple widget version of this: # - simple widget version of this:
@ -424,85 +425,21 @@ class CompleterView(QTreeView):
self.resize() self.resize()
class FontAndChartAwareLineEdit(QtWidgets.QLineEdit):
def __init__(
self,
parent: QWidget,
parent_chart: QWidget, # noqa
font: DpiAwareFont = _font,
) -> None:
super().__init__(parent)
# self.setContextMenuPolicy(Qt.CustomContextMenu)
# self.customContextMenuRequested.connect(self.show_menu)
# self.setStyleSheet(f"font: 18px")
self.dpi_font = font
self.godwidget = parent_chart
# size it as we specify
# https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum
self.setSizePolicy(
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Fixed,
)
self.setFont(font.font)
# witty bit of margin
self.setTextMargins(2, 2, 2, 2)
# chart count which will be used to calculate
# width of input field.
self._chars: int = 9
def sizeHint(self) -> QtCore.QSize:
"""
Scale edit box to size of dpi aware font.
"""
psh = super().sizeHint()
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
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): class SearchBar(FontAndChartAwareLineEdit):
mode_name: str = 'mode: search' mode_name: str = 'search'
def __init__( def __init__(
self, self,
parent: QWidget, parent: QWidget,
godwidget: QWidget,
view: Optional[CompleterView] = None, view: Optional[CompleterView] = None,
**kwargs, **kwargs,
) -> None: ) -> None:
self.godwidget = godwidget
super().__init__(parent, **kwargs) super().__init__(parent, **kwargs)
self.view: CompleterView = view self.view: CompleterView = view
@ -524,7 +461,7 @@ class SearchWidget(QtWidgets.QWidget):
Includes helper methods for item management in the sub-widgets. Includes helper methods for item management in the sub-widgets.
''' '''
mode_name: str = 'mode: search' mode_name: str = 'search'
def __init__( def __init__(
self, self,
@ -573,7 +510,7 @@ class SearchWidget(QtWidgets.QWidget):
self.bar = SearchBar( self.bar = SearchBar(
parent=self, parent=self,
view=self.view, view=self.view,
parent_chart=godwidget, godwidget=godwidget,
) )
self.bar_hbox.addWidget(self.bar) self.bar_hbox.addWidget(self.bar)

View File

@ -18,22 +18,97 @@
Text entry widgets (mostly for configuration). Text entry widgets (mostly for configuration).
''' '''
from typing import Optional
# import trio
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget
from ._search import FontAndChartAwareLineEdit from ._style import hcolor, _font, DpiAwareFont
from ._style import hcolor, _font
class LabeledTextInput(QtGui.QWidget): class FontAndChartAwareLineEdit(QtWidgets.QLineEdit):
def __init__(
self,
parent: QWidget,
# parent_chart: QWidget, # noqa
font: DpiAwareFont = _font,
width_in_chars: int = None,
) -> None:
# self.setContextMenuPolicy(Qt.CustomContextMenu)
# self.customContextMenuRequested.connect(self.show_menu)
# self.setStyleSheet(f"font: 18px")
self.dpi_font = font
# self.godwidget = parent_chart
if width_in_chars:
self._chars = int(width_in_chars)
else:
# chart count which will be used to calculate
# width of input field.
self._chars: int = 9
super().__init__(parent)
# size it as we specify
# https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum
self.setSizePolicy(
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Fixed,
)
self.setFont(font.font)
# witty bit of margin
self.setTextMargins(2, 2, 2, 2)
def sizeHint(self) -> QtCore.QSize:
"""
Scale edit box to size of dpi aware font.
"""
psh = super().sizeHint()
dpi_font = self.dpi_font
psh.setHeight(dpi_font.px_size + 2)
# space for ``._chars: int``
char_w_pxs = dpi_font.boundingRect(self.text()).width()
chars_w = char_w_pxs + 6 # * dpi_font.scale() * self._chars
psh.setWidth(chars_w)
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 FieldsForm(QtGui.QWidget):
def __init__( def __init__(
self, self,
godwidget: 'GodWidget', # type: ignore # noqa
# godwidget: 'GodWidget', # type: ignore # noqa
parent=None, parent=None,
) -> None: ) -> None:
super().__init__(parent or godwidget) super().__init__(parent)
# size it as we specify # size it as we specify
self.setSizePolicy( self.setSizePolicy(
@ -41,22 +116,33 @@ class LabeledTextInput(QtGui.QWidget):
QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed,
) )
self.godwidget = godwidget
# split layout for the (label:| text bar entry) # split layout for the (label:| text bar entry)
self.hbox = QtGui.QHBoxLayout(self) self.hbox = QtGui.QHBoxLayout(self)
self.hbox.setContentsMargins(0, 0, 0, 0) self.hbox.setContentsMargins(16, 0, 16, 0)
self.hbox.setSpacing(4) self.hbox.setSpacing(3)
def add_field(
self,
name: str,
value: str,
widget: Optional[QWidget] = None,
) -> None:
# add label to left of search bar # add label to left of search bar
self.label = label = QtGui.QLabel(parent=self) self.label = label = QtGui.QLabel(parent=self)
label.setTextFormat(3) # markdown label.setTextFormat(3) # markdown
label.setFont(_font.font) label.setFont(_font.font)
label.setStyleSheet( label.setStyleSheet(
f"QLabel {{ color : {hcolor('gunmetal')}; }}" f"QLabel {{ color : {hcolor('papas_special')}; }}"
) )
label.setMargin(4) label.setMargin(4)
label.setText("`$cap:`")
# name = "share cap:"
label.setText(name)
label.setAlignment( label.setAlignment(
QtCore.Qt.AlignVCenter QtCore.Qt.AlignVCenter
| QtCore.Qt.AlignLeft | QtCore.Qt.AlignLeft
@ -67,15 +153,57 @@ class LabeledTextInput(QtGui.QWidget):
self.edit = FontAndChartAwareLineEdit( self.edit = FontAndChartAwareLineEdit(
parent=self, parent=self,
parent_chart=godwidget, # parent_chart=self.godwidget,
# width_in_chars=6,
) )
self.edit.set_width_in_chars(6) self.edit.setStyleSheet(
self.edit.setText('5000') f"QLineEdit {{ color : {hcolor('gunmetal')}; }}"
)
self.edit.setText(str(value))
self.hbox.addWidget(self.edit) self.hbox.addWidget(self.edit)
def sizeHint(self) -> QtCore.QSize:
"""
Scale edit box to size of dpi aware font.
""" # async def handle_form_input(
return self.edit.sizeHint()
# chart: 'ChartPlotWidget', # noqa
# form: FieldsForm,
# recv_chan: trio.abc.ReceiveChannel,
# ) -> None:
# async for event, etype, key, mods, txt in recv_chan:
# print(f'key: {key}, mods: {mods}, txt: {txt}')
# ctl = False
# if mods == Qt.ControlModifier:
# ctl = True
# # cancel and close
# if ctl and key in {
# Qt.Key_C,
# Qt.Key_Space, # i feel like this is the "native" one
# Qt.Key_Alt,
# }:
# # search.bar.unfocus()
# # kill the search and focus back on main chart
# if chart:
# chart.linkedsplits.focus()
# continue
def mk_form(
parent: QWidget,
fields: dict,
# orientation: str = 'horizontal',
) -> FieldsForm:
form = FieldsForm(parent)
for name, value in fields.items():
form.add_field(name, value)
return form