Files
Fs25-mod-und-dashboard/FS25-WebDashboard/desktop-app/widgets/vehicle_panel.py
T
Fojadrachi e5754cca18 feat: add FS25_FarmHUD mod and FarmHUD desktop companion app
Initial commit: the FS25 Lua mod (live.json data export), the
FS25-WebDashboard project (Node/web dashboard + PySide6 desktop
variant), and FarmHUD (standalone PySide6 companion app with
Discord Rich Presence, settings, and installer).
2026-09-27 15:25:07 +02:00

302 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Selected-vehicle panel: gauges, cruise/motor/load, hours/price, fuel/
damage/dirt bars, own fill units, attached implements (recursive chain),
60s speed sparkline. Mirrors `renderSelectedVehicle` + friends in app.js."""
from __future__ import annotations
from typing import Any, Optional
from PySide6.QtCore import Signal
from PySide6.QtWidgets import (
QHBoxLayout,
QLabel,
QPushButton,
QVBoxLayout,
QWidget,
)
from formatting import format_euro, format_number, is_drivable, is_number
from state import AppState
from widgets.common import CollapsiblePanel, LabeledBar, StatChip, clear_layout, style_role
from widgets.gauge import GaugeWidget
from widgets.speed_history import SpeedHistoryWidget
class FillUnitRow(LabeledBar):
pass
class ImplementCard(QWidget):
def __init__(self, name: str, depth: int, parent=None) -> None:
super().__init__(parent)
layout = QVBoxLayout(self)
layout.setContentsMargins(depth * 16, 4, 0, 4)
layout.setSpacing(4)
prefix = "↳ " if depth > 0 else ""
name_label = QLabel(prefix + name)
name_label.setStyleSheet("font-weight: 600;")
layout.addWidget(name_label)
self.body_layout = layout
def add_no_tank(self) -> None:
lbl = QLabel("kein Tank")
style_role(lbl, "stat-label")
self.body_layout.addWidget(lbl)
def add_fill_bar(self, name: str, level: float, capacity: float, accent: str) -> None:
pct = min(100.0, max(0.0, (level / capacity) * 100)) if capacity else 0.0
bar = LabeledBar(name, accent)
bar.set_value(f"{round(level)} / {round(capacity)} l ({round(pct)}%)", pct)
self.body_layout.addWidget(bar)
class VehiclePanel(CollapsiblePanel):
vehicle_selected = Signal(object) # emitted for the "follow my vehicle" button
follow_requested = Signal()
ACCENT_SPEED = "#4da3ff"
ACCENT_RPM = "#ff9f4d"
FUEL_COLOR = "#4da3ff"
DAMAGE_COLOR = "#ff5d5d"
DIRT_COLOR = "#9a7b4f"
def __init__(self, parent=None) -> None:
super().__init__("vehicle", "Kein Fahrzeug ausgewählt", parent)
self.header.sub_label.setText("")
self.follow_btn = QPushButton("📍 Mein Fahrzeug")
self.follow_btn.setProperty("flat", "true")
self.follow_btn.clicked.connect(self.follow_requested.emit)
self.header.insert_extra(self.follow_btn)
self.manual_hint = QLabel(
"🔒 manuell ausgewählt – klicke „Mein Fahrzeug“ um wieder automatisch zu folgen"
)
style_role(self.manual_hint, "stat-label")
self.manual_hint.setWordWrap(True)
self.manual_hint.hide()
self.body_layout.addWidget(self.manual_hint)
gauge_row = QHBoxLayout()
self.speed_gauge = GaugeWidget("km/h", "Geschwindigkeit", self.ACCENT_SPEED)
self.rpm_gauge = GaugeWidget("U/min", "Drehzahl", self.ACCENT_RPM)
gauge_row.addWidget(self.speed_gauge)
gauge_row.addWidget(self.rpm_gauge)
self.body_layout.addLayout(gauge_row)
stat_row_1 = QHBoxLayout()
self.cruise_chip = StatChip("Tempomat")
self.motor_chip = StatChip("Motor")
self.load_chip = StatChip("Last")
for chip in (self.cruise_chip, self.motor_chip, self.load_chip):
stat_row_1.addWidget(chip)
self.body_layout.addLayout(stat_row_1)
stat_row_2 = QHBoxLayout()
self.hours_chip = StatChip("Betriebsstunden")
self.price_chip = StatChip("Neuwert")
self.sell_price_chip = StatChip("Verkaufswert")
for chip in (self.hours_chip, self.price_chip, self.sell_price_chip):
stat_row_2.addWidget(chip)
self.body_layout.addLayout(stat_row_2)
stat_row_3 = QHBoxLayout()
self.position_chip = StatChip("Position")
self.heading_chip = StatChip("Ausrichtung")
for chip in (self.position_chip, self.heading_chip):
stat_row_3.addWidget(chip)
self.body_layout.addLayout(stat_row_3)
bar_row = QHBoxLayout()
self.fuel_bar = LabeledBar("Tank", self.FUEL_COLOR)
self.damage_bar = LabeledBar("Schaden", self.DAMAGE_COLOR)
self.dirt_bar = LabeledBar("Verschmutzung", self.DIRT_COLOR)
for bar in (self.fuel_bar, self.damage_bar, self.dirt_bar):
bar_row.addWidget(bar)
self.body_layout.addLayout(bar_row)
self.body_layout.addWidget(self._section_label("Eigene Ladung / Behälter"))
self.own_fill_container = QVBoxLayout()
self.own_fill_container.setSpacing(6)
self.body_layout.addLayout(self.own_fill_container)
self.body_layout.addWidget(self._section_label("Angekoppelte Geräte"))
self.implements_container = QVBoxLayout()
self.implements_container.setSpacing(6)
self.body_layout.addLayout(self.implements_container)
self.body_layout.addWidget(self._section_label("Geschwindigkeit (letzte 60s)"))
self.speed_history_widget = SpeedHistoryWidget(self.ACCENT_SPEED)
self.body_layout.addWidget(self.speed_history_widget)
self.render_selected(None, [], AppState())
@staticmethod
def _section_label(text: str) -> QLabel:
lbl = QLabel(text)
style_role(lbl, "stat-label")
return lbl
def render_selected(self, vehicle: Optional[dict], vehicles: list[dict], app_state: AppState) -> None:
self.manual_hint.setVisible(not app_state.follow_my_vehicle)
self.follow_btn.setProperty("active", "true" if app_state.follow_my_vehicle else "false")
self.follow_btn.style().unpolish(self.follow_btn)
self.follow_btn.style().polish(self.follow_btn)
if vehicle is None:
self.header.title_label.setText("Kein Fahrzeug ausgewählt")
self.header.sub_label.setText("")
self.speed_gauge.set_value(0, "0")
self.rpm_gauge.set_value(0, "0")
self.cruise_chip.set_value("aus")
self.motor_chip.set_value("–")
self.load_chip.set_value("–")
self.hours_chip.set_value("–")
self.price_chip.set_value("–")
self.sell_price_chip.set_value("–")
self.position_chip.set_value("–")
self.heading_chip.set_value("–")
self.fuel_bar.set_value("–", 0)
self.damage_bar.set_value("–", 0)
self.dirt_bar.set_value("–", 0)
self._render_own_fill_units(None)
self._render_implements(None, vehicles)
self.speed_history_widget.set_points([])
return
self.header.title_label.setText(vehicle.get("name") or "Unbenannt")
self.header.sub_label.setText(vehicle.get("typeName") or "")
vehicle_is_drivable = is_drivable(vehicle)
speed = max(0.0, vehicle.get("speedKmh") or 0.0) if vehicle_is_drivable and is_number(vehicle.get("speedKmh")) else 0.0
speed_max = max(60.0, ((int(speed) // 20) + 1) * 20)
self.speed_gauge.set_value(speed / speed_max if speed_max else 0, format_number(speed, 0) if vehicle_is_drivable else "–")
rpm_fraction = (vehicle.get("rpmPercent") or 0) / 100 if is_number(vehicle.get("rpmPercent")) else 0
self.rpm_gauge.set_value(rpm_fraction, format_number(vehicle.get("rpm"), 0))
if vehicle.get("cruiseControlActive"):
max_text = ""
if is_number(vehicle.get("cruiseControlMaxSpeed")):
max_text = f" (max {format_number(vehicle.get('cruiseControlMaxSpeed'), 0)})"
self.cruise_chip.set_value(
f"an · {format_number(vehicle.get('cruiseControlSpeed'), 0)} km/h{max_text}"
)
else:
self.cruise_chip.set_value("aus")
motor_started = vehicle.get("motorStarted")
if isinstance(motor_started, bool):
self.motor_chip.set_value("läuft" if motor_started else "aus")
else:
self.motor_chip.set_value("–")
load = vehicle.get("motorLoadPercent")
self.load_chip.set_value(f"{round(load)}%" if is_number(load) else "–")
fuel_level = vehicle.get("fuelLevel")
fuel_capacity = vehicle.get("fuelCapacity")
if is_number(fuel_level) and is_number(fuel_capacity) and fuel_capacity > 0:
pct = (fuel_level / fuel_capacity) * 100
fuel_units = vehicle.get("fillUnits") or []
fuel_unit = next((fu for fu in fuel_units if fu.get("fillTypeId") == vehicle.get("fuelType")), None)
fuel_name = f"{fuel_unit.get('fillTypeName')}: " if fuel_unit and fuel_unit.get("fillTypeName") else ""
self.fuel_bar.set_value(
f"{fuel_name}{round(fuel_level)} / {round(fuel_capacity)} l ({round(pct)}%)", pct
)
else:
self.fuel_bar.set_value("–", 0)
damage = vehicle.get("damagePercent")
if is_number(damage):
pct = min(100.0, max(0.0, damage))
self.damage_bar.set_value(f"{round(pct)}%", pct)
else:
self.damage_bar.set_value("–", 0)
dirt = vehicle.get("dirtPercent")
if is_number(dirt):
pct = min(100.0, max(0.0, dirt))
self.dirt_bar.set_value(f"{round(pct)}%", pct)
else:
self.dirt_bar.set_value("–", 0)
hours = vehicle.get("operatingHours")
self.hours_chip.set_value(f"{format_number(hours, 1)} h" if is_number(hours) else "–")
self.price_chip.set_value(format_euro(vehicle.get("price")))
self.sell_price_chip.set_value(format_euro(vehicle.get("sellPrice")))
x, z = vehicle.get("x"), vehicle.get("z")
self.position_chip.set_value(f"{round(x)}, {round(z)}" if is_number(x) and is_number(z) else "–")
heading = vehicle.get("headingRad")
if is_number(heading):
import math
deg = ((heading * 180 / math.pi) % 360 + 360) % 360
self.heading_chip.set_value(f"{round(deg)}°")
else:
self.heading_chip.set_value("–")
self._render_own_fill_units(vehicle)
self._render_implements(vehicle, vehicles)
app_state.push_speed_history(speed)
self.speed_history_widget.set_points(list(app_state.speed_history))
def _render_own_fill_units(self, vehicle: Optional[dict]) -> None:
clear_layout(self.own_fill_container)
fill_units = (vehicle.get("fillUnits") if vehicle else None) or []
fuel_type = vehicle.get("fuelType") if vehicle else None
relevant = [
fu
for fu in fill_units
if is_number(fu.get("capacity")) and fu.get("capacity") > 0
and not (fuel_type is not None and fu.get("fillTypeId") == fuel_type)
]
if not relevant:
lbl = self._section_label("– keine –")
self.own_fill_container.addWidget(lbl)
return
for fu in relevant:
level = fu.get("fillLevel") if is_number(fu.get("fillLevel")) else 0
capacity = fu.get("capacity")
pct = min(100.0, max(0.0, (level / capacity) * 100)) if capacity else 0.0
bar = LabeledBar(fu.get("fillTypeName") or "Unbekannt", self.ACCENT_SPEED)
bar.set_value(f"{round(level)} / {round(capacity)} l ({round(pct)}%)", pct)
self.own_fill_container.addWidget(bar)
def _render_implements(self, vehicle: Optional[dict], vehicles: list[dict]) -> None:
clear_layout(self.implements_container)
root_ids = (vehicle.get("attachedImplementIds") if vehicle else None) or []
if not root_ids:
self.implements_container.addWidget(self._section_label("– keine –"))
return
by_id = {v.get("id"): v for v in vehicles}
visited = {vehicle.get("id")} if vehicle else set()
def append_chain(implement_id: Any, depth: int) -> None:
if implement_id in visited:
return
visited.add(implement_id)
implement = by_id.get(implement_id)
card = ImplementCard(implement.get("name") if implement and implement.get("name") else "Unbekanntes Gerät", depth)
fill_units = (implement.get("fillUnits") if implement else None) or []
relevant = [fu for fu in fill_units if is_number(fu.get("capacity")) and fu.get("capacity") > 0]
if not relevant:
card.add_no_tank()
else:
for fu in relevant:
level = fu.get("fillLevel") if is_number(fu.get("fillLevel")) else 0
card.add_fill_bar(fu.get("fillTypeName") or "Unbekannt", level, fu.get("capacity"), self.ACCENT_SPEED)
self.implements_container.addWidget(card)
child_ids = (implement.get("attachedImplementIds") if implement else None) or []
for child_id in child_ids:
append_chain(child_id, depth + 1)
for root_id in root_ids:
append_chain(root_id, 0)