11import logging
2+ from typing import Dict
3+ from playhouse import signals
24from PyQt5 import uic
35from PyQt5 .QtCore import Qt
46from PyQt5 .QtWidgets import QApplication , QCheckBox , QFormLayout , QHBoxLayout , QLabel , QSizePolicy , QSpacerItem
@@ -22,6 +24,8 @@ def __init__(self, parent=None):
2224 """Init."""
2325 super ().__init__ (parent )
2426 self .setupUi (parent )
27+ self .settings_checkboxes : Dict [str , QCheckBox ] = {}
28+
2529 self .versionLabel .setText (__version__ )
2630 self .logLink .setText (
2731 f'<a href="file://{ LOG_DIR } "><span style="text-decoration:' 'underline; color:#0984e3;">Log</span></a>'
@@ -34,6 +38,7 @@ def __init__(self, parent=None):
3438 self .checkboxLayout .setFieldGrowthPolicy (QFormLayout .FieldGrowthPolicy .FieldsStayAtSizeHint )
3539 self .checkboxLayout .setFormAlignment (Qt .AlignmentFlag .AlignHCenter )
3640 self .tooltip_buttons = []
41+ signals .post_save .connect (self .on_setting_update , sender = SettingsModel )
3742
3843 self .populate ()
3944
@@ -86,8 +91,8 @@ def populate(self):
8691
8792 # create widget
8893 cb = QCheckBox (translate ('settings' , setting .label ))
94+ cb .setChecked (setting .value )
8995 cb .setToolTip (setting .tooltip )
90- cb .setCheckState (setting .value )
9196 cb .setTristate (False )
9297 cb .stateChanged .connect (lambda v , key = setting .key : self .save_setting (key , v ))
9398
@@ -101,6 +106,7 @@ def populate(self):
101106 cbl .addItem (QSpacerItem (0 , 0 , hPolicy = QSizePolicy .Policy .Expanding ))
102107
103108 # add widget
109+ self .settings_checkboxes [setting .key ] = cb
104110 self .checkboxLayout .setLayout (i , QFormLayout .ItemRole .FieldRole , cbl )
105111 self .tooltip_buttons .append (tb )
106112
@@ -109,6 +115,39 @@ def populate(self):
109115
110116 self .set_icons ()
111117
118+ def on_setting_update (self , sender , instance : SettingsModel , created = False ):
119+ """
120+ Handle a update of the settings db.
121+ Non-PyQt slot for peewee's `playhouse.signals` api.
122+ It calls `update_checkbox`.
123+
124+ Parameters
125+ ----------
126+ sender : Type[SettingsModel]
127+ table sending model
128+ instance : SettingsModel
129+ The model instance (row) saved.
130+ created : bool, optional
131+ Whether it was newly created, by default False
132+ """
133+ if not created and instance .type == 'checkbox' :
134+ self .update_checkbox (instance .key , instance .value )
135+
136+ def update_checkbox (self , key , value ):
137+ """
138+ Update the checkbox for a setting with a given key.
139+
140+ Parameters
141+ ----------
142+ key : str
143+ The key of the setting to update.
144+ value : bool
145+ The value to set the checkbox to.
146+ """
147+ checkbox = self .settings_checkboxes .get (key )
148+ if checkbox :
149+ checkbox .setChecked (value )
150+
112151 def set_icons (self ):
113152 """Set or update the icons in this view."""
114153 for button in self .tooltip_buttons :
0 commit comments