1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
#!/usr/bin/env python
# epics/wx/utils.py
"""
This is a collection of general purpose utility functions and classes,
especially useful for wx functionality
"""
import wx
import wx.lib.masked as masked
import os
import array
BAD_FILECHARS = ';~,`!%$@$&^?*#:"/|\'\\\t\r\n (){}[]<>'
GOOD_FILECHARS = '_'*len(BAD_FILECHARS)
TRANS_FILE = str.maketrans(BAD_FILECHARS, GOOD_FILECHARS)
HAS_NUMPY = False
try:
import numpy
HAS_NUMPY = True
except ImportError:
pass
# some common abbrevs for wx ALIGNMENT styles
RIGHT = wx.ALIGN_RIGHT
LEFT = wx.ALIGN_LEFT
CEN = wx.ALIGN_CENTER
LCEN = wx.ALIGN_LEFT
RCEN = wx.ALIGN_RIGHT
CCEN = wx.ALIGN_CENTER
LTEXT = wx.ST_NO_AUTORESIZE|wx.ALIGN_CENTER
def make_steps(prec=3, tmin=0, tmax=10, base=10, steps=(1, 2, 5)):
"""make a list of 'steps' to use for a numeric ComboBox
returns a list of floats, such as
[0.01, 0.02, 0.05, 0.10, 0.20, 0.50, 1.00, 2.00...]
"""
steplist = []
power = -prec
step = tmin
while True:
decade = base**power
for step in (j*decade for j in steps):
if step > 0.99*tmin and step <= tmax and step not in steplist:
steplist.append(step)
if step >= tmax:
break
power += 1
return steplist
def set_sizer(panel, sizer=None, style=wx.VERTICAL, fit=False):
""" utility for setting wx Sizer """
if sizer is None:
sizer = wx.BoxSizer(style)
panel.SetAutoLayout(1)
panel.SetSizer(sizer)
if fit:
sizer.Fit(panel)
def set_float(val):
""" utility to set a floating value,
useful for converting from strings """
out = None
if not val in (None, ''):
try:
out = float(val)
except ValueError:
return None
if HAS_NUMPY:
if numpy.isnan(out):
out = default
else:
if not(out > 0) and not(out<0) and not(out==0):
out = default
return out
def pack(window, sizer):
"simple wxPython pack function"
window.SetSizer(sizer)
sizer.Fit(window)
def add_button(parent, label, size=(-1, -1), action=None):
"add simple button with bound action"
thisb = wx.Button(parent, label=label, size=size)
if callable(action):
thisb.Bind(wx.EVT_BUTTON, action)
return thisb
def add_menu(parent, menu, label='', text='', action=None):
"add submenu"
wid = wx.NewId()
menu.Append(wid, label, text)
if callable(action):
parent.Bind(wx.EVT_MENU, action, id=wid)
def popup(parent, message, title, style=None):
"""
generic popup message dialog, returns
output of MessageDialog.ShowModal()
"""
if style is None:
style = wx.OK|wx.ICON_INFORMATION
dlg = wx.MessageDialog(parent, message, title, style)
ret = dlg.ShowModal()
dlg.Destroy()
return ret
def empty_bitmap(width, height, value=255):
"""return empty wx.BitMap"""
data = array.array('B', [value]*3*width*height)
return wx.BitmapFromBuffer(width, height, data)
def fix_filename(fname):
"""
fix string to be a 'good' filename. This may be a more
restrictive than the OS, but avoids nasty cases.
"""
out = str(s).translate(TRANS_FILE)
if out[0] in '-,;[]{}()~`@#':
out = '_%s' % out
return out
def FileOpen(parent, message, default_dir=None,
default_file=None, multiple=False,
wildcard=None):
"""File Open dialog wrapper.
returns full path on OK or None on Cancel
"""
out = None
if default_dir is None:
default_dir = os.getcwd()
if wildcard is None:
wildcard = 'All files (*.*)|*.*'
style = wx.FD_OPEN|wx.FD_CHANGE_DIR
if multiple:
style = style|wx.MULTIPLE
dlg = wx.FileDialog(parent, message=message,
defaultFile=default_file,
defaultDir=default_dir,
wildcard=wildcard,
style=style)
out = None
if dlg.ShowModal() == wx.ID_OK:
out = os.path.abspath(dlg.GetPath())
dlg.Destroy()
return out
def FileSave(parent, message, default_file=None,
default_dir=None, wildcard=None):
"File Save dialog"
out = None
if wildcard is None:
wildcard = 'All files (*.*)|*.*'
if default_dir is None:
default_dir = os.getcwd()
dlg = wx.FileDialog(parent, message=message,
defaultFile=default_file,
wildcard=wildcard,
style=wx.FD_SAVE|wx.FD_CHANGE_DIR)
if dlg.ShowModal() == wx.ID_OK:
out = os.path.abspath(dlg.GetPath())
dlg.Destroy()
return out
def SelectWorkdir(parent, message='Select Working Folder...'):
"prompt for and change into a working directory "
dlg = wx.DirDialog(parent, message,
style=wx.DD_DEFAULT_STYLE|wx.DD_CHANGE_DIR)
path = os.path.abspath(os.curdir)
dlg.SetPath(path)
if dlg.ShowModal() == wx.ID_CANCEL:
return None
path = os.path.abspath(dlg.GetPath())
dlg.Destroy()
os.chdir(path)
return path
class Closure:
"""A very simple callback class to emulate a closure (reference to
a function with arguments) in python.
This class holds a user-defined function to be executed when the
class is invoked as a function. This is useful in many situations,
especially for 'callbacks' where lambda's are quite enough.
Many Tkinter 'actions' can use such callbacks.
>>>def my_action(x=None):
... print('my action: x = ', x)
>>>c = Closure(my_action,x=1)
..... sometime later ...
>>>c()
my action: x = 1
>>>c(x=2)
my action: x = 2
based on Command class from J. Grayson's Tkinter book.
"""
def __init__(self, func=None, *args, **kws):
self.func = func
self.kws = kws
self.args = args
def __call__(self, *args, **kws):
self.kws.update(kws)
if callable(self.func):
self.args = args
return self.func(*self.args, **self.kws)
class FloatCtrl(wx.TextCtrl):
""" Numerical Float Control::
a wx.TextCtrl that allows only numerical input, can take a precision argument
and optional upper / lower bounds
Options:
"""
def __init__(self, parent, value='', minval=None, maxval=None,
precision=3, bell_on_invalid = True,
act_on_losefocus=False,
action=None, action_kw=None, **kws):
self.__digits = '0123456789.-'
self.__prec = precision
if precision is None:
self.__prec = 0
self.format = '%%.%if' % self.__prec
self.is_valid = True
self.__val = set_float(value)
self.__max = set_float(maxval)
self.__min = set_float(minval)
self.__bound_val = None
self.__mark = None
self.__action = None
self.fgcol_valid = "Black"
self.bgcol_valid = "White"
self.fgcol_invalid = "Red"
self.bgcol_invalid = (254, 254, 80)
self.bell_on_invalid = bell_on_invalid
self.act_on_losefocus = act_on_losefocus
# set up action
if action_kw is None:
action_kw = {}
self.SetAction(action, **action_kw)
this_sty = wx.TE_PROCESS_ENTER|wx.TE_RIGHT
if 'style' in kws:
this_sty = this_sty | kws['style']
kws['style'] = this_sty
wx.TextCtrl.__init__(self, parent, wx.ID_ANY, **kws)
self.__CheckValid(self.__val)
self.SetValue(self.__val)
self.Bind(wx.EVT_CHAR, self.OnChar)
self.Bind(wx.EVT_TEXT, self.OnText)
self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
self.__GetMark()
def SetAction(self, action, **kws):
"set callback action"
if callable(action):
self.__action = Closure(action, **kws)
def SetPrecision(self, prec=0):
"set precision"
self.__prec = prec
self.format = '%%.%if' % prec
def __GetMark(self):
" keep track of cursor position within text"
try:
self.__mark = min(wx.TextCtrl.GetSelection(self)[0],
len(wx.TextCtrl.GetValue(self).strip()))
except:
self.__mark = 0
def __SetMark(self, mark=None):
"set mark for later"
if mark is None:
mark = self.__mark
self.SetSelection(mark, mark)
def SetValue(self, value=None, act=True):
" main method to set value "
if value is None:
value = wx.TextCtrl.GetValue(self).strip()
self.__CheckValid(value)
self.__GetMark()
value = set_float(value)
if value is not None:
wx.TextCtrl.SetValue(self, self.format % value)
if self.is_valid and callable(self.__action) and act:
self.__action(value=self.__val)
elif not self.is_valid and self.bell_on_invalid:
wx.Bell()
self.__SetMark()
def OnKillFocus(self, event):
"focus lost"
self.__GetMark()
if self.act_on_losefocus and callable(self.__action):
self.__action(value=self.__val)
event.Skip()
def OnSetFocus(self, event):
"focus gained - resume editing from last mark point"
self.__SetMark()
event.Skip()
def OnChar(self, event):
""" on Character event"""
key = event.GetKeyCode()
entry = wx.TextCtrl.GetValue(self).strip()
pos = wx.TextCtrl.GetSelection(self)
# really, the order here is important:
# 1. return sends to ValidateEntry
if key == wx.WXK_RETURN:
if not self.is_valid:
wx.TextCtrl.SetValue(self, self.format % set_float(self.__bound_val))
else:
self.SetValue(entry)
return
# 2. other non-text characters are passed without change
if (key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255):
event.Skip()
return
# 3. check for multiple '.' and out of place '-' signs and ignore these
# note that chr(key) will now work due to return at #2
has_minus = '-' in entry
ckey = chr(key)
if ((ckey == '.' and (self.__prec == 0 or '.' in entry) ) or
(ckey == '-' and (has_minus or pos[0] != 0)) or
(ckey != '-' and has_minus and pos[0] == 0)):
return
# 4. allow digits, but not other characters
if chr(key) in self.__digits:
event.Skip()
def OnText(self, event=None):
"text event"
try:
if event.GetString() != '':
self.__CheckValid(event.GetString())
except:
pass
event.Skip()
def GetValue(self):
if self.__prec > 0:
return set_float("%%.%ig" % (self.__prec) % (self.__val))
else:
return int(self.__val)
def GetMin(self):
"return min value"
return self.__min
def GetMax(self):
"return max value"
return self.__max
def SetMin(self, val):
"set min value"
self.__min = set_float(val)
def SetMax(self, val):
"set max value"
self.__max = set_float(val)
def __CheckValid(self, value):
"check for validity of value"
val = self.__val
self.is_valid = True
try:
val = set_float(value)
if self.__min is not None and (val < self.__min):
self.is_valid = False
val = self.__min
if self.__max is not None and (val > self.__max):
self.is_valid = False
val = self.__max
except:
self.is_valid = False
self.__bound_val = self.__val = val
fgcol, bgcol = self.fgcol_valid, self.bgcol_valid
if not self.is_valid:
fgcol, bgcol = self.fgcol_invalid, self.bgcol_invalid
self.SetForegroundColour(fgcol)
self.SetBackgroundColour(bgcol)
self.Refresh()
class NumericCombo(wx.ComboBox):
"""
Numeric Combo: ComboBox with numeric-only choices
"""
def __init__(self, parent, choices, precision=3,
init=0, width=80):
self.fmt = "%%.%if" % precision
self.choices = choices
schoices = [self.fmt % i for i in self.choices]
wx.ComboBox.__init__(self, parent, -1, '', (-1, -1), (width, -1),
schoices, wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
init = min(init, len(self.choices))
self.SetStringSelection(schoices[init])
self.Bind(wx.EVT_TEXT_ENTER, self.OnEnter)
def OnEnter(self, event=None):
"text enter event handler"
thisval = float(event.GetString())
if thisval not in self.choices:
self.choices.append(thisval)
self.choices.sort()
self.Clear()
self.AppendItems([self.fmt % i for i in self.choices])
self.SetSelection(self.choices.index(thisval))
class SimpleText(wx.StaticText):
"simple static text wrapper"
def __init__(self, parent, label, minsize=None,
font=None, colour=None, bgcolour=None, **kws):
wx.StaticText.__init__(self, parent, -1,
label=label, **kws)
if minsize is not None:
self.SetMinSize(minsize)
if font is not None:
self.SetFont(font)
if colour is not None:
self.SetForegroundColour(colour)
if bgcolour is not None:
self.SetBackgroundColour(colour)
class HyperText(wx.StaticText):
"""HyperText is a simple extension of wx.StaticText that
1. adds an underscore to the lable to appear to be a hyperlink
2. performs the supplied action on Left-Up button events
"""
def __init__(self, parent, label, action=None, colour=(50, 50, 180)):
self.action = action
wx.StaticText.__init__(self, parent, -1, label=label)
font = self.GetFont() # .Bold()
font.SetUnderlined(True)
self.SetFont(font)
self.SetForegroundColour(colour)
self.Bind(wx.EVT_LEFT_UP, self.OnSelect)
def OnSelect(self, evt=None):
"Left-Up Event Handler"
if self.action is not None:
self.action(evt=evt, label=self.GetLabel())
evt.Skip()
class DateTimeCtrl(object):
"""
Simple Combined date/time control
"""
def __init__(self, parent, name='datetimectrl', use_now=False):
self.name = name
panel = self.panel = wx.Panel(parent)
bgcol = wx.Colour(250, 250, 250)
datestyle = wx.DP_DROPDOWN|wx.DP_SHOWCENTURY|wx.DP_ALLOWNONE
self.datectrl = wx.DatePickerCtrl(panel, size=(120, -1),
style=datestyle)
self.timectrl = masked.TimeCtrl(panel, -1, name=name,
limited=False,
fmt24hr=True, oob_color=bgcol)
timerheight = self.timectrl.GetSize().height
spinner = wx.SpinButton(panel, -1, wx.DefaultPosition,
(-1, timerheight), wx.SP_VERTICAL )
self.timectrl.BindSpinButton(spinner)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.datectrl, 0, wx.ALIGN_CENTER)
sizer.Add(self.timectrl, 0, wx.ALIGN_CENTER)
sizer.Add(spinner, 0, wx.ALIGN_LEFT)
panel.SetSizer(sizer)
sizer.Fit(panel)
if use_now:
self.timectrl.SetValue(wx.DateTime_Now())
|