SXXXXXXX_ProfileAnalyzer/profileanalyzer/gui/settings_window.py
2025-06-23 15:31:29 +02:00

75 lines
2.8 KiB
Python

# profileanalyzer/gui/settings_window.py
import tkinter as tk
from tkinter import ttk, messagebox
import json
import os
class SettingsWindow(tk.Toplevel):
"""A Toplevel window for managing application settings."""
CONFIG_FILE = "app_config.json"
def __init__(self, master):
super().__init__(master)
self.config_data = {}
self._init_window()
self._init_vars()
self._load_config()
self._create_widgets()
def _init_window(self):
self.title("Settings")
self.geometry("600x200")
self.transient(self.master)
self.grab_set()
def _init_vars(self):
self.editor_command_var = tk.StringVar()
def _load_config(self):
try:
if os.path.exists(self.CONFIG_FILE):
with open(self.CONFIG_FILE, 'r', encoding='utf-8') as f:
self.config_data = json.load(f)
except (IOError, json.JSONDecodeError) as e:
print(f"Could not load config file: {e}")
self.config_data = {}
# Set default value if not present
default_command = 'code -g "{file}:{line}"'
self.editor_command_var.set(self.config_data.get("editor_command", default_command))
def _create_widgets(self):
main_frame = ttk.Frame(self, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
editor_frame = ttk.LabelFrame(main_frame, text="Editor Integration", padding="10")
editor_frame.pack(fill=tk.X, expand=True)
editor_frame.columnconfigure(0, weight=1)
info_label = ttk.Label(editor_frame, text="Command template to open a file in your editor.\nUse {file} for the file path and {line} for the line number.", wraplength=550)
info_label.grid(row=0, column=0, columnspan=2, sticky="w", pady=(0, 10))
ttk.Entry(editor_frame, textvariable=self.editor_command_var).grid(row=1, column=0, sticky="ew")
button_frame = ttk.Frame(main_frame, padding=(0, 10, 0, 0))
button_frame.pack(fill=tk.X, side=tk.BOTTOM)
ttk.Button(button_frame, text="Save", command=self._on_save).pack(side=tk.RIGHT)
ttk.Button(button_frame, text="Cancel", command=self.destroy).pack(side=tk.RIGHT, padx=5)
def _on_save(self):
new_command = self.editor_command_var.get()
if "{file}" not in new_command or "{line}" not in new_command:
messagebox.showwarning("Warning", "The command template should contain both {file} and {line} placeholders.", parent=self)
return
self.config_data["editor_command"] = new_command
try:
with open(self.CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(self.config_data, f, indent=4)
self.destroy()
except IOError as e:
messagebox.showerror("Error", f"Could not save config file: {e}", parent=self)