-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingsdlg.py
108 lines (94 loc) · 3.8 KB
/
settingsdlg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import re
from pathlib import Path
from PyQt5 import QtWidgets
from lib.modal import ModalDialog
from lib.applogger import logFilename
from lib.utilities import restoreFactorySettings
from settings import AppState, AppParams
class SettingsDialog(ModalDialog):
def __init__(self, app, parent, initialParams:object):
super().__init__(parent=parent, templatefile='ui/settingsdlg.ui')
self.app = app
self.params = initialParams
self.initialParams = initialParams.clone()
self.populateThemesCombobox()
self.refreshView()
# listen for changes
self.comboAskBeforeExit.currentTextChanged.connect(
lambda v: self.onChange('ask_before_exit', v.lower()))
self.comboAskBeforeDelete.currentTextChanged.connect(
lambda v: self.onChange('ask_before_delete', v.lower()))
self.comboTheme.currentTextChanged.connect(
lambda v: self.onChange('theme', v.lower()))
self.spinThumbnailsSize.valueChanged.connect(
lambda v: self.onChange('thumbnails_size', v))
self.checkboxOverwriteExistingExpansions.stateChanged.connect(
lambda v: self.onChange('install_overwrite', self.checkboxOverwriteExistingExpansions.isChecked()))
self.checkboxMonitorAlwaysShown.stateChanged.connect(
lambda v: self.onChange('monitor_always_visible', self.checkboxMonitorAlwaysShown.isChecked()))
self.leExtensions.textChanged.connect(
lambda v: self.onChange('package_extensions', tuple(self.extensionsFixup(v))))
self.spinProgressBarHideDelay.valueChanged.connect(
lambda v: self.onChange('monitor_hide_delay_sec', v))
self.comboLogLevel.currentTextChanged.connect(
lambda v: self.onChange('log_level', v.lower()))
self.btnFactoryReset.clicked.connect(self.resetApp)
# listen to the restore-defaults button
btnRestore = self.buttonBox.button(QtWidgets.QDialogButtonBox.RestoreDefaults)
btnRestore.clicked.connect(self.restore)
logfile = Path(logFilename()).absolute()
self.labelLogfileLink.setText(logfile, logfile.stem)
self.refreshButtons()
def accept(self):
super().accept()
def reject(self):
# restore initial params
self.params.copyFrom(self.initialParams)
super().reject()
def onChange(self, key:str, v:any) -> None:
setattr(self.params, key, v)
self.refreshButtons()
def refreshButtons(self):
self.spinProgressBarHideDelay.setEnabled(
not self.checkboxMonitorAlwaysShown.isChecked())
def restore(self):
self.params.copyFrom(self.initialParams)
self.refreshView()
def refreshView(self):
self.comboAskBeforeExit.setCurrentText(
self.params.ask_before_exit.title())
self.comboAskBeforeDelete.setCurrentText(
self.params.ask_before_delete.title())
self.comboTheme.setCurrentText(
self.params.theme.title())
self.spinThumbnailsSize.setValue(
self.params.thumbnails_size)
self.checkboxOverwriteExistingExpansions.setChecked(
self.params.install_overwrite)
self.checkboxMonitorAlwaysShown.setChecked(
self.params.monitor_always_visible)
self.leExtensions.setText(
','.join(self.params.package_extensions))
self.spinProgressBarHideDelay.setValue(
self.params.monitor_hide_delay_sec)
self.comboLogLevel.setCurrentText(
self.params.log_level.title())
def populateThemesCombobox(self):
self.comboTheme.clear()
for theme in self.app.theme.availableThemes():
self.comboTheme.addItem(theme.title())
def extensionsFixup(self, extensions:str) -> list[str]:
def validExtension(e:str) -> bool:
return bool(re.fullmatch(r'\.\w+', e))
def fixExtension(e:str) -> str:
# replace whatever preceeding the extension itself with a dot(.)
return re.sub(r'^[^\w]+', '.', e).lower()
# split
splitted = [ e for e in re.split(r'[,;\s]+', extensions) ]
# fix
fixed = [ fixExtension(e) for e in splitted if e ]
# filter out improperly formatted extensions
return [ e for e in fixed if validExtension(e) ]
def resetApp(self):
restoreFactorySettings()