Package horizons :: Package gui :: Package widgets :: Module stepslider
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.widgets.stepslider

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from fife.extensions.pychan.attrs import IntAttr, UnicodeAttr 
 23  from fife.extensions.pychan.widgets import Slider, Widget 
 24   
 25   
26 -class StepSlider(Slider):
27 """The StepSlider automatically snaps the steps suggested by stepsize.""" 28 29 ATTRIBUTES = Widget.ATTRIBUTES + [ 30 IntAttr('orientation'), 31 IntAttr('marker_length'), 32 UnicodeAttr('steps')] 33
34 - def __init__(self, *args, **kwargs):
35 self._callbacks_by_group = {} # super init calls capture, so we need this here 36 37 super().__init__(*args, **kwargs) 38 39 self._last_step_value = None # for recognizing new steps, self.value is overwritten in the base class sometimes 40 self._steps = None 41 42 super().capture(self._update, 'action', 'stepslider') 43 super().capture(self._mouse_released_snap, 'mouseReleased', 'stepslider')
44
45 - def _mouse_released_snap(self):
46 """ 47 When mouse is released, snap slider marker to discrete value to avoid the marker 48 being displayed in-between steps. 49 """ 50 # Retrieve the value of the step that is closest to marker's current position 51 # (`_get_value`), and set that value on the slider to move the marker (`_set_value`). 52 self.value = self.value
53
54 - def capture(self, callback, event_name='action', group_name='default'):
55 """ 56 Intercept captures for `action` and store the callback in our list. We'll only 57 call them when a step changed. 58 """ 59 if event_name == 'action': 60 self._callbacks_by_group[group_name] = callback 61 else: 62 super().capture(callback, event_name, group_name)
63
64 - def _update(self):
65 """ 66 Notify listeners when a different step was selected. 67 """ 68 if self.value != self._last_step_value: 69 self._last_step_value = self.value 70 for callback in self._callbacks_by_group.values(): 71 callback()
72
73 - def _set_steps(self, steps):
74 if isinstance(steps, str): 75 self._steps = [float(s.strip()) for s in steps.split(';')] 76 else: 77 self._steps = steps 78 79 self.scale_start = 0.0 80 self.scale_end = float(len(self._steps) - 1) 81 self.step_length = 1.0
82
83 - def _get_steps(self):
84 return self._steps
85 86 steps = property(_get_steps, _set_steps) 87
88 - def _get_value(self):
89 value = int(round(self.real_widget.getValue())) 90 return self._steps[value]
91
92 - def _set_value(self, value):
93 try: 94 value = float(self._steps.index(value)) 95 except ValueError: 96 # Invalid value, probably a value before the changes to stepslider. 97 # The values of affected step sliders used to be in [0, N], where N is the 98 # number of steps, so we use the value as index. 99 try: 100 value = self._steps[int(value)] 101 except IndexError: 102 # If that didn't work, reset the slider. 103 value = self._steps[0] 104 105 self.real_widget.setValue(value)
106 107 value = property(_get_value, _set_value)
108