Skip to content

Commit

Permalink
gtk template: settings - plugin settings items
Browse files Browse the repository at this point in the history
this also contains some refactoring to make the dataflow more direct
  • Loading branch information
deltragon committed Aug 7, 2024
1 parent 7281ee1 commit 8643580
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 54 deletions.
6 changes: 3 additions & 3 deletions safeeyes/glade/item_bool.glade
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<interface>
<interface domain="safeeyes">
<requires lib="gtk+" version="3.12"/>
<object class="GtkBox" id="box">
<template parent="GtkBox" class="BoolItem">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
Expand Down Expand Up @@ -57,5 +57,5 @@
<property name="position">1</property>
</packing>
</child>
</object>
</template>
</interface>
6 changes: 3 additions & 3 deletions safeeyes/glade/item_int.glade
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<interface>
<interface domain="safeeyes">
<requires lib="gtk+" version="3.12"/>
<object class="GtkAdjustment" id="adjustment_value">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkBox" id="box">
<template parent="GtkBox" class="IntItem">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
Expand Down Expand Up @@ -62,5 +62,5 @@
<property name="position">1</property>
</packing>
</child>
</object>
</template>
</interface>
6 changes: 3 additions & 3 deletions safeeyes/glade/item_text.glade
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<interface>
<interface domain="safeeyes">
<requires lib="gtk+" version="3.12"/>
<object class="GtkBox" id="box">
<template parent="GtkBox" class="TextItem">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
Expand Down Expand Up @@ -56,5 +56,5 @@
<property name="position">1</property>
</packing>
</child>
</object>
</template>
</interface>
108 changes: 65 additions & 43 deletions safeeyes/ui/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,58 @@ def on_properties_clicked(self, button):
self.on_properties()


@Gtk.Template(filename=SETTINGS_ITEM_INT_GLADE)
class IntItem(Gtk.Box):
__gtype_name__ = "IntItem"

lbl_name = Gtk.Template.Child()
spin_value = Gtk.Template.Child()

def __init__(self, name, value, min_value, max_value):
super().__init__()

self.lbl_name.set_label(_(name))
self.spin_value.set_range(min_value, max_value)
self.spin_value.set_value(value)

def get_value(self):
return self.spin_value.get_value()


@Gtk.Template(filename=SETTINGS_ITEM_TEXT_GLADE)
class TextItem(Gtk.Box):
__gtype_name__ = "TextItem"

lbl_name = Gtk.Template.Child()
txt_value = Gtk.Template.Child()

def __init__(self, name, value):
super().__init__()

self.lbl_name.set_label(_(name))
self.txt_value.set_text(value)

def get_value(self):
return self.txt_value.get_text()


@Gtk.Template(filename=SETTINGS_ITEM_BOOL_GLADE)
class BoolItem(Gtk.Box):
__gtype_name__ = "BoolItem"

lbl_name = Gtk.Template.Child()
switch_value = Gtk.Template.Child()

def __init__(self, name, value):
super().__init__()

self.lbl_name.set_label(_(name))
self.switch_value.set_active(value)

def get_value(self):
return self.switch_value.get_active()


@Gtk.Template(filename=SETTINGS_DIALOG_PLUGIN_GLADE)
class PluginSettingsDialog(Gtk.Window):
"""
Expand All @@ -440,59 +492,29 @@ def __init__(self, config, parent):

for setting in config.get('settings'):
if setting['type'].upper() == 'INT':
self.box_settings.pack_start(self.__load_int_item(setting['label'], setting['id'], setting['safeeyes_config'], setting.get('min', 0), setting.get('max', 120)), False, False, 0)
box = IntItem(
setting['label'],
config['active_plugin_config'][setting['id']],
setting.get('min', 0),
setting.get('max', 120)
)
elif setting['type'].upper() == 'TEXT':
self.box_settings.pack_start(self.__load_text_item(setting['label'], setting['id'], setting['safeeyes_config']), False, False, 0)
box = TextItem(setting['label'], config['active_plugin_config'][setting['id']])
elif setting['type'].upper() == 'BOOL':
self.box_settings.pack_start(self.__load_bool_item(setting['label'], setting['id'], setting['safeeyes_config']), False, False, 0)

def __load_int_item(self, name, key, settings, min_value, max_value):
"""
Load the UI control for int property.
"""
builder = utility.create_gtk_builder(SETTINGS_ITEM_INT_GLADE)
builder.get_object('lbl_name').set_label(_(name))
spin_value = builder.get_object('spin_value')
spin_value.set_range(min_value, max_value)
spin_value.set_value(settings[key])
box = builder.get_object('box')
box.set_visible(True)
self.property_controls.append({'key': key, 'settings': settings, 'value': spin_value.get_value})
return box

def __load_text_item(self, name, key, settings):
"""
Load the UI control for text property.
"""
builder = utility.create_gtk_builder(SETTINGS_ITEM_TEXT_GLADE)
builder.get_object('lbl_name').set_label(_(name))
txt_value = builder.get_object('txt_value')
txt_value.set_text(settings[key])
box = builder.get_object('box')
box.set_visible(True)
self.property_controls.append({'key': key, 'settings': settings, 'value': txt_value.get_text})
return box
box = BoolItem(setting['label'], config['active_plugin_config'][setting['id']])
else:
continue

def __load_bool_item(self, name, key, settings):
"""
Load the UI control for boolean property.
"""
builder = utility.create_gtk_builder(SETTINGS_ITEM_BOOL_GLADE)
builder.get_object('lbl_name').set_label(_(name))
switch_value = builder.get_object('switch_value')
switch_value.set_active(settings[key])
box = builder.get_object('box')
box.set_visible(True)
self.property_controls.append({'key': key, 'settings': settings, 'value': switch_value.get_active})
return box
self.property_controls.append({'key': setting['id'], 'box': box})
self.box_settings.pack_start(box, False, False, 0)

@Gtk.Template.Callback()
def on_window_delete(self, *args):
"""
Event handler for Properties dialog close action.
"""
for property_control in self.property_controls:
property_control['settings'][property_control['key']] = property_control['value']()
self.config['active_plugin_config'][property_control['key']] = property_control['box'].get_value()
self.destroy()

def show(self):
Expand Down
4 changes: 2 additions & 2 deletions safeeyes/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def load_plugins_config(safeeyes_config):
config['id'] = plugin['id']
config['icon'] = icon
config['enabled'] = plugin['enabled']
for setting in config['settings']:
setting['safeeyes_config'] = plugin['settings']
config['active_plugin_config'] = plugin.get('settings')

configs.append(config)
return configs

Expand Down

0 comments on commit 8643580

Please sign in to comment.