Skip to content

Commit

Permalink
Introduce OptionPage.register_setting()
Browse files Browse the repository at this point in the history
- used to restore defaults on the page
- used to list widgets to highlight when using profiles (optional)

Basically it associates a setting editing page to settings it edits.
  • Loading branch information
zas committed Apr 25, 2024
1 parent b3b841d commit 6e09d52
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 153 deletions.
35 changes: 24 additions & 11 deletions picard/ui/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


import re

from PyQt6 import (
Expand All @@ -31,9 +30,13 @@
)

from picard import log
from picard.config import get_config
from picard.config import (
Option,
get_config,
)
from picard.i18n import gettext as _
from picard.plugin import ExtensionPoint
from picard.profile import register_profile_highlights


class OptionsCheckError(Exception):
Expand Down Expand Up @@ -67,6 +70,8 @@ def on_destroyed(obj=None):
self.deleted = True
self.destroyed.connect(on_destroyed)

self._registered_settings = []

def set_dialog(self, dialog):
self.dialog = dialog

Expand All @@ -80,17 +85,17 @@ def save(self):
pass

def restore_defaults(self):
try:
options = self.options
except AttributeError:
return
config = get_config()
old_options = {}
for option in options:
if option.section == 'setting' and config.setting[option.name] != option.default:
log.debug("Option %s %s: %r -> %r" % (self.NAME, option.name, config.setting[option.name], option.default))
old_options[option.name] = config.setting[option.name]
config.setting[option.name] = option.default
for name in self._registered_settings:
if not Option.exists('setting', name):
continue
default_value = Option.get_default('setting', name)
current_value = config.setting[name]
if current_value != default_value:
log.debug("Option %s %s: %r -> %r" % (self.NAME, name, current_value, default_value))
old_options[name] = current_value
config.setting[name] = default_value
self.load()
# Restore the config values incase the user doesn't save after restoring defaults
for key in old_options:
Expand Down Expand Up @@ -125,6 +130,14 @@ def live_checker(text):

regex_edit.textChanged.connect(live_checker)

def register_setting(self, name, highlights=None):
"""Register a setting edited in the page, used to restore defaults
and to highlight when profiles are used"""
self._registered_settings.append(name)
if highlights is not None:
page_name = self.PARENT if self.PARENT else self.NAME
register_profile_highlights(page_name, name, tuple(highlights))


_pages = ExtensionPoint(label='pages')

Expand Down
21 changes: 10 additions & 11 deletions picard/ui/options/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from picard.config import get_config
from picard.i18n import N_
from picard.profile import register_profile_highlights

from picard.ui.options import (
OptionsPage,
Expand All @@ -48,16 +47,16 @@ def __init__(self, parent=None):
self.ui.setupUi(self)
self.init_regex_checker(self.ui.ignore_regex, self.ui.regex_error)

register_profile_highlights('advanced', 'ignore_regex', ['ignore_regex'])
register_profile_highlights('advanced', 'ignore_hidden_files', ['ignore_hidden_files'])
register_profile_highlights('advanced', 'recursively_add_files', ['recursively_add_files'])
register_profile_highlights('advanced', 'ignore_track_duration_difference_under', ['ignore_track_duration_difference_under', 'label_track_duration_diff'])
register_profile_highlights('advanced', 'query_limit', ['query_limit', 'label_query_limit'])
register_profile_highlights('advanced', 'completeness_ignore_videos', ['completeness_ignore_videos'])
register_profile_highlights('advanced', 'completeness_ignore_pregap', ['completeness_ignore_pregap'])
register_profile_highlights('advanced', 'completeness_ignore_data', ['completeness_ignore_data'])
register_profile_highlights('advanced', 'completeness_ignore_silence', ['completeness_ignore_silence'])
register_profile_highlights('advanced', 'compare_ignore_tags', ['groupBox_ignore_tags'])
self.register_setting('ignore_regex', ['ignore_regex'])
self.register_setting('ignore_hidden_files', ['ignore_hidden_files'])
self.register_setting('recursively_add_files', ['recursively_add_files'])
self.register_setting('ignore_track_duration_difference_under', ['ignore_track_duration_difference_under', 'label_track_duration_diff'])
self.register_setting('query_limit', ['query_limit', 'label_query_limit'])
self.register_setting('completeness_ignore_videos', ['completeness_ignore_videos'])
self.register_setting('completeness_ignore_pregap', ['completeness_ignore_pregap'])
self.register_setting('completeness_ignore_data', ['completeness_ignore_data'])
self.register_setting('completeness_ignore_silence', ['completeness_ignore_silence'])
self.register_setting('compare_ignore_tags', ['groupBox_ignore_tags'])

def load(self):
config = get_config()
Expand Down
2 changes: 2 additions & 0 deletions picard/ui/options/cdlookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self, parent=None):
self._device_list = get_cdrom_drives()
self.ui.cd_lookup_device.addItems(self._device_list)

