Skip to content

Commit

Permalink
-Added preference panel for changing of things like increments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eurybiadan committed Sep 19, 2018
1 parent 7d3b1fa commit a633565
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 69 deletions.
106 changes: 55 additions & 51 deletions fixationWidgets/LocSpin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@
import wx
import re
import locale
import string
from math import fabs,ceil,floor
from math import fabs, ceil, floor
from wx.lib.agw.floatspin import FixedPoint

class LocSpin( wx.lib.agw.floatspin.FloatSpin ):

class LocSpin(wx.lib.agw.floatspin.FloatSpin):
'''
classdocs
'''

def __init__(self, parent, id=wx.ID_ANY, pos=wx.Point(-1, -1), size=(65, -1),
style=0, value=0.0, min_val=None, max_val=None, increment=1.0,
digits=-1, extrastyle=2, name='FloatSpin', poslabel='T',neglabel='N'):

self._poslabel = poslabel
self._neglabel = neglabel

super(LocSpin,self).__init__( parent, id, pos, size, style, value,
min_val, max_val, increment, digits,
extrastyle, name)

self._validkeycode.extend(range(65,90)) # Expand the acceptable characters for upper case letters

self._validkeycode.extend(range(97,122)) # Expand the acceptable characters for lower case letters



#Override
style=0, value=0.0, min_val=None, max_val=None, increment=1.0,
digits=2, extrastyle=2, name='FloatSpin', poslabel='T', neglabel='N'):

self._poslabel = poslabel
self._neglabel = neglabel

super(LocSpin, self).__init__(parent, id, pos, size, style, value,
min_val, max_val, increment, digits,
extrastyle, name)

self._validkeycode.extend(range(65, 90)) # Expand the acceptable characters for upper case letters

self._validkeycode.extend(range(97, 122)) # Expand the acceptable characters for lower case letters

# Override
def SyncSpinToText(self, send_event=True, force_valid=True):
"""
Synchronize the underlying `wx.TextCtrl` with `wx.SpinButton`.
Expand All @@ -45,39 +44,39 @@ def SyncSpinToText(self, send_event=True, force_valid=True):
:param `force_valid`: ``True`` to force a valid value (i.e. inside the
provided range), ``False`` otherwise.
"""

if not self._textctrl:
return

curr = self._textctrl.GetValue()

# Added by Robert Cooper to handle input of a number with a string after it
## print curr
postrans = str.maketrans(dict.fromkeys(self._poslabel + "- "))
negtrans = str.maketrans(dict.fromkeys(self._neglabel + "- "))
# First determine if there are any letter characters in the string.
if( re.search('[a-zA-Z]',curr) != None):
if (re.search('[a-zA-Z]', curr) != None):
# If there are, check if they're the characters for our positive and negative label
if( string.find(curr,self._poslabel) != -1):
curr = curr.translate(self._poslabel)
elif(string.find(curr,self._neglabel) != -1):
curr = '-'+curr.translate(self._neglabel)
if curr.find(self._poslabel) != -1:
curr = curr.translate(postrans)
elif curr.find(self._neglabel) != -1:
curr = '-' + curr.translate(negtrans)
# End addition by Robert Cooper

curr = curr.strip()
decimal = locale.localeconv()["decimal_point"]
curr = curr.replace(decimal, ".")

if curr:
try:
curro = float(curr)
curr = FixedPoint(curr, 20)
curr = FixedPoint(curr, 2)
except:
self.SetValue(self._value)
return

if force_valid or not self.HasRange() or self.InRange(curr):

if force_valid and self.HasRange():

curr = self.ClampValue(curr)

if self._value != curr:
Expand All @@ -89,10 +88,10 @@ def SyncSpinToText(self, send_event=True, force_valid=True):
elif force_valid:

# textctrl is out of sync, discard and reset
self.SetValue(self.GetValue())

#Override
self.SetValue(self.GetValue())

# Override

def SetValue(self, value):
"""
Sets the L{FloatSpin} value.
Expand All @@ -101,51 +100,49 @@ def SetValue(self, value):
"""
if value > 0:
ispos = 1
elif value < 0:
elif value < 0:
ispos = -1
value = fabs(value)
else:
value = fabs(value)
ispos = 0



