v0.43.0: visualizer presets

Each visualizer style's settings page has a Preset drop-down and a
"Save as Preset…" button. The drop-down lists the built-in Default, your
presets, and "Edit Presets…" (rename in place, delete); it reads "Custom"
whenever the settings match no preset. Presets sync with the preferences;
the per-machine sync delay is never part of one.
This commit is contained in:
2026-10-10 01:41:01 -07:00
parent 8b42580f97
commit 599972fa1a
6 changed files with 499 additions and 2 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
"""LinTunes — iTunes-style music library manager and player for Linux."""
__version__ = "0.42.0"
__version__ = "0.43.0"
+109 -1
View File
@@ -5,7 +5,8 @@ from PyQt6.QtWidgets import (
QRadioButton, QButtonGroup, QLineEdit, QPushButton, QCheckBox,
QFormLayout, QFontComboBox, QListWidget, QScrollArea, QSplitter,
QStackedWidget, QWidget, QDialogButtonBox, QFrame, QToolButton,
QColorDialog, QMenu, QDoubleSpinBox,
QColorDialog, QMenu, QDoubleSpinBox, QComboBox, QInputDialog,
QMessageBox,
)
from PyQt6.QtCore import Qt, QTimer
from PyQt6.QtGui import QFont, QColor
@@ -14,6 +15,7 @@ from lintunes import theme
from lintunes.config import load_config, save_config
from lintunes.gui import visualizer_styles
from lintunes.gui.range_slider import RangeSlider
from lintunes.gui import visualizer_presets as presets
from lintunes.gui import sounds
from lintunes.theme import HIGHLIGHT_COLORS
@@ -35,6 +37,9 @@ _COLOR_ROWS = [
_VALUE_WIDTH = 56
_EDIT_PRESETS = "\0edit" # the preset drop-down's last item
_SPEAKER_KEYS = ("visualizer_laser_low_hz", "visualizer_laser_high_hz")
# The laser's speaker range: 20 Hz – 20 kHz in log steps of 1/30 octave.
SPEAKER_STEPS = 300
@@ -442,11 +447,14 @@ class PreferencesDialog(QDialog):
# Only the chosen style's settings are shown.
self._viz_pages = QStackedWidget()
self._viz_setters = {} # key → puts a value into its control
self._preset_combos = {} # style → its preset drop-down
fade_tip = ("How long what's left behind takes to fade into the "
"background — 0 is no trail")
bars = QWidget()
form = self._viz_form(bars)
self._preset_row(form, "bars")
self._bars_fade_slider, _ = self._viz_slider(
form, "Fade:", "visualizer_bars_fade", config, scale=20,
fmt=_fade_text, tip=fade_tip)
@@ -454,6 +462,7 @@ class PreferencesDialog(QDialog):
radial = QWidget()
form = self._viz_form(radial)
self._preset_row(form, "radial")
self._fade_slider, _ = self._viz_slider(
form, "Fade:", "visualizer_ghost_fade", config, scale=20,
fmt=_fade_text, tip=fade_tip)
@@ -476,6 +485,7 @@ class PreferencesDialog(QDialog):
laser = QWidget()
form = self._viz_form(laser)
self._preset_row(form, "laser")
self._laser_fade_slider, _ = self._viz_slider(
form, "Afterimage:", "visualizer_laser_afterimage", config,
scale=20, fmt=_fade_text,
@@ -559,8 +569,104 @@ class PreferencesDialog(QDialog):
row.addWidget(box)
row.addStretch()
form.addRow("Line width:", row)
self._viz_setters["visualizer_laser_width"] = box.setValue
return box
# ---- Visualizer presets ----
def _preset_row(self, form, style: str):
combo = QComboBox()
combo.setSizeAdjustPolicy(QComboBox.SizeAdjustPolicy.AdjustToContents)
combo.activated.connect(
lambda index, s=style: self._on_preset_chosen(s, index))
save = QPushButton("Save as Preset…")
save.clicked.connect(lambda _=False, s=style: self._save_preset(s))
row = QHBoxLayout()
row.addWidget(combo, stretch=1)
row.addWidget(save)
form.addRow("Preset:", row)
self._preset_combos[style] = combo
self._sync_preset_combo(style)
if len(self._preset_combos) == 1:
# Any change — a slider, a preset applied, an edit on the other
# machine — may make the settings match a preset, or stop.
self._prefs.changed.connect(self._sync_preset_combos)
def _sync_preset_combos(self):
for style in self._preset_combos:
self._sync_preset_combo(style)
def _sync_preset_combo(self, style: str):
"""List the presets, showing the one the settings match — or
"Custom" when they match none (unsaved changes)."""
combo = self._preset_combos[style]
match = presets.matching(self._prefs, style)
combo.blockSignals(True)
combo.clear()
if match is None:
combo.addItem("Custom", None)
for name in presets.names(self._prefs, style):
combo.addItem(name, name)
combo.insertSeparator(combo.count())
combo.addItem("Edit Presets…", _EDIT_PRESETS)
combo.setCurrentIndex(0 if match is None else combo.findData(match))
combo.blockSignals(False)
def _on_preset_chosen(self, style: str, index: int):
choice = self._preset_combos[style].itemData(index)
if choice == _EDIT_PRESETS:
self._edit_presets(style)
elif choice is not None:
self._apply_preset(style, choice)
self._sync_preset_combo(style)
def _apply_preset(self, style: str, name: str):
"""Move the controls to the preset; they save it as if dragged."""
values = presets.values_of(self._prefs, style, name)
for key, value in values.items():
if key not in _SPEAKER_KEYS:
self._viz_setters[key](value)
if style == "laser":
self._speaker.setValues(
_hz_to_step(values["visualizer_laser_low_hz"]),
_hz_to_step(values["visualizer_laser_high_hz"]))
def _save_preset(self, style: str):
suggestion = presets.matching(self._prefs, style) or ""
if suggestion == presets.DEFAULT_NAME:
suggestion = ""
name = (self._ask_preset_name(suggestion) or "").strip()
if not name:
return
if name == presets.DEFAULT_NAME:
self._preset_warning(
f"“{name}” is the built-in preset. Pick another name.")
return
if name in presets.presets(self._prefs, style) and not \
self._confirm_overwrite(name):
return
presets.save(self._prefs, style, name)
self._sync_preset_combo(style)
def _edit_presets(self, style: str):
presets.PresetEditor(self._prefs, style, _VISUALIZER_STYLES[style],
self).exec()
# Overridable, so tests needn't type into dialogs.
def _ask_preset_name(self, suggestion: str) -> str:
name, ok = QInputDialog.getText(self, "Save as Preset", "Name:",
text=suggestion)
return name if ok else ""
def _confirm_overwrite(self, name: str) -> bool:
return QMessageBox.question(
self, "Save as Preset",
f"Replace the preset “{name}” with these settings?") == \
QMessageBox.StandardButton.Yes
def _preset_warning(self, text: str):
QMessageBox.warning(self, "Save as Preset", text)
def _speaker_range(self, form, config) -> RangeSlider:
"""The speaker under the mirror: which frequencies it plays, on a
log scale (each slider step is the same musical interval)."""
@@ -635,6 +741,7 @@ class PreferencesDialog(QDialog):
box.setToolTip(tip)
box.toggled.connect(lambda on: self._prefs.set(key, on))
form.addRow(box)
self._viz_setters[key] = lambda value: box.setChecked(bool(value))
return box
def _viz_slider(self, form, label, key, config, *, scale=1, fmt=str,
@@ -661,6 +768,7 @@ class PreferencesDialog(QDialog):
row.addSpacing(8)
row.addWidget(text)
form.addRow(label, row)
self._viz_setters[key] = lambda value: slider.setValue(round(value * scale))
return slider, text
def _on_viz_style(self, key: str):
+207
View File
@@ -0,0 +1,207 @@
"""Named presets for each visualizer style.
A preset is the style's own settings (``STYLE_KEYS``) under a name, kept in
preferences.json as ``visualizer_presets: {style: {name: {key: value}}}`` —
so presets ride the Syncthing share like the settings themselves. The sync
delay is never part of one: that is this machine's speakers, in config.json.
``Default`` is built in for every style (the settings as shipped), can't be
renamed, deleted or overwritten, and is the way back after experimenting.
"""
import copy
import math
from PyQt6.QtWidgets import (
QDialog, QVBoxLayout, QHBoxLayout, QListWidget, QListWidgetItem,
QPushButton, QDialogButtonBox, QLabel, QMessageBox,
)
from PyQt6.QtCore import Qt
from lintunes.preferences import DEFAULTS
from lintunes.gui import visualizer_styles
PRESETS_KEY = "visualizer_presets"
DEFAULT_NAME = "Default"
STYLE_KEYS = {
"bars": ("visualizer_bars_fade",),
"radial": ("visualizer_ghost_fade", "visualizer_radial_spokes",
"visualizer_radial_levels", "visualizer_radial_dot",
"visualizer_radial_normalize"),
"laser": ("visualizer_laser_afterimage", "visualizer_laser_low_hz",
"visualizer_laser_high_hz", "visualizer_laser_width",
"visualizer_laser_glow", "visualizer_laser_grid",
"visualizer_laser_head", "visualizer_laser_dot",
"visualizer_laser_distance", "visualizer_laser_pitch",
"visualizer_laser_yaw", "visualizer_laser_roll"),
}
def _all(prefs) -> dict:
saved = prefs.get(PRESETS_KEY) or {}
return saved if isinstance(saved, dict) else {}
def presets(prefs, style: str) -> dict:
"""The user's presets for ``style``, {name: {key: value}} — not Default."""
saved = _all(prefs).get(style) or {}
return {name: values for name, values in saved.items()
if isinstance(values, dict) and name != DEFAULT_NAME}
def names(prefs, style: str) -> list:
"""Default, then the user's presets in alphabetical order."""
return [DEFAULT_NAME] + sorted(presets(prefs, style), key=str.casefold)
def values_of(prefs, style: str, name: str) -> dict:
"""A preset's settings, every key present: a preset saved before a
setting existed takes that setting's default."""
stored = {} if name == DEFAULT_NAME else presets(prefs, style).get(name, {})
return {key: stored.get(key, DEFAULTS[key]) for key in STYLE_KEYS[style]}
def current(prefs, style: str) -> dict:
config = visualizer_styles.settings(prefs)
return {key: config[key] for key in STYLE_KEYS[style]}
def matching(prefs, style: str):
"""The preset the current settings are, or None (unsaved changes)."""
now = current(prefs, style)
for name in names(prefs, style):
if _same(now, values_of(prefs, style, name)):
return name
return None
def _same(a: dict, b: dict) -> bool:
for key, value in a.items():
other = b.get(key)
if isinstance(value, bool) or isinstance(other, bool):
if bool(value) != bool(other):
return False
elif not math.isclose(float(value), float(other),
rel_tol=1e-6, abs_tol=1e-6):
return False
return True
def _write(prefs, style: str, style_presets: dict):
everything = copy.deepcopy(_all(prefs))
everything[style] = style_presets
prefs.set(PRESETS_KEY, everything)
def save(prefs, style: str, name: str):
"""Save the current settings as ``name`` (overwriting one of that name)."""
name = name.strip()
if not name or name == DEFAULT_NAME:
raise ValueError(f"can't save a preset named {name!r}")
style_presets = copy.deepcopy(presets(prefs, style))
style_presets[name] = current(prefs, style)
_write(prefs, style, style_presets)
def rename(prefs, style: str, old: str, new: str):
new = new.strip()
style_presets = copy.deepcopy(presets(prefs, style))
if (old not in style_presets or not new or new == DEFAULT_NAME
or (new != old and new in style_presets)):
raise ValueError(f"can't rename {old!r} to {new!r}")
style_presets[new] = style_presets.pop(old)
_write(prefs, style, style_presets)
def delete(prefs, style: str, name: str):
style_presets = copy.deepcopy(presets(prefs, style))
style_presets.pop(name, None)
_write(prefs, style, style_presets)
class PresetEditor(QDialog):
"""Rename or delete one style's presets. Changes apply at once; Close
just closes. Default isn't listed: it can't be changed."""
def __init__(self, prefs, style: str, title: str, parent=None):
super().__init__(parent)
self._prefs, self._style = prefs, style
self.setWindowTitle(f"{title} Presets")
self.list = QListWidget()
self.list.itemChanged.connect(self._on_item_edited)
self.list.currentRowChanged.connect(lambda _row: self._enable())
self.rename_button = QPushButton("Rename")
self.rename_button.clicked.connect(
lambda: self.list.editItem(self.list.currentItem()))
self.delete_button = QPushButton("Delete")
self.delete_button.clicked.connect(self._delete_current)
self.empty = QLabel("No saved presets yet — use “Save as Preset…”.")
self.empty.setEnabled(False)
side = QVBoxLayout()
side.addWidget(self.rename_button)
side.addWidget(self.delete_button)
side.addStretch()
body = QHBoxLayout()
body.addWidget(self.list, stretch=1)
body.addLayout(side)
buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
buttons.rejected.connect(self.reject)
layout = QVBoxLayout(self)
layout.addLayout(body)
layout.addWidget(self.empty)
layout.addWidget(buttons)
self.resize(360, 280)
self._fill()
def _fill(self, select: str = None):
self.list.blockSignals(True)
self.list.clear()
for name in names(self._prefs, self._style)[1:]:
item = QListWidgetItem(name)
item.setData(Qt.ItemDataRole.UserRole, name)
item.setFlags(item.flags() | Qt.ItemFlag.ItemIsEditable)
self.list.addItem(item)
if name == select:
self.list.setCurrentItem(item)
if self.list.currentRow() < 0 and self.list.count():
self.list.setCurrentRow(0)
self.list.blockSignals(False)
self.empty.setVisible(self.list.count() == 0)
self._enable()
def _enable(self):
chosen = self.list.currentItem() is not None
self.rename_button.setEnabled(chosen)
self.delete_button.setEnabled(chosen)
def _on_item_edited(self, item):
old = item.data(Qt.ItemDataRole.UserRole)
new = item.text().strip()
if new == old:
return
try:
rename(self._prefs, self._style, old, new)
except ValueError:
self._warn(f"“{new}” can't be used — it's empty, taken, or "
f"reserved for the built-in preset.")
self._fill(select=old)
return
self._fill(select=new)
def _delete_current(self):
item = self.list.currentItem()
if item is None:
return
name = item.data(Qt.ItemDataRole.UserRole)
if self._confirm(f"Delete the preset “{name}”?"):
delete(self._prefs, self._style, name)
self._fill()
# Overridable, so tests needn't click through message boxes.
def _confirm(self, text: str) -> bool:
return QMessageBox.question(self, "Delete Preset", text) == \
QMessageBox.StandardButton.Yes
def _warn(self, text: str):
QMessageBox.warning(self, "Rename Preset", text)
+1
View File
@@ -25,6 +25,7 @@ DEFAULTS = {
"visualizer_laser_head": True, # a dot at the moving beam's tip
"visualizer_laser_glow": True, # the haze/scatter bloom
"visualizer_laser_grid": False, # an oscilloscope's dot graticule
"visualizer_presets": {}, # {style: {name: settings}}
"visualizer_laser_low_hz": 20, # the speaker under the mirror:
"visualizer_laser_high_hz": 20000, # what it plays (ends = no filter)
"visualizer_radial_spokes": 120, # frequency bands around the circle