K65 Studio: Linux control app for the Corsair K65 PLUS Wireless
Bragi protocol over hidraw, device service with heartbeat and notifications, PySide6 UI with animated preview, profiles, tray, autostart, udev rule and installer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
7e276912da
23 files changed
+2631
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,450 @@
|
||||
"""Main window: device view on top, settings pages below (iCUE-style)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
from PySide6.QtCore import Qt, QTimer, Signal
|
||||
from PySide6.QtGui import QColor
|
||||
from PySide6.QtWidgets import (
|
||||
QButtonGroup, QCheckBox, QColorDialog, QComboBox, QFrame, QGridLayout, QHBoxLayout,
|
||||
QInputDialog, QLabel, QListWidget, QListWidgetItem, QMainWindow, QMessageBox,
|
||||
QProgressBar, QPushButton, QSlider, QSpinBox, QStackedWidget, QVBoxLayout, QWidget,
|
||||
)
|
||||
|
||||
from .. import autostart
|
||||
from ..device import DeviceState
|
||||
from ..effects import EFFECTS, SPEED_LABELS
|
||||
from ..profiles import Config, Profile
|
||||
from .preview import KeyboardPreview
|
||||
|
||||
PRESETS = ["#ff0000", "#ff7a00", "#ffd400", "#3bff3b", "#19d3c5", "#0077ff", "#7a2cff", "#ff2d95", "#ffffff"]
|
||||
|
||||
|
||||
def _card() -> tuple[QFrame, QVBoxLayout]:
|
||||
frame = QFrame(objectName="card")
|
||||
lay = QVBoxLayout(frame)
|
||||
lay.setContentsMargins(18, 16, 18, 16)
|
||||
lay.setSpacing(10)
|
||||
return frame, lay
|
||||
|
||||
|
||||
def _label(text: str, name: str = "") -> QLabel:
|
||||
lbl = QLabel(text)
|
||||
if name:
|
||||
lbl.setObjectName(name)
|
||||
lbl.setWordWrap(True)
|
||||
return lbl
|
||||
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
profile_changed = Signal(object) # Profile to send to the keyboard
|
||||
reinit_requested = Signal()
|
||||
|
||||
def __init__(self, config: Config):
|
||||
super().__init__()
|
||||
self.config = config
|
||||
self.state = DeviceState()
|
||||
self._loading = False
|
||||
self._save_timer = QTimer(self, singleShot=True, interval=600, timeout=self.config.save)
|
||||
|
||||
self.setWindowTitle("K65 Studio")
|
||||
self.resize(1240, 900)
|
||||
|
||||
root = QWidget()
|
||||
outer = QVBoxLayout(root)
|
||||
outer.setContentsMargins(0, 0, 0, 0)
|
||||
outer.setSpacing(0)
|
||||
outer.addWidget(self._build_header())
|
||||
|
||||
body = QHBoxLayout()
|
||||
body.setContentsMargins(16, 16, 16, 16)
|
||||
body.setSpacing(16)
|
||||
self.nav = QListWidget(objectName="nav")
|
||||
self.nav.setFixedWidth(200)
|
||||
for name in ("Beleuchtung", "Leistung", "Einstellungen", "Geräteinfo"):
|
||||
self.nav.addItem(name)
|
||||
body.addWidget(self.nav)
|
||||
|
||||
right = QVBoxLayout()
|
||||
right.setSpacing(16)
|
||||
preview_card, preview_lay = _card()
|
||||
self.preview = KeyboardPreview()
|
||||
preview_lay.addWidget(self.preview, 1)
|
||||
preview_lay.addWidget(_label("Vorschau (simuliert). Die Tastatur berechnet den Effekt selbst.", "hint"))
|
||||
right.addWidget(preview_card, 4)
|
||||
|
||||
self.pages = QStackedWidget()
|
||||
self.pages.addWidget(self._build_lighting())
|
||||
self.pages.addWidget(self._build_performance())
|
||||
self.pages.addWidget(self._build_settings())
|
||||
self.pages.addWidget(self._build_info())
|
||||
right.addWidget(self.pages, 5)
|
||||
body.addLayout(right, 1)
|
||||
outer.addLayout(body, 1)
|
||||
self.setCentralWidget(root)
|
||||
|
||||
self.nav.currentRowChanged.connect(self.pages.setCurrentIndex)
|
||||
self.nav.setCurrentRow(0)
|
||||
self._reload_profiles()
|
||||
self.set_state(self.state)
|
||||
|
||||
# ---------- header ----------
|
||||
|
||||
def _build_header(self) -> QFrame:
|
||||
header = QFrame(objectName="header")
|
||||
lay = QHBoxLayout(header)
|
||||
lay.setContentsMargins(20, 12, 20, 12)
|
||||
lay.setSpacing(10)
|
||||
lay.addWidget(_label("K65 STUDIO", "title"))
|
||||
lay.addSpacing(24)
|
||||
lay.addWidget(_label("Profil", "muted"))
|
||||
self.profile_box = QComboBox()
|
||||
self.profile_box.setMinimumWidth(180)
|
||||
self.profile_box.currentTextChanged.connect(self._select_profile)
|
||||
lay.addWidget(self.profile_box)
|
||||
for text, slot in (("Neu", self._new_profile), ("Umbenennen", self._rename_profile),
|
||||
("Löschen", self._delete_profile)):
|
||||
b = QPushButton(text)
|
||||
b.clicked.connect(slot)
|
||||
lay.addWidget(b)
|
||||
lay.addStretch(1)
|
||||
self.conn_chip = QLabel(objectName="chip")
|
||||
self.battery_chip = QLabel(objectName="chip")
|
||||
lay.addWidget(self.conn_chip)
|
||||
lay.addWidget(self.battery_chip)
|
||||
return header
|
||||
|
||||
# ---------- pages ----------
|
||||
|
||||
def _build_lighting(self) -> QWidget:
|
||||
page = QWidget()
|
||||
lay = QHBoxLayout(page)
|
||||
lay.setContentsMargins(0, 0, 0, 0)
|
||||
lay.setSpacing(16)
|
||||
|
||||
fx_card, fx_lay = _card()
|
||||
fx_lay.addWidget(_label("Effekt", "section"))
|
||||
self.effect_list = QListWidget()
|
||||
for key, effect in EFFECTS.items():
|
||||
item = QListWidgetItem(effect.label)
|
||||
item.setData(Qt.UserRole, key)
|
||||
self.effect_list.addItem(item)
|
||||
self.effect_list.currentItemChanged.connect(self._effect_selected)
|
||||
fx_lay.addWidget(self.effect_list)
|
||||
fx_card.setFixedWidth(260)
|
||||
lay.addWidget(fx_card)
|
||||
|
||||
opt_card, opt = _card()
|
||||
opt.addWidget(_label("Farben", "section"))
|
||||
colors = QHBoxLayout()
|
||||
self.color_buttons = []
|
||||
for i in range(2):
|
||||
b = QPushButton(objectName="color")
|
||||
b.clicked.connect(lambda _=False, i=i: self._pick_color(i))
|
||||
colors.addWidget(b)
|
||||
self.color_buttons.append(b)
|
||||
self.random_box = QCheckBox("Zufällige Farben")
|
||||
self.random_box.toggled.connect(lambda v: self._update(random_colors=v))
|
||||
colors.addSpacing(12)
|
||||
colors.addWidget(self.random_box)
|
||||
colors.addStretch(1)
|
||||
opt.addLayout(colors)
|
||||
|
||||
swatches = QHBoxLayout()
|
||||
swatches.setSpacing(6)
|
||||
swatches.addWidget(_label("Schnellwahl:", "muted"))
|
||||
for c in PRESETS:
|
||||
s = QPushButton(objectName="swatch")
|
||||
s.setStyleSheet(f"background: {c}; border: 2px solid #2c313a;")
|
||||
s.setToolTip(c)
|
||||
s.clicked.connect(lambda _=False, c=c: self._set_color(0, c))
|
||||
swatches.addWidget(s)
|
||||
swatches.addStretch(1)
|
||||
opt.addLayout(swatches)
|
||||
self.colors_hint = _label("", "hint")
|
||||
opt.addWidget(self.colors_hint)
|
||||
|
||||
opt.addWidget(_label("Geschwindigkeit", "section"))
|
||||
speeds = QHBoxLayout()
|
||||
self.speed_group = QButtonGroup(self)
|
||||
for key, text in SPEED_LABELS.items():
|
||||
b = QPushButton(text, checkable=True)
|
||||
b.setProperty("speed", key)
|
||||
self.speed_group.addButton(b)
|
||||
speeds.addWidget(b)
|
||||
speeds.addStretch(1)
|
||||
self.speed_group.buttonClicked.connect(lambda b: self._update(speed=b.property("speed")))
|
||||
opt.addLayout(speeds)
|
||||
|
||||
opt.addWidget(_label("Helligkeit", "section"))
|
||||
bright = QHBoxLayout()
|
||||
self.brightness = QSlider(Qt.Horizontal, minimum=0, maximum=100, singleStep=5, pageStep=10)
|
||||
self.brightness_value = _label("", "muted")
|
||||
self.brightness_value.setFixedWidth(48)
|
||||
self.brightness.valueChanged.connect(self._brightness_changed)
|
||||
bright.addWidget(self.brightness, 1)
|
||||
bright.addWidget(self.brightness_value)
|
||||
opt.addLayout(bright)
|
||||
opt.addStretch(1)
|
||||
lay.addWidget(opt_card, 1)
|
||||
return page
|
||||
|
||||
def _build_performance(self) -> QWidget:
|
||||
card, lay = _card()
|
||||
lay.addWidget(_label("Spielmodus", "section"))
|
||||
lay.addWidget(_label("Wenn der Spielmodus aktiv ist, sperrt die Tastatur die ausgewählten "
|
||||
"Tasten und Kombinationen, damit sie im Spiel nicht versehentlich auslösen.", "muted"))
|
||||
self.game_mode = QCheckBox("Spielmodus aktivieren")
|
||||
self.game_mode.toggled.connect(self._game_mode_changed)
|
||||
lay.addWidget(self.game_mode)
|
||||
self.locks = {
|
||||
"lock_win": QCheckBox("Windows-/Super-Taste sperren"),
|
||||
"lock_alt_tab": QCheckBox("Alt + Tab sperren"),
|
||||
"lock_alt_f4": QCheckBox("Alt + F4 sperren"),
|
||||
"lock_shift_tab": QCheckBox("Shift + Tab sperren"),
|
||||
}
|
||||
for name, box in self.locks.items():
|
||||
box.toggled.connect(lambda v, name=name: self._update(**{name: v}))
|
||||
box.setContentsMargins(24, 0, 0, 0)
|
||||
lay.addWidget(box)
|
||||
lay.addStretch(1)
|
||||
return card
|
||||
|
||||
def _build_settings(self) -> QWidget:
|
||||
card, lay = _card()
|
||||
lay.addWidget(_label("Energie", "section"))
|
||||
row = QHBoxLayout()
|
||||
row.addWidget(QLabel("Ruhezustand nach"))
|
||||
self.sleep = QSpinBox(minimum=1, maximum=120, suffix=" min")
|
||||
self.sleep.valueChanged.connect(lambda v: self._update(sleep_minutes=v))
|
||||
row.addWidget(self.sleep)
|
||||
row.addWidget(QLabel("ohne Eingabe (gilt im Funkbetrieb)", objectName="muted"))
|
||||
row.addStretch(1)
|
||||
lay.addLayout(row)
|
||||
|
||||
lay.addWidget(_label("Programm", "section"))
|
||||
self.autostart = QCheckBox("Beim Anmelden automatisch starten (im Tray)")
|
||||
self.autostart.setChecked(autostart.is_enabled())
|
||||
self.autostart.toggled.connect(autostart.set_enabled)
|
||||
lay.addWidget(self.autostart)
|
||||
self.start_min = QCheckBox("Minimiert starten")
|
||||
self.start_min.setChecked(self.config.start_minimized)
|
||||
self.start_min.toggled.connect(lambda v: self._set_config(start_minimized=v))
|
||||
lay.addWidget(self.start_min)
|
||||
self.restore_hw = QCheckBox("Beim Beenden an die Tastatur zurückgeben (Hardware-Beleuchtung)")
|
||||
self.restore_hw.setChecked(self.config.restore_hardware_on_exit)
|
||||
self.restore_hw.toggled.connect(lambda v: self._set_config(restore_hardware_on_exit=v))
|
||||
lay.addWidget(self.restore_hw)
|
||||
|
||||
reinit = QPushButton("Tastatur neu initialisieren")
|
||||
reinit.clicked.connect(self.reinit_requested)
|
||||
row = QHBoxLayout()
|
||||
row.addWidget(reinit)
|
||||
row.addStretch(1)
|
||||
lay.addSpacing(8)
|
||||
lay.addLayout(row)
|
||||
lay.addStretch(1)
|
||||
return card
|
||||
|
||||
def _build_info(self) -> QWidget:
|
||||
card, lay = _card()
|
||||
lay.addWidget(_label("CORSAIR K65 PLUS WIRELESS", "section"))
|
||||
grid = QGridLayout()
|
||||
grid.setHorizontalSpacing(24)
|
||||
grid.setVerticalSpacing(8)
|
||||
self.info = {}
|
||||
for i, (key, text) in enumerate((("conn", "Verbindung"), ("serial", "Seriennummer"),
|
||||
("kb_fw", "Firmware Tastatur"), ("rx_fw", "Firmware Empfänger"),
|
||||
("battery", "Akku"))):
|
||||
grid.addWidget(_label(text, "muted"), i, 0)
|
||||
value = QLabel("–")
|
||||
value.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
||||
grid.addWidget(value, i, 1)
|
||||
self.info[key] = value
|
||||
self.battery_bar = QProgressBar(maximum=100, textVisible=False)
|
||||
self.battery_bar.setFixedWidth(220)
|
||||
grid.addWidget(self.battery_bar, 5, 1)
|
||||
grid.setColumnStretch(2, 1)
|
||||
lay.addLayout(grid)
|
||||
self.error_label = _label("", "hint")
|
||||
lay.addWidget(self.error_label)
|
||||
lay.addStretch(1)
|
||||
return card
|
||||
|
||||
# ---------- profile handling ----------
|
||||
|
||||
@property
|
||||
def profile(self) -> Profile:
|
||||
return self.config.profile
|
||||
|
||||
def _reload_profiles(self) -> None:
|
||||
self._loading = True
|
||||
self.profile_box.clear()
|
||||
self.profile_box.addItems(list(self.config.profiles))
|
||||
self.profile_box.setCurrentText(self.config.active)
|
||||
self._loading = False
|
||||
self._load_profile_into_ui()
|
||||
|
||||
def _load_profile_into_ui(self) -> None:
|
||||
p = self.profile
|
||||
self._loading = True
|
||||
for i in range(self.effect_list.count()):
|
||||
if self.effect_list.item(i).data(Qt.UserRole) == p.effect:
|
||||
self.effect_list.setCurrentRow(i)
|
||||
self.random_box.setChecked(p.random_colors)
|
||||
for b in self.speed_group.buttons():
|
||||
b.setChecked(b.property("speed") == p.speed)
|
||||
self.brightness.setValue(p.brightness)
|
||||
self.brightness_value.setText(f"{p.brightness} %")
|
||||
self.game_mode.setChecked(p.game_mode)
|
||||
for name, box in self.locks.items():
|
||||
box.setChecked(getattr(p, name))
|
||||
self.sleep.setValue(p.sleep_minutes)
|
||||
self._loading = False
|
||||
self._refresh_controls()
|
||||
self.preview.set_profile(p)
|
||||
|
||||
def _refresh_controls(self) -> None:
|
||||
p = self.profile
|
||||
effect = EFFECTS.get(p.effect, EFFECTS["static"])
|
||||
for i, b in enumerate(self.color_buttons):
|
||||
c = p.colors[i] if i < len(p.colors) else "#ffffff"
|
||||
fg = "#000" if QColor(c).lightness() > 140 else "#fff"
|
||||
b.setStyleSheet(f"background: {c}; color: {fg}; border: 1px solid #3a404a;")
|
||||
b.setText(c.upper())
|
||||
b.setVisible(i < effect.max_colors)
|
||||
b.setEnabled(not (p.random_colors and effect.random_colors))
|
||||
self.random_box.setVisible(effect.random_colors)
|
||||
if effect.max_colors == 0:
|
||||
hints = {"off": "Beleuchtung ist ausgeschaltet.",
|
||||
"watercolor": "Dieser Effekt verwendet feste Farben."}
|
||||
self.colors_hint.setText(hints.get(p.effect, "Dieser Effekt verwendet den Regenbogen."))
|
||||
elif effect.max_colors == 2:
|
||||
self.colors_hint.setText("Zwei Farben, zwischen denen der Effekt wechselt.")
|
||||
else:
|
||||
self.colors_hint.setText("")
|
||||
for b in self.speed_group.buttons():
|
||||
b.setEnabled(effect.speed)
|
||||
for box in self.locks.values():
|
||||
box.setEnabled(p.game_mode)
|
||||
|
||||
def _update(self, **changes) -> None:
|
||||
if self._loading:
|
||||
return
|
||||
self.config.profiles[self.config.active] = replace(self.profile, **changes)
|
||||
self._refresh_controls()
|
||||
self.preview.set_profile(self.profile)
|
||||
self.profile_changed.emit(self.profile)
|
||||
self._save_timer.start()
|
||||
|
||||
def _set_config(self, **changes) -> None:
|
||||
for k, v in changes.items():
|
||||
setattr(self.config, k, v)
|
||||
self._save_timer.start()
|
||||
|
||||
def _select_profile(self, name: str) -> None:
|
||||
if self._loading or not name or name == self.config.active:
|
||||
return
|
||||
self.activate_profile(name)
|
||||
|
||||
def activate_profile(self, name: str) -> None:
|
||||
if name not in self.config.profiles:
|
||||
return
|
||||
self.config.active = name
|
||||
self.config.save()
|
||||
self._reload_profiles()
|
||||
self.profile_changed.emit(self.profile)
|
||||
|
||||
def _ask_name(self, title: str, default: str) -> str | None:
|
||||
name, ok = QInputDialog.getText(self, title, "Name:", text=default)
|
||||
name = name.strip()
|
||||
if not ok or not name:
|
||||
return None
|
||||
if name in self.config.profiles:
|
||||
QMessageBox.warning(self, title, f"Ein Profil „{name}“ gibt es schon.")
|
||||
return None
|
||||
return name
|
||||
|
||||
def _new_profile(self) -> None:
|
||||
name = self._ask_name("Neues Profil", f"Profil {len(self.config.profiles) + 1}")
|
||||
if name:
|
||||
self.config.profiles[name] = replace(self.profile, colors=list(self.profile.colors))
|
||||
self.activate_profile(name)
|
||||
|
||||
def _rename_profile(self) -> None:
|
||||
old = self.config.active
|
||||
name = self._ask_name("Profil umbenennen", old)
|
||||
if name:
|
||||
self.config.profiles = {(name if k == old else k): v for k, v in self.config.profiles.items()}
|
||||
self.config.active = name
|
||||
self.config.save()
|
||||
self._reload_profiles()
|
||||
|
||||
def _delete_profile(self) -> None:
|
||||
if len(self.config.profiles) <= 1:
|
||||
QMessageBox.information(self, "Profil löschen", "Das letzte Profil kann nicht gelöscht werden.")
|
||||
return
|
||||
name = self.config.active
|
||||
if QMessageBox.question(self, "Profil löschen", f"Profil „{name}“ löschen?") != QMessageBox.Yes:
|
||||
return
|
||||
del self.config.profiles[name]
|
||||
self.activate_profile(next(iter(self.config.profiles)))
|
||||
|
||||
# ---------- control callbacks ----------
|
||||
|
||||
def _effect_selected(self, item: QListWidgetItem | None, _prev=None) -> None:
|
||||
if item is not None:
|
||||
self._update(effect=item.data(Qt.UserRole))
|
||||
|
||||
def _set_color(self, index: int, color: str) -> None:
|
||||
colors = list(self.profile.colors) + ["#ffffff"] * (2 - len(self.profile.colors))
|
||||
colors[index] = color
|
||||
self._update(colors=colors, random_colors=False)
|
||||
self.random_box.blockSignals(True)
|
||||
self.random_box.setChecked(False)
|
||||
self.random_box.blockSignals(False)
|
||||
|
||||
def _pick_color(self, index: int) -> None:
|
||||
current = QColor(self.profile.colors[index] if index < len(self.profile.colors) else "#ffffff")
|
||||
color = QColorDialog.getColor(current, self, "Farbe wählen")
|
||||
if color.isValid():
|
||||
self._set_color(index, color.name())
|
||||
|
||||
def _brightness_changed(self, value: int) -> None:
|
||||
self.brightness_value.setText(f"{value} %")
|
||||
self._update(brightness=value)
|
||||
|
||||
def _game_mode_changed(self, value: bool) -> None:
|
||||
self._update(game_mode=value)
|
||||
|
||||
# ---------- device state ----------
|
||||
|
||||
def set_state(self, s: DeviceState) -> None:
|
||||
self.state = s
|
||||
self.preview.set_connected(s.keyboard)
|
||||
if s.keyboard:
|
||||
mode = "Funk (Slipstream)" if s.wireless else "USB-Kabel"
|
||||
self.conn_chip.setText(f"● Verbunden · {mode}")
|
||||
self.conn_chip.setStyleSheet("color: #3ddc84;")
|
||||
elif s.receiver:
|
||||
self.conn_chip.setText("● Empfänger bereit · Tastatur schläft/aus")
|
||||
self.conn_chip.setStyleSheet("color: #f5b942;")
|
||||
else:
|
||||
self.conn_chip.setText("● Nicht verbunden")
|
||||
self.conn_chip.setStyleSheet("color: #ef5350;")
|
||||
if s.battery is not None:
|
||||
self.battery_chip.setText(f"Akku {s.battery} %")
|
||||
self.battery_chip.setStyleSheet("color: #ef5350;" if s.battery <= 15 else "")
|
||||
self.battery_chip.show()
|
||||
else:
|
||||
self.battery_chip.hide()
|
||||
|
||||
self.info["conn"].setText(self.conn_chip.text().lstrip("● ").strip())
|
||||
self.info["serial"].setText(s.serial or "–")
|
||||
self.info["kb_fw"].setText(s.keyboard_firmware or "–")
|
||||
self.info["rx_fw"].setText(s.receiver_firmware or ("–" if s.wireless else "nicht vorhanden (Kabel)"))
|
||||
self.info["battery"].setText(f"{s.battery} %" if s.battery is not None else "–")
|
||||
self.battery_bar.setValue(s.battery or 0)
|
||||
self.error_label.setText(s.error)
|
||||
self.error_label.setStyleSheet("color: #ef5350;" if s.error else "")
|
||||
@@ -0,0 +1,217 @@
|
||||
"""Animated keyboard preview that approximates the hardware effects."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import colorsys
|
||||
import math
|
||||
import random
|
||||
import time
|
||||
|
||||
from PySide6.QtCore import QRectF, Qt, QTimer
|
||||
from PySide6.QtGui import QColor, QFont, QPainter, QPainterPath, QPen
|
||||
from PySide6.QtWidgets import QWidget
|
||||
|
||||
from .. import layout
|
||||
from ..profiles import Profile
|
||||
|
||||
SPEED_FACTOR = {"slow": 0.5, "medium": 1.0, "fast": 1.8}
|
||||
RGB = tuple[float, float, float]
|
||||
|
||||
|
||||
def _hex(c: str) -> RGB:
|
||||
q = QColor(c)
|
||||
return q.redF(), q.greenF(), q.blueF()
|
||||
|
||||
|
||||
def _hue(h: float) -> RGB:
|
||||
return colorsys.hsv_to_rgb(h % 1.0, 1.0, 1.0)
|
||||
|
||||
|
||||
def _mix(a: RGB, b: RGB, t: float) -> RGB:
|
||||
return tuple(x + (y - x) * t for x, y in zip(a, b))
|
||||
|
||||
|
||||
def _scale(a: RGB, f: float) -> RGB:
|
||||
return tuple(x * f for x in a)
|
||||
|
||||
|
||||
class KeyboardPreview(QWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setMinimumHeight(220)
|
||||
self.profile = Profile()
|
||||
self.connected = True
|
||||
self._t0 = time.monotonic()
|
||||
self._presses: list[tuple[float, layout.Key, RGB]] = []
|
||||
self._next_press = 0.0
|
||||
self._rand = random.Random(7)
|
||||
self._palette_seed = [self._rand.random() for _ in range(64)]
|
||||
self._timer = QTimer(self, interval=33, timeout=self.update)
|
||||
self._timer.start()
|
||||
|
||||
def set_profile(self, profile: Profile) -> None:
|
||||
self.profile = profile
|
||||
self.update()
|
||||
|
||||
def set_connected(self, connected: bool) -> None:
|
||||
self.connected = connected
|
||||
self.update()
|
||||
|
||||
def showEvent(self, e):
|
||||
self._timer.start()
|
||||
super().showEvent(e)
|
||||
|
||||
def hideEvent(self, e):
|
||||
self._timer.stop()
|
||||
super().hideEvent(e)
|
||||
|
||||
# --- effect simulation ---
|
||||
|
||||
def _colors(self) -> tuple[RGB, RGB]:
|
||||
cs = self.profile.colors + ["#ffffff", "#ffffff"]
|
||||
return _hex(cs[0]), _hex(cs[1])
|
||||
|
||||
def _random(self, n: int) -> RGB:
|
||||
return _hue(self._palette_seed[n % len(self._palette_seed)])
|
||||
|
||||
def _key_color(self, k: layout.Key, t: float) -> RGB:
|
||||
p = self.profile
|
||||
s = SPEED_FACTOR.get(p.speed, 1.0)
|
||||
c0, c1 = self._colors()
|
||||
rnd = p.random_colors
|
||||
x, y = k.cx / layout.WIDTH, k.cy / layout.HEIGHT
|
||||
|
||||
match p.effect:
|
||||
case "off":
|
||||
return (0, 0, 0)
|
||||
case "static":
|
||||
return c0
|
||||
case "rainbow_wave":
|
||||
return _hue(x - t * 0.25 * s)
|
||||
case "spiral_rainbow":
|
||||
a = math.atan2(y - 0.5, (x - 0.5) * 2.5) / (2 * math.pi)
|
||||
return _hue(a + t * 0.2 * s)
|
||||
case "color_wave":
|
||||
phase = x - t * 0.3 * s
|
||||
n = math.floor(phase)
|
||||
band = 0.5 + 0.5 * math.cos((phase - n) * 2 * math.pi)
|
||||
col = self._random(-n) if rnd else (c0 if n % 2 == 0 else c1)
|
||||
return _scale(col, band ** 2)
|
||||
case "color_shift":
|
||||
phase = t * 0.25 * s
|
||||
n = math.floor(phase)
|
||||
a = self._random(n) if rnd else (c0 if n % 2 == 0 else c1)
|
||||
b = self._random(n + 1) if rnd else (c1 if n % 2 == 0 else c0)
|
||||
return _mix(a, b, 0.5 - 0.5 * math.cos((phase - n) * math.pi))
|
||||
case "color_pulse":
|
||||
phase = t * 0.5 * s
|
||||
n = math.floor(phase)
|
||||
col = self._random(n) if rnd else (c0 if n % 2 == 0 else c1)
|
||||
return _scale(col, math.sin((phase - n) * math.pi) ** 2)
|
||||
case "rain":
|
||||
col_i = int(k.cx * 2)
|
||||
seed = self._palette_seed[col_i % 64]
|
||||
head = ((t * 0.5 * s + seed * 3) % 1.6) * layout.HEIGHT - 1
|
||||
d = head - k.cy
|
||||
lvl = max(0.0, 1 - d / 2.0) if d >= 0 else 0.0
|
||||
col = self._random(col_i) if rnd else (c0 if col_i % 2 else c1)
|
||||
return _scale(col, lvl)
|
||||
case "watercolor":
|
||||
h = 0.5 + 0.25 * math.sin(x * 4 + t * 0.6 * s) + 0.25 * math.sin(y * 5 - t * 0.4 * s)
|
||||
return colorsys.hsv_to_rgb(h % 1.0, 0.55, 1.0)
|
||||
case "type_key" | "type_ripple":
|
||||
return self._typing(k, t, c0, c1)
|
||||
case "visor":
|
||||
pos = (math.sin(t * 1.2 * s) * 0.5 + 0.5) * layout.WIDTH
|
||||
lvl = max(0.0, 1 - abs(k.cx - pos) / 2.5)
|
||||
col = self._random(int(t * 1.2 * s / math.pi)) if rnd else c0
|
||||
return _scale(col, lvl)
|
||||
return c0
|
||||
|
||||
def _typing(self, k: layout.Key, t: float, c0: RGB, c1: RGB) -> RGB:
|
||||
level = 0.0
|
||||
best: RGB = (0, 0, 0)
|
||||
for start, key, col in self._presses:
|
||||
age = t - start
|
||||
if self.profile.effect == "type_key":
|
||||
if key is k:
|
||||
lvl = max(0.0, 1 - age / 1.2)
|
||||
else:
|
||||
continue
|
||||
else:
|
||||
d = math.hypot(k.cx - key.cx, k.cy - key.cy)
|
||||
front = age * 9 * SPEED_FACTOR.get(self.profile.speed, 1.0)
|
||||
lvl = max(0.0, 1 - abs(d - front) / 1.2) * max(0.0, 1 - age / 1.6)
|
||||
if lvl > level:
|
||||
level, best = lvl, col
|
||||
return _scale(best, level)
|
||||
|
||||
def _update_presses(self, t: float) -> None:
|
||||
if self.profile.effect not in ("type_key", "type_ripple"):
|
||||
self._presses.clear()
|
||||
return
|
||||
if t >= self._next_press:
|
||||
key = self._rand.choice(layout.KEYS)
|
||||
c0, c1 = self._colors()
|
||||
col = _hue(self._rand.random()) if self.profile.random_colors else c0
|
||||
self._presses.append((t, key, col))
|
||||
self._next_press = t + self._rand.uniform(0.15, 0.6)
|
||||
self._presses = [p for p in self._presses if t - p[0] < 2.0]
|
||||
|
||||
# --- painting ---
|
||||
|
||||
def paintEvent(self, _):
|
||||
t = time.monotonic() - self._t0
|
||||
self._update_presses(t)
|
||||
p = QPainter(self)
|
||||
p.setRenderHint(QPainter.Antialiasing)
|
||||
|
||||
margin = 18
|
||||
unit = min((self.width() - 2 * margin) / (layout.WIDTH + 0.5),
|
||||
(self.height() - 2 * margin) / (layout.HEIGHT + 0.5))
|
||||
bw, bh = unit * (layout.WIDTH + 0.5), unit * (layout.HEIGHT + 0.5)
|
||||
ox, oy = (self.width() - bw) / 2, (self.height() - bh) / 2
|
||||
|
||||
body = QRectF(ox, oy, bw, bh)
|
||||
p.setPen(QPen(QColor("#2c2f36"), 1.5))
|
||||
p.setBrush(QColor("#15171b"))
|
||||
p.drawRoundedRect(body, unit * 0.35, unit * 0.35)
|
||||
|
||||
bright = self.profile.brightness / 100 if self.connected else 0.0
|
||||
font = QFont(self.font())
|
||||
font.setPixelSize(max(7, int(unit * 0.24)))
|
||||
p.setFont(font)
|
||||
pad = unit * 0.07
|
||||
kx, ky = ox + unit * 0.25, oy + unit * 0.25
|
||||
|
||||
for k in layout.KEYS:
|
||||
r, g, b = _scale(self._key_color(k, t), bright)
|
||||
glow = QColor.fromRgbF(min(r, 1), min(g, 1), min(b, 1))
|
||||
rect = QRectF(kx + k.x * unit + pad, ky + k.y * unit + pad, k.w * unit - 2 * pad, k.h * unit - 2 * pad)
|
||||
if k.iso_enter:
|
||||
top, stem = QPainterPath(), QPainterPath()
|
||||
top.addRoundedRect(QRectF(rect.x(), rect.y(), rect.width(), unit - 2 * pad), 4, 4)
|
||||
stem.addRoundedRect(QRectF(rect.x() + unit * 0.25, rect.y(), rect.width() - unit * 0.25,
|
||||
rect.height()), 4, 4)
|
||||
path = top.united(stem)
|
||||
else:
|
||||
path = QPainterPath()
|
||||
path.addRoundedRect(rect, 4, 4)
|
||||
# Keycap: dark top, the light leaks around the edge like on the real board.
|
||||
p.setPen(QPen(glow, max(1.5, unit * 0.05)))
|
||||
cap = QColor.fromRgbF(0.09 + r * 0.22, 0.10 + g * 0.22, 0.11 + b * 0.22)
|
||||
p.setBrush(cap)
|
||||
p.drawPath(path)
|
||||
if k.label:
|
||||
p.setPen(QColor.fromRgbF(0.35 + min(r, 1) * 0.65, 0.35 + min(g, 1) * 0.65,
|
||||
0.35 + min(b, 1) * 0.65))
|
||||
label_rect = QRectF(rect.x(), rect.y(), rect.width(), unit - 2 * pad)
|
||||
p.drawText(label_rect, Qt.AlignCenter, k.label)
|
||||
|
||||
if not self.connected:
|
||||
p.setPen(QColor("#9aa0aa"))
|
||||
f = QFont(self.font())
|
||||
f.setPixelSize(15)
|
||||
p.setFont(f)
|
||||
p.drawText(body, Qt.AlignCenter, "Tastatur nicht verbunden")
|
||||
p.end()
|
||||
@@ -0,0 +1,56 @@
|
||||
ACCENT = "#19d3c5"
|
||||
|
||||
STYLESHEET = f"""
|
||||
QWidget {{
|
||||
background: #101216;
|
||||
color: #e6e8eb;
|
||||
font-size: 13px;
|
||||
}}
|
||||
QLabel, QCheckBox {{ background: transparent; }}
|
||||
QLabel#title {{ font-size: 18px; font-weight: 600; letter-spacing: 1px; }}
|
||||
QLabel#section {{ font-size: 15px; font-weight: 600; margin-top: 4px; }}
|
||||
QLabel#muted, QLabel#hint {{ color: #8b919c; }}
|
||||
QLabel#chip {{
|
||||
background: #1b1e24; border: 1px solid #2a2e36; border-radius: 11px;
|
||||
padding: 3px 10px;
|
||||
}}
|
||||
QFrame#header {{ background: #0b0d10; border-bottom: 1px solid #22262d; }}
|
||||
QFrame#card {{ background: #161920; border: 1px solid #23272f; border-radius: 10px; }}
|
||||
QListWidget {{
|
||||
background: transparent; border: none; outline: none;
|
||||
}}
|
||||
QListWidget::item {{
|
||||
padding: 9px 12px; border-radius: 6px; margin: 1px 0;
|
||||
}}
|
||||
QListWidget::item:hover {{ background: #1b1f26; }}
|
||||
QListWidget::item:selected {{ background: #1f2a2e; color: {ACCENT}; }}
|
||||
QListWidget#nav::item {{ padding: 12px 14px; font-size: 14px; }}
|
||||
QPushButton {{
|
||||
background: #1d2128; border: 1px solid #2c313a; border-radius: 6px; padding: 7px 14px;
|
||||
}}
|
||||
QPushButton:hover {{ border-color: {ACCENT}; }}
|
||||
QPushButton:checked {{ background: #17393a; border-color: {ACCENT}; color: {ACCENT}; }}
|
||||
QPushButton:disabled {{ color: #5a606a; border-color: #23272e; }}
|
||||
QPushButton#swatch {{ border-radius: 14px; padding: 0; min-width: 28px; min-height: 28px; max-width: 28px; max-height: 28px; }}
|
||||
QPushButton#color {{ min-width: 110px; min-height: 34px; font-weight: 600; }}
|
||||
QComboBox, QSpinBox {{
|
||||
background: #1d2128; border: 1px solid #2c313a; border-radius: 6px; padding: 5px 10px; min-height: 22px;
|
||||
}}
|
||||
QComboBox:hover, QSpinBox:hover {{ border-color: {ACCENT}; }}
|
||||
QComboBox QAbstractItemView {{ background: #1d2128; selection-background-color: #1f2a2e; }}
|
||||
QCheckBox {{ spacing: 10px; padding: 4px 0; }}
|
||||
QCheckBox::indicator {{ width: 18px; height: 18px; border-radius: 4px; border: 1px solid #3a404a; background: #1d2128; }}
|
||||
QCheckBox::indicator:checked {{ background: {ACCENT}; border-color: {ACCENT}; }}
|
||||
QCheckBox:disabled {{ color: #5a606a; }}
|
||||
QSlider::groove:horizontal {{ height: 6px; background: #262a32; border-radius: 3px; }}
|
||||
QSlider::sub-page:horizontal {{ background: {ACCENT}; border-radius: 3px; }}
|
||||
QSlider::handle:horizontal {{ width: 16px; margin: -6px 0; border-radius: 8px; background: #e6e8eb; }}
|
||||
QProgressBar {{ background: #262a32; border: none; border-radius: 4px; height: 8px; text-align: center; }}
|
||||
QProgressBar::chunk {{ background: {ACCENT}; border-radius: 4px; }}
|
||||
QScrollArea {{ border: none; }}
|
||||
QScrollBar:vertical {{ background: transparent; width: 8px; margin: 2px; }}
|
||||
QScrollBar::handle:vertical {{ background: #2c313a; border-radius: 3px; min-height: 30px; }}
|
||||
QScrollBar::handle:vertical:hover {{ background: #3a404a; }}
|
||||
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{ height: 0; }}
|
||||
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{ background: transparent; }}
|
||||
"""
|
||||
Reference in new issue
Block a user