-
Notifications
You must be signed in to change notification settings - Fork 219
/
ccb.py
87 lines (64 loc) · 3.23 KB
/
ccb.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
# Copyright 2005 Joe Wreschnig, Michael Urman
# 2012-22 Nick Boultbee
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
from gi.repository import Gtk
from quodlibet import config
class ConfigCheckButton(Gtk.CheckButton):
"""A CheckButton that connects to QL's config module, and toggles
a boolean configuration value when it is toggled.
It is initialised to the current config value if `populate` is set True."""
def __init__(self, label, section, option, populate=False, tooltip=None,
default=None):
super().__init__(label=label, use_underline=True)
if default is None:
default = config._config.defaults.getboolean(section, option, True)
if populate:
self.set_active(config.getboolean(section, option, default))
if tooltip:
self.set_tooltip_text(tooltip)
self.connect('toggled', ConfigCheckButton.__toggled, section, option)
def __toggled(self, section, option):
config.set(section, option, str(bool(self.get_active())).lower())
class ConfigSwitch(Gtk.Box):
"""A Switch that connects to QL's config module, and toggles
a boolean configuration value when it is toggled.
It is initialised to the current config value if `populate` is set True."""
def __init__(self, label, section, option, populate=False, tooltip=None,
default=None):
super().__init__()
self.label = Gtk.Label(label, use_underline=True)
self.switch = Gtk.Switch()
self.label.set_mnemonic_widget(self.switch)
self.pack_start(self.label, False, True, 0)
self.pack_end(self.switch, False, True, 0)
if default is None:
default = config._config.defaults.getboolean(section, option, True)
if populate:
self.set_active(config.getboolean(section, option, default))
if tooltip:
self.switch.set_tooltip_text(tooltip)
self.switch.connect('notify::active', self.__activated, section, option)
def set_active(self, value: bool):
self.switch.set_active(value)
def get_active(self) -> bool:
return self.switch.get_active()
def connect(self, *args, **kwargs):
self.switch.connect(*args, **kwargs)
def __activated(self, switch, state, section, option):
config.set(section, option, str(switch.get_active()).lower())
class ConfigCheckMenuItem(Gtk.CheckMenuItem):
"""A CheckMenuItem that connects to QL's config module, and toggles
a boolean configuration value when it is toggled.
It is initialised to the current config value if `populate` is set True."""
def __init__(self, label, section, option, populate=False, default=False):
super().__init__(
label=label, use_underline=True)
if populate:
self.set_active(config.getboolean(section, option, default))
self.connect('toggled', ConfigCheckMenuItem.__toggled, section, option)
def __toggled(self, section, option):
config.set(section, option, str(bool(self.get_active())).lower())