# -*- python-mode -*- # -*- coding: UTF-8 -*- ## Copyright (C) 2012-2013 Daniel Pavel ## ## 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. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from __future__ import absolute_import, division, print_function, unicode_literals from threading import Timer as _Timer from gi.repository import GLib, Gtk from logitech_receiver.settings import KIND as _SETTING_KIND from solaar.i18n import _ from solaar.ui import ui_async as _ui_async # # # def _read_async(setting, force_read, sbox, device_is_online): def _do_read(s, force, sb, online): v = s.read(not force) GLib.idle_add(_update_setting_item, sb, v, online, priority=99) _ui_async(_do_read, setting, force_read, sbox, device_is_online) def _write_async(setting, value, sbox): failed, spinner, control = _get_failed_spinner_control(sbox) control.set_sensitive(False) failed.set_visible(False) spinner.set_visible(True) spinner.start() def _do_write(s, v, sb): v = setting.write(v) GLib.idle_add(_update_setting_item, sb, v, True, priority=99) _ui_async(_do_write, setting, value, sbox) def _write_async_key_value(setting, key, value, sbox): failed, spinner, control = _get_failed_spinner_control(sbox) control.set_sensitive(False) failed.set_visible(False) spinner.set_visible(True) spinner.start() def _do_write_key_value(s, k, v, sb): v = setting.write_key_value(k, v) GLib.idle_add(_update_setting_item, sb, {k: v}, True, priority=99) _ui_async(_do_write_key_value, setting, key, value, sbox) def _write_async_item_value(setting, item, value, sbox): failed, spinner, control = _get_failed_spinner_control(sbox) control.set_sensitive(False) failed.set_visible(False) spinner.set_visible(True) spinner.start() def _do_write_item_value(s, k, v, sb): v = setting.write_item_value(k, v) GLib.idle_add(_update_setting_item, sb, {k: v}, True, priority=99) _ui_async(_do_write_item_value, setting, item, value, sbox) # # # def _create_toggle_control(setting): def _switch_notify(switch, _ignore, s): if switch.get_sensitive(): _write_async(s, switch.get_active() is True, switch.get_parent()) c = Gtk.Switch() c.connect('notify::active', _switch_notify, setting) return c def _create_choice_control(setting): def _combo_notify(cbbox, s): if cbbox.get_sensitive(): _write_async(s, cbbox.get_active_id(), cbbox.get_parent()) c = Gtk.ComboBoxText() # TODO i18n text entries for entry in setting.choices: c.append(str(int(entry)), str(entry)) c.connect('changed', _combo_notify, setting) return c def _create_map_choice_control(setting): def _map_value_notify_key(cbbox, s): setting, valueBox = s key_choice = int(cbbox.get_active_id()) if cbbox.get_sensitive(): valueBox.remove_all() _map_populate_value_box(valueBox, setting, key_choice) def _map_value_notify_value(cbbox, s): setting, keyBox = s key_choice = keyBox.get_active_id() if key_choice is not None and cbbox.get_sensitive() and cbbox.get_active_id(): if setting._value.get(key_choice) != int(cbbox.get_active_id()): setting._value[key_choice] = int(cbbox.get_active_id()) _write_async_key_value(setting, key_choice, setting._value[key_choice], cbbox.get_parent().get_parent()) def _map_populate_value_box(valueBox, setting, key_choice): choices = None choices = setting.choices[key_choice] current = setting._value.get(str(key_choice)) if setting._value else None if choices: # TODO i18n text entries for choice in choices: valueBox.append(str(int(choice)), str(choice)) if current is not None: valueBox.set_active_id(str(int(current))) c = Gtk.HBox(homogeneous=False, spacing=6) keyBox = Gtk.ComboBoxText() valueBox = Gtk.ComboBoxText() c.pack_start(keyBox, False, False, 0) c.pack_end(valueBox, False, False, 0) # TODO i18n text entries for entry in setting.choices: keyBox.append(str(int(entry)), str(entry)) keyBox.set_active(0) keyBox.connect('changed', _map_value_notify_key, (setting, valueBox)) _map_populate_value_box(valueBox, setting, int(keyBox.get_active_id())) valueBox.connect('changed', _map_value_notify_value, (setting, keyBox)) return c def _create_slider_control(setting): class SliderControl: __slots__ = ('gtk_range', 'timer', 'setting') def __init__(self, setting): self.setting = setting self.timer = None self.gtk_range = Gtk.Scale() self.gtk_range.set_range(*self.setting.range) self.gtk_range.set_round_digits(0) self.gtk_range.set_digits(0) self.gtk_range.set_increments(1, 5) self.gtk_range.connect('value-changed', lambda _, c: c._changed(), self) def _write(self): _write_async(self.setting, int(self.gtk_range.get_value()), self.gtk_range.get_parent()) self.timer.cancel() def _changed(self): if self.gtk_range.get_sensitive(): if self.timer: self.timer.cancel() self.timer = _Timer(0.5, lambda: GLib.idle_add(self._write)) self.timer.start() control = SliderControl(setting) return control.gtk_range def _create_multiple_toggle_control(setting): def _toggle_notify(control, _, setting): if control.get_sensitive(): key = control._setting_key new_state = control.get_active() if setting._value[key] != new_state: setting._value[key] = new_state p = control for _ in range(5): p = p.get_parent() _write_async_key_value(setting, key, new_state, p) def _toggle_display(lb): lb._showing = not lb._showing if not lb._showing: for c in lb.get_children(): c.hide() lb.hide() else: lb.show() for c in lb.get_children(): c.show_all() lb = Gtk.ListBox() lb._toggle_display = (lambda l: (lambda: _toggle_display(l)))(lb) lb._showing = True lb.set_selection_mode(Gtk.SelectionMode.NONE) lb.set_no_show_all(True) btn = Gtk.Button('? / ?') for k in setting._validator.all_options(): h = Gtk.HBox(homogeneous=False, spacing=0) lbl_text = str(k) lbl_tooltip = None if hasattr(setting, '_labels'): l1, l2 = setting._labels.get(k, (None, None)) if l1: lbl_text = l1 if l2: lbl_tooltip = l2 lbl = Gtk.Label(lbl_text) h.set_tooltip_text(lbl_tooltip or ' ') control = Gtk.Switch() control._setting_key = str(int(k)) control.connect('notify::active', _toggle_notify, setting) h.pack_start(lbl, False, False, 0) h.pack_end(control, False, False, 0) lbl.set_alignment(0.0, 0.5) lbl.set_margin_left(30) lb.add(h) lb._toggle_display() btn.connect('clicked', lambda _: lb._toggle_display()) hbox = Gtk.HBox(homogeneous=False, spacing=60) hbox.pack_end(btn, False, False, 0) btn.set_alignment(1.0, 0.5) vbox = Gtk.VBox(homogeneous=False, spacing=6) vbox.pack_start(hbox, True, True, 0) vbox.pack_end(lb, True, True, 0) return vbox def _create_multiple_range_control(setting): def _write(control, setting, item, sub_item): control._timer.cancel() delattr(control, '_timer') new_state = int(control.get_value()) if setting._value[str(int(item))][str(sub_item)] != new_state: setting._value[str(int(item))][str(sub_item)] = new_state p = control for _i in range(7): p = p.get_parent() _write_async_item_value(setting, str(int(item)), setting._value[str(int(item))], p) def _changed(control, setting, item, sub_item): if control.get_sensitive(): if hasattr(control, '_timer'): control._timer.cancel() control._timer = _Timer(0.5, lambda: GLib.idle_add(_write, control, setting, item, sub_item)) control._timer.start() def _toggle_display(lb): lb._showing = not lb._showing if not lb._showing: for c in lb.get_children(): c.hide() lb.hide() else: lb.show() for c in lb.get_children(): c.show_all() lb = Gtk.ListBox() lb._toggle_display = (lambda l: (lambda: _toggle_display(l)))(lb) lb.set_selection_mode(Gtk.SelectionMode.NONE) btn = Gtk.Button('...') lb._showing = True lb.set_no_show_all(True) for item in setting._validator.items: lbl_text = str(item) lbl_tooltip = None if hasattr(setting, '_labels'): l1, l2 = setting._labels.get(item, (None, None)) if l1: lbl_text = l1 if l2: lbl_tooltip = l2 item_lbl = Gtk.Label(lbl_text) lb.add(item_lbl) lb.set_tooltip_text(lbl_tooltip or ' ') item_lb = Gtk.ListBox() item_lb.set_selection_mode(Gtk.SelectionMode.NONE) for sub_item in setting._validator.sub_items[item]: h = Gtk.HBox(homogeneous=False, spacing=20) lbl_text = str(sub_item) lbl_tooltip = None if hasattr(setting, '_labels_sub'): l1, l2 = setting._labels_sub.get(str(sub_item), (None, None)) if l1: lbl_text = l1 if l2: lbl_tooltip = l2 sub_item_lbl = Gtk.Label(lbl_text) h.set_tooltip_text(lbl_tooltip or ' ') h.pack_start(sub_item_lbl, False, False, 0) sub_item_lbl.set_margin_left(30) sub_item_lbl.set_alignment(0.0, 0.5) if sub_item.widget == 'Scale': control = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, sub_item.minimum, sub_item.maximum, 1) control.set_round_digits(0) control.set_digits(0) h.pack_end(control, True, True, 0) elif sub_item.widget == 'SpinButton': control = Gtk.SpinButton.new_with_range(sub_item.minimum, sub_item.maximum, 1) control.set_digits(0) h.pack_end(control, False, False, 0) else: raise NotImplementedError control.connect('value-changed', _changed, setting, item, sub_item) item_lb.add(h) h._setting_sub_item = sub_item item_lb._setting_item = item lb.add(item_lb) lb._toggle_display() btn.connect('clicked', lambda _: lb._toggle_display()) btn.set_alignment(1.0, 0.5) hbox = Gtk.HBox(homogeneous=False, spacing=6) hbox.pack_end(btn, False, False, 0) vbox = Gtk.VBox(homogeneous=False, spacing=6) vbox.pack_start(hbox, True, True, 0) vbox.pack_end(lb, True, True, 0) return vbox # # # def _create_sbox(s): sbox = Gtk.HBox(homogeneous=False, spacing=6) label = Gtk.Label(s.label) sbox.pack_start(label, False, False, 0) spinner = Gtk.Spinner() spinner.set_tooltip_text(_('Working') + '...') failed = Gtk.Image.new_from_icon_name('dialog-warning', Gtk.IconSize.SMALL_TOOLBAR) failed.set_tooltip_text(_('Read/write operation failed.')) if s.kind == _SETTING_KIND.toggle: control = _create_toggle_control(s) sbox.pack_end(control, False, False, 0) elif s.kind == _SETTING_KIND.choice: control = _create_choice_control(s) sbox.pack_end(control, False, False, 0) elif s.kind == _SETTING_KIND.range: control = _create_slider_control(s) sbox.pack_end(control, True, True, 0) elif s.kind == _SETTING_KIND.map_choice: control = _create_map_choice_control(s) sbox.pack_end(control, True, True, 0) elif s.kind == _SETTING_KIND.multiple_toggle: vbox = _create_multiple_toggle_control(s) control = vbox.get_children()[1] sbox.remove(label) vbox.get_children()[0].pack_start(label, False, False, 0) label.set_alignment(0.0, 0.5) sbox.pack_start(vbox, True, True, 0) elif s.kind == _SETTING_KIND.multiple_range: vbox = _create_multiple_range_control(s) control = vbox.get_children()[1] sbox.remove(label) vbox.get_children()[0].pack_start(label, False, False, 0) label.set_alignment(0.0, 0.5) sbox.pack_start(vbox, True, True, 0) else: raise Exception('NotImplemented') control.set_sensitive(False) # the first read will enable it control.kind = s.kind if s.kind in [_SETTING_KIND.multiple_toggle, _SETTING_KIND.multiple_range]: vbox.get_children()[0].pack_end(spinner, False, False, 0) vbox.get_children()[0].pack_end(failed, False, False, 0) else: sbox.pack_end(spinner, False, False, 0) sbox.pack_end(failed, False, False, 0) if s.description: sbox.set_tooltip_text(s.description) sbox.show_all() spinner.start() # the first read will stop it failed.set_visible(False) return sbox def _update_setting_item(sbox, value, is_online=True): failed, spinner, control = _get_failed_spinner_control(sbox) spinner.set_visible(False) spinner.stop() if value is None: control.set_sensitive(False) failed.set_visible(is_online) return control.set_sensitive(False) failed.set_visible(False) if isinstance(control, Gtk.Switch): control.set_active(value) elif isinstance(control, Gtk.ComboBoxText): control.set_active_id(str(int(value))) elif isinstance(control, Gtk.Scale): control.set_value(int(value)) elif isinstance(control, Gtk.HBox): kbox, vbox = control.get_children() # depends on box layout if value.get(kbox.get_active_id()): vbox.set_active_id(str(value.get(kbox.get_active_id()))) elif isinstance(control, Gtk.ListBox): if control.kind == _SETTING_KIND.multiple_toggle: total = len(control.get_children()) active = 0 to_join = [] for ch in control.get_children(): elem = ch.get_children()[0].get_children()[-1] v = value.get(elem._setting_key, None) if v is not None: elem.set_active(v) if elem.get_active(): active += 1 to_join.append(elem.get_parent().get_children()[0].get_text() + ': ' + str(elem.get_active())) b = ', '.join(to_join) btn = control.get_parent().get_children()[0].get_children()[-1] btn.set_label(f'{active} / {total}') btn.set_tooltip_text(b) elif control.kind == _SETTING_KIND.multiple_range: b = '' n = 0 for ch in control.get_children()[1:]: # item item = ch.get_children()[0]._setting_item v = value.get(str(int(item)), None) if v is not None: b += str(item) + ': (' to_join = [] for c in ch.get_children()[0].get_children(): # sub-item row = c.get_children()[0] sub_item = row._setting_sub_item elem = row.get_children()[-1] elem.set_value(v[str(sub_item)]) n += 1 to_join.append(str(sub_item) + f'={v[str(sub_item)]}') b += ', '.join(to_join) + ') ' btn = control.get_parent().get_children()[0].get_children()[-1] btn.set_label(f'{n} value' + ('s' if n != 1 else '')) # TODO: i18n, singular/plural btn.set_tooltip_text(b) else: raise NotImplementedError else: raise Exception('NotImplemented') control.set_sensitive(True) def _get_failed_spinner_control(sbox): children = sbox.get_children() if len(children) == 4: _ignore, failed, spinner, control = sbox.get_children() # depends on box layout else: assert len(children) == 1 control = children[0].get_children()[-1] failed = children[0].get_children()[0].get_children()[1] spinner = children[0].get_children()[0].get_children()[2] return failed, spinner, control # # # # config panel _box = None _items = {} def create(): global _box assert _box is None _box = Gtk.VBox(homogeneous=False, spacing=8) _box._last_device = None return _box def update(device, is_online=None): assert _box is not None assert device device_id = (device.receiver.path, device.number) if is_online is None: is_online = bool(device.online) # if the device changed since last update, clear the box first if device_id != _box._last_device: _box.set_visible(False) _box._last_device = device_id # hide controls belonging to other devices for k, sbox in _items.items(): sbox = _items[k] sbox.set_visible(k[0:2] == device_id) for s in device.settings: k = (device_id[0], device_id[1], s.name) if k in _items: sbox = _items[k] else: sbox = _items[k] = _create_sbox(s) _box.pack_start(sbox, False, False, 0) _read_async(s, False, sbox, is_online) _box.set_visible(True) def clean(device): """Remove the controls for a given device serial. Needed after the device has been unpaired. """ assert _box is not None device_id = (device.receiver.path, device.number) for k in list(_items.keys()): if k[0:2] == device_id: _box.remove(_items[k]) del _items[k] def destroy(): global _box _box = None _items.clear()