|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import random |
3 | 4 | from typing import TYPE_CHECKING |
4 | 5 |
|
5 | 6 | from PyQt6.QtGui import QFont, QPixmap |
|
9 | 10 | from src.database_commander import DB_COMMANDER |
10 | 11 | from src.dialog_handler import UI_LANGUAGE |
11 | 12 | from src.display_controller import DP_CONTROLLER |
| 13 | +from src.filepath import DEFAULT_COCKTAIL_IMAGE |
12 | 14 | from src.image_utils import find_cocktail_image |
13 | 15 | from src.models import Cocktail, Ingredient |
14 | 16 | from src.ui.creation_utils import LARGE_FONT, create_button, create_label, set_underline |
|
23 | 25 | class CocktailSelection(QDialog, Ui_CocktailSelection): |
24 | 26 | """Class for the Cocktail selection view.""" |
25 | 27 |
|
26 | | - def __init__(self, mainscreen: MainScreen, cocktail: Cocktail) -> None: |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + mainscreen: MainScreen, |
| 31 | + cocktail: Cocktail, |
| 32 | + random_mode: bool = False, |
| 33 | + random_pool: list[Cocktail] | None = None, |
| 34 | + ) -> None: |
27 | 35 | super().__init__(parent=mainscreen) |
28 | 36 | self.setupUi(self) |
29 | 37 | DP_CONTROLLER.initialize_window_object(self) |
30 | 38 | self.cocktail = cocktail |
31 | 39 | self.mainscreen = mainscreen |
| 40 | + self.random_mode = random_mode |
| 41 | + self.random_pool = random_pool or [] |
32 | 42 | # Store references for dynamic button label updates |
33 | 43 | self._volume_buttons: list[tuple[int, QPushButton]] = [] # list of (volume, button) tuples |
34 | 44 | # build the image |
@@ -94,8 +104,53 @@ def set_cocktail(self, cocktail: Cocktail) -> None: |
94 | 104 | self.cocktail = cocktail |
95 | 105 | self._set_image() |
96 | 106 |
|
| 107 | + def update_random_display(self) -> None: |
| 108 | + """Update the display for random cocktail mode.""" |
| 109 | + pixmap = QPixmap(str(DEFAULT_COCKTAIL_IMAGE)) |
| 110 | + self.image_container.setPixmap(pixmap) |
| 111 | + random_label = UI_LANGUAGE.get_translation("random_cocktail_label", "main_window") |
| 112 | + surprise_label = UI_LANGUAGE.get_translation("random_be_surprised", "main_window") |
| 113 | + self.LAlkoholname.setText(random_label) |
| 114 | + self.LAlkoholgehalt.setText("?") |
| 115 | + self.LMenge.setText("") |
| 116 | + # hide alcohol low/high buttons |
| 117 | + self.increase_alcohol.setVisible(False) |
| 118 | + self.decrease_alcohol.setVisible(False) |
| 119 | + # show virgin toggle only if any cocktail in pool has virgin_available |
| 120 | + has_virgin = any(c.virgin_available for c in self.random_pool) |
| 121 | + self.virgin_toggle.setVisible(has_virgin) |
| 122 | + self.virgin_toggle.setChecked(False) |
| 123 | + # show surprise message in first ingredient label, clear rest |
| 124 | + fields_ingredient = self.get_labels_maker_ingredients() |
| 125 | + fields_volume = self.get_labels_maker_volume() |
| 126 | + for field_ingredient, field_volume in zip(fields_ingredient, fields_volume): |
| 127 | + field_ingredient.setText("") |
| 128 | + field_volume.setText("") |
| 129 | + if fields_ingredient: |
| 130 | + fields_ingredient[0].setText(surprise_label) |
| 131 | + |
| 132 | + def _prepare_random_cocktail(self, amount: int) -> None: |
| 133 | + """Pick a random cocktail from the pool and prepare it.""" |
| 134 | + if self.is_virgin: |
| 135 | + pool = [c for c in self.random_pool if c.virgin_available] |
| 136 | + else: |
| 137 | + pool = [c for c in self.random_pool if not c.only_virgin] |
| 138 | + if not pool: |
| 139 | + return |
| 140 | + chosen = random.choice(pool) |
| 141 | + # Re-fetch from DB for up-to-date data |
| 142 | + db_cocktail = DB_COMMANDER.get_cocktail(chosen.id) |
| 143 | + if db_cocktail is not None: |
| 144 | + chosen = db_cocktail |
| 145 | + self.cocktail = chosen |
| 146 | + self._scale_cocktail(amount) |
| 147 | + qt_prepare_flow(self.mainscreen, self.cocktail) |
| 148 | + |
97 | 149 | def _prepare_cocktail(self, amount: int) -> None: |
98 | 150 | """Prepare the cocktail and switches to the maker screen, if successful.""" |
| 151 | + if self.random_mode: |
| 152 | + self._prepare_random_cocktail(amount) |
| 153 | + return |
99 | 154 | # same applies here, need to refetch the cocktail from db |
100 | 155 | db_cocktail = DB_COMMANDER.get_cocktail(self.cocktail.id) |
101 | 156 | if db_cocktail is not None: |
@@ -245,7 +300,8 @@ def _toggle_virgin(self, _: bool) -> None: |
245 | 300 | """Toggle the virgin option.""" |
246 | 301 | self.decrease_alcohol.setChecked(False) |
247 | 302 | self.increase_alcohol.setChecked(False) |
248 | | - self.update_cocktail_data() |
| 303 | + if not self.random_mode: |
| 304 | + self.update_cocktail_data() |
249 | 305 |
|
250 | 306 | def _adjust_preparation_buttons(self) -> None: |
251 | 307 | """Decide if to use a single or multiple buttons and adjusts the text accordingly. |
|
0 commit comments