Merge pull request #158 from MightyCreak/fix-prefs-crash

Fix preferences crash
This commit is contained in:
Creak 2022-04-15 13:00:38 -04:00 committed by GitHub
commit c48ed957c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 20 deletions

View File

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Translation: added French translation - Translation: added French translation
### Fixed ### Fixed
- Fixed #156: Preferences cannot be saved in latest release
- Cleanups: use constructors instead of `new()` whenever possible in GTK - Cleanups: use constructors instead of `new()` whenever possible in GTK
classes classes

View File

@ -208,13 +208,13 @@ class Preferences:
if len(a) > 0: if len(a) > 0:
p = a[0] p = a[0]
if len(a) == 2 and p in self.bool_prefs: if len(a) == 2 and p in self.bool_prefs:
self.bool_prefs[p] = (a[1] == 'True') self.setBool(p, a[1] == 'True')
elif len(a) == 2 and p in self.int_prefs: elif len(a) == 2 and p in self.int_prefs:
self.int_prefs[p] = max( self.setInt(p, max(
self.int_prefs_min[p], self.int_prefs_min[p],
min(int(a[1]), self.int_prefs_max[p])) min(int(a[1]), self.int_prefs_max[p])))
elif len(a) == 2 and p in self.string_prefs: elif len(a) == 2 and p in self.string_prefs:
self.string_prefs[p] = a[1] self.setString(p, a[1])
else: else:
raise ValueError() raise ValueError()
except ValueError: except ValueError:
@ -237,13 +237,13 @@ class Preferences:
self._initFromTemplate(template[i]) self._initFromTemplate(template[i])
i += 1 i += 1
elif template[0] == 'Boolean': elif template[0] == 'Boolean':
self.bool_prefs[template[1]] = template[2] self.setBool(template[1], template[2])
elif template[0] == 'Integer': elif template[0] == 'Integer':
self.int_prefs[template[1]] = template[2] self.setInt(template[1], template[2])
self.int_prefs_min[template[1]] = template[4] self.int_prefs_min[template[1]] = template[4]
self.int_prefs_max[template[1]] = template[5] self.int_prefs_max[template[1]] = template[5]
elif template[0] in ['String', 'File', 'Font', 'Encoding']: elif template[0] in ['String', 'File', 'Font', 'Encoding']:
self.string_prefs[template[1]] = template[2] self.setString(template[1], template[2])
# callback used when a preference is toggled # callback used when a preference is toggled
def _toggled_cb(self, widget, widgets, name): def _toggled_cb(self, widget, widgets, name):
@ -273,11 +273,12 @@ class Preferences:
accept = (dialog.run() == Gtk.ResponseType.OK) accept = (dialog.run() == Gtk.ResponseType.OK)
if accept: if accept:
for k in self.bool_prefs: for k in self.bool_prefs:
self.bool_prefs[k] = widgets[k].get_active() self.setBool(k, widgets[k].get_active())
for k in self.int_prefs: for k in self.int_prefs:
self.int_prefs[k] = widgets[k].get_value_as_int() self.setInt(k, widgets[k].get_value_as_int())
for k in self.string_prefs: for k in self.string_prefs:
self.string_prefs[k] = utils.null_to_empty(widgets[k].get_text()) text = self._getWidgetText(widgets[k])
self.setString(k, utils.null_to_empty(text))
try: try:
ss = [] ss = []
for k, bool_value in self.bool_prefs.items(): for k, bool_value in self.bool_prefs.items():
@ -329,7 +330,7 @@ class Preferences:
w.show() w.show()
elif tpl_section == 'Boolean': elif tpl_section == 'Boolean':
button = Gtk.CheckButton.new_with_mnemonic(tpl[3]) button = Gtk.CheckButton.new_with_mnemonic(tpl[3])
button.set_active(self.bool_prefs[tpl[1]]) button.set_active(self.getBool(tpl[1]))
widgets[tpl[1]] = button widgets[tpl[1]] = button
table.attach(button, 1, i, 1, 1) table.attach(button, 1, i, 1, 1)
button.connect('toggled', self._toggled_cb, widgets, tpl[1]) button.connect('toggled', self._toggled_cb, widgets, tpl[1])
@ -340,14 +341,14 @@ class Preferences:
label.set_yalign(0.5) label.set_yalign(0.5)
table.attach(label, 0, i, 1, 1) table.attach(label, 0, i, 1, 1)
label.show() label.show()
if tpl[0] in ['Font', 'Integer']: if tpl_section in ['Font', 'Integer']:
entry = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0) entry = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
if tpl[0] == 'Font': if tpl_section == 'Font':
button = Gtk.FontButton() button = Gtk.FontButton()
button.set_font(self.string_prefs[tpl[1]]) button.set_font(self.getString(tpl[1]))
else: else:
adj = Gtk.Adjustment( adj = Gtk.Adjustment(
value=self.int_prefs[tpl[1]], value=self.getInt(tpl[1]),
lower=tpl[4], lower=tpl[4],
upper=tpl[5], upper=tpl[5],
step_increment=1, step_increment=1,
@ -361,20 +362,34 @@ class Preferences:
entry.pack_start(button, False, False, 0) entry.pack_start(button, False, False, 0)
button.show() button.show()
else: else:
if tpl[0] == 'Encoding': if tpl_section == 'Encoding':
entry = utils.EncodingMenu(self) entry = utils.EncodingMenu(self)
entry.set_text(tpl[3]) entry.set_text(tpl[3])
elif tpl[0] == 'File': elif tpl_section == 'File':
entry = _FileEntry(parent, tpl[3]) entry = _FileEntry(parent, tpl[3])
else: else:
entry = Gtk.Entry() entry = Gtk.Entry()
widgets[tpl[1]] = entry widgets[tpl[1]] = entry
entry.set_text(self.string_prefs[tpl[1]]) entry.set_text(self.getString(tpl[1]))
table.attach(entry, 1, i, 1, 1) table.attach(entry, 1, i, 1, 1)
entry.show() entry.show()
table.show() table.show()
return table return table
def _getWidgetText(self, widget):
text = ""
if (
isinstance(widget, Gtk.Entry) or
isinstance(widget, utils.EncodingMenu) or
isinstance(widget, _FileEntry)
):
text = widget.get_text()
elif isinstance(widget, Gtk.FontButton):
text = widget.get_font()
else:
raise TypeError(f"Don't know how to get text from type: {type(widget)}")
return text
# get/set methods to manipulate the preference values # get/set methods to manipulate the preference values
def getBool(self, name: str) -> bool: def getBool(self, name: str) -> bool:
return self.bool_prefs[name] return self.bool_prefs[name]
@ -385,6 +400,9 @@ class Preferences:
def getInt(self, name: str) -> int: def getInt(self, name: str) -> int:
return self.int_prefs[name] return self.int_prefs[name]
def setInt(self, name: str, value: int) -> None:
self.int_prefs[name] = value
def getString(self, name: str) -> str: def getString(self, name: str) -> str:
return self.string_prefs[name] return self.string_prefs[name]
@ -395,10 +413,10 @@ class Preferences:
return self.encodings return self.encodings
def _getDefaultEncodings(self) -> List[str]: def _getDefaultEncodings(self) -> List[str]:
return self.string_prefs['encoding_auto_detect_codecs'].split() return self.getString('encoding_auto_detect_codecs').split()
def getDefaultEncoding(self) -> str: def getDefaultEncoding(self) -> str:
return self.string_prefs['encoding_default_codec'] return self.getString('encoding_default_codec')
# attempt to convert a string to unicode from an unknown encoding # attempt to convert a string to unicode from an unknown encoding
def convertToUnicode(self, s): def convertToUnicode(self, s):