Skip to content

Instantly share code, notes, and snippets.

@Phuket2
Created September 27, 2016 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phuket2/14c69bc64c0f38b51ef7bd5d69d5061b to your computer and use it in GitHub Desktop.
Save Phuket2/14c69bc64c0f38b51ef7bd5d69d5061b to your computer and use it in GitHub Desktop.
simple_view2.py
# Pythonista Forum - @Phuket2
import ui, editor
'''
a_path = os.path.expanduser('~/Documents/MyProjects/MyViews')
sys.path.append(a_path)
import class_walker as cw
'''
# https://gist.github.com/42924cb9a311ae47084e7f0d05f362f4
def add_labels_to_sv(parent):
# just for debuging. add a label to each subview with the views name
for v in parent.subviews:
lb = ui.Label()
lb.text = str(v.name)
lb.text_color = 'black'
lb.size_to_fit()
lb.center = v.bounds.center()
lb.flex = 'lrtb'
v.add_subview(lb)
def add_label(v):
# just for debuging. add a label to each subview with the views name
lb = ui.Label()
lb.text = v.name
lb.text_color = 'black'
lb.size_to_fit()
lb.center = v.bounds.center()
lb.flex = 'lrtb'
v.add_subview(lb)
def translate_number(wh, n):
# i know can be done better... but thinking will expand this
# wh is either width or height
# n is a number that we translate
if n == 0 or n > 1:
return n
elif n == -1:
return wh
elif n < 1.0:
return wh * n
def apply_kwargs(kwargs, obj):
for k, v in kwargs.items():
if hasattr(obj, k):
setattr(obj, k, v)
def create_content_view(parent, name='cv', margin=(0, 0), **kwargs):
cv = ui.View(name=name, frame=ui.Rect(*parent.frame).inset(*margin))
cv.flex = 'wh'
apply_kwargs(kwargs, cv)
return cv
def insert_into_view(view, v_list, v_names=None, vert=True,
v_margin=(0, 0), **kwargs):
# if v_names (view names list), not passed, we make sure we have a
# valid list.
v_names = v_names if v_names else []
# the v_names list is made vaild here. we ensure we have a entry for
# every view that will be created. If the v_names list is shorter
# than the v_list, the list is appened to with generated view names.
# The names are generated in the form {parent.name}-{counter}. To
# help aviod conflicts, the counter starts with the number of
# subviews in the parent. Its ok to pass an incomplete list of names,
# the ones supplied will be used, the unsupplied will be generated.
if len(v_names) < len(v_list):
cnt = len(view.subviews)
for i in range(len(v_names), len(v_list)):
v_names.append('{}-{}'.format(view.name, cnt))
cnt += 1
print(v_names)
r = ui.Rect(*view.bounds)
def get_v():
# return based on vertical or horizontal, maybe not good. But
# i find this helpful to try to keep the code generic as i can
return r.height if vert else r.width
# translate the numbers in v_list to absolute numbers, except zero
v_list = [translate_number(get_v(), x) if x else x for x in v_list]
# var is the space left over in the view after the absolute heights
# have been subtracted divided by the number if items that are
# equal to 0
zero_count = v_list.count(0)
# aviod divide by zero error
var = (get_v() - sum(v_list)) / zero_count if zero_count else\
(get_v() - sum(v_list))
# replaces 0 in v_list with var.
v_list = [var if x == 0 else x for x in v_list]
lst = [] # a list of the views created, we return this
xy = 0 # keep track of width or height
# go through v_list, create the views as subviews of cv, and apply
# some attrs to the created views
for i, num in enumerate(v_list):
frame = ui.Rect(0, xy, r.width, num).inset(*v_margin) if vert\
else ui.Rect(xy, 0, num, r.height).inset(*v_margin)
#v_name = v_names[i].get()
v = ui.View(name=v_names[i], frame=frame)
v.flex = 'whlrtb'
apply_kwargs(kwargs, v)
view.add_subview(v)
xy += num
lst.append(v)
return lst # return a list of the newly created views
def split_view_h(parent, view, v_list, v_names=None,h_gap=0):
#split a view horizontally.
v_names = v_names if v_names else []
if len(v_names) < len(v_list):
cnt = len(view.subviews)
for i in range(len(v_names), len(v_list)):
v_names.append('{}-{}'.format(view.name, cnt))
cnt += 1
r = ui.Rect(*view.frame)
# reduce our width to allow for the h_gap param
r.width -= h_gap * (len(v_list) - 1)
# translate the numbers in v_list to absolute numbers
v_list = [translate_number(r.width, x) if x else x for x in v_list]
# var is the space left over in the view after the absolute heights
# have been subtracted divided by the number if items that are
# equal to 0
zero_count = v_list.count(0)
# aviod divide by zero error
var = (r.width - sum(v_list)) / zero_count if zero_count else\
(r.width - sum(v_list))
# replaces 0 in v_list with var.
v_list = [var if x == 0 else x for x in v_list]
x = r.x
y = r.y
num_views = len(parent.subviews)
lst = [] # a list of the views created, we return this
for i, w in enumerate(v_list):
frame = ui.Rect(x, y, w, r.height)
if i is 0:
# this is the frame we split. we just resize it, the view is
# NOT renamed
view.frame = frame
else:
frame.x += (h_gap * i)
v = ui.View(name=v_names[i], frame=frame)
v.flex = 'whlrtb'
v.border_width = view.border_width
v.corner_radius = view.corner_radius
parent.add_subview(v)
lst.append(v)
x += w
return lst # return a list of the newly created views
class MyClass(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cv = None
self.make_view()
def make_view(self):
# dont need this, just if you want an enclosing view,
# can be handy.
cv =create_content_view(self, margin=(5, 5),
bg_color='pink', corner_radius=6)
self.cv = cv
self.add_subview(cv)
insert_into_view(cv, [44, 0],
#v_names=['title', 'content', 'tb'],
v_names=['title', 'content', 'tb'],
vert=True, v_margin=(4, 4),
border_color='maroon', corner_radius=6,
border_width=.5)
split_view_h(cv, cv['title'], [.5, 0], h_gap=5)
'''
insert_into_view(cv['root1'], [44, 0, 60],
bn ='root', vert=True, v_margin=(4, 4),
border_color='maroon', corner_radius=6,
border_width=.5)
insert_into_view(cv['root0'], [0, 0, 0, 0, 0],
bn ='root', vert=False, v_margin=(4, 4),
border_color='maroon', corner_radius=6,
border_width=.5)
insert_into_view(cv['h4'], [0, 0, 0],
bn ='root', vert=True, v_margin=(4, 4),
border_color='maroon', corner_radius=6,
border_width=.5)
'''
#cv.width = 200
#cv.y += 44
#cv.height -=44
if __name__ == '__main__':
_use_theme = False
w, h = 600, 800
w, h = 375, 667
w, h = 320, 568
f = (0, 0, w, h)
style = 'sheet'
mc = MyClass(frame=f, bg_color='white')
if not _use_theme:
mc.present(style=style, animated=False)
else:
editor.present_themed(mc, theme_name='Oceanic', style=style, animated=False)
add_labels_to_sv(mc.cv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment