Add support for range features in GUI

This commit is contained in:
Javier Torres 2016-06-09 19:42:53 +02:00
parent bbadd3e755
commit 2442fee341
1 changed files with 39 additions and 15 deletions

View File

@ -20,7 +20,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals from __future__ import absolute_import, division, print_function, unicode_literals
from gi.repository import Gtk, GLib from gi.repository import Gtk, GLib
from threading import Timer as _Timer
from solaar.i18n import _ from solaar.i18n import _
from solaar.ui import async as _ui_async from solaar.ui import async as _ui_async
@ -76,15 +76,37 @@ def _create_choice_control(setting):
c.connect('changed', _combo_notify, setting) c.connect('changed', _combo_notify, setting)
return c return c
# def _create_slider_control(setting): def _create_slider_control(setting):
# def _slider_notify(slider, s): class SliderControl:
# if slider.get_sensitive(): __slots__ = ('gtk_range', 'timer', 'setting')
# _apply_queue.put(('write', s, slider.get_value(), slider.get_parent())) def __init__(self, setting):
# self.setting = setting
# c = Gtk.Scale(setting.choices) self.timer = None
# c.connect('value-changed', _slider_notify, setting)
# self.gtk_range = Gtk.Scale()
# return c 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
# #
# #
@ -102,15 +124,17 @@ def _create_sbox(s):
if s.kind == _SETTING_KIND.toggle: if s.kind == _SETTING_KIND.toggle:
control = _create_toggle_control(s) control = _create_toggle_control(s)
sbox.pack_end(control, False, False, 0)
elif s.kind == _SETTING_KIND.choice: elif s.kind == _SETTING_KIND.choice:
control = _create_choice_control(s) control = _create_choice_control(s)
# elif s.kind == _SETTING_KIND.range: sbox.pack_end(control, False, False, 0)
# control = _create_slider_control(s) elif s.kind == _SETTING_KIND.range:
control = _create_slider_control(s)
sbox.pack_end(control, True, True, 0)
else: else:
raise NotImplemented raise NotImplemented
control.set_sensitive(False) # the first read will enable it control.set_sensitive(False) # the first read will enable it
sbox.pack_end(control, False, False, 0)
sbox.pack_end(spinner, False, False, 0) sbox.pack_end(spinner, False, False, 0)
sbox.pack_end(failed, False, False, 0) sbox.pack_end(failed, False, False, 0)
@ -140,8 +164,8 @@ def _update_setting_item(sbox, value, is_online=True):
control.set_active(value) control.set_active(value)
elif isinstance(control, Gtk.ComboBoxText): elif isinstance(control, Gtk.ComboBoxText):
control.set_active_id(str(value)) control.set_active_id(str(value))
# elif isinstance(control, Gtk.Scale): elif isinstance(control, Gtk.Scale):
# control.set_value(int(value)) control.set_value(int(value))
else: else:
raise NotImplemented raise NotImplemented
control.set_sensitive(True) control.set_sensitive(True)