Refactor: Module-level UI construction (MIDI IN column)
This commit is contained in:
+84
-1
@@ -1,11 +1,20 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from PyQt6.QtWidgets import (
|
||||
QApplication, QMainWindow, QWidget, QVBoxLayout,
|
||||
QHBoxLayout, QSlider, QComboBox, QLabel, QPushButton,
|
||||
QSizePolicy, QSpinBox, QLineEdit, QInputDialog, QFrame,
|
||||
QCheckBox, QMessageBox
|
||||
)
|
||||
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
PRESETS_DIR = os.path.join(BASE_DIR, "presets")
|
||||
PRESETS_FILE = os.path.join(PRESETS_DIR, "presets.json")
|
||||
os.makedirs(PRESETS_DIR, exist_ok=True)
|
||||
|
||||
|
||||
def load_presets_file():
|
||||
if os.path.exists(PRESETS_FILE):
|
||||
with open(PRESETS_FILE, "r") as f:
|
||||
@@ -14,4 +23,78 @@ def load_presets_file():
|
||||
|
||||
def save_presets_file(data):
|
||||
with open(PRESETS_FILE, "w") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
def ensure_preset_structure(presets):
|
||||
if "presets" not in presets:
|
||||
return {"__last_used__": None, "presets": presets}
|
||||
return presets
|
||||
|
||||
def on_preset_selected(menu_presets: QComboBox, app_window: QMainWindow, ready: bool, loaded_presets: dict):
|
||||
|
||||
selectedOption = menu_presets.currentText()
|
||||
|
||||
if ready and selectedOption and selectedOption in loaded_presets["presets"]:
|
||||
loaded_presets["__last_used__"] = selectedOption
|
||||
save_presets_file(loaded_presets)
|
||||
app_window.apply_preset(loaded_presets["presets"][selectedOption])
|
||||
|
||||
def save_preset(app_window: QMainWindow, menu_presets: QComboBox, loaded_presets: dict):
|
||||
|
||||
selectedOption = menu_presets.currentText()
|
||||
|
||||
if not selectedOption or selectedOption not in loaded_presets["presets"]:
|
||||
app_window.save_preset_as() # not self
|
||||
return
|
||||
loaded_presets["presets"][selectedOption] = app_window._build_preset_data(selectedOption) # not self
|
||||
loaded_presets["__last_used__"] = selectedOption
|
||||
save_presets_file(loaded_presets)
|
||||
app_window._show_save_status() # not self
|
||||
print(f"Saved preset: {selectedOption}")
|
||||
|
||||
def save_preset_as(app_window: QMainWindow, menu_presets: QComboBox, loaded_presets: dict):
|
||||
|
||||
print(f"save as button tapped")
|
||||
|
||||
selectedOption = menu_presets.currentText()
|
||||
|
||||
selectedOption, ok = QInputDialog.getText(app_window, "Save As", "Preset name:")
|
||||
if not ok or not selectedOption.strip():
|
||||
return
|
||||
selectedOption = selectedOption.strip()
|
||||
loaded_presets["presets"][selectedOption] = app_window._build_preset_data(selectedOption)
|
||||
loaded_presets["__last_used__"] = selectedOption
|
||||
save_presets_file(loaded_presets)
|
||||
app_window.refresh_preset_combo()
|
||||
menu_presets.setCurrentText(selectedOption)
|
||||
app_window._show_save_status()
|
||||
print(f"Saved preset as: {selectedOption}")
|
||||
|
||||
|
||||
def delete_preset(app_window: QMainWindow, menu_presets: QComboBox, loaded_presets: dict):
|
||||
|
||||
selectedOption = menu_presets.currentText()
|
||||
|
||||
selectedOption = menu_presets.currentText()
|
||||
|
||||
if not selectedOption or selectedOption not in loaded_presets["presets"]:
|
||||
return
|
||||
|
||||
msg = QMessageBox(app_window)
|
||||
msg.setWindowTitle("Remove Preset")
|
||||
msg.setText(f'Are you sure you want to remove "{selectedOption}"?')
|
||||
msg.setIcon(QMessageBox.Icon.NoIcon)
|
||||
msg.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
|
||||
reply = msg.exec()
|
||||
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
|
||||
del loaded_presets["presets"][selectedOption]
|
||||
|
||||
if loaded_presets["__last_used__"] == selectedOption:
|
||||
loaded_presets["__last_used__"] = None
|
||||
|
||||
save_presets_file(loaded_presets)
|
||||
app_window.refresh_preset_combo()
|
||||
print(f"Deleted preset: {selectedOption}")
|
||||
Reference in New Issue
Block a user