"""
Preferences Dialog
Sets settings for various biotoolkit features
"""
import logging
from decologr import Decologr as log
from PySide6 import QtCore, QtWidgets
from PySide6.QtCore import QSettings, QSize
from PySide6.QtWidgets import (
QCheckBox,
QComboBox,
QDialog,
QGroupBox,
QHBoxLayout,
QLabel,
)
from jdxi_editor.project import (
__organization_name__,
__package_name__,
__program__,
)
from jdxi_editor.ui.common import JDXi, QVBoxLayout, QWidget
[docs]
def log_settings() -> None:
settings = QSettings(__organization_name__, __program__)
print(f'log_level {settings.value("log_level", logging.DEBUG, type=int)}')
print(f'logging {settings.value("logging", True, type=bool)}')
[docs]
class UiPreferencesDialog(QDialog):
def __init__(self, parent):
super().__init__(parent)
[docs]
self.settings = QSettings(__organization_name__, __program__)
[docs]
def ui_setup(self, parent: QWidget = None):
"""
ui_setup
:param parent: QWidget
:return: None
"""
self.icon_size = QSize(40, 40)
"""if darkdetect.isLight():
theme = "light"
else:
theme = "dark"""
log_settings()
self.resize(508, 420)
self.setWindowTitle("JD-XI Editor settings")
main_layout = QVBoxLayout(self)
# Create a QComboBox
self.log_level_combo = QComboBox()
self.log_levels = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
self.log_level_combo.addItems(self.log_levels.keys())
# Load the log level from QSettings
# self.load_log_level_from_settings()
from jdxi_editor.log_files import saved_level
level = saved_level(self.settings)
self.log_level_combo.setCurrentIndex(
list(self.log_levels.values()).index(level)
)
# the signal gives the row: the level is the row's value
self.log_level_combo.currentIndexChanged.connect(
lambda row: self.update_log_level(list(self.log_levels.values())[row])
)
self.log_level_layout = QHBoxLayout(self)
self.log_icon = QLabel()
self.log_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.REPORT).pixmap(self.icon_size)
)
self.log_level_label = QLabel("Log file error reporting level:")
self.log_level_layout.addWidget(self.log_icon)
self.log_level_layout.addWidget(self.log_level_label)
self.log_level_layout.addWidget(self.log_level_combo)
self.logging_layout = QHBoxLayout(self)
self.logging_icon = QLabel()
self.logging_checkbox = QCheckBox("Enable Logging?")
self.logging_checkbox.setLayoutDirection(QtCore.Qt.RightToLeft)
self.logging_checkbox.setChecked(
bool(self.settings.value("logging", type=bool))
)
self.logging_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.REPORT).pixmap(self.icon_size)
)
self.logging_label = QLabel("Logging On or Off:")
self.logging_layout.addWidget(self.logging_icon)
self.logging_layout.addWidget(self.logging_label)
self.logging_layout.addWidget(self.logging_checkbox)
# Silence MIDI note logging (note on/off only) to reduce log volume
from jdxi_editor.globals import SILENCE_MIDI_NOTE_LOGGING_KEY
self.silence_midi_notes_layout = QHBoxLayout(self)
self.silence_midi_notes_icon = QLabel()
self.silence_midi_notes_checkbox = QCheckBox(
"Silence MIDI note logging (note on/off)"
)
self.silence_midi_notes_checkbox.setLayoutDirection(QtCore.Qt.RightToLeft)
_val = self.settings.value(SILENCE_MIDI_NOTE_LOGGING_KEY, True)
_checked = (
_val
if isinstance(_val, bool)
else (str(_val).lower() in ("true", "1", "yes"))
)
self.silence_midi_notes_checkbox.setChecked(_checked)
self.silence_midi_notes_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.REPORT).pixmap(self.icon_size)
)
self.silence_midi_notes_label = QLabel("Reduce log volume from MIDI notes:")
self.silence_midi_notes_layout.addWidget(self.silence_midi_notes_icon)
self.silence_midi_notes_layout.addWidget(self.silence_midi_notes_label)
self.silence_midi_notes_layout.addWidget(self.silence_midi_notes_checkbox)
from jdxi_editor.globals import MIDI_SELECTION_VALIDATION_KEY
self.midi_validation_layout = QHBoxLayout(self)
self.midi_validation_icon = QLabel()
self.midi_validation_checkbox = QCheckBox(
"Validate MIDI sends against combo selection (log only)"
)
self.midi_validation_checkbox.setLayoutDirection(QtCore.Qt.RightToLeft)
_val_mv = self.settings.value(MIDI_SELECTION_VALIDATION_KEY, False)
_checked_mv = (
_val_mv
if isinstance(_val_mv, bool)
else (str(_val_mv).lower() in ("true", "1", "yes"))
)
self.midi_validation_checkbox.setChecked(_checked_mv)
self.midi_validation_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.REPORT).pixmap(self.icon_size)
)
self.midi_validation_label = QLabel("MIDI selection cross-validation:")
self.midi_validation_layout.addWidget(self.midi_validation_icon)
self.midi_validation_layout.addWidget(self.midi_validation_label)
self.midi_validation_layout.addWidget(self.midi_validation_checkbox)
# --- Automatic update check (on by default)
from jdxi_editor.update.checker import (
AUTO_CHECK_DEFAULT,
AUTO_CHECK_KEY,
settings_bool,
)
self.update_check_layout = QHBoxLayout()
self.update_check_icon = QLabel()
self.update_check_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.SETTINGS).pixmap(self.icon_size)
)
self.update_check_label = QLabel(
"Check GitHub for new releases once a day (on by default):"
)
self.update_check_checkbox = QCheckBox("Check for updates automatically")
self.update_check_checkbox.setLayoutDirection(QtCore.Qt.RightToLeft)
self.update_check_checkbox.setChecked(
settings_bool(self.settings, AUTO_CHECK_KEY, AUTO_CHECK_DEFAULT)
)
self.update_check_layout.addWidget(self.update_check_icon)
self.update_check_layout.addWidget(self.update_check_label)
self.update_check_layout.addWidget(self.update_check_checkbox)
# --- Text size: the text only (knobs, graphs and spacing stay); from the next start
from jdxi_editor.ui import text_size
self.text_size_layout = QHBoxLayout()
self.text_size_icon = QLabel()
self.text_size_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.SETTINGS).pixmap(self.icon_size)
)
self.text_size_label = QLabel("Text size (from the next start):")
self.text_size_combo = QComboBox()
for value, name in text_size.SIZES.items():
self.text_size_combo.addItem(f"{name} ({value}%)", value)
current = int(self.settings.value(text_size.SETTING, 100) or 100)
self.text_size_combo.setCurrentIndex(
max(0, self.text_size_combo.findData(current))
)
self.text_size_layout.addWidget(self.text_size_icon)
self.text_size_layout.addWidget(self.text_size_label)
self.text_size_layout.addWidget(self.text_size_combo)
# --- Advanced features
from jdxi_editor.globals import (
LILYPOND_ENABLED_DEFAULT,
LILYPOND_ENABLED_KEY,
)
from jdxi_editor.update.checker import settings_bool
self.advanced_group = QGroupBox("Advanced")
advanced_layout = QVBoxLayout(self.advanced_group)
self.lilypond_layout = QHBoxLayout()
self.lilypond_icon = QLabel()
self.lilypond_icon.setPixmap(
JDXi.UI.Icon.get_icon(JDXi.UI.Icon.FILE_DOCUMENT).pixmap(self.icon_size)
)
self.lilypond_label = QLabel(
"LilyPond PDF export (File → Open current MIDI as PDF):"
)
self.lilypond_checkbox = QCheckBox("Enable LilyPond PDF export")
self.lilypond_checkbox.setLayoutDirection(QtCore.Qt.RightToLeft)
self.lilypond_checkbox.setToolTip(
"Requires LilyPond installed on this machine. "
"When off, the File menu hides “Open current MIDI as PDF”."
)
self.lilypond_checkbox.setChecked(
settings_bool(self.settings, LILYPOND_ENABLED_KEY, LILYPOND_ENABLED_DEFAULT)
)
self.lilypond_layout.addWidget(self.lilypond_icon)
self.lilypond_layout.addWidget(self.lilypond_label)
self.lilypond_layout.addWidget(self.lilypond_checkbox)
advanced_layout.addLayout(self.lilypond_layout)
self.buttonBox = QtWidgets.QDialogButtonBox(self)
self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(
QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok
)
self.buttonBox.setObjectName("buttonBox")
self.buttonBox.rejected.connect(self.close)
self.buttonBox.accepted.connect(self.on_save_settings)
self.buttonBox.accepted.connect(self.close)
QtCore.QMetaObject.connectSlotsByName(self)
main_widget = QWidget()
main_layout.addWidget(main_widget)
main_content_layout = QVBoxLayout()
main_content_layout.addLayout(self.log_level_layout)
main_content_layout.addLayout(self.logging_layout)
main_content_layout.addLayout(self.silence_midi_notes_layout)
main_content_layout.addLayout(self.midi_validation_layout)
main_content_layout.addLayout(self.update_check_layout)
main_content_layout.addLayout(self.text_size_layout)
main_content_layout.addWidget(self.advanced_group)
main_widget.setLayout(main_content_layout)
main_layout.addWidget(self.buttonBox)
self.setLayout(main_layout)
[docs]
def load_log_level_from_settings(self):
"""
Load and migrate log level from settings (handles legacy string values).
"""
value = self.settings.value("log_level", logging.DEBUG)
# Case 1: Value already an integer (new style)
if isinstance(value, int):
log_level = value
# Case 2: Legacy value stored as string (e.g., "CRITICAL")
elif isinstance(value, str):
# Try to parse as integer first
try:
log_level = int(value)
except ValueError:
# Convert known string names
log_level = self.log_levels.get(value.upper(), logging.DEBUG)
# Migrate: store the numeric version back
self.settings.setValue("log_level", log_level)
else:
# Fallback
log_level = logging.DEBUG
# Set combo box to correct index
level_name = next(
(k for k, v in self.log_levels.items() if v == log_level), "DEBUG"
)
index = list(self.log_levels.keys()).index(level_name)
self.log_level_combo.setCurrentIndex(index)
# Set the logger level immediately
logging.getLogger(__package_name__).setLevel(log_level)
[docs]
def update_log_level(self, level):
"""
Update log level for the current logger and all handlers
"""
from jdxi_editor.log_files import apply_log_level
log.parameter("level", level)
apply_log_level(level)
self.settings.setValue("log_level", level)
self.settings.sync()
[docs]
def on_save_settings(self):
"""
on_save_settings
:return: None
"""
from jdxi_editor.globals import (
LILYPOND_ENABLED_KEY,
MIDI_SELECTION_VALIDATION_KEY,
SILENCE_MIDI_NOTE_LOGGING_KEY,
)
settings = self.settings
try:
from jdxi_editor.ui import text_size
settings.setValue(
text_size.SETTING, int(self.text_size_combo.currentData() or 100)
)
settings.setValue("logging", bool(self.logging_checkbox.isChecked()))
settings.setValue(
SILENCE_MIDI_NOTE_LOGGING_KEY,
bool(self.silence_midi_notes_checkbox.isChecked()),
)
settings.setValue(
MIDI_SELECTION_VALIDATION_KEY,
bool(self.midi_validation_checkbox.isChecked()),
)
from jdxi_editor.update.checker import AUTO_CHECK_KEY
settings.setValue(
AUTO_CHECK_KEY, bool(self.update_check_checkbox.isChecked())
)
settings.setValue(
LILYPOND_ENABLED_KEY, bool(self.lilypond_checkbox.isChecked())
)
settings.sync()
log_settings()
except Exception as ex:
print(f"Error {ex} occurred saving settings")