|
@@ -4,16 +4,14 @@
|
|
|
# All rights reserved
|
|
|
#
|
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
-from collections import OrderedDict
|
|
|
+from typing import Iterable
|
|
|
import urwid
|
|
|
from decimal import Decimal
|
|
|
from urwid import numedit
|
|
|
-
|
|
|
-import app
|
|
|
-from app.db_utils import NON_IDENTIFIER_COLUMNS
|
|
|
+from urwid.wimp import PopUpLauncher
|
|
|
|
|
|
class AutoCompleteEdit(urwid.Edit):
|
|
|
- def __init__(self, name, *args, apply_change_func=None, **kwargs):
|
|
|
+ def __init__(self, name: str, *args: Iterable, **kwargs):
|
|
|
if isinstance(name, tuple):
|
|
|
pallete, title = name
|
|
|
self.name = title
|
|
@@ -23,10 +21,9 @@ class AutoCompleteEdit(urwid.Edit):
|
|
|
self.name = name
|
|
|
title = name.title()
|
|
|
passthrough = u' ' if name.lower() == 'unit' else u''
|
|
|
-
|
|
|
+
|
|
|
super().__init__(passthrough, *args, **kwargs)
|
|
|
- self.apply = apply_change_func
|
|
|
-
|
|
|
+
|
|
|
def keypress(self, size, key):
|
|
|
if key == 'enter':
|
|
|
self.apply(self.name)
|
|
@@ -37,9 +34,8 @@ class AutoCompleteEdit(urwid.Edit):
|
|
|
|
|
|
return super().keypress(size, key)
|
|
|
|
|
|
-
|
|
|
class AutoCompleteFloatEdit(numedit.FloatEdit):
|
|
|
- def __init__(self, name, *args, apply_change_func=None, **kwargs):
|
|
|
+ def __init__(self, name: str, *args: Iterable, **kwargs: dict):
|
|
|
self.last_val = None
|
|
|
self.op = '='
|
|
|
self.pallete = None
|
|
@@ -51,10 +47,9 @@ class AutoCompleteFloatEdit(numedit.FloatEdit):
|
|
|
self.name = name
|
|
|
title = name.title()
|
|
|
passthrough = title
|
|
|
-
|
|
|
- super(AutoCompleteFloatEdit, self).__init__(passthrough, *args, **kwargs)
|
|
|
- self.apply = apply_change_func
|
|
|
-
|
|
|
+
|
|
|
+ super().__init__(passthrough, *args, **kwargs)
|
|
|
+
|
|
|
def update_caption(self):
|
|
|
if self.pallete is not None:
|
|
|
self.set_caption((self.pallete, f'{self.op} '))
|
|
@@ -205,28 +200,46 @@ class FocusWidget(urwid.WidgetPlaceholder):
|
|
|
|
|
|
self._set_focus_path(self.initial_focus)
|
|
|
|
|
|
-class SuggestionPopup(urwid.Overlay):
|
|
|
+class AutoCompletePopUp(PopUpLauncher):
|
|
|
+ def __init__(self, widget, apply_change_func=None, apply_choice_cb=None):
|
|
|
+ super().__init__(widget)
|
|
|
+ self._original_widget.apply = lambda name: apply_change_func(name, self._open_pop_up)
|
|
|
+ self._original_widget.apply_choice_cb = apply_choice_cb
|
|
|
+
|
|
|
+ def _open_pop_up(self, options):
|
|
|
+ self.options = options
|
|
|
+ self.open_pop_up()
|
|
|
|
|
|
- def __init__(self, under, name, options, apply_cb, esc_cb):
|
|
|
- self.esc_cb = esc_cb
|
|
|
- self.under = under
|
|
|
- body = [urwid.Text(name.title()), urwid.Divider()]
|
|
|
+ def create_pop_up(self):
|
|
|
+ pop_up = SuggestionPopup(
|
|
|
+ self._original_widget.name, self.options,
|
|
|
+ lambda w,x: self._original_widget.apply_choice_cb(self._original_widget.name, w, x),
|
|
|
+ )
|
|
|
+ urwid.connect_signal(pop_up, 'close',
|
|
|
+ lambda _: self.close_pop_up())
|
|
|
+ return pop_up
|
|
|
+
|
|
|
+ def get_pop_up_parameters(self):
|
|
|
+ return {'left':0, 'top':1, 'overlay_width':32, 'overlay_height': 10}
|
|
|
+
|
|
|
+class SuggestionPopup(urwid.WidgetWrap):
|
|
|
+
|
|
|
+ signals = ['close']
|
|
|
+
|
|
|
+ def __init__(self, name, options, apply_cb):
|
|
|
+ body = [] #[urwid.Text(name.title()), urwid.Divider()]
|
|
|
for c in options:
|
|
|
button = urwid.Button(c)
|
|
|
urwid.connect_signal(button, 'click', apply_cb, c)
|
|
|
+ urwid.connect_signal(button, 'click', lambda _: self._emit("close"))
|
|
|
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
|
|
|
walker = urwid.SimpleFocusListWalker(body, wrap_around=False)
|
|
|
listbox = urwid.ListBox(walker)
|
|
|
- pad = urwid.Padding(listbox, left=2, right=2)
|
|
|
- pad = urwid.AttrMap(pad, 'banner')
|
|
|
- super().__init__(pad, under,
|
|
|
- align='center', width=('relative', 60),
|
|
|
- valign='middle', height=('relative', 60),
|
|
|
- min_width=20, min_height=9)
|
|
|
-
|
|
|
+ super().__init__(urwid.AttrWrap(listbox, 'banner'))
|
|
|
+
|
|
|
def keypress(self, size, key):
|
|
|
if key == 'esc':
|
|
|
- self.esc_cb()
|
|
|
+ self._emit("close")
|
|
|
return
|
|
|
|
|
|
if key == 'tab':
|