Pārlūkot izejas kodu

improve op indicator for FloatEdit fields, improve ordering of side pane, and add support for cycling focus through fields via tab

Daniel Sheffield 3 gadi atpakaļ
vecāks
revīzija
bb9e374434
1 mainītis faili ar 46 papildinājumiem un 21 dzēšanām
  1. 46 21
      grocery_transactions.py

+ 46 - 21
grocery_transactions.py

@@ -80,13 +80,7 @@ def records(cursor, col_idx_map):
 
 def record_matches(record, strict=None, **kwargs):
     strict = strict or []
-    if kwargs['product']:
-        with open('log', 'a') as f:
-            f.write(f"Filter {strict}: {kwargs}\n")
-        
     for k,v in kwargs.items():
-        with open('log', 'a') as f:
-            f.write(f"Record: {record}\n")
         if not v:
             continue
         
@@ -94,12 +88,8 @@ def record_matches(record, strict=None, **kwargs):
             return False
         
         if v.lower() not in record[k].lower():
-            with open('log', 'a') as f:
-                f.write(f"No match\n")
             return False
         
-    with open('log', 'a') as f:
-        f.write(f"Match\n")
     return True
 
 def unique_suggestions(name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
@@ -116,7 +106,7 @@ def show_or_exit(key):
     if isinstance(key, tuple):
         return
     
-    if key in ('esc'):
+    if key in ('esc',):
         raise urwid.ExitMainLoop()
 
 def interleave(_list, div):
@@ -163,7 +153,7 @@ class AutoCompleteFloatEdit(numedit.FloatEdit):
 
     def update_caption(self):
         if self.pallete is not None:
-            self.set_caption((self.pallete, f'{self.op}'))
+            self.set_caption((self.pallete, f'{self.op} '))
         else:
             self.set_caption(f'{self.op} ')
 
@@ -206,17 +196,34 @@ class AutoCompleteFloatEdit(numedit.FloatEdit):
         elif key in ('=', 'enter',):
             self.calc()
             return
-        elif key in ('esc', 'left', 'right', 'up', 'down', 'backspace', 'delete',) or \
-             key in '0123456789.':
-            return super(AutoCompleteFloatEdit, self).keypress(size, key)
+        #elif key in ('esc', 'left', 'right', 'up', 'down', 'backspace', 'delete',) or \
+        #     key in '0123456789.':
+        return super(AutoCompleteFloatEdit, self).keypress(size, key)
+
+class NoTabCheckBox(urwid.CheckBox):
+    def keypress(self, size, key):
+        if not isinstance(key, tuple) and key == 'tab':
+            return 
         else:
-            return
+            return super(NoTabCheckBox, self).keypress(size, key)
 
+def _set_focus_path(container, path):
+    try:
+        container.set_focus_path(path)
+        return
+    except IndexError:
+        pass
+        
+    if path[-1] == 0 and len(path) > 1:
+        _set_focus_path(container, path[:-1])
+        return
+    
+    raise IndexError
 
 class GroceryTransactionEditor(urwid.WidgetPlaceholder):
     def __init__(self, fields):
         super(GroceryTransactionEditor, self).__init__(urwid.SolidFill(u'/'))
-        self.organic_checkbox = urwid.CheckBox(
+        self.organic_checkbox = NoTabCheckBox(
             u"Organic",
             on_state_change=self.apply_organic_state
         )
@@ -224,15 +231,32 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
         self.widgets = dict()
         self.show('transaction')
 
+    def advance_focus(self):
+        container = self.original_widget.original_widget.original_widget
+        path = container.get_focus_path()
+        for idx, part in [ i for i in enumerate(path)][::-1]:
+            p = [ i for i in path ]
+            p[idx] += 1
+            try:
+                _set_focus_path(container, p)
+                if path == [3]:
+                    self.advance_focus()
+                return
+            except IndexError:
+                path[idx] = 0
+        
+        container.set_focus_path([2,0,0,0])
+
+        
     def keypress(self, size, key):
         if isinstance(key, tuple):
             return
         
         if key == 'tab':
-            # need to change this to self.advance_focus
-            return super(GroceryTransactionEditor, self).keypress(size, 'enter')
-        else:
+            self.advance_focus()
+        else: #if key == 'esc':
             return super(GroceryTransactionEditor, self).keypress(size, key)
+        
 
     def _init_data(self, fields):
         self.data = OrderedDict([
@@ -379,7 +403,7 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
             fields[r] if r is not None else urwid.Divider() for r in side_pane
         ]))
         main_pane_widgets = []
-        for r in grid_layout:
+        for i, r in enumerate(grid_layout):
             widgets = []
             for c in r:
                 if c is not None:
@@ -391,6 +415,7 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
                     widgets.append(urwid.Divider())
             main_pane_widgets.append(urwid.Columns(widgets))
 
+
         main_pane_widget = urwid.Pile(main_pane_widgets)
         
         widget = urwid.Pile([