if not self._textctrl or not self.InRange(value):
return

if self._snapticks and self._increment != 0.0:

finite, snap_value = self.IsFinite(value)

if not finite: # FIXME What To Do About A Failure?
if not finite: # FIXME What To Do About A Failure?

if (snap_value - floor(snap_value) < ceil(snap_value) - snap_value):
value = self._defaultvalue + floor(snap_value)*self._increment
value = self._defaultvalue + floor(snap_value) * self._increment
else:
value = self._defaultvalue + ceil(snap_value)*self._increment
value = self._defaultvalue + ceil(snap_value) * self._increment

decimal = locale.localeconv()["decimal_point"]
strs = ("%100." + str(self._digits) + self._textformat[1])%value
strs = ("%100." + str(self._digits) + self._textformat[1]) % value
strs = strs.replace(".", decimal)
strs = strs.strip()
strs = self.ReplaceDoubleZero(strs)

# Added by Robert F Cooper to allow a label change instead of a positive/negative switch.
if ispos == 1:
strs = strs + ' ' + self._poslabel
elif ispos == -1:
strs = strs + ' ' + self._neglabel
value = -value
else:
pass # Don't add any label if it is 0.
pass # Don't add any label if it is 0.

# Ended addition by Robert F Cooper

if value != self._value or strs != self._textctrl.GetValue():

self._textctrl.SetValue(strs)
self._textctrl.DiscardEdits()
self._value = value

def get_label_value(self):
return self._textctrl.GetValue()

Expand All @@ -154,12 +151,19 @@ def get_value(self):

def set_positive_label(self, label):
self._poslabel = label

def set_negative_label(self, label):
self._neglabel = label

def flip_labels(self):
label=self._poslabel
label = self._poslabel
self._poslabel = self._neglabel
self._neglabel = label


# Override so that we don't capture spaces.
def OnChar(self, event):
keycode = event.GetKeyCode()
if keycode != wx.WXK_SPACE:
super(LocSpin, self).OnChar(event)
else:
event.Skip()
81 changes: 81 additions & 0 deletions fixationgui/PreferencesDialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'''
Created on Sept 19, 2018
@author: Robert F Cooper
'''

import wx
from wx.lib.agw.floatspin import FloatSpin


class PreferencesDialog(wx.Dialog):

def __init__(self, parent, id=-1, title='Preferences', style=wx.DEFAULT_DIALOG_STYLE,
major_increment=0.75, minor_increment=0.1):
super(PreferencesDialog, self).__init__(parent, id, title, (-1, -1), (-1, -1), style)
self.SetBackgroundColour('black')

labelFont = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False)

# Spinners for setting the increments
self.maj_increment_spin = FloatSpin(self, wx.ID_ANY, value=major_increment, digits=2, increment=0.1, size=(65, -1)) # Button
maj_label = wx.StaticText(self, wx.ID_ANY, 'Major Increment', style=wx.ALIGN_CENTER) # Label
maj_label.SetForegroundColour('white')
maj_label.SetFont(labelFont)

self.min_increment_spin = FloatSpin(self, wx.ID_ANY, value=minor_increment, digits=2, increment=0.1, size=(65, -1)) # Button
min_label = wx.StaticText(self, wx.ID_ANY, 'Minor Increment', style=wx.ALIGN_CENTER) # Label
min_label.SetForegroundColour('white')
min_label.SetFont(labelFont)

# To keep the items properly aligned
major_increment_sizer = wx.BoxSizer(wx.HORIZONTAL)
minor_increment_sizer = wx.BoxSizer(wx.HORIZONTAL)

major_increment_sizer.Add(maj_label, 0, wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT, 10)
major_increment_sizer.AddSpacer(6)
major_increment_sizer.Add(self.maj_increment_spin, 0, wx.ALIGN_LEFT | wx.BOTTOM | wx.RIGHT, 10)
minor_increment_sizer.Add(min_label, 0, wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT, 10)
minor_increment_sizer.AddSpacer(6)
minor_increment_sizer.Add(self.min_increment_spin, 0, wx.ALIGN_LEFT | wx.BOTTOM | wx.RIGHT, 10)

