aboutsummaryrefslogtreecommitdiffstats
path: root/examples/elementary/test_genlist_1.py
blob: 3827c7361e5785d4b7f3f293b800306d396c8109 (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# encoding: utf-8


from efl.evas import Rectangle, EXPAND_BOTH, FILL_BOTH, EVAS_CALLBACK_MOUSE_DOWN

from efl import elementary as elm
from efl.elementary import StandardWindow, Box, Label, Button, Radio, Icon, \
    Genlist, GenlistItemClass, ELM_LIST_COMPRESS


# item class "classic" implementation method
def itc1_text_get(obj, part, item_data):
    return "Item # %i" % item_data

def itc1_content_get(obj, part, item_data):
    if part == 'elm.swallow.icon':
        return  Icon(obj, standard='user-home')
    elif part == 'elm.swallow.end':
        return  Icon(obj, standard='user-trash')

itc1 = GenlistItemClass(item_style="default",
                        text_get_func=itc1_text_get,
                        content_get_func=itc1_content_get)


# item class "subclass" implementation method
class MyItemClass(GenlistItemClass):
    def __init__(self):
        GenlistItemClass.__init__(self, item_style="default")

    def text_get(self, obj, part, item_data):
        return "Item # %i (itc2)" % item_data

    def content_get(self, obj, part, item_data):
        if part == 'elm.swallow.icon':
            return  Icon(obj, standard='user-trash')
        elif part == 'elm.swallow.end':
            return  Icon(obj, standard='user-home')

itc2 = MyItemClass()


# list items callbacks
def item_selected_cb(item, gl, item_data):
    print("\n---GenlistItem selected---")
    print(item)
    print(gl)
    print(("item_data: %s" % item_data))


# list callbacks
def list_selected_cb(gl, gli, *args, **kwargs):
    print("\n---Genlist selected---")
    print(gl)
    print(gli)
    print(args)
    print(kwargs)

def list_clicked_double_cb(gl, gli):
    print("\n---Genlist double clcked---")
    print(gl)
    print(gli)

def list_longpressed_cb(gl, gli):
    print("\n---Genlist longpressed---")
    print(gl)
    print(gli)


# over rect callbacks
def rect_mouse_down_cb(evas, evt, gl):
    print("\n---OverRect click---")
    item, pos = gl.at_xy_item_get(evt.position.canvas.x, evt.position.canvas.y)
    if item:
        print("Over item: %s" % item)
        print("At pos: %d" % pos)
    else:
        print("Over none")

# tooltip creation function
def tooltip_content_cb(gl, item, obj):
    txt = "Tooltip <b>from callback</b> for item # %d" % item.data
    return Label(gl, text=txt)


def test_genlist_1(parent):
    win = StandardWindow("Genlist", "Genlist test 1",
                         size=(480, 800), autodel=True)

    # main vertical box
    box = Box(win, size_hint_weight=EXPAND_BOTH)
    win.resize_object_add(box)
    box.show()

    # Genlist
    gl = Genlist(win, homogeneous=True, mode=ELM_LIST_COMPRESS,
                 size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
    gl.callback_selected_add(list_selected_cb, "arg1", "arg2",
                             kwarg1="kwarg1", kwarg2="kwarg2")
    gl.callback_clicked_double_add(list_clicked_double_cb)
    gl.callback_longpressed_add(list_longpressed_cb)
    box.pack_end(gl)
    gl.show()

    # over evas rectangle (transparent, used to catch mouse events)
    over = Rectangle(win.evas, color=(0,0,0,0), repeat_events=True,
                     size_hint_expand=EXPAND_BOTH)
    over.event_callback_add(EVAS_CALLBACK_MOUSE_DOWN, rect_mouse_down_cb, gl)
    win.resize_object_add(over)
    over.show()

    # populate the genlist
    for i in range(0, 2000):
        
        if i % 2:
            item = gl.item_append(itc1, i, func=item_selected_cb)
            item.tooltip_text_set("Static Tooltip for item # %d" % i)
        else:
            item = gl.item_append(itc2, i, func=item_selected_cb)
            item.tooltip_content_cb_set(tooltip_content_cb)

        if i == 50:     it_50 = item
        elif i == 1500: it_1500 = item

    # scroll_to methods
    hbox = Box(win, horizontal=True)
    box.pack_end(hbox)
    hbox.show()

    rdg = rd = Radio(win, text='SCROLL_IN',
                     state_value=elm.ELM_GENLIST_ITEM_SCROLLTO_IN)
    hbox.pack_end(rd)
    rd.show()

    rd = Radio(win, text='SCROLL_TOP',
               state_value=elm.ELM_GENLIST_ITEM_SCROLLTO_TOP)
    rd.group_add(rdg)
    hbox.pack_end(rd)
    rd.show()

    rd = Radio(win, text='SCROLL_MIDDLE',
               state_value=elm.ELM_GENLIST_ITEM_SCROLLTO_MIDDLE)
    rd.group_add(rdg)
    hbox.pack_end(rd)
    rd.show()

    rd = Radio(win, text='SCROLL_BOTTOM',
               state_value=elm.ELM_GENLIST_ITEM_SCROLLTO_BOTTOM)
    rd.group_add(rdg)
    hbox.pack_end(rd)
    rd.show()

    rdg.value = elm.ELM_GENLIST_ITEM_SCROLLTO_IN

    # item 50/1500 buttons
    hbox = Box(win, horizontal=True)
    box.pack_end(hbox)
    hbox.show()

    bt = Button(win, text="Show 50")
    bt.callback_clicked_add(lambda bt: it_50.show(rdg.value))
    hbox.pack_end(bt)
    bt.show()

    bt = Button(win, text="BringIn 50")
    bt.callback_clicked_add(lambda bt: it_50.bring_in(rdg.value))
    hbox.pack_end(bt)
    bt.show()

    bt = Button(win, text="Show 1500")
    bt.callback_clicked_add(lambda bt: it_1500.show(rdg.value))
    hbox.pack_end(bt)
    bt.show()

    bt = Button(win, text="BringIn 1500")
    bt.callback_clicked_add(lambda bt: it_1500.bring_in(rdg.value))
    hbox.pack_end(bt)
    bt.show()

    # item content unset
    def content_unset_clicked(bt, gl):
        item = gl.selected_item
        if item is None:
            print("You must select an item first!")
        else:
            contents = item.all_contents_unset()
            print(contents)
            for obj in contents:
                obj.pos = (200, 0)
                obj.show()
            # Now all the unsetted objects are orphan in the canvas,
            # the user should do something with them
        
    bt = Button(win, text="Item content unset")
    bt.callback_clicked_add(content_unset_clicked, gl)
    hbox.pack_end(bt)
    bt.show()

    # show the window
    win.show()


if __name__ == "__main__":
    elm.policy_set(elm.ELM_POLICY_QUIT, elm.ELM_POLICY_QUIT_LAST_WINDOW_CLOSED)
    test_genlist_1(None)
    elm.run()