From 4ae8ac13972d832919608beb8079693e8e33840f Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Tue, 3 Aug 2021 10:40:47 -0700 Subject: [PATCH] Fix up loading and saving of options; Pass on options to the renderer --- src/ukatali/configuredialog.py | 68 ++++++++++++++++++++++++++++++---- src/ukatali/mainwindow.py | 15 +++++++- 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/ukatali/configuredialog.py b/src/ukatali/configuredialog.py index eaae727..6859c61 100644 --- a/src/ukatali/configuredialog.py +++ b/src/ukatali/configuredialog.py @@ -1,9 +1,19 @@ # -*- coding: utf-8 -*- -from PyQt5 import QtCore, QtWidgets +from functools import partial + +from PyQt5 import QtCore, QtGui, QtWidgets from chordpro.renderers.html import get_options, get_option_groups +def _coerce_bool(value): + """Coerce a value to be a boolean""" + if isinstance(value, str): + return value[0].lower() in ['t', 'y', '1'] + else: + return bool(value) + + class ConfigureDialog(QtWidgets.QDialog): def __init__(self, parent=None): super().__init__(parent) @@ -44,7 +54,6 @@ class ConfigureDialog(QtWidgets.QDialog): self.render_options_stack = QtWidgets.QStackedWidget(self) self.render_options_stack.setObjectName("render_options_stack") self.render_layout.addWidget(self.render_options_stack) - # self.renderer_page.setLayout(self.render_layout) self.button_box = QtWidgets.QDialogButtonBox(self) self.button_box.setOrientation(QtCore.Qt.Horizontal) self.button_box.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | @@ -65,8 +74,13 @@ class ConfigureDialog(QtWidgets.QDialog): self.category_list_widget.item(1).setText(_translate('ConfigureDialog', 'Editor')) self.category_list_widget.item(2).setText(_translate('ConfigureDialog', 'Render')) + def _set_value(self, name, value): + """Set an option value""" + self.option_values[name] = value + def setup_options(self): self.option_widgets = {} + self.option_values = {} for group in get_option_groups(): pretty_group = group.replace('_', ' ').title() self.render_options_combobox.addItem(pretty_group) @@ -74,26 +88,64 @@ class ConfigureDialog(QtWidgets.QDialog): page_layout = QtWidgets.QFormLayout(page_widget) for name, details in get_options(group).items(): pretty_name = name.replace(group, '').replace('_', ' ').title() + self.option_values[name] = details['default'] + widget = None + set_value = partial(self._set_value, name) if details["type"] is int: - self.option_widgets[name] = QtWidgets.QSpinBox(page_widget) + widget = QtWidgets.QSpinBox(page_widget) + if details['default']: + widget.setValue(details['default']) + widget.valueChanged.connect(set_value) elif details["type"] is bool: - self.option_widgets[name] = QtWidgets.QCheckBox(page_widget) + widget = QtWidgets.QCheckBox(page_widget) + widget.setChecked(_coerce_bool(details['default'])) + widget.toggled.connect(set_value) elif 'font' in name: - self.option_widgets[name] = QtWidgets.QFontComboBox(page_widget) + widget = QtWidgets.QFontComboBox(page_widget) + if details['default']: + widget.setFont(QtGui.QFont(details['default'])) + widget.currentFontChanged.connect(set_value) else: - self.option_widgets[name] = QtWidgets.QLineEdit(page_widget) - page_layout.addRow(pretty_name, self.option_widgets[name]) + widget = QtWidgets.QLineEdit(page_widget) + if details['default']: + widget.setText(details['default']) + widget.textChanged.connect(set_value) + if widget: + widget.setObjectName(name) + self.option_widgets[name] = widget + page_layout.addRow(pretty_name, widget) + if details['description']: + label = QtWidgets.QLabel(page_widget) + label.setText(details['description']) + page_layout.addRow('', label) self.render_options_stack.addWidget(page_widget) def load_settings(self): """Load the settings""" for name, details in get_options().items(): if self.settings.contains(name): + value = self.settings.value(name) + if value is None: + continue if details["type"] is int: - self.option_widgets[name].setValue(self.settings.value(name)) + value = int(value) + self.option_widgets[name].setValue(value) + elif details['type'] is bool: + value = _coerce_bool(value) + self.option_widgets[name].setChecked(value) + elif 'font' in name: + value = QtGui.QFont(value) + self.option_widgets[name].setCurrentFont(value) + else: + self.option_widgets[name].setText(value) + self.option_values[name] = value def save_settings(self): """Save the settings""" + for name, value in self.option_values.items(): + if isinstance(value, QtGui.QFont): + value = value.family() + self.settings.setValue(name, value) def exec(self): """Execute the dialog""" diff --git a/src/ukatali/mainwindow.py b/src/ukatali/mainwindow.py index 14f65eb..c9fe6b0 100644 --- a/src/ukatali/mainwindow.py +++ b/src/ukatali/mainwindow.py @@ -272,12 +272,23 @@ class MainWindow(QtWidgets.QMainWindow): def on_configure_clicked(self): """Show the configuration dialog""" - self.configure_dialog.exec() + if self.configure_dialog.exec() == QtWidgets.QDialog.Accepted: + self.on_text_changed() + + def _get_render_options(self): + """Get all the render options from the settings""" + options = {} + settings = QtCore.QSettings() + settings.beginGroup('render') + for key in settings.allKeys(): + options[key] = settings.value(key) + return options def on_text_changed(self): """Update the preview when the text changes""" + options = self._get_render_options() text = self.file_editor.text() song = Song() song.parse(text) - html = render(song) + html = render(song, options) self.preview_view.setHtml(html)