9
This code is still experimental, and its API is subject to change in a
12
An :class:`~kivy.adapters.adapter.Adapter` is a bridge between data and
13
an :class:`~kivy.uix.abstractview.AbstractView` or one of its subclasses, such
14
as a :class:`~kivy.uix.listview.ListView`.
16
The following arguments can be passed to the contructor to initialise the
17
corresponding properties:
19
* :attr:`~Adapter.data`: for any sort of data to be used in a view. For an
20
:class:`~kivy.adapters.adapter.Adapter`, data can be an object as well as a
21
list, dict, etc. For a :class:`~kivy.adapters.listadapter.ListAdapter`, data
22
should be a list. For a :class:`~kivy.adapters.dictadapter.DictAdapter`,
23
data should be a dict.
25
* :attr:`~Adapter.cls`: the class used to instantiate each list item view
26
instance (Use this or the template argument).
28
* :attr:`~Adapter.template`: a kv template to use to instantiate each list item
29
view instance (Use this or the cls argument).
31
* :attr:`~Adapter.args_converter`: a function used to transform the data items
32
in preparation for either a cls instantiation or a kv template
33
invocation. If no args_converter is provided, the data items are assumed
36
Please refer to the :mod:`~kivy.adapters` documentation for an overview of how
41
__all__ = ('Adapter', )
43
from kivy.event import EventDispatcher
44
from kivy.properties import ObjectProperty
45
from kivy.lang import Builder
46
from kivy.adapters.args_converters import list_item_args_converter
47
from kivy.factory import Factory
48
from kivy.compat import string_types
51
class Adapter(EventDispatcher):
52
'''An :class:`~kivy.adapters.adapter.Adapter` is a bridge between data and
53
an :class:`~kivy.uix.abstractview.AbstractView` or one of its subclasses,
54
such as a :class:`~kivy.uix.listview.ListView`.
57
data = ObjectProperty(None)
59
The data for which a view is to be constructed using either the cls or
60
template provided, together with the args_converter provided or the default
63
In this base class, data is an ObjectProperty, so it could be used for a
64
wide variety of single-view needs.
66
Subclasses may override it in order to use another data type, such as a
67
:class:`~kivy.properties.ListProperty` or
68
:class:`~kivy.properties.DictProperty` as appropriate. For example, in a
69
:class:`~.kivy.adapters.listadapter.ListAdapter`, data is a
70
:class:`~kivy.properties.ListProperty`.
72
:attr:`data` is an :class:`~kivy.properties.ObjectProperty` and defaults
76
cls = ObjectProperty(None)
78
A class for instantiating a given view item (Use this or template). If this
79
is not set and neither is the template, a :class:`~kivy.uix.label.Label`
80
is used for the view item.
82
:attr:`cls` is an :class:`~kivy.properties.ObjectProperty` and defaults
86
template = ObjectProperty(None)
88
A kv template for instantiating a given view item (Use this or cls).
90
:attr:`template` is an :class:`~kivy.properties.ObjectProperty` and defaults
94
args_converter = ObjectProperty(None)
96
A function that prepares an args dict for the cls or kv template to build
97
a view from a data item.
99
If an args_converter is not provided, a default one is set that assumes
100
simple content in the form of a list of strings.
102
:attr:`args_converter` is an :class:`~kivy.properties.ObjectProperty` and
106
def __init__(self, **kwargs):
108
if 'data' not in kwargs:
109
raise Exception('adapter: input must include data argument')
112
if 'template' in kwargs:
113
msg = 'adapter: cannot use cls and template at the same time'
115
elif not kwargs['cls']:
116
raise Exception('adapter: a cls or template must be defined')
118
if 'template' in kwargs:
119
if not kwargs['template']:
120
msg = 'adapter: a cls or template must be defined'
123
raise Exception('adapter: a cls or template must be defined')
125
if 'args_converter' in kwargs:
126
self.args_converter = kwargs['args_converter']
128
self.args_converter = list_item_args_converter
130
super(Adapter, self).__init__(**kwargs)
132
def bind_triggers_to_view(self, func):
135
def get_data_item(self):
140
.. versionadded:: 1.9.0
142
Returns the widget type specified by self.cls. If it is a
143
string, the :class:`~kivy.factory.Factory` is queried to retrieve the
144
widget class with the given name, otherwise it is returned directly.
147
if isinstance(cls, string_types):
149
cls = getattr(Factory, cls)
150
except AttributeError:
151
raise AttributeError(
152
'Listadapter cls widget does not exist.')
155
def get_view(self, index): # pragma: no cover
156
item_args = self.args_converter(self.data)
160
return cls(**item_args)
162
return Builder.template(self.template, **item_args)