From 64c52ab25a8cb908b235c4c01cfc2243131b8928 Mon Sep 17 00:00:00 2001 From: Richie Yeung Date: Wed, 17 Jul 2024 11:53:52 -0300 Subject: [PATCH] Fix all bool settings --- zxlive/common.py | 5 +++++ zxlive/graphview.py | 12 ++++++++++-- zxlive/mainwindow.py | 9 ++++++++- zxlive/rewrite_action.py | 2 +- zxlive/settings.py | 11 ++++++++--- zxlive/settings_dialog.py | 1 + 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/zxlive/common.py b/zxlive/common.py index 2cb0465..b821f29 100644 --- a/zxlive/common.py +++ b/zxlive/common.py @@ -20,6 +20,11 @@ def get_data(path: str) -> str: return os.path.join(os.environ.get("_MEIPASS", _ROOT), path) +def set_settings_value(arg: str, val: T, _type: Type[T], settings: QSettings | None = None) -> None: + _settings = settings or QSettings("zxlive", "zxlive") + if not isinstance(val, _type): + raise ValueError(f"Unexpected type for {arg}: expected {_type}, got {type(val)}") + _settings.setValue(arg, val) def get_settings_value(arg: str, _type: Type[T], default: T | None = None, settings: QSettings | None = None) -> T: _settings = settings or QSettings("zxlive", "zxlive") diff --git a/zxlive/graphview.py b/zxlive/graphview.py index c8246a0..1fbb7a5 100644 --- a/zxlive/graphview.py +++ b/zxlive/graphview.py @@ -28,7 +28,8 @@ from dataclasses import dataclass from . import animations as anims -from .common import GraphT, SCALE, OFFSET_X, OFFSET_Y, MIN_ZOOM, MAX_ZOOM +from .common import (GraphT, SCALE, OFFSET_X, OFFSET_Y, MIN_ZOOM, MAX_ZOOM, + get_settings_value, set_settings_value) from .graphscene import GraphScene, VItem, EItem, EditGraphScene from .settings import display_setting from .vitem import PHASE_ITEM_Z @@ -99,10 +100,17 @@ def __init__(self, graph_scene: GraphScene) -> None: self.centerOn(OFFSET_X,OFFSET_Y) - self.sparkle_mode = False self.sparkles = Sparkles(self.graph_scene) QShortcut(QKeySequence("Ctrl+Shift+Alt+S"), self).activated.connect(self._toggle_sparkles) + @property + def sparkle_mode(self) -> bool: + return get_settings_value("sparkle-mode", bool) + + @sparkle_mode.setter + def sparkle_mode(self, value: bool) -> None: + set_settings_value("sparkle-mode", value, bool) + def _toggle_sparkles(self) -> None: self.sparkle_mode = not self.sparkle_mode diff --git a/zxlive/mainwindow.py b/zxlive/mainwindow.py index de7b5af..1a6b0a3 100644 --- a/zxlive/mainwindow.py +++ b/zxlive/mainwindow.py @@ -177,7 +177,6 @@ def __init__(self) -> None: self.effects = {e: load_sfx(e) for e in SFXEnum} - self.sfx_on = self.settings.value("sound-effects") QShortcut(QKeySequence("Ctrl+B"), self).activated.connect(self._toggle_sfx) def open_demo_graph(self) -> None: @@ -588,6 +587,14 @@ def update_colors(self) -> None: if self.active_panel is not None: self.active_panel.update_colors() + @property + def sfx_on(self) -> bool: + return self.settings.value("sound-effects") + + @sfx_on.setter + def sfx_on(self, value: bool) -> None: + self.settings.setValue("sound-effects", value) + def play_sound(self, s: SFXEnum) -> None: if self.sfx_on: self.effects[s].play() diff --git a/zxlive/rewrite_action.py b/zxlive/rewrite_action.py index 4ca0c0d..5a245ee 100644 --- a/zxlive/rewrite_action.py +++ b/zxlive/rewrite_action.py @@ -44,7 +44,7 @@ class RewriteAction: @classmethod def from_rewrite_data(cls, d: RewriteData) -> RewriteAction: - if display_setting.PREVIEWS_SHOW and ('picture' in d or 'custom_rule' in d): + if display_setting.previews_show and ('picture' in d or 'custom_rule' in d): if 'custom_rule' in d: # We will create a custom tooltip picture representing the custom rewrite graph_scene_left = GraphScene() diff --git a/zxlive/settings.py b/zxlive/settings.py index ec0e2d5..549c995 100644 --- a/zxlive/settings.py +++ b/zxlive/settings.py @@ -35,6 +35,7 @@ class ColorScheme(TypedDict): "snap-granularity": '4', "input-circuit-format": 'openqasm', "previews-show": True, + "sparkle-mode": True, 'sound-effects': False, } @@ -186,7 +187,6 @@ def _get_synonyms(key: str, default: list[str]) -> list[str]: class DisplaySettings: SNAP_DIVISION = 4 # Should be an integer dividing SCALE - PREVIEWS_SHOW = True def __init__(self) -> None: self.colors = color_schemes[str(settings.value("color-scheme"))] @@ -202,9 +202,14 @@ def update(self) -> None: get_settings_value("font/size", int) ) self.SNAP = SCALE / self.SNAP_DIVISION - self.PREVIEWS_SHOW = get_settings_value("previews-show",bool) - self.PREVIEWS_SHOW = True + @property + def previews_show(self) -> bool: + return get_settings_value("previews-show", bool) + + @previews_show.setter + def previews_show(self, value: bool) -> None: + settings.setValue("previews-show", value) # Initialise settings settings = QSettings("zxlive", "zxlive") diff --git a/zxlive/settings_dialog.py b/zxlive/settings_dialog.py index 14ca83a..c320600 100644 --- a/zxlive/settings_dialog.py +++ b/zxlive/settings_dialog.py @@ -79,6 +79,7 @@ class SettingsData(TypedDict): {"id": "snap-granularity", "label": "Snap-to-grid granularity", "type": FormInputType.Combo, "data": snap_to_grid_data}, {"id": "input-circuit-format", "label": "Input Circuit as", "type": FormInputType.Combo, "data": input_circuit_formats}, {"id": "previews-show", "label": "Show rewrite previews","type": FormInputType.Bool}, + {"id": "sparkle-mode", "label": "Sparkle Mode", "type": FormInputType.Bool}, {"id": "sound-effects", "label": "Sound Effects", "type": FormInputType.Bool}, ]