self.register_setting('cd_lookup_device')

def load(self):
config = get_config()
device = config.setting['cd_lookup_device']
Expand Down
17 changes: 8 additions & 9 deletions picard/ui/options/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
N_,
gettext as _,
)
from picard.profile import register_profile_highlights

from picard.ui.checkbox_list_item import CheckboxListItem
from picard.ui.moveable_list_view import MoveableListView
Expand Down Expand Up @@ -67,14 +66,14 @@ def __init__(self, parent=None):
self.move_view = MoveableListView(self.ui.ca_providers_list, self.ui.up_button,
self.ui.down_button)

register_profile_highlights('cover', 'save_images_to_tags', ['save_images_to_tags'])
register_profile_highlights('cover', 'embed_only_one_front_image', ['cb_embed_front_only'])
register_profile_highlights('cover', 'save_images_to_files', ['save_images_to_files'])
register_profile_highlights('cover', 'cover_image_filename', ['cover_image_filename'])
register_profile_highlights('cover', 'save_images_overwrite', ['save_images_overwrite'])
register_profile_highlights('cover', 'save_only_one_front_image', ['save_only_one_front_image'])
register_profile_highlights('cover', 'image_type_as_filename', ['image_type_as_filename'])
register_profile_highlights('cover', 'ca_providers', ['ca_providers_list'])
self.register_setting('save_images_to_tags', ['save_images_to_tags'])
self.register_setting('embed_only_one_front_image', ['cb_embed_front_only'])
self.register_setting('save_images_to_files', ['save_images_to_files'])
self.register_setting('cover_image_filename', ['cover_image_filename'])
self.register_setting('save_images_overwrite', ['save_images_overwrite'])
self.register_setting('save_only_one_front_image', ['save_only_one_front_image'])
self.register_setting('image_type_as_filename', ['image_type_as_filename'])
self.register_setting('ca_providers', ['ca_providers_list'])

def restore_defaults(self):
# Remove previous entries
Expand Down
7 changes: 7 additions & 0 deletions picard/ui/options/fingerprinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def __init__(self, parent=None):
self.ui.acoustid_apikey_get.clicked.connect(self.acoustid_apikey_get)
self.ui.acoustid_apikey.setValidator(ApiKeyValidator())

self.register_setting('fingerprinting_system')
self.register_setting('acoustid_fpcalc')
self.register_setting('acoustid_apikey')
self.register_setting('ignore_existing_acoustid_fingerprints')
self.register_setting('save_acoustid_fingerprints')
self.register_setting('fpcalc_threads')

def load(self):
config = get_config()
if config.setting['fingerprinting_system'] == 'acoustid':
Expand Down
21 changes: 11 additions & 10 deletions picard/ui/options/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
gettext as _,
gettext_constants,
)
from picard.profile import register_profile_highlights
from picard.util.mbserver import is_official_server

