def test_inputIncr(self): """checks increment value entry in options panel widget """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) oppanel.idf.entryByName['inpIncr']['wcfg']['textvariable'].set(1) oppanel.master.update() pause() self.assertEqual(oppanel.incrInput.get(),'1') oppanel.master.destroy()
def test_toggleIncr(self): """checks toggleIncr button """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) oppanel.idf.entryByName['togIncr']['widget'].invoke() oppanel.master.update() pause() self.assertEqual(oppanel.incr_entry.cget('state'),'normal') oppanel.master.destroy()
def test_setSensitivity(self): """checks setting sensitivity """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) oppanel.idf.entryByName['inpSens']['wcfg']['textvariable'].set(300) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['inpSens']['widget'].get(),'300') oppanel.master.destroy()
def test_inpMin(self): """checks minimum value entry in options panel widget """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) oppanel.bmin_entry.invoke() oppanel.idf.entryByName['inpMin']['wcfg']['textvariable'].set(1) oppanel.master.update() pause() self.assertEqual(oppanel.minInput.get(),'1') oppanel.master.destroy()
def test_Precision(self): """checks setting precision 1 to 10 """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) items = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] for i in range(10): oppanel.idf.entryByName['selPrec']['widget'].setitems(items=('1', '2', '3', '4', '5', '6', '7', '8', '9', '10'),index=i) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['selPrec']['widget'].getvalue(),items[i]) oppanel.master.destroy()
def test_Continuous(self): """checks continuous setting on,off """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) pause() oppanel.idf.entryByName['togCont']['widget'].setitems(items=('on','off'), index=1) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['togCont']['widget'].getvalue(),'off') oppanel.idf.entryByName['togCont']['widget'].setitems(items=('on','off'), index=0) oppanel.master.destroy()
def test_type(self): """checks setting type int,float """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) oppanel.master.update() pause() oppanel.idf.entryByName['togIntFloat']['widget'].setitems(items=('float','int'), index=1) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['togIntFloat']['widget'].getvalue(),'int') oppanel.idf.entryByName['togIntFloat']['widget'].setitems(items=('float','int'), index=0) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['togIntFloat']['widget'].getvalue(),'float') oppanel.master.destroy()
def test_showLabel(self): """checks show label setting all 'never', 'always', 'move' """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) pause() oppanel.lab_entry.setitems(('never', 'always', 'move'), index=0) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['togLabel']['widget'].getvalue(),'never') oppanel.lab_entry.setitems(('never', 'always', 'move'), index=1) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['togLabel']['widget'].getvalue(),'always') oppanel.lab_entry.setitems(('never', 'always', 'move'), index=2) oppanel.master.update() pause() self.assertEqual(oppanel.idf.entryByName['togLabel']['widget'].getvalue(),'move') oppanel.master.destroy()
def test_setTitle(self): """checks setting title """ oppanel = OptionsPanel(title="My Options Panel") oppanel.displayPanel(create=1) oppanel.setTitle("mypanel") self.assertEqual(oppanel.title ,'mypanel') oppanel.master.destroy()
def __init__(self, master=None, type='float', labCfg={'fg':'black','side':'left', 'text':None}, min=None, max=None, showLabel=1, value=0.0, continuous=1, precision=2, callback=None, lockMin=0, lockBMin=0, lockMax=0, lockBMax=0, lockPrecision=0,lockShowLabel=0, lockValue=0, lockType=0, lockContinuous=0, signatures=None, **kw): Tkinter.Frame.__init__(self, master) Tkinter.Pack.config(self) self.callbacks = CallbackManager() # object to manage callback # functions. They get called with the # current value as an argument # initialize various attributes with default values self.height = 100 # widget height self.width = 256 # widget height self.widthMinusOne = self.width - 1 self.min = 0 # minimum value self.max = 1 # maximum value self.range = self.max - self.min self.precision = 2 # decimal places self.minOld = 0. # used to store old values self.maxOld = 0. self.size = 50 # defines widget size self.offsetValue = 0. # used to set increment correctly self.lab = None # label self.callback = None # user specified callback self.opPanel = None # option panel widget self.value = 0.0 # current value of widget self.oldValue = 0.0 # old value of widget self.showLabel = 1 # turn on to display label on self.continuous = 1 # set to 1 to call callbacks at # each value change, else gets called # on button release event self.labCfg = labCfg # Tkinter Label options self.labelFont = ( ensureFontCase('helvetica'), 14, 'bold') # label font self.labelColor = 'yellow' # label color self.canvas = None # the canvas to create the widget in self.lockMin = lockMin # lock<X> vars are used in self.lock() self.lockMax = lockMax # to lock/unlock entries in optionpanel self.lockBMin = lockBMin self.lockBMax = lockBMax self.lockPrecision = 0 self.lockShowLabel = lockShowLabel self.lockValue = lockValue self.lockType = lockType self.lockContinuous = lockContinuous # configure with user-defined values self.setCallback(callback) self.setContinuous(continuous) self.setType(type) self.setPrecision(precision) self.setMin(min) self.setMax(max) self.setShowLabel(showLabel) self.setValue(value) self.setLabel(self.labCfg) if master is None: master = Tkinter.Toplevel() self.master = master # widget master self.createCanvas(master) Tkinter.Widget.bind(self.canvas, "<ButtonPress-1>", self.mouseDown) Tkinter.Widget.bind(self.canvas, "<B1-Motion>", self.mouseMove) Tkinter.Widget.bind(self.canvas, "<ButtonRelease-1>", self.mouseUp) # create cursor self.cursorTk = self.canvas.create_line( 0, 0, 0, 0, tags=['cursor']) self.increment = 0.0 self.incrementOld = 0. self.lockIncrement = 0 self.lockBIncrement = 0 self.oneTurn = 360. self.lockOneTurn = 0 self.opPanel = OptionsPanel(master = self, title="Slider graph Options") self.signatures = None # Signature objects fro isocontouring lib self.sigData = [] # list of (x,y) values arrays for each signature self.maxFun = [] # max Y value in each signature self.minFun = [] # min Y value in each signature self.yratios = [] # normalization factors self.colors = ['red', 'green', 'blue', 'orange'] self.tkLines = [] # list of Tkids for lines if signatures: self.setSignatures(signatures)
class ContourSpectrum(Tkinter.Frame): """This class implements a ContourSpectrum widget. it is fully a copy/paste from Dial """ def __init__(self, master=None, type='float', labCfg={'fg':'black','side':'left', 'text':None}, min=None, max=None, showLabel=1, value=0.0, continuous=1, precision=2, callback=None, lockMin=0, lockBMin=0, lockMax=0, lockBMax=0, lockPrecision=0,lockShowLabel=0, lockValue=0, lockType=0, lockContinuous=0, signatures=None, **kw): Tkinter.Frame.__init__(self, master) Tkinter.Pack.config(self) self.callbacks = CallbackManager() # object to manage callback # functions. They get called with the # current value as an argument # initialize various attributes with default values self.height = 100 # widget height self.width = 256 # widget height self.widthMinusOne = self.width - 1 self.min = 0 # minimum value self.max = 1 # maximum value self.range = self.max - self.min self.precision = 2 # decimal places self.minOld = 0. # used to store old values self.maxOld = 0. self.size = 50 # defines widget size self.offsetValue = 0. # used to set increment correctly self.lab = None # label self.callback = None # user specified callback self.opPanel = None # option panel widget self.value = 0.0 # current value of widget self.oldValue = 0.0 # old value of widget self.showLabel = 1 # turn on to display label on self.continuous = 1 # set to 1 to call callbacks at # each value change, else gets called # on button release event self.labCfg = labCfg # Tkinter Label options self.labelFont = ( ensureFontCase('helvetica'), 14, 'bold') # label font self.labelColor = 'yellow' # label color self.canvas = None # the canvas to create the widget in self.lockMin = lockMin # lock<X> vars are used in self.lock() self.lockMax = lockMax # to lock/unlock entries in optionpanel self.lockBMin = lockBMin self.lockBMax = lockBMax self.lockPrecision = 0 self.lockShowLabel = lockShowLabel self.lockValue = lockValue self.lockType = lockType self.lockContinuous = lockContinuous # configure with user-defined values self.setCallback(callback) self.setContinuous(continuous) self.setType(type) self.setPrecision(precision) self.setMin(min) self.setMax(max) self.setShowLabel(showLabel) self.setValue(value) self.setLabel(self.labCfg) if master is None: master = Tkinter.Toplevel() self.master = master # widget master self.createCanvas(master) Tkinter.Widget.bind(self.canvas, "<ButtonPress-1>", self.mouseDown) Tkinter.Widget.bind(self.canvas, "<B1-Motion>", self.mouseMove) Tkinter.Widget.bind(self.canvas, "<ButtonRelease-1>", self.mouseUp) # create cursor self.cursorTk = self.canvas.create_line( 0, 0, 0, 0, tags=['cursor']) self.increment = 0.0 self.incrementOld = 0. self.lockIncrement = 0 self.lockBIncrement = 0 self.oneTurn = 360. self.lockOneTurn = 0 self.opPanel = OptionsPanel(master = self, title="Slider graph Options") self.signatures = None # Signature objects fro isocontouring lib self.sigData = [] # list of (x,y) values arrays for each signature self.maxFun = [] # max Y value in each signature self.minFun = [] # min Y value in each signature self.yratios = [] # normalization factors self.colors = ['red', 'green', 'blue', 'orange'] self.tkLines = [] # list of Tkids for lines if signatures: self.setSignatures(signatures) def setCallback(self, cb): """Set widget callback. Must be callable function. Callback is called every time the widget value is set/modified""" assert cb is None or callable(cb) or type(cb) is types.ListType,\ "Illegal callback: must be either None or callable, or list. Got %s"%cb if cb is None: return elif type(cb) is types.ListType: for func in cb: assert callable(func), "Illegal callback must be callable. Got %s"%func self.callbacks.AddCallback(func) else: self.callbacks.AddCallback(cb) self.callback = cb def toggleOptPanel(self, event=None): if self.opPanel.flag: self.opPanel.Dismiss_cb() else: if not hasattr(self.opPanel, 'optionsForm'): self.opPanel.displayPanel(create=1) else: self.opPanel.displayPanel(create=0) def mouseDown(self, event): # remember where the mouse went down #self.lastx = event.x #self.lasty = event.y self.setXcursor(event.x) def mouseUp(self, event): # call callbacks if not in continuous mode if not self.continuous: self.callbacks.CallCallbacks(self.opPanel.valInput.get()) if self.showLabel == 2: # no widget labels on mouse release self.canvas.itemconfigure(self.labelId2, text='') self.canvas.itemconfigure(self.labelId, text='') def mouseMove(self, event): # move the cursor self.setXcursor(event.x) #self.lastx = event.x def printLabel(self): if self.canvas is None: return self.canvas.itemconfigure(self.labelId2, text=self.labelFormat%self.value)#newVal) self.canvas.itemconfigure(self.labelId, text=self.labelFormat%self.value)#newVal) def drawCursor(self, x): if self.canvas: self.canvas.coords(self.cursorTk, x, 0, x, self.height) def get(self): return self.type(self.value) def setXcursor(self, x, update=1, force=0): """ x is a cursor position in pixel between 1 and self.width """ if x < 1: x = 1 if x > self.width: x = self.width self.drawCursor(x) # the mouse return position from 1 to self.width (x=0 is not drawn) # we need cursor position from 0 (so last x is self.width-1) x = x - 1 if self.range is not None: self.value = self.min + x * self.range / float(self.widthMinusOne) newVal = self.get() if self.continuous or force: if update and self.oldValue != newVal or force: self.oldValue = newVal self.callbacks.CallCallbacks(newVal) if self.showLabel==2: self.printLabel() else: if self.showLabel==2: self.printLabel() if self.showLabel==1: self.printLabel() if self.opPanel: self.opPanel.valInput.set(self.labelFormat%newVal) def set(self, x, update=1, force=0): """ x is a value between self.min and self.max """ #print "set ContourSpectrum" if self.range is not None: xcursor = (x - self.min) * float(self.widthMinusOne) / self.range xcursor = xcursor + 1 self.drawCursor(xcursor) self.setXcursor(xcursor,update,force) def createCanvas(self, master): self.frame = Tkinter.Frame(self, borderwidth=3, relief='sunken') self.canvas = Tkinter.Canvas(self.frame, width=self.width, height=self.height) self.xm = 25 self.ym = 25 self.labelId2 = self.canvas.create_text(self.xm+2, self.ym+2, fill='black', justify='center', text='', font = self.labelFont) self.labelId = self.canvas.create_text(self.xm, self.ym, fill=self.labelColor, justify='center', text='', font = self.labelFont) # pack em up self.canvas.pack(side=Tkinter.TOP) self.frame.pack(expand=1, fill='x') self.toggleWidgetLabel(self.showLabel) def toggleWidgetLabel(self, val): if val == 0: # no widget labels self.showLabel=0 self.canvas.itemconfigure(self.labelId2, text='') self.canvas.itemconfigure(self.labelId, text='') if val == 1: # show always widget labels self.showLabel=1 self.printLabel() if val == 2: # show widget labels only when mouse moves self.showLabel=2 self.canvas.itemconfigure(self.labelId2, text='') self.canvas.itemconfigure(self.labelId, text='') def setValue(self, val): #print "setValue" assert type(val) in [types.IntType, types.FloatType],\ "Illegal type for value: expected %s or %s, got %s"%( type(1), type(1.0), type(val) ) # setValue does NOT call a callback! if self.min is not None and val < self.min: val = self.min if self.max is not None and val > self.max: val = self.max self.value = self.type(val) self.offsetValue=self.value self.oldValue = self.value #print "setValue ContourSpectrum" if self.range is not None: xcursor = (val - self.min) * float(self.widthMinusOne) / self.range xcursor = xcursor + 1 self.drawCursor(xcursor) if self.showLabel == 1: self.printLabel() if self.opPanel: self.opPanel.valInput.set(self.labelFormat%self.value) def setLabel(self, labCfg): self.labCfg = labCfg text = labCfg.get('text', None) if text is None or text=='': return d={} for k, w in self.labCfg.items(): if k == 'side': continue else: d[k] = w if not 'side' in self.labCfg.keys(): self.labCfg['side'] = 'left' if not self.lab: self.lab = Tkinter.Label(self, d) self.lab.pack(side=self.labCfg['side']) self.lab.bind("<Button-3>", self.toggleOptPanel) else: self.lab.configure(text) ##################################################################### # the 'configure' methods: ##################################################################### def configure(self, **kw): for key,value in kw.items(): # the 'set' parameter callbacks if key=='labCfg': self.setLabel(value) elif key=='type': self.setType(value) elif key=='min': self.setMin(value) elif key=='max': self.setMax(value) elif key=='precision': self.setPrecision(value) elif key=='showLabel': self.setShowLabel(value) elif key=='continuous': self.setContinuous(value) # the 'lock' entries callbacks elif key=='lockType': self.lockTypeCB(value) elif key=='lockMin': self.lockMinCB(value) elif key=='lockBMin': self.lockBMinCB(value) elif key=='lockMax': self.lockMaxCB(value) elif key=='lockBMax': self.lockBMaxCB(value) elif key=='lockPrecision': self.lockPrecisionCB(value) elif key=='lockShowLabel': self.lockShowLabelCB(value) elif key=='lockValue': self.lockValueCB(value) elif key=='lockContinuous': self.lockContinuousCB(value) def setType(self, Type): assert type(Type) in [types.StringType, types.TypeType],\ "Illegal type for datatype. Expected %s or %s, got %s"%( type('a'), type(type), type(Type) ) if type(Type) == type(""): # type str assert Type in ('int','float'),\ "Illegal type descriptor. Expected 'int' or 'float', got '%s'"%Type self.type = eval(Type) else: self.type = Type if self.type == int: self.labelFormat = "%d" self.int_value = self.value else: self.labelFormat = "%."+str(self.precision)+"f" if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['togIntFloat']['widget'] if self.type == int: w.setvalue('int') elif self.type == 'float': w.setvalue('float') if self.opPanel: self.opPanel.updateDisplay() # and update the printed label if self.canvas and self.showLabel == 1: self.printLabel() def setMin(self, min): if min is not None: assert type(min) in [types.IntType, types.FloatType, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float, numpy.float32, numpy.float64],\ "Illegal type for minimum. Expected type %s or %s, got %s"%( type(0), type(0.0), type(min) ) if self.max and min > self.max: min = self.max self.min = self.type(min) if self.showLabel == 1: self.printLabel() if self.value < self.min: self.set(self.min) if hasattr(self.opPanel, 'optionsForm'): self.opPanel.minInput.set(self.labelFormat%self.min) self.opPanel.toggleMin.set(1) self.opPanel.min_entry.configure(state='normal', fg='gray0') self.minOld = self.min else: self.min = None if hasattr(self.opPanel, 'optionsForm'): self.opPanel.toggleMin.set(0) self.opPanel.min_entry.configure(state='disabled', fg='gray40') if self.min is not None and self.max is not None: self.range = float(self.max - self.min) else: self.range = None def setMax(self, max): if max is not None: assert type(max) in [types.IntType, types.FloatType, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float, numpy.float32, numpy.float64],\ "Illegal type for maximum. Expected type %s or %s, got %s"%( type(0), type(0.0), type(max) ) if self.min and max < self.min: max = self.min self.max = self.type(max) if self.showLabel == 1: self.printLabel() if self.value > self.max: self.set(self.max) if hasattr(self.opPanel, 'optionsForm'): self.opPanel.maxInput.set(self.labelFormat%self.max) self.opPanel.toggleMax.set(1) self.opPanel.max_entry.configure(state='normal', fg='gray0') self.maxOld = self.max else: self.max = None if hasattr(self.opPanel, 'optionsForm'): self.opPanel.toggleMax.set(0) self.opPanel.max_entry.configure(state='disabled', fg='gray40') if self.min is not None and self.max is not None: self.range = float(self.max - self.min) else: self.range = None def setPrecision(self, val): assert type(val) in [types.IntType, types.FloatType, numpy.int, numpy.float32],\ "Illegal type for precision. Expected type %s or %s, got %s"%( type(0), type(0.0), type(val) ) val = int(val) if val > 10: val = 10 if val < 1: val = 1 self.precision = val if self.type == float: self.labelFormat = "%."+str(self.precision)+"f" else: self.labelFormat = "%d" if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['selPrec']['widget'] w.setvalue(val) if self.opPanel: self.opPanel.updateDisplay() # and update the printed label if self.canvas and self.showLabel == 1: self.printLabel() def setContinuous(self, cont): """ cont can be None, 0 or 1 """ assert cont in [None, 0, 1],\ "Illegal value for continuous: expected None, 0 or 1, got %s"%cont if cont != 1: cont = None self.continuous = cont if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['togCont']['widget'] if cont: w.setvalue('on')#i=1 else: w.setvalue('off')#i=0 if self.opPanel: self.opPanel.updateDisplay() def setShowLabel(self, val): """Show label can be 0, 1 or 2 0: no label 1: label is always shown 2: show label only when value changes""" assert val in [0,1,2],\ "Illegal value for showLabel. Expected 0, 1 or 2, got %s"%val if val != 0 and val != 1 and val != 2: print "Illegal value. Must be 0, 1 or 2" return self.showLabel = val self.toggleWidgetLabel(val) if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['togLabel']['widget'] if self.showLabel == 0: label = 'never' elif self.showLabel == 1: label = 'always' elif self.showLabel == 2: label = 'move' w.setvalue(label) if self.opPanel: self.opPanel.updateDisplay() ##################################################################### # the 'lock' methods: ##################################################################### def lockTypeCB(self, mode): if mode != 0: mode = 1 self.lockType = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockMinCB(self, mode): #min entry field if mode != 0: mode = 1 self.lockMin = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockBMinCB(self, mode): # min checkbutton if mode != 0: mode = 1 self.lockBMin = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockMaxCB(self, mode): # max entry field if mode != 0: mode = 1 self.lockMax = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockBMaxCB(self, mode): # max checkbutton if mode != 0: mode = 1 self.lockBMax = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockPrecisionCB(self, mode): if mode != 0: mode = 1 self.lockPrecision = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockShowLabelCB(self, mode): if mode != 0: mode = 1 self.lockShowLabel = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockValueCB(self, mode): if mode != 0: mode = 1 self.lockValue = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockContinuousCB(self, mode): if mode != 0: mode = 1 self.lockContinuous = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def setSignatures(self, signatures): self.signatures = signatures self.sigData = [] # get the values self.maxFun = [] self.minFun = [] for s in self.signatures: x = Numeric.zeros( (s.nval,), 'f') s.getFx(x) self.minix = mini = min(x) if (isinstance(mini, Numeric.ArrayType)) and (mini.shape == () ): mini = mini[0] maxi = max(x) if (isinstance(maxi, Numeric.ArrayType)) and (maxi.shape == () ): maxi = maxi[0] self.rangex = range = maxi-mini if range != 0: x = (((x-mini)/range)*self.widthMinusOne).astype('i') y = Numeric.zeros( (s.nval,), 'f') s.getFy(y) self.sigData.append( (x,y) ) self.maxFun.append(max(y)) self.minFun.append(min(y)) self.setMin(mini) self.setMax(maxi) # iso value with hightest value in first function if len(self.sigData): ind = list(self.sigData[0][1]).index(max(self.sigData[0][1])) self.setXcursor(ind) else: self.setXcursor(0.0) self.drawSignatures() def drawSignatures(self): # compute normalization factors self.yratios = [] maxi = max(self.maxFun) for i in range(4): h = self.height-1. if maxi != 0 and self.maxFun[i] != 0: self.yratios.append( (h/maxi)*(maxi/self.maxFun[i]) ) else: self.yratios.append( 0 ) for l in self.tkLines: self.canvas.delete(l) for i, f in enumerate(self.sigData): coords = [] for x,y in zip (f[0], f[1]): coords.append(x) coords.append(self.height-y*self.yratios[i]) self.tkLines.append( apply( self.canvas.create_line, coords, {'fill':self.colors[i]}) )
def __init__(self, master=None, type='float', labCfg={ 'fg': 'black', 'side': 'left', 'text': None }, min=None, max=None, showLabel=1, value=0.0, continuous=1, precision=2, callback=None, lockMin=0, lockBMin=0, lockMax=0, lockBMax=0, lockPrecision=0, lockShowLabel=0, lockValue=0, lockType=0, lockContinuous=0, signatures=None, **kw): Tkinter.Frame.__init__(self, master) Tkinter.Pack.config(self) self.callbacks = CallbackManager() # object to manage callback # functions. They get called with the # current value as an argument # initialize various attributes with default values self.height = 100 # widget height self.width = 256 # widget height self.widthMinusOne = self.width - 1 self.min = 0 # minimum value self.max = 1 # maximum value self.range = self.max - self.min self.precision = 2 # decimal places self.minOld = 0. # used to store old values self.maxOld = 0. self.size = 50 # defines widget size self.offsetValue = 0. # used to set increment correctly self.lab = None # label self.callback = None # user specified callback self.opPanel = None # option panel widget self.value = 0.0 # current value of widget self.oldValue = 0.0 # old value of widget self.showLabel = 1 # turn on to display label on self.continuous = 1 # set to 1 to call callbacks at # each value change, else gets called # on button release event self.labCfg = labCfg # Tkinter Label options self.labelFont = (ensureFontCase('helvetica'), 14, 'bold' ) # label font self.labelColor = 'yellow' # label color self.canvas = None # the canvas to create the widget in self.lockMin = lockMin # lock<X> vars are used in self.lock() self.lockMax = lockMax # to lock/unlock entries in optionpanel self.lockBMin = lockBMin self.lockBMax = lockBMax self.lockPrecision = 0 self.lockShowLabel = lockShowLabel self.lockValue = lockValue self.lockType = lockType self.lockContinuous = lockContinuous # configure with user-defined values self.setCallback(callback) self.setContinuous(continuous) self.setType(type) self.setPrecision(precision) self.setMin(min) self.setMax(max) self.setShowLabel(showLabel) self.setValue(value) self.setLabel(self.labCfg) if master is None: master = Tkinter.Toplevel() self.master = master # widget master self.createCanvas(master) Tkinter.Widget.bind(self.canvas, "<ButtonPress-1>", self.mouseDown) Tkinter.Widget.bind(self.canvas, "<B1-Motion>", self.mouseMove) Tkinter.Widget.bind(self.canvas, "<ButtonRelease-1>", self.mouseUp) # create cursor self.cursorTk = self.canvas.create_line(0, 0, 0, 0, tags=['cursor']) self.increment = 0.0 self.incrementOld = 0. self.lockIncrement = 0 self.lockBIncrement = 0 self.oneTurn = 360. self.lockOneTurn = 0 self.opPanel = OptionsPanel(master=self, title="Slider graph Options") self.signatures = None # Signature objects fro isocontouring lib self.sigData = [] # list of (x,y) values arrays for each signature self.maxFun = [] # max Y value in each signature self.minFun = [] # min Y value in each signature self.yratios = [] # normalization factors self.colors = ['red', 'green', 'blue', 'orange'] self.tkLines = [] # list of Tkids for lines if signatures: self.setSignatures(signatures)
class ContourSpectrum(Tkinter.Frame): """This class implements a ContourSpectrum widget. it is fully a copy/paste from Dial """ def __init__(self, master=None, type='float', labCfg={ 'fg': 'black', 'side': 'left', 'text': None }, min=None, max=None, showLabel=1, value=0.0, continuous=1, precision=2, callback=None, lockMin=0, lockBMin=0, lockMax=0, lockBMax=0, lockPrecision=0, lockShowLabel=0, lockValue=0, lockType=0, lockContinuous=0, signatures=None, **kw): Tkinter.Frame.__init__(self, master) Tkinter.Pack.config(self) self.callbacks = CallbackManager() # object to manage callback # functions. They get called with the # current value as an argument # initialize various attributes with default values self.height = 100 # widget height self.width = 256 # widget height self.widthMinusOne = self.width - 1 self.min = 0 # minimum value self.max = 1 # maximum value self.range = self.max - self.min self.precision = 2 # decimal places self.minOld = 0. # used to store old values self.maxOld = 0. self.size = 50 # defines widget size self.offsetValue = 0. # used to set increment correctly self.lab = None # label self.callback = None # user specified callback self.opPanel = None # option panel widget self.value = 0.0 # current value of widget self.oldValue = 0.0 # old value of widget self.showLabel = 1 # turn on to display label on self.continuous = 1 # set to 1 to call callbacks at # each value change, else gets called # on button release event self.labCfg = labCfg # Tkinter Label options self.labelFont = (ensureFontCase('helvetica'), 14, 'bold' ) # label font self.labelColor = 'yellow' # label color self.canvas = None # the canvas to create the widget in self.lockMin = lockMin # lock<X> vars are used in self.lock() self.lockMax = lockMax # to lock/unlock entries in optionpanel self.lockBMin = lockBMin self.lockBMax = lockBMax self.lockPrecision = 0 self.lockShowLabel = lockShowLabel self.lockValue = lockValue self.lockType = lockType self.lockContinuous = lockContinuous # configure with user-defined values self.setCallback(callback) self.setContinuous(continuous) self.setType(type) self.setPrecision(precision) self.setMin(min) self.setMax(max) self.setShowLabel(showLabel) self.setValue(value) self.setLabel(self.labCfg) if master is None: master = Tkinter.Toplevel() self.master = master # widget master self.createCanvas(master) Tkinter.Widget.bind(self.canvas, "<ButtonPress-1>", self.mouseDown) Tkinter.Widget.bind(self.canvas, "<B1-Motion>", self.mouseMove) Tkinter.Widget.bind(self.canvas, "<ButtonRelease-1>", self.mouseUp) # create cursor self.cursorTk = self.canvas.create_line(0, 0, 0, 0, tags=['cursor']) self.increment = 0.0 self.incrementOld = 0. self.lockIncrement = 0 self.lockBIncrement = 0 self.oneTurn = 360. self.lockOneTurn = 0 self.opPanel = OptionsPanel(master=self, title="Slider graph Options") self.signatures = None # Signature objects fro isocontouring lib self.sigData = [] # list of (x,y) values arrays for each signature self.maxFun = [] # max Y value in each signature self.minFun = [] # min Y value in each signature self.yratios = [] # normalization factors self.colors = ['red', 'green', 'blue', 'orange'] self.tkLines = [] # list of Tkids for lines if signatures: self.setSignatures(signatures) def setCallback(self, cb): """Set widget callback. Must be callable function. Callback is called every time the widget value is set/modified""" assert cb is None or callable(cb) or type(cb) is types.ListType,\ "Illegal callback: must be either None or callable, or list. Got %s"%cb if cb is None: return elif type(cb) is types.ListType: for func in cb: assert callable( func), "Illegal callback must be callable. Got %s" % func self.callbacks.AddCallback(func) else: self.callbacks.AddCallback(cb) self.callback = cb def toggleOptPanel(self, event=None): if self.opPanel.flag: self.opPanel.Dismiss_cb() else: if not hasattr(self.opPanel, 'optionsForm'): self.opPanel.displayPanel(create=1) else: self.opPanel.displayPanel(create=0) def mouseDown(self, event): # remember where the mouse went down #self.lastx = event.x #self.lasty = event.y self.setXcursor(event.x) def mouseUp(self, event): # call callbacks if not in continuous mode if not self.continuous: self.callbacks.CallCallbacks(self.opPanel.valInput.get()) if self.showLabel == 2: # no widget labels on mouse release self.canvas.itemconfigure(self.labelId2, text='') self.canvas.itemconfigure(self.labelId, text='') def mouseMove(self, event): # move the cursor self.setXcursor(event.x) #self.lastx = event.x def printLabel(self): if self.canvas is None: return self.canvas.itemconfigure(self.labelId2, text=self.labelFormat % self.value) #newVal) self.canvas.itemconfigure(self.labelId, text=self.labelFormat % self.value) #newVal) def drawCursor(self, x): if self.canvas: self.canvas.coords(self.cursorTk, x, 0, x, self.height) def get(self): return self.type(self.value) def setXcursor(self, x, update=1, force=0): """ x is a cursor position in pixel between 1 and self.width """ if x < 1: x = 1 if x > self.width: x = self.width self.drawCursor(x) # the mouse return position from 1 to self.width (x=0 is not drawn) # we need cursor position from 0 (so last x is self.width-1) x = x - 1 if self.range is not None: self.value = self.min + x * self.range / float(self.widthMinusOne) newVal = self.get() if self.continuous or force: if update and self.oldValue != newVal or force: self.oldValue = newVal self.callbacks.CallCallbacks(newVal) if self.showLabel == 2: self.printLabel() else: if self.showLabel == 2: self.printLabel() if self.showLabel == 1: self.printLabel() if self.opPanel: self.opPanel.valInput.set(self.labelFormat % newVal) def set(self, x, update=1, force=0): """ x is a value between self.min and self.max """ #print "set ContourSpectrum" if self.range is not None: xcursor = (x - self.min) * float(self.widthMinusOne) / self.range xcursor = xcursor + 1 self.drawCursor(xcursor) self.setXcursor(xcursor, update, force) def createCanvas(self, master): self.frame = Tkinter.Frame(self, borderwidth=3, relief='sunken') self.canvas = Tkinter.Canvas(self.frame, width=self.width, height=self.height) self.xm = 25 self.ym = 25 self.labelId2 = self.canvas.create_text(self.xm + 2, self.ym + 2, fill='black', justify='center', text='', font=self.labelFont) self.labelId = self.canvas.create_text(self.xm, self.ym, fill=self.labelColor, justify='center', text='', font=self.labelFont) # pack em up self.canvas.pack(side=Tkinter.TOP) self.frame.pack(expand=1, fill='x') self.toggleWidgetLabel(self.showLabel) def toggleWidgetLabel(self, val): if val == 0: # no widget labels self.showLabel = 0 self.canvas.itemconfigure(self.labelId2, text='') self.canvas.itemconfigure(self.labelId, text='') if val == 1: # show always widget labels self.showLabel = 1 self.printLabel() if val == 2: # show widget labels only when mouse moves self.showLabel = 2 self.canvas.itemconfigure(self.labelId2, text='') self.canvas.itemconfigure(self.labelId, text='') def setValue(self, val): #print "setValue" assert type(val) in [types.IntType, types.FloatType],\ "Illegal type for value: expected %s or %s, got %s"%( type(1), type(1.0), type(val) ) # setValue does NOT call a callback! if self.min is not None and val < self.min: val = self.min if self.max is not None and val > self.max: val = self.max self.value = self.type(val) self.offsetValue = self.value self.oldValue = self.value #print "setValue ContourSpectrum" if self.range is not None: xcursor = (val - self.min) * float(self.widthMinusOne) / self.range xcursor = xcursor + 1 self.drawCursor(xcursor) if self.showLabel == 1: self.printLabel() if self.opPanel: self.opPanel.valInput.set(self.labelFormat % self.value) def setLabel(self, labCfg): self.labCfg = labCfg text = labCfg.get('text', None) if text is None or text == '': return d = {} for k, w in self.labCfg.items(): if k == 'side': continue else: d[k] = w if not 'side' in self.labCfg.keys(): self.labCfg['side'] = 'left' if not self.lab: self.lab = Tkinter.Label(self, d) self.lab.pack(side=self.labCfg['side']) self.lab.bind("<Button-3>", self.toggleOptPanel) else: self.lab.configure(text) ##################################################################### # the 'configure' methods: ##################################################################### def configure(self, **kw): for key, value in kw.items(): # the 'set' parameter callbacks if key == 'labCfg': self.setLabel(value) elif key == 'type': self.setType(value) elif key == 'min': self.setMin(value) elif key == 'max': self.setMax(value) elif key == 'precision': self.setPrecision(value) elif key == 'showLabel': self.setShowLabel(value) elif key == 'continuous': self.setContinuous(value) # the 'lock' entries callbacks elif key == 'lockType': self.lockTypeCB(value) elif key == 'lockMin': self.lockMinCB(value) elif key == 'lockBMin': self.lockBMinCB(value) elif key == 'lockMax': self.lockMaxCB(value) elif key == 'lockBMax': self.lockBMaxCB(value) elif key == 'lockPrecision': self.lockPrecisionCB(value) elif key == 'lockShowLabel': self.lockShowLabelCB(value) elif key == 'lockValue': self.lockValueCB(value) elif key == 'lockContinuous': self.lockContinuousCB(value) def setType(self, Type): assert type(Type) in [types.StringType, types.TypeType],\ "Illegal type for datatype. Expected %s or %s, got %s"%( type('a'), type(type), type(Type) ) if type(Type) == type(""): # type str assert Type in ('int','float'),\ "Illegal type descriptor. Expected 'int' or 'float', got '%s'"%Type self.type = eval(Type) else: self.type = Type if self.type == int: self.labelFormat = "%d" self.int_value = self.value else: self.labelFormat = "%." + str(self.precision) + "f" if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['togIntFloat']['widget'] if self.type == int: w.setvalue('int') elif self.type == 'float': w.setvalue('float') if self.opPanel: self.opPanel.updateDisplay() # and update the printed label if self.canvas and self.showLabel == 1: self.printLabel() def setMin(self, min): if min is not None: assert type(min) in [types.IntType, types.FloatType, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float, numpy.float32, numpy.float64],\ "Illegal type for minimum. Expected type %s or %s, got %s"%( type(0), type(0.0), type(min) ) if self.max and min > self.max: min = self.max self.min = self.type(min) if self.showLabel == 1: self.printLabel() if self.value < self.min: self.set(self.min) if hasattr(self.opPanel, 'optionsForm'): self.opPanel.minInput.set(self.labelFormat % self.min) self.opPanel.toggleMin.set(1) self.opPanel.min_entry.configure(state='normal', fg='gray0') self.minOld = self.min else: self.min = None if hasattr(self.opPanel, 'optionsForm'): self.opPanel.toggleMin.set(0) self.opPanel.min_entry.configure(state='disabled', fg='gray40') if self.min is not None and self.max is not None: self.range = float(self.max - self.min) else: self.range = None def setMax(self, max): if max is not None: assert type(max) in [types.IntType, types.FloatType, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float, numpy.float32, numpy.float64],\ "Illegal type for maximum. Expected type %s or %s, got %s"%( type(0), type(0.0), type(max) ) if self.min and max < self.min: max = self.min self.max = self.type(max) if self.showLabel == 1: self.printLabel() if self.value > self.max: self.set(self.max) if hasattr(self.opPanel, 'optionsForm'): self.opPanel.maxInput.set(self.labelFormat % self.max) self.opPanel.toggleMax.set(1) self.opPanel.max_entry.configure(state='normal', fg='gray0') self.maxOld = self.max else: self.max = None if hasattr(self.opPanel, 'optionsForm'): self.opPanel.toggleMax.set(0) self.opPanel.max_entry.configure(state='disabled', fg='gray40') if self.min is not None and self.max is not None: self.range = float(self.max - self.min) else: self.range = None def setPrecision(self, val): assert type(val) in [types.IntType, types.FloatType, numpy.int, numpy.float32],\ "Illegal type for precision. Expected type %s or %s, got %s"%( type(0), type(0.0), type(val) ) val = int(val) if val > 10: val = 10 if val < 1: val = 1 self.precision = val if self.type == float: self.labelFormat = "%." + str(self.precision) + "f" else: self.labelFormat = "%d" if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['selPrec']['widget'] w.setvalue(val) if self.opPanel: self.opPanel.updateDisplay() # and update the printed label if self.canvas and self.showLabel == 1: self.printLabel() def setContinuous(self, cont): """ cont can be None, 0 or 1 """ assert cont in [None, 0, 1],\ "Illegal value for continuous: expected None, 0 or 1, got %s"%cont if cont != 1: cont = None self.continuous = cont if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['togCont']['widget'] if cont: w.setvalue('on') #i=1 else: w.setvalue('off') #i=0 if self.opPanel: self.opPanel.updateDisplay() def setShowLabel(self, val): """Show label can be 0, 1 or 2 0: no label 1: label is always shown 2: show label only when value changes""" assert val in [0,1,2],\ "Illegal value for showLabel. Expected 0, 1 or 2, got %s"%val if val != 0 and val != 1 and val != 2: print "Illegal value. Must be 0, 1 or 2" return self.showLabel = val self.toggleWidgetLabel(val) if hasattr(self.opPanel, 'optionsForm'): w = self.opPanel.idf.entryByName['togLabel']['widget'] if self.showLabel == 0: label = 'never' elif self.showLabel == 1: label = 'always' elif self.showLabel == 2: label = 'move' w.setvalue(label) if self.opPanel: self.opPanel.updateDisplay() ##################################################################### # the 'lock' methods: ##################################################################### def lockTypeCB(self, mode): if mode != 0: mode = 1 self.lockType = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockMinCB(self, mode): #min entry field if mode != 0: mode = 1 self.lockMin = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockBMinCB(self, mode): # min checkbutton if mode != 0: mode = 1 self.lockBMin = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockMaxCB(self, mode): # max entry field if mode != 0: mode = 1 self.lockMax = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockBMaxCB(self, mode): # max checkbutton if mode != 0: mode = 1 self.lockBMax = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockPrecisionCB(self, mode): if mode != 0: mode = 1 self.lockPrecision = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockShowLabelCB(self, mode): if mode != 0: mode = 1 self.lockShowLabel = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockValueCB(self, mode): if mode != 0: mode = 1 self.lockValue = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def lockContinuousCB(self, mode): if mode != 0: mode = 1 self.lockContinuous = mode if hasattr(self.opPanel, 'optionsForm'): self.opPanel.lockUnlockDisplay() def setSignatures(self, signatures): self.signatures = signatures self.sigData = [] # get the values self.maxFun = [] self.minFun = [] for s in self.signatures: x = Numeric.zeros((s.nval, ), 'f') s.getFx(x) self.minix = mini = min(x) if (isinstance(mini, Numeric.ArrayType)) and (mini.shape == ()): mini = mini[0] maxi = max(x) if (isinstance(maxi, Numeric.ArrayType)) and (maxi.shape == ()): maxi = maxi[0] self.rangex = range = maxi - mini if range != 0: x = (((x - mini) / range) * self.widthMinusOne).astype('i') y = Numeric.zeros((s.nval, ), 'f') s.getFy(y) self.sigData.append((x, y)) self.maxFun.append(max(y)) self.minFun.append(min(y)) self.setMin(mini) self.setMax(maxi) # iso value with hightest value in first function if len(self.sigData): ind = list(self.sigData[0][1]).index(max(self.sigData[0][1])) self.setXcursor(ind) else: self.setXcursor(0.0) self.drawSignatures() def drawSignatures(self): # compute normalization factors self.yratios = [] maxi = max(self.maxFun) for i in range(4): h = self.height - 1. if maxi != 0 and self.maxFun[i] != 0: self.yratios.append((h / maxi) * (maxi / self.maxFun[i])) else: self.yratios.append(0) for l in self.tkLines: self.canvas.delete(l) for i, f in enumerate(self.sigData): coords = [] for x, y in zip(f[0], f[1]): coords.append(x) coords.append(self.height - y * self.yratios[i]) self.tkLines.append( apply(self.canvas.create_line, coords, {'fill': self.colors[i]}))