"""Farm panel: name/money/farmland stats, other farms, own farmland list. Mirrors `renderFarm` + `renderPlayerStatus`.""" from __future__ import annotations from typing import Optional from PySide6.QtWidgets import QHBoxLayout, QLabel, QVBoxLayout, QWidget from formatting import format_euro, is_number from widgets.common import CollapsiblePanel, StatChip, clear_layout, style_role class FarmPanel(CollapsiblePanel): def __init__(self, parent=None) -> None: super().__init__("farm", "Betrieb", parent) chip_row = QHBoxLayout() self.name_chip = StatChip("Betrieb") self.money_chip = StatChip("Kontostand") self.farmland_count_chip = StatChip("Eigene Grundstücke") self.farmland_value_chip = StatChip("Grundstückswert") self.player_status_chip = StatChip("Spielerstatus") for chip in ( self.name_chip, self.money_chip, self.farmland_count_chip, self.farmland_value_chip, self.player_status_chip, ): chip_row.addWidget(chip) self.body_layout.addLayout(chip_row) self.other_farms_head = QLabel("Andere Betriebe") style_role(self.other_farms_head, "stat-label") self.other_farms_head.hide() self.body_layout.addWidget(self.other_farms_head) self.other_farms_list = QVBoxLayout() self.body_layout.addLayout(self.other_farms_list) farmland_head = QLabel("Eigene Grundstücke") style_role(farmland_head, "stat-label") self.body_layout.addWidget(farmland_head) self.farmland_list = QVBoxLayout() self.body_layout.addLayout(self.farmland_list) @staticmethod def _row(name: str, value: str) -> QWidget: # Wrapped in a QWidget (not a bare QHBoxLayout) so a single # addWidget()/clear_layout() pair in render_farm() can add and later # remove it as one unit - a layout added via addLayout() can't be # torn back down that way (clear_layout only knows how to hide # *widgets* it finds via item.widget()), which would otherwise leave # every farmland/other-farm row from every past render's data # stacked on screen forever. row = QWidget() row_layout = QHBoxLayout(row) row_layout.setContentsMargins(0, 0, 0, 0) name_lbl = QLabel(name) value_lbl = QLabel(value) row_layout.addWidget(name_lbl) row_layout.addStretch(1) row_layout.addWidget(value_lbl) return row def render_player_status(self, player: Optional[dict]) -> None: if not player: self.player_status_chip.set_value("–") return if player.get("isControllingVehicle"): self.player_status_chip.set_value("im Fahrzeug") elif is_number(player.get("x")) and is_number(player.get("z")): self.player_status_chip.set_value(f"zu Fuß ({round(player['x'])}, {round(player['z'])})") else: self.player_status_chip.set_value("zu Fuß") def render_farm(self, mission: Optional[dict], farmlands: Optional[list], farms: Optional[list]) -> None: clear_layout(self.other_farms_list) clear_layout(self.farmland_list) if not mission: self.name_chip.set_value("–") self.money_chip.set_value("–") self.farmland_count_chip.set_value("–") self.farmland_value_chip.set_value("–") self.other_farms_head.hide() self.farmland_list.addWidget(self._muted("– keine –")) return farm_arr = farms or [] name = mission.get("farmName") or f"Betrieb #{mission.get('farmId', '–')}" if len(farm_arr) > 1: name += f" (von {len(farm_arr)} Betrieben)" self.name_chip.set_value(name) self.money_chip.set_value(format_euro(mission.get("money"))) owned = [ f for f in (farmlands or []) if f.get("isOwned") and f.get("farmId") == mission.get("farmId") ] self.farmland_count_chip.set_value(str(len(owned)) if owned else "–") total_value = sum(f.get("price") or 0 for f in owned) self.farmland_value_chip.set_value(format_euro(total_value) if owned else "–") others = [f for f in farm_arr if f.get("id") != mission.get("farmId")] self.other_farms_head.setVisible(bool(others)) for f in others: self.other_farms_list.addWidget( self._row( f.get("name") or f"Betrieb #{f.get('id')}", format_euro(f.get("money")) if is_number(f.get("money")) else "–", ) ) if not owned: self.farmland_list.addWidget(self._muted("– keine –")) else: for f in owned: self.farmland_list.addWidget( self._row( f.get("name") or f"Grundstück #{f.get('id')}", format_euro(f.get("price")) if is_number(f.get("price")) else "–", ) ) @staticmethod def _muted(text: str) -> QLabel: lbl = QLabel(text) style_role(lbl, "stat-label") return lbl