from picard.ui.options import (
Expand Down Expand Up @@ -73,15 +72,17 @@ def __init__(self, parent=None):
self.ui.login_error.setStyleSheet(self.STYLESHEET_ERROR)
self.ui.login_error.hide()
self.update_login_logout()
register_profile_highlights('general', 'server_host', ['server_host'])
register_profile_highlights('general', 'server_port', ['server_port'])
register_profile_highlights('general', 'analyze_new_files', ['analyze_new_files'])
register_profile_highlights('general', 'cluster_new_files', ['cluster_new_files'])
register_profile_highlights('general', 'ignore_file_mbids', ['ignore_file_mbids'])
register_profile_highlights('general', 'check_for_plugin_updates', ['check_for_plugin_updates'])
register_profile_highlights('general', 'check_for_updates', ['check_for_updates'])
register_profile_highlights('general', 'update_check_days', ['update_check_days'])
register_profile_highlights('general', 'update_level', ['update_level'])

self.register_setting('server_host', ['server_host'])
self.register_setting('server_port', ['server_port'])
self.register_setting('analyze_new_files', ['analyze_new_files'])
self.register_setting('cluster_new_files', ['cluster_new_files'])
self.register_setting('ignore_file_mbids', ['ignore_file_mbids'])
self.register_setting('check_for_plugin_updates', ['check_for_plugin_updates'])
self.register_setting('check_for_updates', ['check_for_updates'])
self.register_setting('update_check_days', ['update_check_days'])
self.register_setting('update_level', ['update_level'])
self.register_setting('use_server_for_submission')

def load(self):
config = get_config()
Expand Down
17 changes: 8 additions & 9 deletions picard/ui/options/genres.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
N_,
gettext as _,
)
from picard.profile import register_profile_highlights
from picard.track import TagGenreFilter

from picard.ui.options import (
Expand Down Expand Up @@ -111,14 +110,14 @@ def __init__(self, parent=None):
self.fmt_clear = QTextBlockFormat()
self.fmt_clear.clearBackground()

register_profile_highlights('metadata', 'use_genres', [])
register_profile_highlights('metadata', 'only_my_genres', ['only_my_genres'])
register_profile_highlights('metadata', 'artists_genres', ['artists_genres'])
register_profile_highlights('metadata', 'folksonomy_tags', ['folksonomy_tags'])
register_profile_highlights('metadata', 'min_genre_usage', ['min_genre_usage'])
register_profile_highlights('metadata', 'max_genres', ['max_genres'])
register_profile_highlights('metadata', 'join_genres', ['join_genres'])
register_profile_highlights('metadata', 'genres_filter', ['genres_filter'])
self.register_setting('use_genres', [])
self.register_setting('only_my_genres', ['only_my_genres'])
self.register_setting('artists_genres', ['artists_genres'])
self.register_setting('folksonomy_tags', ['folksonomy_tags'])
self.register_setting('min_genre_usage', ['min_genre_usage'])
self.register_setting('max_genres', ['max_genres'])
self.register_setting('join_genres', ['join_genres'])
self.register_setting('genres_filter', ['genres_filter'])

def load(self):
config = get_config()
Expand Down
27 changes: 13 additions & 14 deletions picard/ui/options/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
gettext as _,
gettext_constants,
)
from picard.profile import register_profile_highlights
from picard.util import strxfrm

