add edit name

This commit is contained in:
Jannes Magnusson
2025-10-23 20:27:50 +02:00
parent 1e62bd77d1
commit 90257a62a0
6 changed files with 75 additions and 40 deletions

View File

@@ -89,24 +89,24 @@ class MainView(tk.Frame, ButtonInterface):
deactivate_command=self.enter_main_mode) deactivate_command=self.enter_main_mode)
self.refresh(0.0) self.refresh(0.0)
def enter_edit_recipe(self, recipe: Recipe = None): def enter_edit_recipe(self, recipe_id: int = None):
self.curr_mode = DISPLAY_MODES.EDIT_RECIPE self.curr_mode = DISPLAY_MODES.EDIT_RECIPE
self.buttons.current_view = EditRecipe(self, self.buttons.current_view = EditRecipe(self,
self.im_size, self.center, self.im_size, self.center,
recipe=recipe, recipe_id=recipe_id,
recipe_manager=self.recipes_manager, recipe_manager=self.recipes_manager,
edit_step_command=self.enter_edit_step, edit_step_command=self.enter_edit_step,
deactivate_command=self.enter_recipe_selection) deactivate_command=self.enter_recipe_selection)
self.refresh(0.0) self.refresh(0.0)
def enter_edit_step(self, recipe: Recipe, step_idx: int): def enter_edit_step(self, recipe_id: int, step_idx: int):
self.curr_mode = DISPLAY_MODES.EDIT_STEP self.curr_mode = DISPLAY_MODES.EDIT_STEP
self.buttons.current_view = EditStep(self, self.buttons.current_view = EditStep(self,
self.im_size, self.center, self.im_size, self.center,
recipe=recipe, recipe_id=recipe_id,
step_index=step_idx, step_index=step_idx,
recipe_manager=self.recipes_manager, recipe_manager=self.recipes_manager,
deactivate_command=lambda: self.enter_edit_recipe(recipe)) deactivate_command=lambda: self.enter_edit_recipe(recipe_id))
self.refresh(0.0) self.refresh(0.0)
################ VIEW MANAGEMENT ################ ################ VIEW MANAGEMENT ################

View File

@@ -5,20 +5,24 @@ from ..base import View
from ..button_interface import ButtonInterface from ..button_interface import ButtonInterface
from .recipe_manager import RecipeManager from .recipe_manager import RecipeManager
from .recipe import Recipe, Step from .recipe import Recipe
class EditRecipe(View, ButtonInterface): class EditRecipe(View, ButtonInterface):
def __init__(self, parent, im_size, center, def __init__(self, parent, im_size, center,
recipe_manager: RecipeManager, recipe_manager: RecipeManager,
edit_step_command, edit_step_command,
deactivate_command, deactivate_command,
recipe: Recipe = None): recipe_id: int = None):
self.deactivate_command = deactivate_command self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager self.recipe_manager = recipe_manager
self.recipe = recipe self.recipe_id = recipe_id
self.edit_step_command = edit_step_command self.edit_step_command = edit_step_command
if recipe is None: self.is_add_form = recipe_id is None
if recipe_id is None:
self.recipe = Recipe("New", []) self.recipe = Recipe("New", [])
self.recipe_manager.tmp_recipe = self.recipe
else:
self.recipe = recipe_manager.get_recipe(recipe_id)
self.confirm_view = False self.confirm_view = False
self.selected_field = 0 # 0: name, 1+: steps self.selected_field = 0 # 0: name, 1+: steps
@@ -94,14 +98,18 @@ class EditRecipe(View, ButtonInterface):
pass pass
else: else:
# edit name # edit name
self.edit_step_command(self.recipe, self.selected_field) self.edit_step_command(self.recipe_id, self.selected_field)
def right_press(self): def right_press(self):
self.selected_field = (self.selected_field + 1) % (len(self.recipe.steps) + 3) self.selected_field = (self.selected_field + 1) % (len(self.recipe.steps) + 3)
def right_long_press(self): def right_long_press(self):
# save # save
self.recipe_manager.add_recipe(self.recipe) if self.is_add_form:
self.recipe_manager.add_recipe(self.recipe)
else:
self.recipe_manager.update_recipe(self.recipe_id, self.recipe)
# if view is in edit mode, the recipe is already updated (by reference)
self.deactivate_command() self.deactivate_command()
def has_button(self) -> Tuple[bool, bool, bool, bool]: def has_button(self) -> Tuple[bool, bool, bool, bool]:

View File

