forked from humberry/ui-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThree-Column-Sortable-TableView.py
128 lines (116 loc) · 5.22 KB
/
Three-Column-Sortable-TableView.py
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
# coding: utf-8
import ui, os, datetime
from operator import itemgetter
class MyTableViewDataSource (object):
def __init__(self, row_height):
self.row_height = row_height
self.width = None
def tableview_number_of_rows(self, tableview, section):
return len(tableview.data_source.items)
def tableview_cell_for_row(self, tableview, section, row):
self.width, height = ui.get_screen_size()
cell = ui.TableViewCell()
cell.bounds = (0,0,self.width,self.row_height)
for i in range(3):
self.make_labels(cell, tableview.data_source.items[row][i], i)
return cell
def make_labels(self, cell, text, pos):
label = ui.Label()
label.border_color = 'lightgrey'
label.border_width = 0.5
if pos == 2:
label.text = str(datetime.datetime.fromtimestamp(text))
else:
label.text = str(text)
label.frame = (pos*self.width/3,0,self.width/3,self.row_height)
label.alignment = ui.ALIGN_CENTER
cell.content_view.add_subview(label)
class MyTableViewDelegate(object):
def tableview_did_select(self, tableview, section, row):
print 'select'
def tableview_did_deselect(self, tableview, section, row):
print 'deselect'
class MyTableView(ui.View):
def __init__(self):
self.dirs = []
self.files = []
self.select_color = 'lightgrey'
self.unselect_color = 'white'
self.active_button = None
self.button_height = 50
self.btn_name = self.make_buttons('Name')
self.btn_size = self.make_buttons('Size')
self.btn_date = self.make_buttons('Date')
self.tv = ui.TableView()
self.tv.row_height = 30
self.tv.data_source = MyTableViewDataSource(self.tv.row_height)
self.get_dir()
self.all_items = self.dirs + self.files
self.tv.data_source.items = self.all_items
self.name = 'TableView-Test'
#self.tv.delegate = MyTableViewDelegate()
self.tv.allows_selection = False
self.add_subview(self.tv)
self.present('full_screen')
def make_buttons(self, name):
button = ui.Button()
button.name = name
button.title = name
button.border_color = 'blue'
button.border_width = 1
button.corner_radius = 3
button.background_color = self.unselect_color
button.action = self.btn_action
self.add_subview(button)
return button
def btn_action(self, sender):
names = [self.btn_name.name, self.btn_size.name, self.btn_date.name]
sender_index = names.index(sender.name)
if sender.background_color == (1.0, 1.0, 1.0, 1.0): #thrid click on the same column doesn't work if it's no hardcoded color
if sender.background_color == self.unselect_color:
sender.background_color = self.select_color
self.all_items = sorted(self.all_items, key=itemgetter(sender_index))
else:
sender.background_color = self.unselect_color
self.all_items = sorted(self.all_items, key=itemgetter(sender_index), reverse=True)
else:
if self.active_button == None:
self.active_button = sender.name
if sender.name == self.btn_name.name:
self.btn_name.background_color = self.select_color
self.all_items = sorted(self.all_items, key=itemgetter(sender_index))
else:
self.btn_name.background_color = self.unselect_color
if sender.name == self.btn_size.name:
self.btn_size.background_color = self.select_color
self.all_items = sorted(self.all_items, key=itemgetter(sender_index))
else:
self.btn_size.background_color = self.unselect_color
if sender.name == self.btn_date.name:
self.btn_date.background_color = self.select_color
self.all_items = sorted(self.all_items, key=itemgetter(sender_index))
else:
self.btn_date.background_color = self.unselect_color
self.tv.data_source.items = self.all_items
self.tv.reload()
self.active_button = sender.name
def layout(self):
self.tv.reload()
self.btn_name.frame =(0*self.width/3, 0, self.width/3, self.button_height)
self.btn_size.frame =(1*self.width/3, 0, self.width/3, self.button_height)
self.btn_date.frame =(2*self.width/3, 0, self.width/3, self.button_height)
self.tv.frame = (0, self.button_height, self.width, self.height - self.button_height)
def get_dir(self):
path = os.getcwd()
self.dirs = [] if path == os.path.expanduser('~') else [['..', '<DIR>', 0.0]]
self.files = []
for entry in sorted(os.listdir(path)):
full_pathname = path + '/' + entry
if os.path.isdir(full_pathname):
date = os.path.getmtime(full_pathname)
self.dirs.append((entry, '<DIR>', date))
else:
size = os.path.getsize(full_pathname)
date = os.path.getmtime(full_pathname)
self.files.append((entry, size, date))
MyTableView()