from picard.ui.options import (
Expand Down Expand Up @@ -126,19 +125,19 @@ def fcmp(x):

self.ui.allow_multi_dirs_selection.stateChanged.connect(self.multi_selection_warning)

register_profile_highlights('interface', 'toolbar_show_labels', ['toolbar_show_labels'])
register_profile_highlights('interface', 'show_menu_icons', ['show_menu_icons'])
register_profile_highlights('interface', 'ui_language', ['ui_language', 'label'])
register_profile_highlights('interface', 'ui_theme', ['ui_theme', 'label_theme'])
register_profile_highlights('interface', 'allow_multi_dirs_selection', ['allow_multi_dirs_selection'])
register_profile_highlights('interface', 'builtin_search', ['builtin_search'])
register_profile_highlights('interface', 'use_adv_search_syntax', ['use_adv_search_syntax'])
register_profile_highlights('interface', 'show_new_user_dialog', ['new_user_dialog'])
register_profile_highlights('interface', 'quit_confirmation', ['quit_confirmation'])
register_profile_highlights('interface', 'file_save_warning', ['file_save_warning'])
register_profile_highlights('interface', 'filebrowser_horizontal_autoscroll', ['filebrowser_horizontal_autoscroll'])
register_profile_highlights('interface', 'starting_directory', ['starting_directory'])
register_profile_highlights('interface', 'starting_directory_path', ['starting_directory_path'])
self.register_setting('toolbar_show_labels', ['toolbar_show_labels'])
self.register_setting('show_menu_icons', ['show_menu_icons'])
self.register_setting('ui_language', ['ui_language', 'label'])
self.register_setting('ui_theme', ['ui_theme', 'label_theme'])
self.register_setting('allow_multi_dirs_selection', ['allow_multi_dirs_selection'])
self.register_setting('builtin_search', ['builtin_search'])
self.register_setting('use_adv_search_syntax', ['use_adv_search_syntax'])
self.register_setting('show_new_user_dialog', ['new_user_dialog'])
self.register_setting('quit_confirmation', ['quit_confirmation'])
self.register_setting('file_save_warning', ['file_save_warning'])
self.register_setting('filebrowser_horizontal_autoscroll', ['filebrowser_horizontal_autoscroll'])
self.register_setting('starting_directory', ['starting_directory'])
self.register_setting('starting_directory_path', ['starting_directory_path'])

def load(self):
# Don't display the multi-selection warning when loading values.
Expand Down
5 changes: 2 additions & 3 deletions picard/ui/options/interface_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
N_,
gettext as _,
)
from picard.profile import register_profile_highlights

from picard.ui.colors import interface_colors
from picard.ui.options import (
Expand Down Expand Up @@ -105,8 +104,8 @@ def __init__(self, parent=None):
self.colors_list = QtWidgets.QVBoxLayout()
self.ui.colors.setLayout(self.colors_list)

register_profile_highlights('interface', 'interface_colors', ['colors'])
register_profile_highlights('interface', 'interface_colors_dark', ['colors'])
self.register_setting('interface_colors', ['colors'])
self.register_setting('interface_colors_dark', ['colors'])

def update_color_selectors(self):
if self.colors_list:
Expand Down
3 changes: 1 addition & 2 deletions picard/ui/options/interface_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
N_,
gettext as _,
)
from picard.profile import register_profile_highlights
from picard.util import icontheme

from picard.ui import PicardDialog
Expand Down Expand Up @@ -141,7 +140,7 @@ def __init__(self, parent=None):
self.ui.down_button, self.update_action_buttons)
self.update_buttons = self.move_view.update_buttons

register_profile_highlights('interface', 'toolbar_layout', ['toolbar_layout_list'])
self.register_setting('toolbar_layout', ['toolbar_layout_list'])

def load(self):
self.populate_action_list()
Expand Down
3 changes: 1 addition & 2 deletions picard/ui/options/interface_top_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from picard.config import get_config
from picard.i18n import N_
from picard.profile import register_profile_highlights

from picard.ui.options import (
OptionsPage,
Expand All @@ -47,7 +46,7 @@ def __init__(self, parent=None):
self.ui = Ui_InterfaceTopTagsOptionsPage()
self.ui.setupUi(self)

register_profile_highlights('interface', 'metadatabox_top_tags', ['top_tags_groupBox'])
self.register_setting('metadatabox_top_tags', ['top_tags_groupBox'])

def load(self):
config = get_config()
Expand Down
3 changes: 1 addition & 2 deletions picard/ui/options/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
N_,
gettext as _,
)
from picard.profile import register_profile_highlights
from picard.util import open_local_path

