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
57from vorta ._version import __version__
8+ from vorta .autostart import open_app_at_startup
69from vorta .config import LOG_DIR
710from vorta .i18n import translate
811from vorta .store .models import BackupProfileMixin , SettingsModel
@@ -22,6 +25,8 @@ def __init__(self, parent=None):
2225 """Init."""
2326 super ().__init__ (parent )
2427 self .setupUi (parent )
28+ self .settings_checkboxes : Dict [str , QCheckBox ] = {}
29+
2530 self .versionLabel .setText (__version__ )
2631 self .logLink .setText (
2732 f'<a href="file://{ LOG_DIR } "><span style="text-decoration:' 'underline; color:#0984e3;">Log</span></a>'
@@ -34,6 +39,7 @@ def __init__(self, parent=None):
3439 self .checkboxLayout .setFieldGrowthPolicy (QFormLayout .FieldGrowthPolicy .FieldsStayAtSizeHint )
3540 self .checkboxLayout .setFormAlignment (Qt .AlignmentFlag .AlignHCenter )
3641 self .tooltip_buttons = []
42+ signals .post_save .connect (self .on_setting_update , sender = SettingsModel )
3743
3844 self .populate ()
3945
@@ -86,8 +92,8 @@ def populate(self):
8692
8793 # create widget
8894 cb = QCheckBox (translate ('settings' , setting .label ))
95+ cb .setChecked (setting .value )
8996 cb .setToolTip (setting .tooltip )
90- cb .setCheckState (setting .value )
9197 cb .setTristate (False )
9298 cb .stateChanged .connect (lambda v , key = setting .key : self .save_setting (key , v ))
9399
@@ -101,6 +107,7 @@ def populate(self):
101107 cbl .addItem (QSpacerItem (0 , 0 , hPolicy = QSizePolicy .Policy .Expanding ))
102108
103109 # add widget
110+ self .settings_checkboxes [setting .key ] = cb
104111 self .checkboxLayout .setLayout (i , QFormLayout .ItemRole .FieldRole , cbl )
105112 self .tooltip_buttons .append (tb )
106113
@@ -109,6 +116,41 @@ def populate(self):
109116
110117 self .set_icons ()
111118
119+ def on_setting_update (self , sender , instance : SettingsModel , created = False ):
120+ """
121+ Handle a update of the settings db.
122+ Non-PyQt slot for peewee's `playhouse.signals` api.
123+ It calls `update_checkbox`.
124+
125+ Parameters
126+ ----------
127+ sender : Type[SettingsModel]
128+ table sending model
129+ instance : SettingsModel
130+ The model instance (row) saved.
131+ created : bool, optional
132+ Whether it was newly created, by default False
133+ """
134+ if not created and instance .type == 'checkbox' :
135+ self .update_checkbox (instance .key , instance .value )
136+ if instance .key == 'autostart' :
137+ open_app_at_startup (instance .value )
138+
139+ def update_checkbox (self , key , value ):
140+ """
141+ Update the checkbox for a setting with a given key.
142+
143+ Parameters
144+ ----------
145+ key : str
146+ The key of the setting to update.
147+ value : bool
148+ The value to set the checkbox to.
149+ """
150+ checkbox = self .settings_checkboxes .get (key )
151+ if checkbox :
152+ checkbox .setChecked (value )
153+
112154 def set_icons (self ):
113155 """Set or update the icons in this view."""
114156 for button in self .tooltip_buttons :
0 commit comments