-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_GUI.py
More file actions
143 lines (121 loc) · 5.31 KB
/
Copy pathconfig_GUI.py
File metadata and controls
143 lines (121 loc) · 5.31 KB
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
import threading
from watchdog.observers import Observer
from Wplace.config import ConfigHandler
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QLineEdit, QPushButton, QGroupBox, QScrollArea, QHBoxLayout
from PySide6.QtCore import Qt
from PySide6.QtGui import QKeySequence
import yaml
from collections import OrderedDict
config_path = 'config.yaml'
example_path = 'config_example.yaml'
class ConfigEditor(QMainWindow):
def __init__(self, config_path, example_path):
super().__init__()
self.config_path = config_path
self.example_path = example_path
self.setWindowTitle("Config Editor")
self.resize(600, 800)
# Load config
self.config_data = self.load_config()
self.temp_data = self.config_data.copy()
# Use a scroll area for better navigation
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
self.central_widget = QWidget()
scroll_area.setWidget(self.central_widget)
self.setCentralWidget(scroll_area)
self.layout = QVBoxLayout()
self.central_widget.setLayout(self.layout)
# Create a top bar for Save and Reset buttons
self.top_bar = QHBoxLayout()
self.save_button = QPushButton("Save")
self.save_button.clicked.connect(self.save_config)
self.reset_button = QPushButton("Reset")
self.reset_button.clicked.connect(self.reset_config)
self.top_bar.addWidget(self.save_button)
self.top_bar.addWidget(self.reset_button)
# Main layout with top bar and scroll area
self.main_layout = QVBoxLayout()
self.main_layout.addLayout(self.top_bar)
self.main_layout.addWidget(scroll_area)
central_widget = QWidget()
central_widget.setLayout(self.main_layout)
self.setCentralWidget(central_widget)
# Display config parameters with nested support
self.input_fields = {}
self.display_config(self.config_data, self.layout)
# Enable undo/redo for input fields
for input_field in self.input_fields.values():
input_field.setUpdatesEnabled(True)
def load_config(self):
with open(self.config_path, 'r', encoding='utf-8') as f:
return yaml.load(f, Loader=yaml.SafeLoader)
def save_config(self):
with open(self.config_path, 'w', encoding='utf-8') as f:
yaml.dump(self.temp_data, f, Dumper=yaml.SafeDumper, sort_keys=False)
self.config_data = self.temp_data.copy()
def reset_config(self):
example_path = 'config_example.yaml'
with open(example_path, 'r', encoding='utf-8') as f:
self.temp_data = yaml.load(f, Loader=yaml.SafeLoader)
def update_fields(config, prefix=""):
for key, value in config.items():
full_key = f"{prefix}.{key}" if prefix else key
if isinstance(value, dict):
update_fields(value, full_key)
else:
if full_key in self.input_fields:
self.input_fields[full_key].setText(str(value))
update_fields(self.temp_data)
def display_config(self, config, parent_layout, prefix=""):
for key, value in config.items():
full_key = f"{prefix}.{key}" if prefix else key
if isinstance(value, dict):
group_box = QGroupBox(key)
group_layout = QVBoxLayout()
group_box.setLayout(group_layout)
parent_layout.addWidget(group_box)
self.display_config(value, group_layout, full_key)
else:
label = QLabel(key)
input_field = QLineEdit(str(value))
input_field.setMinimumWidth(300) # Set appropriate size
input_field.textChanged.connect(lambda text, k=full_key: self.update_temp_data(k, text))
parent_layout.addWidget(label)
parent_layout.addWidget(input_field)
self.input_fields[full_key] = input_field
def update_temp_data(self, key, text):
keys = key.split('.')
temp = self.temp_data
for k in keys[:-1]:
temp = temp.setdefault(k, {})
try:
temp[keys[-1]] = eval(text) # Convert to Python type if possible
except:
temp[keys[-1]] = text
def keyPressEvent(self, event):
# Capture Ctrl+Z for undo and Ctrl+Y for redo
if event.matches(QKeySequence.Undo):
print("Undo key detected")
focused_widget = self.focusWidget()
if isinstance(focused_widget, QLineEdit):
focused_widget.undo()
print("Undo performed")
elif event.matches(QKeySequence.Redo):
print("Redo key detected")
focused_widget = self.focusWidget()
if isinstance(focused_widget, QLineEdit):
focused_widget.redo()
print("Redo performed")
else:
super().keyPressEvent(event)
if __name__ == "__main__":
reload_event = threading.Event()
cfg = ConfigHandler(config_path, reload_event)
observer = Observer()
observer.schedule(cfg, path='.', recursive=False)
observer.start()
app = QApplication([])
editor = ConfigEditor(config_path, example_path)
editor.show()
app.exec()