How to create presets

Hi,

My motivation is to get these styles to presets.

Does somebody used presets for styling for an add-on? or knows an add-on that does? So a user can click or select the preset and there is nothing else to be done by the user. Alternatively, there is the possibility to make a user custom settings via add-on config.

I don’t know if there is an add-on that does it, but implementing that functionality is easy. Try the following:

  1. Create presets folder in the root of your add-on, then put some preset files(json) in the folder.
  2. Create load_preset.py, then put the following code into it:
    import pathlib
    import json
    import aqt
    from . import config
    
    def load_json(path: pathlib.Path):
        try:
            with path.open() as f:
                return json.load(f)
        except BaseException:
            return None
    
    @aqt.pyqtSlot(pathlib.Path)
    def on_preset_selected(path: pathlib.Path):
        preset = load_json(path)
        if preset:
            aqt.mw.addonManager.writeConfig(__name__, preset)
            config.update(None)
    
    def create_addon_menu() -> aqt.QMenu:
        addon_menu = aqt.QMenu('Show total review count in main screen', aqt.mw)
        preset_menu = addon_menu.addMenu('Choose a preset ...')
        presets_path = pathlib.Path(__file__).resolve().parent / 'presets'
        for file_path in sorted(presets_path.glob('*.json')):
            act = preset_menu.addAction(file_path.stem)
            act.triggered.connect(
                lambda _, path=file_path: on_preset_selected(path))
        return addon_menu
    
    aqt.mw.form.menuTools.addMenu(create_addon_menu())
    
  3. Import load_preset.py in __init__.py.

Below is a demo video(gif):
addon_load_preset

7 Likes

Wow, that is far more than I expected. Thank you so much for taking some of your time. I’ll try it out.

No problem, glad if it helped.

1 Like