~ubuntu-branches/debian/stretch/electrum/stretch

« back to all changes in this revision

Viewing changes to gui/kivy/tools/.buildozer/android/platform/python-for-android/dist/kivy/python-install/share/kivy-examples/widgets/lists/list_ops.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2016-04-04 03:02:39 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20160404030239-0szgkio8yryjv7c9
Tags: 2.6.3-1
* New upstream release.
  - Drop backported install-wizard-connect.patch.
* Add Suggests: python-zbar and update the installation hint to suggest
  apt-get instead of pip (closes: #819517).
* Bump Standards-Version to 3.9.7 (no changes).
* Update Vcs-* links.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from kivy.adapters.dictadapter import DictAdapter
 
2
from kivy.properties import NumericProperty, ListProperty, \
 
3
        BooleanProperty, AliasProperty, ObjectProperty
 
4
from kivy.uix.boxlayout import BoxLayout
 
5
from kivy.uix.gridlayout import GridLayout
 
6
from kivy.uix.listview import ListView, ListItemButton
 
7
from kivy.uix.label import Label
 
8
from kivy.uix.button import Button
 
9
from kivy.uix.widget import Widget
 
10
 
 
11
 
 
12
class OpsDictAdapter(DictAdapter):
 
13
 
 
14
    listview_id = NumericProperty(0)
 
15
    owning_view = ObjectProperty(None)
 
16
 
 
17
    def __init__(self, **kwargs):
 
18
        self.listview_id = kwargs['listview_id']
 
19
        super(OpsDictAdapter, self).__init__(**kwargs)
 
20
 
 
21
    def on_selection_change(self, *args):
 
22
        for i in range(len(self.selection)):
 
23
            listview_selection_buttons[self.listview_id][i].text = \
 
24
                    self.selection[i].text
 
25
 
 
26
        if self.listview_id is 0:
 
27
            # Scroll to the most recently selected item.
 
28
            if len(self.selection) > 0:
 
29
                print('selection', self.selection)
 
30
                self.owning_view.scroll_to(
 
31
                    index=self.sorted_keys.index(self.selection[-1].text))
 
32
 
 
33
        elif self.listview_id is 1:
 
34
            # Scroll to the selected item that is the minimum of a sort.
 
35
            if len(self.selection) > 0:
 
36
                self.owning_view.scroll_to(
 
37
                    index=self.sorted_keys.index(
 
38
                        sorted([sel.text for sel in self.selection])[0]))
 
39
 
 
40
        elif self.listview_id is 2:
 
41
            # Scroll to the selected item that is the maximum of a sort.
 
42
            if len(self.selection) > 0:
 
43
                self.owning_view.scroll_to(
 
44
                    index=self.sorted_keys.index(
 
45
                        sorted([sel.text for sel in self.selection])[-1]))
 
46
 
 
47
 
 
48
class SelectionMonitor(Widget):
 
49
 
 
50
    def get_count_string(self):
 
51
        return "Total sel: " + str(self.sel_count_0 +
 
52
                                   self.sel_count_1 +
 
53
                                   self.sel_count_2 +
 
54
                                   self.sel_count_3 +
 
55
                                   self.sel_count_4 +
 
56
                                   self.sel_count_5 +
 
57
                                   self.sel_count_6)
 
58
 
 
59
    def set_count_string(self, value):
 
60
        self.count_string = value
 
61
 
 
62
    sel_count_0 = NumericProperty(0)
 
63
    sel_count_1 = NumericProperty(0)
 
64
    sel_count_2 = NumericProperty(0)
 
65
    sel_count_3 = NumericProperty(0)
 
66
    sel_count_4 = NumericProperty(0)
 
67
    sel_count_5 = NumericProperty(0)
 
68
    sel_count_6 = NumericProperty(0)
 
69
 
 
70
    count_string = AliasProperty(get_count_string,
 
71
                                 set_count_string,
 
72
                                 bind=('sel_count_0',
 
73
                                       'sel_count_1',
 
74
                                       'sel_count_2',
 
75
                                       'sel_count_3',
 
76
                                       'sel_count_4',
 
77
                                       'sel_count_5',
 
78
                                       'sel_count_6'))
 
79
 
 
80
    def __init__(self, **kwargs):
 
81
        super(SelectionMonitor, self).__init__(**kwargs)
 
82
 
 
83
    def update_sel_count_0(self, adapter, *args):
 
84
        self.sel_count_0 = len(adapter.selection)
 
85
 
 
86
    def update_sel_count_1(self, adapter, *args):
 
87
        self.sel_count_1 = len(adapter.selection)
 
88
 
 
89
    def update_sel_count_2(self, adapter, *args):
 
90
        self.sel_count_2 = len(adapter.selection)
 
91
 
 
92
    def update_sel_count_3(self, adapter, *args):
 
93
        self.sel_count_3 = len(adapter.selection)
 
94
 
 
95
    def update_sel_count_4(self, adapter, *args):
 
96
        self.sel_count_4 = len(adapter.selection)
 
97
 
 
98
    def update_sel_count_5(self, adapter, *args):
 
99
        self.sel_count_5 = len(adapter.selection)
 
100
 
 
101
    def update_sel_count_6(self, adapter, *args):
 
102
        self.sel_count_6 = len(adapter.selection)
 
103
 
 
104
 
 
105
letters_dict = \
 
106
    {l: {'text': l, 'is_selected': False} for l in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}
 
107
 
 
108
listview_selection_buttons = {}
 
109
 
 
110
 
 
111
class OpsView(BoxLayout):
 
112
    '''Seven list views are shown at the bottom, each focusing on one of the
 
113
    available operations for collection adapters: scroll_to, trim_to_sel,
 
114
    trim_left_of_sel, etc. At the top is a display that shows individual
 
115
    items selected across the seven lists, along with a total of all selected
 
116
    items for the lists.
 
117
    '''
 
118
    def __init__(self, **kwargs):
 
119
        kwargs['orientation'] = 'vertical'
 
120
        super(OpsView, self).__init__(**kwargs)
 
121
 
 
122
        # UPPER PANEL
 
123
        #
 
124
        # Create an upper panel with labels for items selected in the
 
125
        # listviews shown in the lower panel.
 
126
        #
 
127
        upper_panel = BoxLayout()
 
128
 
 
129
        grid_layout = GridLayout(cols=1,
 
130
                                 row_force_default=True,
 
131
                                 row_default_height=40)
 
132
 
 
133
        # On the left side of the upper panel, show the selected items. There
 
134
        # is a total possible of 5 for each listview, so 5 buttons are made.
 
135
        #
 
136
        for listview_id in range(7):
 
137
            box_layout = BoxLayout()
 
138
            listview_selection_buttons[listview_id] = []
 
139
 
 
140
            box_layout.add_widget(
 
141
                    Label(text="Listview #{0} selection".format(listview_id)))
 
142
 
 
143
            for i in range(5):
 
144
                button = Button(size_hint_x=None, width=50,
 
145
                                size_hint_y=None, height=35,
 
146
                                background_color=[.25, .25, .6, 1.0])
 
147
                listview_selection_buttons[listview_id].append(button)
 
148
                box_layout.add_widget(button)
 
149
 
 
150
            grid_layout.add_widget(box_layout)
 
151
 
 
152
        upper_panel.add_widget(grid_layout)
 
153
 
 
154
        # On the right side of the upper panel, show the total selected count.
 
155
 
 
156
        total_selection_button = Button(text="Total: 0",
 
157
                                        size_hint=(.5, 1.0),
 
158
                                        background_color=[.25, .25, .6, 1.0])
 
159
        selection_monitor = SelectionMonitor()
 
160
        selection_monitor.bind(
 
161
            count_string=total_selection_button.setter('text'))
 
162
 
 
163
        upper_panel.add_widget(total_selection_button)
 
164
 
 
165
        self.add_widget(upper_panel)
 
166
 
 
167
        # LOWER PANEL
 
168
        #
 
169
        # Show 6 listviews with either a header label or a header button.
 
170
        #
 
171
        grid_layout = GridLayout(cols=7)
 
172
 
 
173
        list_item_args_converter = \
 
174
                lambda row_index, rec: {'text': rec['text'],
 
175
                                        'size_hint_y': None,
 
176
                                        'height': 25}
 
177
 
 
178
        letters = [l for l in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']
 
179
 
 
180
        adapters = []
 
181
 
 
182
        # Create 7 listviews, limiting selection to 5 items for the first 3,
 
183
        # and allowing unlimited selection for the others, by setting the
 
184
        # selection limit to 1000.
 
185
        #
 
186
        # Use OpsDictAdapter, from above, which will post selections to
 
187
        # the display in the top panel.
 
188
        #
 
189
        listview_header_widgets = [Label(text="scroll_to rec",
 
190
                                         size_hint_y=None,
 
191
                                         height=25),
 
192
                                   Label(text="scroll_to min",
 
193
                                         size_hint_y=None,
 
194
                                         height=25),
 
195
                                   Label(text="scroll_to max",
 
196
                                         size_hint_y=None,
 
197
                                         height=25),
 
198
                                   Button(text="trim_left_of_sel",
 
199
                                          size_hint_y=None,
 
200
                                          height=25),
 
201
                                   Button(text="trim_right_of_sel",
 
202
                                          size_hint_y=None,
 
203
                                          height=25),
 
204
                                   Button(text="trim_to_sel",
 
205
                                          size_hint_y=None,
 
206
                                          height=25),
 
207
                                   Button(text="cut_to_sel",
 
208
                                          size_hint_y=None,
 
209
                                          height=25)]
 
210
 
 
211
        for listview_id in range(7):
 
212
 
 
213
            box_layout = BoxLayout(orientation='vertical')
 
214
 
 
215
            letters_dict_adapter = \
 
216
                    OpsDictAdapter(
 
217
                        listview_id=listview_id,
 
218
                        sorted_keys=letters[:],
 
219
                        data=letters_dict,
 
220
                        args_converter=list_item_args_converter,
 
221
                        selection_mode='multiple',
 
222
                        selection_limit=5 if listview_id < 3 else 1000,
 
223
                        allow_empty_selection=True,
 
224
                        cls=ListItemButton)
 
225
 
 
226
            adapters.append(letters_dict_adapter)
 
227
 
 
228
            letters_list_view = ListView(adapter=letters_dict_adapter)
 
229
 
 
230
            letters_dict_adapter.owning_view = letters_list_view
 
231
 
 
232
            box_layout.add_widget(listview_header_widgets[listview_id])
 
233
            box_layout.add_widget(letters_list_view)
 
234
 
 
235
            grid_layout.add_widget(box_layout)
 
236
 
 
237
        # Bind selection of each list to the selection monitor.
 
238
        adapters[0].bind(selection=selection_monitor.update_sel_count_0)
 
239
        adapters[1].bind(selection=selection_monitor.update_sel_count_1)
 
240
        adapters[2].bind(selection=selection_monitor.update_sel_count_2)
 
241
        adapters[3].bind(selection=selection_monitor.update_sel_count_3)
 
242
        adapters[4].bind(selection=selection_monitor.update_sel_count_4)
 
243
        adapters[5].bind(selection=selection_monitor.update_sel_count_5)
 
244
        adapters[6].bind(selection=selection_monitor.update_sel_count_6)
 
245
 
 
246
        # For the last three listviews, bind the header buttons to the trim
 
247
        # op method in the associated dict adapter instance.
 
248
        button_3 = listview_header_widgets[3]
 
249
        button_4 = listview_header_widgets[4]
 
250
        button_5 = listview_header_widgets[5]
 
251
        button_6 = listview_header_widgets[6]
 
252
        button_3.bind(on_release=adapters[3].trim_left_of_sel)
 
253
        button_4.bind(on_release=adapters[4].trim_right_of_sel)
 
254
        button_5.bind(on_release=adapters[5].trim_to_sel)
 
255
        button_6.bind(on_release=adapters[6].cut_to_sel)
 
256
 
 
257
        self.add_widget(grid_layout)
 
258
 
 
259
 
 
260
if __name__ == '__main__':
 
261
    from kivy.base import runTouchApp
 
262
    runTouchApp(OpsView(width=800))