Fix profile loading to not modify settings.json, preserve patterns

This commit is contained in:
2025-11-30 17:03:43 +13:00
parent 517750e5f6
commit 42575b9d2e
2 changed files with 77 additions and 15 deletions

View File

@@ -113,9 +113,9 @@ class App:
tab_management_frame = tk.Frame(self.root, bg=bg_color)
tab_management_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=self.scale_size(10), pady=self.scale_size(5))
# Create Notebook for tabs (packed after buttons so it takes remaining space)
# Create Notebook for tabs (packed before buttons so it takes remaining space)
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(expand=1, fill="both", before=tab_management_frame)
self.notebook.pack(expand=True, fill="both", before=tab_management_frame)
add_tab_btn = tk.Button(
tab_management_frame,
@@ -233,19 +233,31 @@ class App:
with open(profile_path, 'r') as file:
profile_data = json.load(file)
# Update settings with profile data
# Load profile data into settings for use in the app
# Remove current_profile from profile data if it exists (shouldn't be in profiles)
profile_data.pop("current_profile", None)
self.settings.clear()
# Preserve patterns and other settings.json-only data
patterns_backup = self.settings.get("patterns", {})
tab_password_backup = self.settings.get("tab_password", "")
# Update with profile data (lights, tab_order, tab_password from profile)
self.settings.update(profile_data)
self.settings["current_profile"] = current_profile # Store in settings.json, not profile
# Restore settings.json-only data
self.settings["patterns"] = patterns_backup
self.settings["current_profile"] = current_profile
# Only save current_profile to settings.json, not profile data
settings_to_save = {
"tab_password": tab_password_backup,
"current_profile": current_profile,
"patterns": patterns_backup
}
with open("settings.json", 'w') as f:
json.dump(settings_to_save, f, indent=4)
# Ensure tab_order exists in profile
if "tab_order" not in self.settings:
if "lights" in self.settings:
self.settings["tab_order"] = list(self.settings["lights"].keys())
else:
self.settings["tab_order"] = []
self.settings.save()
except Exception as e:
print(f"Error loading current profile '{current_profile}': {e}")
@@ -388,19 +400,31 @@ class App:
with open(profile_path, 'r') as file:
profile_data = json.load(file)
# Update settings with profile data
# Load profile data into settings for use in the app
# Remove current_profile from profile data if it exists (shouldn't be in profiles)
profile_data.pop("current_profile", None)
self.settings.clear()
# Preserve patterns and other settings.json-only data
patterns_backup = self.settings.get("patterns", {})
tab_password_backup = self.settings.get("tab_password", "")
# Update with profile data (lights, tab_order, tab_password from profile)
self.settings.update(profile_data)
self.settings["current_profile"] = profile_name # Store in settings.json, not profile
# Restore settings.json-only data
self.settings["patterns"] = patterns_backup
self.settings["current_profile"] = profile_name
# Only save current_profile to settings.json, not profile data
settings_to_save = {
"tab_password": tab_password_backup,
"current_profile": profile_name,
"patterns": patterns_backup
}
with open("settings.json", 'w') as f:
json.dump(settings_to_save, f, indent=4)
# Ensure tab_order exists in profile
if "tab_order" not in self.settings:
if "lights" in self.settings:
self.settings["tab_order"] = list(self.settings["lights"].keys())
else:
self.settings["tab_order"] = []
self.settings.save()
# Recreate tabs with new settings
self.create_tabs()
@@ -929,7 +953,7 @@ class App:
# Right panel for IDs, Patterns, and NEW Color Palette
right_panel_frame = tk.Frame(main_tab_frame, bg=bg_color)
right_panel_frame.pack(side=tk.LEFT, padx=20, pady=10, anchor="n", expand=True, fill="both")
right_panel_frame.pack(side=tk.LEFT, padx=self.scale_size(20), pady=self.scale_size(10), anchor="n", expand=True, fill="both")
# IDs section - MODIFIED TO BE SIDE-BY-SIDE
ids_frame = tk.Frame(right_panel_frame, bg=bg_color)
@@ -947,11 +971,11 @@ class App:
# --- New Frame to hold Patterns and Color Palette side-by-side ---
patterns_and_palette_frame = tk.Frame(right_panel_frame, bg=bg_color)
patterns_and_palette_frame.pack(pady=20, fill=tk.BOTH, expand=True)
patterns_and_palette_frame.pack(pady=self.scale_size(20), fill=tk.BOTH, expand=True)
# Patterns section
patterns_frame = tk.Frame(patterns_and_palette_frame, bg=bg_color, bd=2, relief=tk.GROOVE)
patterns_frame.pack(side=tk.LEFT, padx=10, pady=5, fill=tk.BOTH, expand=True) # Pack to the left
patterns_frame.pack(side=tk.LEFT, padx=self.scale_size(10), pady=self.scale_size(5), fill=tk.BOTH, expand=True) # Pack to the left
tk.Label(patterns_frame, text="Patterns:", font=("Arial", self.scale_font(20)), bg=bg_color, fg=fg_color).pack(pady=self.scale_size(10))
tab.pattern_buttons = {}
@@ -968,14 +992,14 @@ class App:
pady=self.scale_size(5),
relief=tk.FLAT,
)
button.pack(pady=5, fill=tk.X)
button.pack(pady=self.scale_size(5), fill=tk.X)
tab.pattern_buttons[pattern_name] = button
self.highlight_pattern_button(tab, initial_pattern)
# --- Color Palette Editor Section ---
color_palette_editor_frame = tk.Frame(patterns_and_palette_frame, bg=bg_color, bd=2, relief=tk.GROOVE)
color_palette_editor_frame.pack(side=tk.LEFT, padx=10, pady=5, fill=tk.BOTH, expand=True) # Pack to the left
color_palette_editor_frame.pack(side=tk.LEFT, padx=self.scale_size(10), pady=self.scale_size(5), fill=tk.BOTH, expand=True) # Pack to the left
tab.color_palette_editor_frame = color_palette_editor_frame # Store reference for update_ui_for_pattern
tk.Label(color_palette_editor_frame, text="Color Palette:", font=("Arial", self.scale_font(20)), bg=bg_color, fg=fg_color).pack(

View File

@@ -10,7 +10,11 @@ class Settings(dict):
def save(self):
try:
# Create a copy without lights and tab_order (these belong in profiles, not settings.json)
# But keep patterns, tab_password, and current_profile
settings_to_save = {k: v for k, v in self.items() if k not in ["lights", "tab_order"]}
# Ensure patterns are always included if they exist
if "patterns" in self:
settings_to_save["patterns"] = self["patterns"]
j = json.dumps(settings_to_save, indent=4)
with open(self.SETTINGS_FILE, 'w') as file:
file.write(j)
@@ -23,6 +27,40 @@ class Settings(dict):
with open(self.SETTINGS_FILE, 'r') as file:
loaded_settings = json.load(file)
self.update(loaded_settings)
# Ensure patterns exist (they should always be in settings.json)
if "patterns" not in self:
# Initialize with default patterns if missing
self["patterns"] = {
"on": {"min_delay": 10, "max_delay": 10000},
"off": {"min_delay": 10, "max_delay": 10000},
"rainbow": {"Step Rate": "n1", "min_delay": 10, "max_delay": 10000},
"transition": {"min_delay": 10, "max_delay": 10000},
"chase": {
"Colour 1 Length": "n1",
"Colour 2 Length": "n2",
"Step 1": "n3",
"Step 2": "n4",
"min_delay": 10,
"max_delay": 10000
},
"pulse": {
"Attack": "n1",
"Hold": "n2",
"Decay": "n3",
"min_delay": 10,
"max_delay": 10000
},
"circle": {
"Head Rate": "n1",
"Max Length": "n2",
"Tail Rate": "n3",
"Min Length": "n4",
"min_delay": 10,
"max_delay": 10000
},
"blink": {"min_delay": 10, "max_delay": 10000}
}
self.save() # Save to persist the default patterns
print("Settings loaded successfully.")
except Exception as e:
print(f"Error loading settings {e}")