# OK/Cancel bar
self.ok_button = wx.Button(self, id=wx.OK, label="OK", size=(65,-1))
self.ok_button.SetBackgroundColour('medium gray')
self.ok_button.SetForegroundColour('white')
self.ok_button.Bind(wx.EVT_BUTTON, self.on_okay)

self.cancel_button = wx.Button(self, id=wx.CANCEL, label="Cancel", size=(65,-1))
self.cancel_button.SetBackgroundColour('medium gray')
self.cancel_button.SetForegroundColour('white')
self.cancel_button.Bind(wx.EVT_BUTTON, self.on_cancel)

okbar_sizer = wx.BoxSizer(wx.HORIZONTAL)

okbar_sizer.Add(self.ok_button, 0, wx.ALIGN_LEFT | wx.BOTTOM | wx.LEFT, 2)
okbar_sizer.AddSpacer(20)
okbar_sizer.Add(self.cancel_button, 0, wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, 2)

sizer = wx.GridBagSizer()
sizer.AddGrowableCol(0, 4)

sizer.Add(major_increment_sizer, (0, 0), (1, 2), wx.ALIGN_CENTER | wx.ALL, 5)
sizer.Add(minor_increment_sizer, (1, 0), (1, 2), wx.ALIGN_CENTER | wx.ALL, 5)
sizer.Add(okbar_sizer, (2, 0), (1, 2), wx.ALIGN_CENTER | wx.ALL, 5)

self.SetSizerAndFit(sizer)

self.SetAffirmativeId(wx.OK)
self.SetEscapeId(wx.CANCEL)
self.AddMainButtonId(wx.OK)

def on_okay(self, evt):
self.EndModal(1)

def on_cancel(self, evt):
self.EndModal(-1)

def get_prefs(self):
return dict(minor_increment=self.min_increment_spin.GetValue(),
major_increment=self.maj_increment_spin.GetValue())
12 changes: 6 additions & 6 deletions fixationgui/controlPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, s
self.vertcontrol = LocSpin(self, wx.ID_ANY, min_val=-20, max_val=20, increment=parent.get_minor_increment(),
value=0, extrastyle=FS.FS_LEFT, poslabel='S', neglabel='I')
self.vertcontrol.SetFormat('%f')
self.vertcontrol.SetDigits(1)
self.vertcontrol.SetDigits(2)
#
# Horizontal Floatspin control
self.horzcontrol = LocSpin(self, wx.ID_ANY, min_val=-20, max_val=20, increment=parent.get_minor_increment(),
value=0, extrastyle=FS.FS_LEFT, poslabel='T', neglabel='N')
self.horzcontrol.SetFormat('%f')
self.horzcontrol.SetDigits(1)
self.horzcontrol.SetDigits(2)

""" Cursor tools """
# Anchor cursor as center
Expand All @@ -71,9 +71,9 @@ def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, s
self.anchorbut.SetForegroundColour('white')

# Reset marked locations
self.resetlocs = wx.Button(self, label='Reset Locations', size=(-1, 30))
self.resetlocs.SetBackgroundColour('medium gray')
self.resetlocs.SetForegroundColour('white')
#self.resetlocs = wx.Button(self, label='Reset Locations', size=(-1, 30))
#self.resetlocs.SetBackgroundColour('medium gray')
#self.resetlocs.SetForegroundColour('white')

""" Initialization Buttons """
# Image initialization pane
Expand All @@ -93,7 +93,7 @@ def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, s
sizer.Add(self.vertcontrol, (2, 2), (1, 2), wx.ALIGN_CENTER | wx.ALL, 5)
""" Initialization Buttons """
sizer.Add(self.anchorbut, (3, 0), wx.DefaultSpan, wx.ALIGN_CENTER | wx.ALL, 5)
sizer.Add(self.resetlocs, (3, 2), wx.DefaultSpan, wx.ALIGN_CENTER | wx.ALL, 5)
#sizer.Add(self.resetlocs, (3, 2), wx.DefaultSpan, wx.ALIGN_CENTER | wx.ALL, 5)

sizer.Add(self._cursorpane, (4, 0), (2, 4), wx.EXPAND, wx.ALL, 5)
sizer.Add(self._iminitpane, (8, 0), (2, 4), wx.EXPAND, wx.ALL, 5)
Expand Down
Loading

0 comments on commit a633565

Please sign in to comment.