from picard.ui.options import (
Expand Down Expand Up @@ -117,7 +116,7 @@ def __init__(self, parent=None):
self.ui.config_file.setPalette(palette_readonly)
self.last_valid_path = _safe_autobackup_dir('')

register_profile_highlights('advanced', 'autobackup_directory', ['autobackup_dir'])
self.register_setting('autobackup_directory', ['autobackup_dir'])

def get_current_autobackup_dir(self):
return _safe_autobackup_dir(self.ui.autobackup_dir.text())
Expand Down
7 changes: 3 additions & 4 deletions picard/ui/options/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from picard.config import get_config
from picard.i18n import N_
from picard.profile import register_profile_highlights

from picard.ui.options import (
OptionsPage,
Expand All @@ -49,9 +48,9 @@ def __init__(self, parent=None):
self.ui = Ui_MatchingOptionsPage()
self.ui.setupUi(self)

register_profile_highlights('advanced', 'file_lookup_threshold', ['file_lookup_threshold'])
register_profile_highlights('advanced', 'cluster_lookup_threshold', ['cluster_lookup_threshold'])
register_profile_highlights('advanced', 'track_matching_threshold', ['track_matching_threshold'])
self.register_setting('file_lookup_threshold', ['file_lookup_threshold'])
self.register_setting('cluster_lookup_threshold', ['cluster_lookup_threshold'])
self.register_setting('track_matching_threshold', ['track_matching_threshold'])

def load(self):
config = get_config()
Expand Down
25 changes: 12 additions & 13 deletions picard/ui/options/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
gettext as _,
gettext_constants,
)
from picard.profile import register_profile_highlights

from picard.ui import PicardDialog
from picard.ui.moveable_list_view import MoveableListView
Expand Down Expand Up @@ -97,18 +96,18 @@ def __init__(self, parent=None):
self.ui.translate_artist_names.stateChanged.connect(self.set_enabled_states)
self.ui.translate_artist_names_script_exception.stateChanged.connect(self.set_enabled_states)

register_profile_highlights('metadata', 'translate_artist_names', ['translate_artist_names'])
register_profile_highlights('metadata', 'artist_locales', ['selected_locales'])
register_profile_highlights('metadata', 'translate_artist_names_script_exception', ['translate_artist_names_script_exception'])
register_profile_highlights('metadata', 'script_exceptions', ['selected_scripts'])
register_profile_highlights('metadata', 'standardize_artists', ['standardize_artists'])
register_profile_highlights('metadata', 'standardize_instruments', ['standardize_instruments'])
register_profile_highlights('metadata', 'convert_punctuation', ['convert_punctuation'])
register_profile_highlights('metadata', 'release_ars', ['release_ars'])
register_profile_highlights('metadata', 'track_ars', ['track_ars'])
register_profile_highlights('metadata', 'guess_tracknumber_and_title', ['guess_tracknumber_and_title'])
register_profile_highlights('metadata', 'va_name', ['va_name'])
register_profile_highlights('metadata', 'nat_name', ['nat_name'])
self.register_setting('translate_artist_names', ['translate_artist_names'])
self.register_setting('artist_locales', ['selected_locales'])
self.register_setting('translate_artist_names_script_exception', ['translate_artist_names_script_exception'])
self.register_setting('script_exceptions', ['selected_scripts'])
self.register_setting('standardize_artists', ['standardize_artists'])
self.register_setting('standardize_instruments', ['standardize_instruments'])
self.register_setting('convert_punctuation', ['convert_punctuation'])
self.register_setting('release_ars', ['release_ars'])
self.register_setting('track_ars', ['track_ars'])
self.register_setting('guess_tracknumber_and_title', ['guess_tracknumber_and_title'])
self.register_setting('va_name', ['va_name'])
self.register_setting('nat_name', ['nat_name'])

def load(self):
config = get_config()
Expand Down
Loading

0 comments on commit 6e09d52

Please sign in to comment.