Fix up loading and saving of options; Pass on options to the renderer
This commit is contained in:
parent
27238aa0ca
commit
4ae8ac1397
@ -1,9 +1,19 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- 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
|
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):
|
class ConfigureDialog(QtWidgets.QDialog):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
@ -44,7 +54,6 @@ class ConfigureDialog(QtWidgets.QDialog):
|
|||||||
self.render_options_stack = QtWidgets.QStackedWidget(self)
|
self.render_options_stack = QtWidgets.QStackedWidget(self)
|
||||||
self.render_options_stack.setObjectName("render_options_stack")
|
self.render_options_stack.setObjectName("render_options_stack")
|
||||||
self.render_layout.addWidget(self.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 = QtWidgets.QDialogButtonBox(self)
|
||||||
self.button_box.setOrientation(QtCore.Qt.Horizontal)
|
self.button_box.setOrientation(QtCore.Qt.Horizontal)
|
||||||
self.button_box.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel |
|
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(1).setText(_translate('ConfigureDialog', 'Editor'))
|
||||||
self.category_list_widget.item(2).setText(_translate('ConfigureDialog', 'Render'))
|
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):
|
def setup_options(self):
|
||||||
self.option_widgets = {}
|
self.option_widgets = {}
|
||||||
|
self.option_values = {}
|
||||||
for group in get_option_groups():
|
for group in get_option_groups():
|
||||||
pretty_group = group.replace('_', ' ').title()
|
pretty_group = group.replace('_', ' ').title()
|
||||||
self.render_options_combobox.addItem(pretty_group)
|
self.render_options_combobox.addItem(pretty_group)
|
||||||
@ -74,26 +88,64 @@ class ConfigureDialog(QtWidgets.QDialog):
|
|||||||
page_layout = QtWidgets.QFormLayout(page_widget)
|
page_layout = QtWidgets.QFormLayout(page_widget)
|
||||||
for name, details in get_options(group).items():
|
for name, details in get_options(group).items():
|
||||||
pretty_name = name.replace(group, '').replace('_', ' ').title()
|
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:
|
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:
|
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:
|
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:
|
else:
|
||||||
self.option_widgets[name] = QtWidgets.QLineEdit(page_widget)
|
widget = QtWidgets.QLineEdit(page_widget)
|
||||||
page_layout.addRow(pretty_name, self.option_widgets[name])
|
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)
|
self.render_options_stack.addWidget(page_widget)
|
||||||
|
|
||||||
def load_settings(self):
|
def load_settings(self):
|
||||||
"""Load the settings"""
|
"""Load the settings"""
|
||||||
for name, details in get_options().items():
|
for name, details in get_options().items():
|
||||||
if self.settings.contains(name):
|
if self.settings.contains(name):
|
||||||
|
value = self.settings.value(name)
|
||||||
|
if value is None:
|
||||||
|
continue
|
||||||
if details["type"] is int:
|
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):
|
def save_settings(self):
|
||||||
"""Save the settings"""
|
"""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):
|
def exec(self):
|
||||||
"""Execute the dialog"""
|
"""Execute the dialog"""
|
||||||
|
@ -272,12 +272,23 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
|
|
||||||
def on_configure_clicked(self):
|
def on_configure_clicked(self):
|
||||||
"""Show the configuration dialog"""
|
"""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):
|
def on_text_changed(self):
|
||||||
"""Update the preview when the text changes"""
|
"""Update the preview when the text changes"""
|
||||||
|
options = self._get_render_options()
|
||||||
text = self.file_editor.text()
|
text = self.file_editor.text()
|
||||||
song = Song()
|
song = Song()
|
||||||
song.parse(text)
|
song.parse(text)
|
||||||
html = render(song)
|
html = render(song, options)
|
||||||
self.preview_view.setHtml(html)
|
self.preview_view.setHtml(html)
|
||||||
|
Loading…
Reference in New Issue
Block a user