inkstitch/lib/utils/settings.py

85 wiersze
2.3 KiB
Python

import json
import os
from collections.abc import MutableMapping
from .paths import get_user_dir
# These settings are the defaults for SVG metadata settings of the same name in
# lib.extensions.base.InkstitchMetadata
DEFAULT_METADATA = {
"min_stitch_len_mm": 0.1,
"collapse_len_mm": 3,
}
DEFAULT_SETTINGS = {
2024-12-07 14:13:22 +00:00
# Ink/Stitch preferences
"cache_size": 100,
"pop_out_simulator": False,
2024-12-07 14:13:22 +00:00
# simulator
"simulator_adaptive_speed": True,
"simulator_speed": 16,
"simulator_line_width": 0.1,
"simulator_npp_size": 0.5,
2024-12-07 14:13:22 +00:00
"npp_button_status": False,
"jump_button_status": False,
"trim_button_status": False,
"stop_button_status": False,
"color_change_button_status": False,
"toggle_page_button_status": True,
"display_crosshair": True,
2024-12-07 14:13:22 +00:00
# apply palette
Sew Stack first steps (#3133) * handle more recursive cases * scaffolding for stitch layers * scaffolding for SewStack * always use DotDict when parsing json params * add DefaultDotDict + DotDict fixes * first working SewStack (no UI yet) * ignore inkstitch_debug.log and .svg * refactor * early WIP: property grid display temporarily in stitch plan preview * start of sew stack editor extension * add layer properties panel and splitter * spacing and better icon * handle checkbox * add layer action buttons * show selected property help text in an HtmlWindow * rename * rephrase help text for tolerance * refactor into separate file * simplify structure * better property type handling * add randomization button * add random seed re-roll button * simulator preview * update preview in a few more cases * always DotDict * avoid ridiculously slow simulations * preview selected layer or all layers * edit multiple objects and save only modified properties into the SVG * better preview handling * add reverse and jitter * add stitch path jitter * fix types * fix random shuffle button * fixes * fix repeats * type hinting to please pycharm * show layer description * avoid exception in properties with multiple values * fix typing * fix new layer * draw a box around property grid and help box * confirm before closing * rename properties and fix seed * fix close/cancel logic * add buttons to undo changes and reset to default value * set not modified if default is original setting * fix invisible icon * more space for properties * fix random properties * better regulation of simulator rendering speed * Fixed timer being passed a float * fix get_json_param() default handling * fix tests * add checkbox for sew stack only * fix property help * adjustable stitch layer editor help box size, with persistence * repeat exact stitches * "fix" style * adjust for new next_element stuff --------- Co-authored-by: CapellanCitizen <thecapellancitizen@gmail.com>
2025-01-29 17:04:07 +00:00
"last_applied_palette": "",
# sew stack editor
"stitch_layer_editor_sash_position": -200,
# lettering (all lettering applications)
"last_font": "Ink/Stitch Small Font",
2025-02-13 17:06:12 +00:00
# lettering
"lettering_align_text": 0,
"lettering_trim_option": 0,
"lettering_use_command_symbols": False,
# font sampling
"font_sampling_max_line_width": 180
}
class GlobalSettings(MutableMapping):
def __init__(self):
super().__init__()
self.__settings_file = os.path.join(get_user_dir(), "settings.json")
self.__settings = {}
for name, value in DEFAULT_METADATA.items():
self.__settings[f"default_{name}"] = value
self.__settings.update(DEFAULT_SETTINGS)
try:
with open(self.__settings_file, 'r') as settings_file:
self.__settings.update(json.load(settings_file))
except (OSError, json.JSONDecodeError, ValueError):
pass
def __setitem__(self, item, value):
self.__settings[item] = value
with open(self.__settings_file, 'w') as settings_file:
json.dump(self.__settings, settings_file)
def __getitem__(self, item):
return self.__settings[item]
def __delitem__(self, item):
del self.__settings[item]
def __iter__(self):
return iter(self.__settings)
def __len__(self):
return len(self.__settings)
def __json__(self):
return self.__settings
global_settings = GlobalSettings()