WIP add input handler for each widget in the form

ordermodepps_backup
Tyler Goodlet 2021-07-25 15:43:41 -04:00
parent fd73d1eef1
commit 4336939507
1 changed files with 60 additions and 32 deletions

View File

@ -18,13 +18,16 @@
Text entry "forms" widgets (mostly for configuration and UI user input). Text entry "forms" widgets (mostly for configuration and UI user input).
''' '''
from functools import partial
from typing import Optional from typing import Optional
from contextlib import asynccontextmanager
# import trio import trio
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QSize, QModelIndex from PyQt5.QtCore import QSize, QModelIndex, Qt, QEvent
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QWidget, QWidget,
QLabel,
QComboBox, QComboBox,
QLineEdit, QLineEdit,
QProgressBar, QProgressBar,
@ -33,6 +36,7 @@ from PyQt5.QtWidgets import (
QStyleOptionViewItem, QStyleOptionViewItem,
) )
from ._event import open_handlers
from ._style import hcolor, _font, DpiAwareFont from ._style import hcolor, _font, DpiAwareFont
@ -144,7 +148,7 @@ class FontScaledDelegate(QStyledItemDelegate):
return super().sizeHint(option, index) return super().sizeHint(option, index)
class FieldsForm(QtGui.QWidget): class FieldsForm(QWidget):
def __init__( def __init__(
self, self,
@ -166,6 +170,9 @@ class FieldsForm(QtGui.QWidget):
self.hbox.setContentsMargins(16, 0, 16, 0) self.hbox.setContentsMargins(16, 0, 16, 0)
self.hbox.setSpacing(3) self.hbox.setSpacing(3)
self.labels: dict[str, QLabel] = {}
self.fields: dict[str, QWidget] = {}
def add_field_label( def add_field_label(
self, self,
name: str, name: str,
@ -190,6 +197,8 @@ class FieldsForm(QtGui.QWidget):
label.show() label.show()
self.hbox.addWidget(label) self.hbox.addWidget(label)
self.labels[name] = label
return label return label
def add_edit_field( def add_edit_field(
@ -200,21 +209,25 @@ class FieldsForm(QtGui.QWidget):
widget: Optional[QWidget] = None, widget: Optional[QWidget] = None,
) -> None: ) -> FontAndChartAwareLineEdit:
# TODO: maybe a distint layout per "field" item? # TODO: maybe a distint layout per "field" item?
self.add_field_label(name) self.add_field_label(name)
self.edit = FontAndChartAwareLineEdit( edit = FontAndChartAwareLineEdit(
parent=self, parent=self,
# parent_chart=self.godwidget, # parent_chart=self.godwidget,
# width_in_chars=6, # width_in_chars=6,
) )
self.edit.setStyleSheet( edit.setStyleSheet(
f"QLineEdit {{ color : {hcolor('gunmetal')}; }}" f"QLineEdit {{ color : {hcolor('gunmetal')}; }}"
) )
self.edit.setText(str(value)) edit.setText(str(value))
self.hbox.addWidget(self.edit) self.hbox.addWidget(edit)
self.fields[name] = edit
return edit
def add_select_field( def add_select_field(
self, self,
@ -264,37 +277,39 @@ class FieldsForm(QtGui.QWidget):
return select return select
# async def handle_form_input( async def handle_field_input(
# chart: 'ChartPlotWidget', # noqa # chart: 'ChartPlotWidget', # noqa
# form: FieldsForm, widget: QWidget,
# recv_chan: trio.abc.ReceiveChannel, recv_chan: trio.abc.ReceiveChannel,
form: FieldsForm,
# ) -> None: ) -> None:
# async for event, etype, key, mods, txt in recv_chan: async for event, etype, key, mods, txt in recv_chan:
# print(f'key: {key}, mods: {mods}, txt: {txt}') print(f'key: {key}, mods: {mods}, txt: {txt}')
# ctl = False ctl = False
# if mods == Qt.ControlModifier: if mods == Qt.ControlModifier:
# ctl = True ctl = True
# # cancel and close # cancel and close
# if ctl and key in { if ctl and key in {
# Qt.Key_C, Qt.Key_C,
# Qt.Key_Space, # i feel like this is the "native" one Qt.Key_Space, # i feel like this is the "native" one
# Qt.Key_Alt, Qt.Key_Alt,
# }: }:
# # search.bar.unfocus() # search.bar.unfocus()
# # kill the search and focus back on main chart # kill the search and focus back on main chart
# if chart: if chart:
# chart.linkedsplits.focus() chart.linkedsplits.focus()
# continue continue
def mk_form( @asynccontextmanager
async def mk_form(
parent: QWidget, parent: QWidget,
fields: dict, fields: dict,
@ -307,7 +322,7 @@ def mk_form(
for name, value in fields.items(): for name, value in fields.items():
form.add_edit_field(name, value) form.add_edit_field(name, value)
form.add_select_field('policy:', ['uniform', 'halfs']) form.add_select_field('policy:', ['uniform'])
form.add_field_label('fills:') form.add_field_label('fills:')
fill_bar = QProgressBar(form) fill_bar = QProgressBar(form)
@ -317,4 +332,17 @@ def mk_form(
form.hbox.addWidget(fill_bar) form.hbox.addWidget(fill_bar)
return form async with open_handlers(
list(form.fields.values()),
event_types={QEvent.KeyPress},
async_handler=partial(
handle_field_input,
form=form,
),
# block key repeats?
filter_auto_repeats=True,
):
yield form