wxwidgets: Fix some issues with WxSlider, code cleanup

This commit is contained in:
rdb 2023-10-22 11:26:59 +02:00
parent eec66c585e
commit 2539de14fc

View File

@ -7,14 +7,16 @@ __all__ = ['WxSlider']
import wx import wx
class WxSlider(wx.Slider): class WxSlider(wx.Slider):
def __init__(self, parent, id, value, minValue, maxValue,\ def __init__(self, parent, id, value, minValue, maxValue,
pos=wx.DefaultPosition, size=wx.DefaultSize,\ pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.SL_HORIZONTAL, validator=wx.DefaultValidator, name="slider", textSize=(40,20)): style=wx.SL_HORIZONTAL, validator=wx.DefaultValidator,
name="slider", textSize=(40, 20)):
self.maxValue = maxValue self.maxValue = maxValue
self.minValue = minValue self.minValue = minValue
intVal = 100.0 / (self.maxValue - self.minValue) * (value - self.minValue) intVal = int(100.0 / (self.maxValue - self.minValue) * (value - self.minValue))
intMin = 0 intMin = 0
intMax = 100 intMax = 100
@ -24,42 +26,46 @@ class WxSlider(wx.Slider):
if style & wx.SL_HORIZONTAL: if style & wx.SL_HORIZONTAL:
newStyle = wx.SL_HORIZONTAL newStyle = wx.SL_HORIZONTAL
if style & wx.SL_LABELS: if style & wx.SL_LABELS:
wx.StaticText(parent, -1, "%.2f"%minValue, (pos[0], pos[1])) wx.StaticText(parent, -1, "%.2f" % minValue, (pos[0], pos[1]))
strMaxValue = "%.2f"%maxValue strMaxValue = "%.2f" % maxValue
wx.StaticText(parent, -1, strMaxValue, (pos[0] + size[0] - len(strMaxValue) * 8 , pos[1])) wx.StaticText(parent, -1, strMaxValue, (pos[0] + size[0] - len(strMaxValue) * 8, pos[1]))
strValue = "%.2f"%value strValue = "%.2f" % value
self.textValue = wx.TextCtrl(parent, -1, strValue,\ self.textValue = wx.TextCtrl(parent, -1, strValue,
(pos[0] + size[0] /2 - textSize[0]/2, pos[1]), textSize,\ (pos[0] + size[0] / 2 - textSize[0] / 2, pos[1]),
wx.TE_CENTER | wx.TE_PROCESS_ENTER) textSize, wx.TE_CENTER | wx.TE_PROCESS_ENTER)
self.textValue.Disable() self.textValue.Disable()
newPos = (pos[0], pos[1] + 20) pos = (pos[0], pos[1] + 20)
else: else:
newStyle = wx.SL_VERTICAL newStyle = wx.SL_VERTICAL
newPos = (pos[0], pos[1] + 40) pos = (pos[0], pos[1] + 40)
if style & wx.SL_AUTOTICKS: if style & wx.SL_AUTOTICKS:
newStyle |= wx.SL_AUTOTICKS newStyle |= wx.SL_AUTOTICKS
wx.Slider.__init__(self, parent, id, intVal, intMin, intMax, newPos, size, style=newStyle) wx.Slider.__init__(self, parent, id, intVal, intMin, intMax, pos, size, style=newStyle)
self.Disable() self.Disable()
def GetValue(self): def GetValue(self):
# overriding wx.Slider.GetValue() # overriding wx.Slider.GetValue()
#return (wx.Slider.GetValue(self) * (self.maxValue - self.minValue) / 100.0 + self.minValue) if self.textValue is not None: # Horizontal with labels
return float(self.textValue.GetValue()) # [gjeon] since the value from the slider is not as precise as the value entered by the user return float(self.textValue.GetValue()) # [gjeon] since the value from the slider is not as precise as the value entered by the user
else:
return (wx.Slider.GetValue(self) * (self.maxValue - self.minValue) / 100.0 + self.minValue)
def SetValue(self, value): def SetValue(self, value):
# overriding wx.Slider.SetValue() # overriding wx.Slider.SetValue()
self.textValue.SetValue("%.2f"%value) if self.textValue is not None:
self.textValue.SetValue("%.2f" % value)
intVal = 100.0 / (self.maxValue - self.minValue) * (value - self.minValue) intVal = 100.0 / (self.maxValue - self.minValue) * (value - self.minValue)
wx.Slider.SetValue(self, intVal) wx.Slider.SetValue(self, intVal)
def onChange(self, event): def onChange(self, event):
# update textValue from slider # update textValue from slider
self.textValue.Clear() if self.textValue is not None:
floatVal = wx.Slider.GetValue(self) * (self.maxValue - self.minValue) / 100.0 + self.minValue self.textValue.Clear()
self.textValue.WriteText("%.2f"%floatVal) floatVal = wx.Slider.GetValue(self) * (self.maxValue - self.minValue) / 100.0 + self.minValue
self.textValue.WriteText("%.2f" % floatVal)
if self.updateCB: # callback function sould receive event as the argument if self.updateCB: # callback function sould receive event as the argument
self.updateCB(event) self.updateCB(event)
event.Skip() event.Skip()
@ -80,14 +86,14 @@ class WxSlider(wx.Slider):
def Disable(self): def Disable(self):
# overriding wx.Slider.Disable() # overriding wx.Slider.Disable()
wx.Slider.Disable(self) wx.Slider.Disable(self)
self.textValue.Disable() if self.textValue is not None:
self.textValue.Disable()
def Enable(self): def Enable(self):
# overriding wx.Slider.Enable() # overriding wx.Slider.Enable()
wx.Slider.Enable(self) wx.Slider.Enable(self)
self.Bind(wx.EVT_SLIDER, self.onChange) self.Bind(wx.EVT_SLIDER, self.onChange)
if not self.textValue is None: if self.textValue is not None:
self.textValue.Enable() self.textValue.Enable()
self.textValue.Bind(wx.EVT_TEXT_ENTER, self.onEnter) self.textValue.Bind(wx.EVT_TEXT_ENTER, self.onEnter)