@@ -12,16 +12,16 @@ from .recipe import Recipe, Step, StepType
class EditStep(View, ButtonInterface): class EditStep(View, ButtonInterface):
def __init__(self, parent, im_size, center, def __init__(self, parent, im_size, center,
recipe: Recipe, recipe_id: int,
step_index: int, step_index: int,
recipe_manager: RecipeManager, recipe_manager: RecipeManager,
deactivate_command): deactivate_command):
self.deactivate_command = deactivate_command self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager self.recipe_manager = recipe_manager
self.recipe = recipe self.recipe_id = recipe_id
self.step_index = step_index self.step_index = step_index
if step_index == 0: if step_index == 0:
self.step = recipe.name self.step = recipe_manager.get_recipe(recipe_id).name
self.confirm_view = False self.confirm_view = False
self.edit_step = 0 # 0: type, 1: step/name value self.edit_step = 0 # 0: type, 1: step/name value
@@ -53,21 +53,37 @@ class EditStep(View, ButtonInterface):
self.new_value += decode(self.morse_buffer, language=self.morse_code_language).upper() self.new_value += decode(self.morse_buffer, language=self.morse_code_language).upper()
else: else:
self.value_cursor -= 1 self.value_cursor -= 1
self.value_cursor = max(0, self.value_cursor)
self.new_value = self.new_value[:-1] self.new_value = self.new_value[:-1]
# process morse buffer # process morse buffer
self.last_input = None self.last_input = None
self.morse_buffer = '' self.morse_buffer = ''
if isinstance(self.step, str): if isinstance(self.step, str):
draw.text((x, 10), "Name:", fill='black') font_size = 16
draw.text((x, 30), self.new_value, fill='black') y_start = 30
draw.text((x, y_start), "Name:", fill='black')
draw.text((x, y_start + 20), self.new_value, fill='black', font_size=font_size)
if self.value_cursor_pulse > 1.0: if self.value_cursor_pulse > 1.0:
draw.rectangle((x + self.value_cursor * 8, 28, x + self.value_cursor * 8 + 8, 40), fill='black') draw.rectangle((x + self.value_cursor * 10,
y_start + 20 - 2,
x + self.value_cursor * 10 + 8,
y_start + 34),
fill='black')
if self.value_cursor_pulse > 2.0: if self.value_cursor_pulse > 2.0:
self.value_cursor_pulse = 0.0 self.value_cursor_pulse = 0.0
self.value_cursor_pulse += 0.1 self.value_cursor_pulse += 0.1
draw.line((x, 45, x + 80, 45), fill='black') draw.line((x, y_start + 37, x + 80, y_start + 37), fill='black')
if self.morse_buffer != '':
for i, suffix in enumerate(['']):#, '.', '-', '.' * 10]):
letter = 'del'
if len(suffix) != 10 and len(self.morse_buffer) + len(suffix) < 10:
letter = decode(self.morse_buffer + suffix, language=self.morse_code_language)
draw.text((x, y_start + 50 + i * 15),
f'{self.morse_buffer + suffix} {letter}',
fill='black', font_size=14)
elif self.edit_step == 0: elif self.edit_step == 0:
pass pass
else: else:
@@ -103,20 +119,16 @@ class EditStep(View, ButtonInterface):
return im return im
def left_press(self): def left_press(self):
self.selected_field = (self.selected_field - 1) % (len(self.recipe.steps) + 3) self.deactivate_command()
def left_long_press(self): def left_long_press(self):
if self.selected_field == len(self.recipe.steps) + 2: if self.step_index == 0:
# back # editing name
if self.new_value:
recipe = self.recipe_manager.get_recipe(self.recipe_id)
recipe.name = self.new_value
self.deactivate_command() self.deactivate_command()
elif self.selected_field == len(self.recipe.steps) + 1:
# add step
pass
elif self.selected_field == 0:
# edit name
pass
else: else:
# edit entry
pass pass
def right_press(self): def right_press(self):
@@ -130,9 +142,8 @@ class EditStep(View, ButtonInterface):
def has_button(self) -> Tuple[bool, bool, bool, bool]: def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, False return True, True, True, False
def render_left_press(self, draw, x, y): def render_left_press(self, draw, x, y):
draw.regular_polygon((x, y+2, 5), 3, fill='black') draw.text((x, y-5), 'Cancel', fill='black')
def render_left_long_press(self, draw, x, y): def render_left_long_press(self, draw, x, y):
draw.text((x, y-5), 'Next', fill='black') draw.text((x, y-5), 'Next', fill='black')

View File

@@ -1,5 +1,5 @@
from __future__ import annotations from __future__ import annotations
from typing import Union from copy import deepcopy
from enum import Enum from enum import Enum
@@ -13,6 +13,8 @@ class Recipe:
def __str__(self): def __str__(self):
return self.name return self.name
def copy(self) -> Recipe:
return deepcopy(self)
class StepType(Enum): class StepType(Enum):
SECTION = 0 SECTION = 0

View File

@@ -1,4 +1,4 @@
from .recipe import V60, ESPRESSO from .recipe import V60, ESPRESSO, Recipe
class RecipeManager: class RecipeManager:
def __init__(self): def __init__(self):
@@ -6,9 +6,21 @@ class RecipeManager:
V60, V60,
ESPRESSO ESPRESSO
] ]
self.tmp_recipe = None
def add_recipe(self, recipe): def add_recipe(self, recipe):
self.recipes.append(recipe) self.recipes.append(recipe)
def update_recipe(self, recipe_id, new_recipe):
self.recipes[recipe_id] = new_recipe
def remove_recipe(self, recipe): def remove_recipe(self, recipe):
self.recipes.remove(recipe) self.recipes.remove(recipe)
def get_recipe_id(self, recipe):
return self.recipes.index(recipe)
def get_recipe(self, recipe_id) -> Recipe:
if recipe_id == None:
return self.tmp_recipe
return self.recipes[recipe_id]

View File

@@ -64,7 +64,7 @@ class RecipeSelection(View, ButtonInterface):
def left_long_press(self): def left_long_press(self):
if self.selected_index < len(self.recipes): if self.selected_index < len(self.recipes):
self.edit_recipe_command(self.recipes[self.selected_index]) self.edit_recipe_command(self.selected_index)
def right_press(self): def right_press(self):
self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2) self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2)
@@ -74,8 +74,10 @@ class RecipeSelection(View, ButtonInterface):
# activate selected recipe # activate selected recipe
print(f"Activating recipe: {self.recipes[self.selected_index]}") print(f"Activating recipe: {self.recipes[self.selected_index]}")
elif self.selected_index == len(self.recipes): elif self.selected_index == len(self.recipes):
# add new recipe
self.edit_recipe_command() self.edit_recipe_command()
else: else:
# back
self.selected_index = 0 self.selected_index = 0
self.deactivate_command() self.deactivate_command()