diff --git a/piker/ui/_search.py b/piker/ui/_search.py index c6bd5902..542f9e7b 100644 --- a/piker/ui/_search.py +++ b/piker/ui/_search.py @@ -72,6 +72,7 @@ from ._style import ( _font, DpiAwareFont, ) +from ._text_entry import FontAndChartAwareLineEdit log = get_logger(__name__) @@ -95,7 +96,7 @@ class SimpleDelegate(QStyledItemDelegate): class CompleterView(QTreeView): - mode_name: str = 'mode: search-nav' + mode_name: str = 'search-nav' # XXX: relevant docs links: # - simple widget version of this: @@ -424,85 +425,21 @@ class CompleterView(QTreeView): 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): - mode_name: str = 'mode: search' + mode_name: str = 'search' def __init__( self, parent: QWidget, + godwidget: QWidget, view: Optional[CompleterView] = None, **kwargs, ) -> None: + self.godwidget = godwidget super().__init__(parent, **kwargs) self.view: CompleterView = view @@ -524,7 +461,7 @@ class SearchWidget(QtWidgets.QWidget): Includes helper methods for item management in the sub-widgets. ''' - mode_name: str = 'mode: search' + mode_name: str = 'search' def __init__( self, @@ -573,7 +510,7 @@ class SearchWidget(QtWidgets.QWidget): self.bar = SearchBar( parent=self, view=self.view, - parent_chart=godwidget, + godwidget=godwidget, ) self.bar_hbox.addWidget(self.bar) diff --git a/piker/ui/_text_entry.py b/piker/ui/_text_entry.py index 38d624c4..49fb62b1 100644 --- a/piker/ui/_text_entry.py +++ b/piker/ui/_text_entry.py @@ -18,22 +18,97 @@ Text entry widgets (mostly for configuration). ''' +from typing import Optional + +# import trio from PyQt5 import QtCore, QtGui from PyQt5 import QtWidgets +from PyQt5.QtWidgets import QWidget -from ._search import FontAndChartAwareLineEdit -from ._style import hcolor, _font +from ._style import hcolor, _font, DpiAwareFont -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__( self, - godwidget: 'GodWidget', # type: ignore # noqa + + # godwidget: 'GodWidget', # type: ignore # noqa parent=None, ) -> None: - super().__init__(parent or godwidget) + super().__init__(parent) # size it as we specify self.setSizePolicy( @@ -41,22 +116,33 @@ class LabeledTextInput(QtGui.QWidget): QtWidgets.QSizePolicy.Fixed, ) - self.godwidget = godwidget - # split layout for the (label:| text bar entry) self.hbox = QtGui.QHBoxLayout(self) - self.hbox.setContentsMargins(0, 0, 0, 0) - self.hbox.setSpacing(4) + self.hbox.setContentsMargins(16, 0, 16, 0) + self.hbox.setSpacing(3) + + def add_field( + self, + + name: str, + value: str, + + widget: Optional[QWidget] = None, + + ) -> None: # add label to left of search bar self.label = label = QtGui.QLabel(parent=self) label.setTextFormat(3) # markdown label.setFont(_font.font) label.setStyleSheet( - f"QLabel {{ color : {hcolor('gunmetal')}; }}" + f"QLabel {{ color : {hcolor('papas_special')}; }}" ) label.setMargin(4) - label.setText("`$cap:`") + + # name = "share cap:" + label.setText(name) + label.setAlignment( QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft @@ -67,15 +153,57 @@ class LabeledTextInput(QtGui.QWidget): self.edit = FontAndChartAwareLineEdit( parent=self, - parent_chart=godwidget, + # parent_chart=self.godwidget, + # width_in_chars=6, ) - self.edit.set_width_in_chars(6) - self.edit.setText('5000') + self.edit.setStyleSheet( + f"QLineEdit {{ color : {hcolor('gunmetal')}; }}" + ) + self.edit.setText(str(value)) self.hbox.addWidget(self.edit) - def sizeHint(self) -> QtCore.QSize: - """ - Scale edit box to size of dpi aware font. - """ - return self.edit.sizeHint() +# async def handle_form_input( + +# 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