from typing import Tuple from PIL import ImageDraw, Image from ..base import View from ..button_interface import ButtonInterface from .recipe_manager import RecipeManager from .recipe import Recipe class EditRecipe(View, ButtonInterface): def __init__(self, parent, im_size, center, recipe_manager: RecipeManager, edit_step_command, deactivate_command, recipe_id: int = None): self.deactivate_command = deactivate_command self.recipe_manager = recipe_manager self.recipe_id = recipe_id self.edit_step_command = edit_step_command 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 super().__init__(parent, im_size, center) def _get_visual_steps(self): steps = self.recipe.steps + ['+', 'BACK'] if len(steps) < 4: return steps, 0 start = max(0, self.selected_field - 2) end = min(len(steps), start + 4) steps = steps[start:end] return steps, start def update_weight(self, weight: float) -> Image.Image: im = self.bkg_im.copy() draw = ImageDraw.Draw(im) x = 40 draw.text((x, 10), "Name:", fill='black') if self.selected_field == 0: r = 10 offset = 35 for i in range(0, 90, r // 2): draw.circle((x + i, offset), r, fill='black') draw.text((x, 30), self.recipe.name, fill='white') else: draw.text((x, 30), self.recipe.name, fill='black') visual_steps, start = self._get_visual_steps() for idx, step in enumerate(visual_steps): y_pos = 60 + idx * 20 if start + idx + 1 == self.selected_field: r = 10 offset = 15 for i in range(0, 90, r // 2): draw.circle((x + i, y_pos), r, fill='black') if str(step) == '+': draw.text((x, y_pos - 5), '+', fill='white') elif str(step) == 'BACK': draw.regular_polygon((x + 5, y_pos, 5), 3, fill='white', rotation=90) else: step.step_type.render(draw, (x, y_pos - 5), fill='white') draw.text((x + 30, y_pos - 5), step.value_str, fill='white') elif str(step) == '+': draw.text((x, y_pos - 5), '+', fill='black') elif str(step) == 'BACK': draw.regular_polygon((x + 5, y_pos, 5), 3, fill='black', rotation=90) else: step.step_type.render(draw, (x, y_pos - 5), fill='black') 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) def left_long_press(self): if self.selected_field == len(self.recipe.steps) + 2: # back self.deactivate_command() elif self.selected_field == len(self.recipe.steps) + 1: # add step pass else: # edit name 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 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]: return True, True, True, True def render_left_press(self, draw, x, y): draw.regular_polygon((x, y+2, 5), 3, fill='black') def render_left_long_press(self, draw, x, y): draw.text((x, y-5), 'Enter', fill='black') def render_right_press(self, draw, x, y): draw.regular_polygon((x, y+4, 5), 3, fill='black', rotation=180) def render_right_long_press(self, draw, x, y): draw.text((x - 20, y-5), 'Save', fill='black')