From 90257a62a01ba9fd3391635d78a63eb51b97c913 Mon Sep 17 00:00:00 2001 From: Jannes Magnusson Date: Thu, 23 Oct 2025 20:27:50 +0200 Subject: [PATCH] add edit name --- frontend/views/main_view.py | 14 +++--- frontend/views/recipes/edit_recipe.py | 22 ++++++--- frontend/views/recipes/edit_step.py | 55 +++++++++++++--------- frontend/views/recipes/recipe.py | 4 +- frontend/views/recipes/recipe_manager.py | 16 ++++++- frontend/views/recipes/recipe_selection.py | 4 +- 6 files changed, 75 insertions(+), 40 deletions(-) diff --git a/frontend/views/main_view.py b/frontend/views/main_view.py index f6b6d3f..9bd6db1 100644 --- a/frontend/views/main_view.py +++ b/frontend/views/main_view.py @@ -88,25 +88,25 @@ class MainView(tk.Frame, ButtonInterface): edit_recipe_command=self.enter_edit_recipe, deactivate_command=self.enter_main_mode) 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.buttons.current_view = EditRecipe(self, self.im_size, self.center, - recipe=recipe, + recipe_id=recipe_id, recipe_manager=self.recipes_manager, edit_step_command=self.enter_edit_step, deactivate_command=self.enter_recipe_selection) 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.buttons.current_view = EditStep(self, self.im_size, self.center, - recipe=recipe, + recipe_id=recipe_id, step_index=step_idx, 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) ################ VIEW MANAGEMENT ################ diff --git a/frontend/views/recipes/edit_recipe.py b/frontend/views/recipes/edit_recipe.py index dc5c34a..5d855ab 100644 --- a/frontend/views/recipes/edit_recipe.py +++ b/frontend/views/recipes/edit_recipe.py @@ -5,21 +5,25 @@ from ..base import View from ..button_interface import ButtonInterface from .recipe_manager import RecipeManager -from .recipe import Recipe, Step +from .recipe import Recipe class EditRecipe(View, ButtonInterface): def __init__(self, parent, im_size, center, recipe_manager: RecipeManager, edit_step_command, deactivate_command, - recipe: Recipe = None): + recipe_id: int = None): self.deactivate_command = deactivate_command self.recipe_manager = recipe_manager - self.recipe = recipe + self.recipe_id = recipe_id 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_manager.tmp_recipe = self.recipe + else: + self.recipe = recipe_manager.get_recipe(recipe_id) + self.confirm_view = False self.selected_field = 0 # 0: name, 1+: steps @@ -94,14 +98,18 @@ class EditRecipe(View, ButtonInterface): pass else: # 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): self.selected_field = (self.selected_field + 1) % (len(self.recipe.steps) + 3) def right_long_press(self): # 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() def has_button(self) -> Tuple[bool, bool, bool, bool]: diff --git a/frontend/views/recipes/edit_step.py b/frontend/views/recipes/edit_step.py index 8f7222b..eba9127 100644 --- a/frontend/views/recipes/edit_step.py +++ b/frontend/views/recipes/edit_step.py @@ -12,17 +12,17 @@ from .recipe import Recipe, Step, StepType class EditStep(View, ButtonInterface): def __init__(self, parent, im_size, center, - recipe: Recipe, + recipe_id: int, step_index: int, recipe_manager: RecipeManager, deactivate_command): self.deactivate_command = deactivate_command self.recipe_manager = recipe_manager - self.recipe = recipe + self.recipe_id = recipe_id self.step_index = step_index if step_index == 0: - self.step = recipe.name - + self.step = recipe_manager.get_recipe(recipe_id).name + self.confirm_view = False self.edit_step = 0 # 0: type, 1: step/name value self.new_type = '' @@ -53,21 +53,37 @@ class EditStep(View, ButtonInterface): self.new_value += decode(self.morse_buffer, language=self.morse_code_language).upper() else: self.value_cursor -= 1 + self.value_cursor = max(0, self.value_cursor) self.new_value = self.new_value[:-1] # process morse buffer self.last_input = None self.morse_buffer = '' if isinstance(self.step, str): - draw.text((x, 10), "Name:", fill='black') - draw.text((x, 30), self.new_value, fill='black') + font_size = 16 + 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: - 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: self.value_cursor_pulse = 0.0 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: pass else: @@ -101,22 +117,18 @@ class EditStep(View, ButtonInterface): # draw.text((x + 30, y_pos - 5), step.value_str, fill='black') return im - + def left_press(self): - self.selected_field = (self.selected_field - 1) % (len(self.recipe.steps) + 3) + self.deactivate_command() def left_long_press(self): - if self.selected_field == len(self.recipe.steps) + 2: - # back + if self.step_index == 0: + # editing name + if self.new_value: + recipe = self.recipe_manager.get_recipe(self.recipe_id) + recipe.name = self.new_value self.deactivate_command() - elif self.selected_field == len(self.recipe.steps) + 1: - # add step - pass - elif self.selected_field == 0: - # edit name - pass else: - # edit entry pass def right_press(self): @@ -128,11 +140,10 @@ class EditStep(View, ButtonInterface): self.morse_buffer += '-' 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): - 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): draw.text((x, y-5), 'Next', fill='black') diff --git a/frontend/views/recipes/recipe.py b/frontend/views/recipes/recipe.py index 623594f..91a6bb6 100644 --- a/frontend/views/recipes/recipe.py +++ b/frontend/views/recipes/recipe.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import Union +from copy import deepcopy from enum import Enum @@ -13,6 +13,8 @@ class Recipe: def __str__(self): return self.name + def copy(self) -> Recipe: + return deepcopy(self) class StepType(Enum): SECTION = 0 diff --git a/frontend/views/recipes/recipe_manager.py b/frontend/views/recipes/recipe_manager.py index 699be15..d4fa162 100644 --- a/frontend/views/recipes/recipe_manager.py +++ b/frontend/views/recipes/recipe_manager.py @@ -1,4 +1,4 @@ -from .recipe import V60, ESPRESSO +from .recipe import V60, ESPRESSO, Recipe class RecipeManager: def __init__(self): @@ -6,9 +6,21 @@ class RecipeManager: V60, ESPRESSO ] + self.tmp_recipe = None def add_recipe(self, recipe): self.recipes.append(recipe) + def update_recipe(self, recipe_id, new_recipe): + self.recipes[recipe_id] = new_recipe + def remove_recipe(self, recipe): - self.recipes.remove(recipe) \ No newline at end of file + 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] \ No newline at end of file diff --git a/frontend/views/recipes/recipe_selection.py b/frontend/views/recipes/recipe_selection.py index 6115166..16db186 100644 --- a/frontend/views/recipes/recipe_selection.py +++ b/frontend/views/recipes/recipe_selection.py @@ -64,7 +64,7 @@ class RecipeSelection(View, ButtonInterface): def left_long_press(self): 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): self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2) @@ -74,8 +74,10 @@ class RecipeSelection(View, ButtonInterface): # activate selected recipe print(f"Activating recipe: {self.recipes[self.selected_index]}") elif self.selected_index == len(self.recipes): + # add new recipe self.edit_recipe_command() else: + # back self.selected_index = 0 self.deactivate_command()