forked from inclement/noGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylistview.py
1033 lines (797 loc) · 38.3 KB
/
mylistview.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
List View
===========
.. versionadded:: 1.5
.. warning::
This code is still experimental, and its API is subject to change in a
future version.
The :class:`~kivy.uix.listview.ListView` widget provides a scrollable/pannable
viewport that is clipped at the scrollview's bounding box, which contains
list item view instances.
:class:`~kivy.uix.listview.ListView` implements :class:`AbstractView` as a
vertical scrollable list. :class:`AbstractView` has one property, adapter.
:class:`~kivy.uix.listview.ListView` sets adapter to one of:
:class:`~kivy.adapters.simplelistadapter.SimpleListAdapter`,
:class:`~kivy.adapters.listadapter.ListAdapter`, or
:class:`~kivy.adapters.dictadapter.DictAdapter`.
Introduction
------------
Lists are central parts of many software projects. Kivy's approach to lists
includes providing solutions for simple lists, along with a substantial
framework for building lists of moderate to advanced complexity. For the new
user of the system, it can be difficult to ramp up from simple to advanced. For
this reason, Kivy provides an extensive set of examples that you may wish to
run first, to get a taste of the range of functionality offered. You can tell
from the names of the examples that they illustrate the "ramping up" from
simple to advanced:
* kivy/examples/widgets/lists/list_simple.py
* kivy/examples/widgets/lists/list_simple_in_kv.py
* kivy/examples/widgets/lists/list_simple_in_kv_2.py
* kivy/examples/widgets/lists/list_master_detail.py
* kivy/examples/widgets/lists/list_two_up.py
* kivy/examples/widgets/lists/list_kv.py
* kivy/examples/widgets/lists/list_composite.py
* kivy/examples/widgets/lists/list_cascade.py
* kivy/examples/widgets/lists/list_cascade_dict.py
* kivy/examples/widgets/lists/list_cascade_images.py
* kivy/examples/widgets/lists/list_ops.py
Many of the examples feature selection, some restricting selection to single
selection, where only one item at at time can be selected, and others allowing
multiple item selection. Many of the examples illustrate how selection in one
list can be connected to action and selection in another view or another list.
Find your own way of reading the documentation here, examining the source code
for the example apps, and running the examples. Some may prefer to read the
documentation through first, others may want to run the examples and view their
code. No matter what you do, going back and forth will likely be needed.
Basic Example
-------------
In its simplest form, we make a listview with 100 items::
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
class MainView(GridLayout):
def __init__(self, **kwargs):
kwargs['cols'] = 2
super(MainView, self).__init__(**kwargs)
list_view = ListView(
item_strings=[str(index) for index in xrange(100)])
self.add_widget(list_view)
if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(MainView(width=800))
Or, we could declare the listview in using the kv language::
from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
Builder.load_string("""
<ListViewModal>:
size_hint: None, None
size: 400, 400
ListView:
size_hint: .8, .8
item_strings: [str(index) for index in xrange(100)]
""")
class ListViewModal(ModalView):
def __init__(self, **kwargs):
super(ListViewModal, self).__init__(**kwargs)
class MainView(GridLayout):
def __init__(self, **kwargs):
kwargs['cols'] = 1
super(MainView, self).__init__(**kwargs)
listview_modal = ListViewModal()
self.add_widget(listview_modal)
if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(MainView(width=800))
Using an Adapter
-------------------
Behind the scenes, the basic example above uses
:class:`~kivy.adapters.simplelistadapter.SimpleListAdapter`. When the
constructor for :class:`~kivy.uix.listview.ListView` sees that only a list of
strings is provided as an argument, called item_strings, it creates an instance
of :class:`~kivy.adapters.simplelistadapter.SimpleListAdapter` using the list
of strings.
Simple in :class:`~kivy.adapters.simplelistadapter.SimpleListAdapter` means:
*without selection support*. It is a scrollable list of items that do not
respond to touch events.
To use :class:`SimpleListAdaper` explicitly in creating a ListView instance,
do::
simple_list_adapter = SimpleListAdapter(
data=["Item #{0}".format(i) for i in xrange(100)],
cls=Label)
list_view = ListView(adapter=simple_list_adapter)
The instance of :class:`~kivy.adapters.simplelistadapter.SimpleListAdapter` has
a required data argument, which contains data items to use for instantiating
Label views for the list view (Note the cls=Label argument). The data items are
strings. Each item string is set by
:class:`~kivy.adapters.simplelistadapter.SimpleListAdapter` as the *text*
argument for each Label instantiation.
You can declare a ListView with an adapter in a kv file, with special attention
given to the way longer python blocks are indented::
from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.factory import Factory
# Note the special nature of indentation in the adapter declaration, where
# the adapter: is on one line, then the value side must be given at one
# level of indentation.
Builder.load_string("""
#:import label kivy.uix.label
#:import sla kivy.adapters.simplelistadapter
<ListViewModal>:
size_hint: None, None
size: 400, 400
ListView:
size_hint: .8, .8
adapter:
sla.SimpleListAdapter(
data=["Item #{0}".format(i) for i in xrange(100)],
cls=label.Label)
""")
class ListViewModal(ModalView):
def __init__(self, **kwargs):
super(ListViewModal, self).__init__(**kwargs)
class MainView(GridLayout):
def __init__(self, **kwargs):
kwargs['cols'] = 1
super(MainView, self).__init__(**kwargs)
listview_modal = ListViewModal()
self.add_widget(listview_modal)
if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(MainView(width=800))
ListAdapter and DictAdapter
---------------------------
For many uses of a list, the data is more than a simple list of strings.
Selection functionality is also often needed.
:class:`~kivy.adapters.listadapter.ListAdapter` and
:class:`~kivy.adapters.dictadapter.DictAdapter` cover these more elaborate
needs.
:class:`~kivy.adapters.listadapter.ListAdapter` is the base class for
:class:`~kivy.adapters.dictadapter.DictAdapter`, so we can start with it.
See the :class:`~kivy.adapters.listadapter.ListAdapter` docs for details, but
here are synopses of its arguments:
* *data*: strings, class instances, dicts, etc. that form the basis data
for instantiating views.
* *cls*: a Kivy view that is to be instantiated for each list item. There
are several built-in types available, including ListItemLabel and
ListItemButton, or you can make your own class that mixes in the
required `~kivy.uix.listview.SelectableView`.
* *template*: the name of a Kivy language (kv) template that defines the
Kivy view for each list item.
.. note::
Pick only one, cls or template, to provide as an argument.
* *args_converter*: a function that takes a data item object as input, and
uses it to build and return an args dict, ready
to be used in a call to instantiate item views useing the item view cls
or template. In the case of cls, the args dict acts as a
kwargs object. For a template, it is treated as a context
(ctx), but is essentially similar in form to kwargs usage.
* *selection_mode*: a string for: 'single', 'multiple' or others (See docs).
* *allow_empty_selection*: a boolean, which if False, the default, forces
there to always be a selection, if there is data
available. If True, selection happens only as a
result of user action.
In narrative, we can summarize with:
A listview's adapter takes data items and uses an args_converter
function to transform them into arguments for making list item view
instances, using either a cls or a kv template.
In a graphic, a summary of the relationship between a listview and its
list adapter, looks like this::
- ------------ ListAdapter or DictAdapter ------------
- | |
- | <list item views> (cls or template) <data items> |
- ListView --> | [args_converter] |
- | |
- | <<< selection handling >>> |
- | |
- ----------------------------------------------------
:class:`~kivy.adapters.dictadapter.DictAdapter` has the same arguments and
requirements as :class:`~kivy.adapters.listadapter.ListAdapter`, except for two
things:
1) There is an additional argument, sorted_keys, which must meet the
requirements of normal python dictionary keys.
2) The data argument is, as you would expect, a dict. Keys in the dict
must include the keys in the sorted_keys argument, but they may form a
superset of the keys in sorted_keys. Values may be strings, class
instances, dicts, etc. (The args_converter uses it, accordingly).
Using an Args Converter
-----------------------
:class:`~kivy.uix.listview.ListView` allows use of built-in list item views,
such as :class:`~kivy.uix.listview.ListItemButton`, your own custom item view
class, or a custom kv template. Whichever type of list item view is used, an
args_converter function is needed to prepare, per list data item, args for
either a cls or template.
.. note::
ListItemLabel and ListItemButton, or custom classes like them, and not the
bare Label nor Button classes, are to be used in the listview system.
Here is an args_converter for use with the built-in
:class:`~kivy.uix.listview.ListItemButton`, specified as a normal Python
function::
def args_converter(row_index, an_obj):
return {'text': an_obj.text,
'size_hint_y': None,
'height': 25}
and as a lambda:
args_converter = lambda row_index, an_obj: {'text': an_obj.text,
'size_hint_y': None,
'height': 25}
In the args converter example above, the data item is assumed to be an object
(class instance), hence the reference an_obj.text.
Here is an example of an args converter that works with list data items that
are dicts::
args_converter = lambda row_index, obj: {'text': obj['text'],
'size_hint_y': None,
'height': 25}
So, it is the responsibility of the developer to code the args_converter
according to the data at hand. The row_index argument can be useful in some
cases, such as when custome labels are needed.
An Example ListView
-------------------
Now, to some example code::
from kivy.adapters.listadapter import ListAdapter
from kivy.uix.listview import ListItemButton, ListView
data = [{'text': str(i), 'is_selected': False} for i in xrange(100)]
args_converter = lambda row_index, rec: {'text': rec['text'],
'size_hint_y': None,
'height': 25}
list_adapter = ListAdapter(data=data,
args_converter=args_converter,
cls=ListItemButton,
selection_mode='single',
allow_empty_selection=False)
list_view = ListView(adapter=list_adapter)
This listview will show 100 buttons with 0..100 labels. The args converter
function works on dict items in the data. ListItemButton views will be
intantiated from the args converted by args_converter for each data item. The
listview will only allow single selection -- additional touches will be
ignored. When the listview is first shown, the first item will already be
selected, because allow_empty_selection is False.
:class:`~kivy.uix.listview.ListItemLabel` works much the same way as
:class:`~kivy.uix.listview.ListItemButton`.
Using a Custom Item View Class
------------------------------
The data used in an adapter can be any of the normal Python types, such as
strings, class instances, and dictionaries. It is up to the programmer to
assure that the args_converter has appropriate functionality.
Here we make a simple DataItem class that has the required text and
is_selected properties::
from kivy.uix.listview import ListItemButton
class DataItem(object):
def __init__(self, text='', is_selected=False):
self.text = text
self.is_selected = is_selected
data_items = []
data_items.append(DataItem(text='cat'))
data_items.append(DataItem(text='dog'))
data_items.append(DataItem(text='frog'))
list_item_args_converter = lambda row_index, obj: {'text': obj.text,
'size_hint_y': None,
'height': 25}
list_adapter = ListAdapter(data=data_items,
args_converter=list_item_args_converter,
selection_mode='single',
propagate_selection_to_data=True,
allow_empty_selection=False,
cls=ListItemButton)
list_view = ListView(adapter=list_adapter)
The data is set in a :class:`~kivy.adapters.listadapter.ListAdapter` along
with a list item args_converter function above (lambda) and arguments
concerning selection: only single selection is allowed, and selection in the
listview will propagate to the data items. The propagation setting means that
the is_selected property for each data item will be set and kept in sync with
the list item views. By having allow_empty_selection=False, when the listview
first appears, the first item, 'cat', will already be selected. The list
adapter will instantiate a :class:`~kivy.uix.listview.ListItemButton` class
instance for each data item, using the assigned args_converter.
The list_vew would be added to a view with add_widget() after the last line,
where it is created. See the basic example at the top of this documentation for
an example of add_widget() use in the context of a sample app.
You may also use the provided :class:`SelectableDataItem` mixin to make a
custom class. Instead of the "manually-constructed" DataItem class above,
we could do::
from kivy.adapters.models import SelectableDataItem
class DataItem(SelectableDataItem):
# Add properties here.
pass
:class:`SelectableDataItem` is a simple mixin class that has an is_selected
property.
Using an Item View Template
---------------------------
:class:`~kivy.uix.listview.SelectableView` is another simple mixin class that
has required properties for a list item: text, and is_selected. To make your
own template, mix it in as follows::
from kivy.uix.listview import ListItemButton
from kivy.uix.listview import SelectableView
Builder.load_string("""
[CustomListItem@SelectableView+BoxLayout]:
size_hint_y: ctx.size_hint_y
height: ctx.height
ListItemButton:
text: ctx.text
is_selected: ctx.is_selected
""")
A class called CustomListItem will be instantiated for each list item. Note
that it is a layout, BoxLayout, and is thus a kind of container. It contains a
:class:`~kivy.uix.listview.ListItemButton` instance.
Using the power of the Kivy language (kv), you can easily build composite list
items -- in addition to ListItemButton, you could have a ListItemLabel, or a
custom class you have defined and registered with the system.
An args_converter needs to be constructed that goes along with such a kv
template. For example, to use the kv template above::
list_item_args_converter = \
lambda row_index, rec: {'text': rec['text'],
'is_selected': rec['is_selected'],
'size_hint_y': None,
'height': 25}
integers_dict = \
{ str(i): {'text': str(i), 'is_selected': False} for i in xrange(100)}
dict_adapter = DictAdapter(sorted_keys=[str(i) for i in xrange(100)],
data=integers_dict,
args_converter=list_item_args_converter,
template='CustomListItem')
list_view = ListView(adapter=dict_adapter)
A dict adapter is created with 1..100 integer strings as sorted_keys, and an
integers_dict as data. integers_dict has the integer strings as keys and dicts
with text and is_selected properties. The CustomListItem defined above in the
Builder.load_string() call is set as the kv template for the list item views.
The list_item_args_converter lambda function will take each dict in
integers_dict and will return an args dict, ready for passing as the context
(ctx) for the template.
The list_vew would be added to a view with add_widget() after the last line,
where it is created. Again, see the basic example above for add_widget() use.
Using CompositeListItem
-----------------------
The class :class:`~kivy.uix.listview.CompositeListItem` is another option for
building advanced composite list items. The kv language approach has its
advantages, but here we build a composite list view using a straight Kivy
widget method::
args_converter = lambda row_index, rec: \
{'text': rec['text'],
'size_hint_y': None,
'height': 25,
'cls_dicts': [{'cls': ListItemButton,
'kwargs': {'text': rec['text']}},
{'cls': ListItemLabel,
'kwargs': {'text': "Middle-{0}".format(rec['text']),
'is_representing_cls': True}},
{'cls': ListItemButton,
'kwargs': {'text': rec['text']}}]}
item_strings = ["{0}".format(index) for index in xrange(100)]
integers_dict = \
{ str(i): {'text': str(i), 'is_selected': False} for i in xrange(100)}
dict_adapter = DictAdapter(sorted_keys=item_strings,
data=integers_dict,
args_converter=args_converter,
selection_mode='single',
allow_empty_selection=False,
cls=CompositeListItem)
list_view = ListView(adapter=dict_adapter)
The args_converter is somewhat complicated, so we should go through the
details. Observe in the :class:`~kivy.adapters.dictadapter.DictAdapter`
instantiation that :class:`~kivy.uix.listview.CompositeListItem` instance is
set as the cls to be instantiated for each list item. The args_converter will
make args dicts for this cls. In the args_converter, the first three items,
text, size_hint_y, and height, are arguments for CompositeListItem itself.
After that you see a cls_dicts list that contains argument sets for each of the
member widgets for this composite: :class:`~kivy.uix.listview.ListItemButton`
and :class:`~kivy.uix.listview.ListItemLabel`. This is a similar approach to
using a kv template described above.
The sorted_keys and data arguments for the dict adapter are the same as in the
previous code example.
For details on how :class:`~kivy.uix.listview.CompositeListItem` works,
examine the code, looking for how parsing of the cls_dicts list and kwargs
processing is done.
Uses for Selection
------------------
What can we do with selection? Combining selection with the system of bindings
in Kivy, we can build a wide range of user interface designs.
We could make data items that contain the names of dog breeds, and connect
the selection of dog breed to the display of details in another view, which
would update automatically on selection. This is done via a binding to the
on_selection_change event::
list_adapter.bind(on_selection_change=callback_function)
where callback_function() does whatever is needed for the update. See the
example called list_master_detail.py, and imagine that the list one the left
would be a list of dog breeds, and the detail view on the right would show
details for a selected dog breed.
In another example, we could set the selection_mode of a listview to
'multiple', and load it with a list of answers to a multiple-choice question.
The question could have several correct answers. A color swatch view could be
bound to selection change, as above, so that it turns green as soon as the
correct choices are made, unless the number of touches exeeds a limit, when the
answer session would be terminated. See the examples that feature thumbnail
images to get some ideas, e.g., list_cascade_dict.py.
In a more involved example, we could chain together three listviews, where
selection in the first controls the items shown in the second, and selection in
the second controls the items shown in the third. If allow_empty_selection were
set to False for these listviews, a dynamic system of selection "cascading"
from one list to the next, would result.
There are so many ways that listviews and Kivy bindings functionality can be
used, that we have only scratched the surface here. For on-disk examples, see
these::
kivy/examples/widgets/lists/list_*.py
Several examples show the "cascading" behavior described above. Others
demonstrate the use of kv templates and composite list views.
'''
__all__ = ('SelectableView', 'ListItemButton', 'ListItemLabel',
'CompositeListItem', 'ListView', )
from kivy.event import EventDispatcher
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.adapters.simplelistadapter import SimpleListAdapter
from kivy.uix.abstractview import AbstractView
from kivy.properties import ObjectProperty, DictProperty, \
NumericProperty, ListProperty, BooleanProperty
from kivy.lang import Builder
from math import ceil, floor
class SelectableView(object):
'''The :class:`~kivy.uix.listview.SelectableView` mixin is used to design
list item and other classes that are to be instantiated by an adapter to be
used in a listview. The :class:`~kivy.adapters.listadapter.ListAdapter`
and :class:`~kivy.adapters.dictadapter.DictAdapter` adapters are
selection-enabled. select() and deselect() are to be overridden with
display code to mark items as selected or not, if desired.
'''
index = NumericProperty(-1)
'''The index into the underlying data list or the data item this view
represents.
:data:`index` is a :class:`~kivy.properties.NumericProperty`, default
to -1.
'''
is_selected = BooleanProperty(False)
'''A SelectableView instance carries this property, which should be kept
in sync with the equivalent property in the data item it represents.
:data:`is_selected` is a :class:`~kivy.properties.BooleanProperty`, default
to False.
'''
def __init__(self, **kwargs):
super(SelectableView, self).__init__(**kwargs)
def select(self, *args):
'''The list item is responsible for updating the display for
being selected, if desired.
'''
self.is_selected = True
def deselect(self, *args):
'''The list item is responsible for updating the display for
being unselected, if desired.
'''
self.is_selected = False
class ListItemButton(SelectableView, Button):
''':class:`~kivy.uix.listview.ListItemButton` mixes
:class:`~kivy.uix.listview.SelectableView` with
:class:`~kivy.uix.button.Button` to produce a button suitable for use in
:class:`~kivy.uix.listview.ListView`.
'''
selected_color = ListProperty([1., 0., 0., 1])
'''
:data:`selected_color` is a :class:`~kivy.properties.ListProperty`,
default to [1., 0., 0., 1].
'''
deselected_color = ListProperty([0., 1., 0., 1])
'''
:data:`selected_color` is a :class:`~kivy.properties.ListProperty`,
default to [0., 1., 0., 1].
'''
def __init__(self, **kwargs):
super(ListItemButton, self).__init__(**kwargs)
# Set deselected_color to be default Button bg color.
self.deselected_color = self.background_color
def select(self, *args):
self.background_color = self.selected_color
if type(self.parent) is CompositeListItem:
self.parent.select_from_child(self, *args)
def deselect(self, *args):
self.background_color = self.deselected_color
if type(self.parent) is CompositeListItem:
self.parent.deselect_from_child(self, *args)
def select_from_composite(self, *args):
self.background_color = self.selected_color
def deselect_from_composite(self, *args):
self.background_color = self.deselected_color
def __repr__(self):
return '<%s text=%s>' % (self.__class__.__name__, self.text)
# [TODO] Why does this mix in SelectableView -- that makes it work like
# button, which is redundant.
class ListItemLabel(SelectableView, Label):
''':class:`~kivy.uix.listview.ListItemLabel` mixes
:class:`~kivy.uix.listview.SelectableView` with
:class:`~kivy.uix.label.Label` to produce a label suitable for use in
:class:`~kivy.uix.listview.ListView`.
'''
def __init__(self, **kwargs):
super(ListItemLabel, self).__init__(**kwargs)
def select(self, *args):
self.bold = True
if type(self.parent) is CompositeListItem:
self.parent.select_from_child(self, *args)
def deselect(self, *args):
self.bold = False
if type(self.parent) is CompositeListItem:
self.parent.deselect_from_child(self, *args)
def select_from_composite(self, *args):
self.bold = True
def deselect_from_composite(self, *args):
self.bold = False
def __repr__(self):
return '<%s text=%s>' % (self.__class__.__name__, self.text)
class CompositeListItem(SelectableView, BoxLayout):
''':class:`~kivy.uix.listview.CompositeListItem` mixes
:class:`~kivy.uix.listview.SelectableView` with :class:`BoxLayout` for a
generic container-style list item, to be used in
:class:`~kivy.uix.listview.ListView`.
'''
background_color = ListProperty([1, 1, 1, 1])
'''ListItem sublasses Button, which has background_color, but
for a composite list item, we must add this property.
:data:`background_color` is a :class:`~kivy.properties.ListProperty`,
default to [1, 1, 1, 1].
'''
selected_color = ListProperty([1., 0., 0., 1])
'''
:data:`selected_color` is a :class:`~kivy.properties.ListProperty`,
default to [1., 0., 0., 1].
'''
deselected_color = ListProperty([.33, .33, .33, 1])
'''
:data:`deselected_color` is a :class:`~kivy.properties.ListProperty`,
default to [.33, .33, .33, 1].
'''
representing_cls = ObjectProperty(None)
'''Which component view class, if any, should represent for the
composite list item in __repr__()?
:data:`representing_cls` is an :class:`~kivy.properties.ObjectProperty`,
default to None.
'''
def __init__(self, **kwargs):
super(CompositeListItem, self).__init__(**kwargs)
# Example data:
#
# 'cls_dicts': [{'cls': ListItemButton,
# 'kwargs': {'text': "Left"}},
# 'cls': ListItemLabel,
# 'kwargs': {'text': "Middle",
# 'is_representing_cls': True}},
# 'cls': ListItemButton,
# 'kwargs': {'text': "Right"}]
# There is an index to the data item this composite list item view
# represents. Get it from kwargs and pass it along to children in the
# loop below.
index = kwargs['index']
for cls_dict in kwargs['cls_dicts']:
cls = cls_dict['cls']
cls_kwargs = cls_dict.get('kwargs', None)
if cls_kwargs:
cls_kwargs['index'] = index
if 'selection_target' not in cls_kwargs:
cls_kwargs['selection_target'] = self
if 'text' not in cls_kwargs:
cls_kwargs['text'] = kwargs['text']
if 'is_representing_cls' in cls_kwargs:
self.representing_cls = cls
self.add_widget(cls(**cls_kwargs))
else:
cls_kwargs = {}
cls_kwargs['index'] = index
if 'text' in kwargs:
cls_kwargs['text'] = kwargs['text']
self.add_widget(cls(**cls_kwargs))
def select(self, *args):
self.background_color = self.selected_color
def deselect(self, *args):
self.background_color = self.deselected_color
def select_from_child(self, child, *args):
for c in self.children:
if c is not child:
c.select_from_composite(*args)
def deselect_from_child(self, child, *args):
for c in self.children:
if c is not child:
c.deselect_from_composite(*args)
def __repr__(self):
if self.representing_cls is not None:
return '<%r>, representing <%s>' % (
self.representing_cls, self.__class__.__name__)
else:
return '<%s>' % (self.__class__.__name__)
Builder.load_string('''
<ListView>:
container: container
ScrollView:
pos: root.pos
on_scroll_y: root._scroll(args[1])
do_scroll_x: False
GridLayout:
cols: 1
id: container
size_hint_y: None
''')
class ListView(AbstractView, EventDispatcher):
''':class:`~kivy.uix.listview.ListView` is a primary high-level widget,
handling the common task of presenting items in a scrolling list.
Flexibility is afforded by use of a variety of adapters to interface with
data.
The adapter property comes via the mixed in
:class:`~kivy.uix.abstractview.AbstractView` class.
:class:`~kivy.uix.listview.ListView` also subclasses
:class:`EventDispatcher` for scrolling. The event *on_scroll_complete* is
used in refreshing the main view.
For a simple list of string items, without selection, use
:class:`~kivy.adapters.simplelistadapter.SimpleListAdapter`. For list items
that respond to selection, ranging from simple items to advanced
composites, use :class:`~kivy.adapters.listadapter.ListAdapter`. For an
alternate powerful adapter, use
:class:`~kivy.adapters.dictadapter.DictAdapter`, rounding out the choice
for designing highly interactive lists.
:Events:
`on_scroll_complete`: (boolean, )
Fired when scrolling completes.
'''
divider = ObjectProperty(None)
'''[TODO] Not used.
'''
divider_height = NumericProperty(2)
'''[TODO] Not used.
'''
container = ObjectProperty(None)
'''The container is a :class:`~kivy.uix.gridlayout.GridLayout` widget held
within a :class:`~kivy.uix.scrollview.ScrollView` widget. (See the
associated kv block in the Builder.load_string() setup). Item view
instances managed and provided by the adapter are added to this container.
The container is cleared with a call to clear_widgets() when the list is
rebuilt by the populate() method. A padding
:class:`~kivy.uix.widget.Widget` instance is also added as needed,
depending on the row height calculations.
:data:`container` is an :class:`~kivy.properties.ObjectProperty`,
default to None.
'''
row_height = NumericProperty(None)
'''The row_height property is calculated on the basis of the height of the
container and the count of items.
:data:`row_height` is a :class:`~kivy.properties.NumericProperty`,
default to None.
'''
item_strings = ListProperty([])
'''If item_strings is provided, create an instance of
:class:`~kivy.adapters.simplelistadapter.SimpleListAdapter` with this list
of strings, and use it to manage a no-selection list.
:data:`item_strings` is a :class:`~kivy.properties.ListProperty`,
default to [].
'''
scrolling = BooleanProperty(False)
'''If the scroll_to() method is called while scrolling operations are
happening, a call recursion error can occur. scroll_to() checks to see that
scrolling is False before calling populate(). scroll_to() dispatches a
scrolling_complete event, which sets scrolling back to False.
:data:`scrolling` is a :class:`~kivy.properties.BooleanProperty`,
default to False.
'''
_index = NumericProperty(0)
_sizes = DictProperty({})
_count = NumericProperty(0)
_wstart = NumericProperty(0)
_wend = NumericProperty(None)
__events__ = ('on_scroll_complete', )
def __init__(self, **kwargs):
# Check for an adapter argument. If it doesn't exist, we
# check for item_strings in use with SimpleListAdapter
# to make a simple list.
if 'adapter' not in kwargs:
if 'item_strings' not in kwargs:
# Could be missing, or it could be that the ListView is
# declared in a kv file. If kv is in use, and item_strings is
# declared there, then item_strings will not be set until after
# __init__(). So, the data=[] set will temporarily serve for
# SimpleListAdapter instantiation, with the binding to
# item_strings_changed() handling the eventual set of the
# item_strings property from the application of kv rules.
list_adapter = SimpleListAdapter(data=[],
cls=Label)
else:
list_adapter = SimpleListAdapter(data=kwargs['item_strings'],
cls=Label)
kwargs['adapter'] = list_adapter
super(ListView, self).__init__(**kwargs)
self._trigger_populate = Clock.create_trigger(self._spopulate, -1)
self.bind(size=self._trigger_populate,
pos=self._trigger_populate,
item_strings=self.item_strings_changed,
adapter=self._trigger_populate)
# The bindings setup above sets self._trigger_populate() to fire
# when the adapter changes, but we also need this binding for when
# adapter.data and other possible triggers change for view updating.
# We don't know that these are, so we ask the adapter to set up the
# bindings back to the view updating function here.
self.adapter.bind_triggers_to_view(self._trigger_populate)
# Added to set data when item_strings is set in a kv template, but it will
# be good to have also if item_strings is reset generally.
def item_strings_changed(self, *args):
self.adapter.data = self.item_strings
def _scroll(self, scroll_y):
if self.row_height is None:
return
scroll_y = 1 - min(1, max(scroll_y, 0))
container = self.container
mstart = (container.height - self.height) * scroll_y
mend = mstart + self.height
# convert distance to index
rh = self.row_height
istart = int(ceil(mstart / rh))
iend = int(floor(mend / rh))
istart = max(0, istart - 1)
iend = max(0, iend - 1)
if istart < self._wstart:
rstart = max(0, istart - 10)
self.populate(rstart, iend)
self._wstart = rstart
self._wend = iend
elif iend > self._wend:
self.populate(istart, iend + 10)
self._wstart = istart
self._wend = iend + 10
def _spopulate(self, *dt):
self.populate()
def populate(self, istart=None, iend=None):
print '###'
print 'Asked to populate list!'
print '###'
container = self.container
sizes = self._sizes
rh = self.row_height
# ensure we know what we want to show
if istart is None:
istart = self._wstart
iend = self._wend
# clear the view
container.clear_widgets()
# guess only ?
if iend is not None:
# fill with a "padding"
fh = 0
for x in xrange(istart):
fh += sizes[x] if x in sizes else rh
container.add_widget(Widget(size_hint_y=None, height=fh))
# now fill with real item_view
index = istart
while index <= iend:
item_view = self.adapter.get_view(index)
index += 1
if item_view is None:
continue
sizes[index] = item_view.height
container.add_widget(item_view)
else:
available_height = self.height