-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsettings.py
166 lines (133 loc) · 6.08 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import tkinter as tk
from tkinter import messagebox
import ttkbootstrap as ttk
import darkdetect
from settings.config_manager import load_config, save_config
from settings.directory_manager import DirectoryManager
from settings.clip_manager import CLIPManager
from settings.text_embed_manager import TextEmbedManager
class CLIPPyXSettings:
"""
Manages the CLIPPyX Settings interface.
This class initializes and displays a graphical user interface (GUI) for configuring
various settings related to the CLIPPyX application. It loads an existing configuration
from a YAML file, constructs the interface elements, and provides functionality to update
and save new settings.
Attributes:
config (dict): Holds the loaded configuration data from 'config.yaml'.
root (ttk.Window): The root window for the settings interface.
main_frame (ttk.Frame): The main frame holding the scrollable canvas.
canvas (tk.Canvas): A canvas to enable scrolling for the settings form.
scrollbar (ttk.Scrollbar): A scrollbar for the settings canvas.
frame (ttk.Frame): A frame nested within the canvas to hold UI components.
directory_manager (DirectoryManager): Manages directory settings.
clip_manager (CLIPManager): Manages CLIP-related settings.
text_embed_manager (TextEmbedManager): Manages text embedding settings.
"""
def __init__(self):
"""
Initialize CLIPPyXSettings.
Loads the configuration from 'config.yaml' and calls `setup_ui()` to set up the GUI.
"""
self.config = load_config("config.yaml")
self.setup_ui()
def setup_ui(self):
"""
Set up the main window and UI components.
Determines whether to use a light or dark theme (via `darkdetect`), then creates
and configures the main application window, including an icon, minimum size,
and title. Finally, calls other methods to create the main frame, directory,
CLIP, and text embedding options, and a button panel.
"""
theme = darkdetect.theme()
theme_dict = {"Light": "lumen", "Dark": "darkly"}
self.root = ttk.Window(themename=theme_dict[theme])
self.root.title("CLIPPyX Settings")
self.root.minsize(840, 860)
self.root.iconphoto(False, tk.PhotoImage(file="assets/icon.png"))
self.create_main_frame()
self.create_directory_options()
self.create_clip_options()
self.create_text_embed_options()
self.create_buttons()
def create_main_frame(self):
"""
Create and configure the main scrollable frame.
Sets up a canvas and scrollbar to allow the interface to be scrollable if
it exceeds the window size. Places the actual UI components inside a nested
frame added to the canvas.
"""
self.main_frame = ttk.Frame(self.root)
self.main_frame.pack(fill=tk.BOTH, expand=1)
self.canvas = tk.Canvas(self.main_frame)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
self.scrollbar = ttk.Scrollbar(
self.main_frame,
orient=tk.VERTICAL,
command=self.canvas.yview
)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.bind(
"<Configure>",
lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")),
)
self.frame = ttk.Frame(self.canvas)
self.canvas.create_window((0, 0), window=self.frame, anchor="nw")
def create_directory_options(self):
"""
Create and initialize directory management settings.
Instantiates a DirectoryManager object, providing it the main content frame
(`self.frame`) and the current `config`.
"""
self.directory_manager = DirectoryManager(self.frame, self.config)
def create_clip_options(self):
"""
Create and initialize CLIP-related settings.
Instantiates a CLIPManager object, which controls settings related to CLIP models
and configurations. It is given the main content frame and the current `config`.
"""
self.clip_manager = CLIPManager(self.frame, self.config)
def create_text_embed_options(self):
"""
Create and initialize text embedding settings.
Instantiates a TextEmbedManager object, providing it the main content frame and
the current `config`. This allows the user to configure text embedding parameters.
"""
self.text_embed_manager = TextEmbedManager(self.frame, self.config)
def create_buttons(self):
"""
Create and configure buttons for saving and quitting.
Adds "Save" and "Quit" buttons to the bottom of the settings UI. The "Save" button
triggers `save_changes()` and the "Quit" button closes the application.
"""
buttons_frame = ttk.Frame(self.frame)
buttons_frame.pack(pady=10)
ttk.Button(buttons_frame, text="Save", command=self.save_changes).pack(
side=tk.LEFT, padx=10
)
ttk.Button(buttons_frame, text="Quit", command=self.root.quit).pack(
side=tk.LEFT, padx=10
)
def save_changes(self):
"""
Save configuration changes.
Gathers updated settings from the DirectoryManager, CLIPManager, and
TextEmbedManager, merges them into `self.config`, and saves them to 'config.yaml'.
Displays a success message upon completion.
"""
# Update config with values from all managers
self.config.update(self.directory_manager.get_config())
self.config.update(self.clip_manager.get_config())
self.config.update(self.text_embed_manager.get_config())
save_config(self.config, "config.yaml")
messagebox.showinfo("Success", "Configuration saved successfully!")
def run(self):
"""
Start the main event loop.
Blocks execution until the GUI is closed.
"""
self.root.mainloop()
if __name__ == "__main__":
app = CLIPPyXSettings()
app.run()