Skip to content

Commit

Permalink
Reworked all properties in Label to be decorator-style
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Fries committed Apr 28, 2015
1 parent dcc198a commit 0d19b38
Showing 1 changed file with 89 additions and 109 deletions.
198 changes: 89 additions & 109 deletions Packages/vcs/Lib/vtk_ui/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,119 +217,99 @@ def __init__(self, interactor, string, movable=False, on_move=None, on_drag=None

self.register()

def text():
doc = "Property that provides access to the text actor's input"

def fget(self):
return self.get_text()

def fset(self, value):
self.set_text(value)

return locals()
text = property(**text())

def x():
doc = "The x coordinate of the text actor."

def fget(self):
return self.actor.GetPosition()[0]

def fset(self, value):
self.actor.SetPosition(value, self.y)
return locals()
x = property(**x())

def y():
doc = "The y coordinate of the text actor."

def fget(self):
return self.actor.GetPosition()[1]

def fset(self, value):
self.actor.SetPosition(self.x, value)
return locals()
y = property(**y())

def left():
@property
def text(self):
"""
Provides support for the old-style of coordinates; left + top, vs x + y
Get/Set the text on the actor
"""
doc = "The left side of the text actor, adjusted by width/justification"

def fget(self):
halign = self.actor.GetTextProperty().GetJustificationAsString()
if halign == "Left":
return self.x

w, h = text_dimensions(self.text, self.actor.GetTextProperty())
if halign == "Centered":
return self.x - math.floor(w / 2.)

if halign == "Right":
return self.x - w

def fset(self, l):

halign = self.actor.GetTextProperty().GetJustificationAsString()
if halign == "Left":
self.x = l

w, h = text_dimensions(self.text, self.actor.GetTextProperty())
if halign == "Centered":
self.x = l + math.floor(w / 2.)

if halign == "Right":
self.x = l + w
return locals()
left = property(**left())

def top():
return self.get_text()

@text.setter
def text(self, value):
self.set_text(value)

@property
def x(self):
"""The x coordinate of the text actor."""
return self.actor.GetPosition()[0]

@x.setter
def x(self, value):
self.actor.SetPosition(value, self.y)

@property
def y(self):
"""The y coordinate of the text actor."""
return self.actor.GetPosition()[1]

@y.setter
def y(self, value):
self.actor.SetPosition(self.x, value)

@property
def left(self):
"""The left side of the text actor, adjusted by width/justification"""
halign = self.actor.GetTextProperty().GetJustificationAsString()
if halign == "Left":
return self.x

w, h = text_dimensions(self.text, self.actor.GetTextProperty())
if halign == "Centered":
return self.x - math.floor(w / 2.)

if halign == "Right":
return self.x - w

@left.setter
def left(self, l):
halign = self.actor.GetTextProperty().GetJustificationAsString()
if halign == "Left":
self.x = l

w, h = text_dimensions(self.text, self.actor.GetTextProperty())
if halign == "Centered":
self.x = l + math.floor(w / 2.)

if halign == "Right":
self.x = l + w

@property
def top(self):
"""The top side of the text actor, adjusted by height/justification"""
w, h = text_dimensions(self.text, self.actor.GetTextProperty())
valign = self.actor.GetTextProperty().GetVerticalJustificationAsString()
y = self.y
# Adjust from alignment point to top of the actor
if valign == "Top":
pass
if valign == "Centered":
y += math.floor(h / 2.) + 1
if valign == "Bottom":
y += h
# Transform from y position to distance from top of screen to top of actor
w, h = self.interactor.GetRenderWindow().GetSize()
return h - y

@top.setter
def top(self, t):
"""
Provides support for the old-style of coordinates; left + top, vs x + y
Sets actor y using distance in pixels from top of window to top of actor
"""
doc = "The top side of the text actor, adjusted by height/justification"

def fget(self):
"""
Returns distance in pixels from top of window to top of actor
"""
w, h = text_dimensions(self.text, self.actor.GetTextProperty())
valign = self.actor.GetTextProperty().GetVerticalJustificationAsString()
y = self.y
# Adjust from alignment point to top of the actor
if valign == "Top":
pass
if valign == "Centered":
y += math.floor(h / 2.) + 1
if valign == "Bottom":
y += h
# Transform from y position to distance from top of screen to top of actor
w, h = self.interactor.GetRenderWindow().GetSize()
return h - y

def fset(self, t):
"""
Sets actor y using distance in pixels from top of window to top of actor
"""
# Get the text's size to adjust for alignment
w, h = text_dimensions(self.text, self.actor.GetTextProperty())

valign = self.actor.GetTextProperty().GetVerticalJustificationAsString()
# Adjust position based on alignment
if valign == "Top":
y = t
# Since it's not top-aligned, alignment point will be lower (and we're in units from top)
elif valign == "Centered":
y = t + math.floor(h / 2.) + 1
elif valign == "Bottom":
y = t + h
# Convert the y from pixels from top to pixels from bottom
w, h = self.interactor.GetRenderWindow().GetSize()
self.y = h - y

return locals()
top = property(**top())
# Get the text's size to adjust for alignment
w, h = text_dimensions(self.text, self.actor.GetTextProperty())

valign = self.actor.GetTextProperty().GetVerticalJustificationAsString()
# Adjust position based on alignment
if valign == "Top":
y = t
# Since it's not top-aligned, alignment point will be lower (and we're in units from top)
elif valign == "Centered":
y = t + math.floor(h / 2.) + 1
elif valign == "Bottom":
y = t + h
# Convert the y from pixels from top to pixels from bottom
w, h = self.interactor.GetRenderWindow().GetSize()
self.y = h - y

def __repr__(self):
return "<Label Widget: %s>" % self.get_text()
Expand Down

0 comments on commit 0d19b38

Please sign in to comment.