Skip to content

Commit

Permalink
Merge pull request #330 from zxcalc/settings
Browse files Browse the repository at this point in the history
Fix all bool settings
  • Loading branch information
RazinShaikh authored Jul 18, 2024
2 parents 4141cab + 64c52ab commit 01c8d0e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions zxlive/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 10 additions & 2 deletions zxlive/graphview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion zxlive/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion zxlive/rewrite_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions zxlive/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ColorScheme(TypedDict):
"snap-granularity": '4',
"input-circuit-format": 'openqasm',
"previews-show": True,
"sparkle-mode": True,
'sound-effects': False,
}

Expand Down Expand Up @@ -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"))]
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions zxlive/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
]

Expand Down

0 comments on commit 01c8d0e

Please sign in to comment.