|
1 |
| -'''See :class:`CompoundSelectionBehavior` for more details. |
| 1 | +''' |
| 2 | +Compound Selection Behavior |
| 3 | +=========================== |
| 4 | +
|
| 5 | +The :class:`~kivy.uix.behaviors.compoundselection.CompoundSelectionBehavior` |
| 6 | +`mixin <https://en.wikipedia.org/wiki/Mixin>`_ class implements the logic |
| 7 | +behind keyboard and touch selection of selectable widgets managed by the |
| 8 | +derived widget. For example, it can be combined with a |
| 9 | +:class:`~kivy.uix.gridlayout.GridLayout` to add selection to the layout. |
| 10 | +
|
| 11 | +At its core, it keeps a dynamic list of widgets that can be selected. |
| 12 | +Then, as the touches and keyboard input are passed in, it selects one or |
| 13 | +more of the widgets based on these inputs. For example, it uses the mouse |
| 14 | +scroll and keyboard up/down buttons to scroll through the list of widgets. |
| 15 | +Multiselection can also be achieved using the keyboard shift and ctrl keys. |
| 16 | +Finally, in addition to the up/down type keyboard inputs, it can also |
| 17 | +accepts letters from the keyboard to be used to select nodes with |
| 18 | +associated strings that start with those letters, similar to how files |
| 19 | +are selected by a file browser. |
| 20 | +
|
| 21 | +When the controller needs to select a node, it calls :meth:`select_node` and |
| 22 | +:meth:`deselect_node`. Therefore, they must be overwritten in order alter |
| 23 | +node selection. By default, the class doesn't listen for keyboard or |
| 24 | +touch events, so the derived widget must call |
| 25 | +:meth:`select_with_touch`, :meth:`select_with_key_down`, and |
| 26 | +:meth:`select_with_key_up` on events that it wants to pass on for selection |
| 27 | +purposes. |
| 28 | +
|
| 29 | +Example |
| 30 | +------- |
| 31 | +
|
| 32 | +To add selection to a grid layout which will contain |
| 33 | +:class:`~kivy.uix.Button` widgets:: |
| 34 | +
|
| 35 | + class SelectableGrid(CompoundSelectionBehavior, GridLayout): |
| 36 | +
|
| 37 | + def __init__(self, **kwargs): |
| 38 | + super(CompoundSelectionBehavior, self).__init__(**kwargs) |
| 39 | + keyboard = Window.request_keyboard(None, self) |
| 40 | + keyboard.bind(on_key_down=self.select_with_key_down, |
| 41 | + on_key_up=self.select_with_key_up) |
| 42 | +
|
| 43 | + def select_node(self, node): |
| 44 | + node.background_color = (1, 0, 0, 1) |
| 45 | + return super(CompoundSelectionBehavior, self).select_node(node) |
| 46 | +
|
| 47 | + def deselect_node(self, node): |
| 48 | + node.background_color = (1, 1, 1, 1) |
| 49 | + super(CompoundSelectionBehavior, self).deselect_node(node) |
| 50 | +
|
| 51 | +Then, for each button added to the layout, bind the |
| 52 | +:attr:`~kivy.uix.widget.Widget.on_touch_down` of the button |
| 53 | +to :meth:`select_with_touch` to pass on the touch events. |
| 54 | +
|
| 55 | +.. warning:: |
| 56 | +
|
| 57 | + This code is still experimental, and its API is subject to change in a |
| 58 | + future version. |
| 59 | +
|
2 | 60 | '''
|
3 | 61 |
|
4 | 62 | __all__ = ('CompoundSelectionBehavior', )
|
|
11 | 69 | class CompoundSelectionBehavior(object):
|
12 | 70 | '''The Selection behavior `mixin <https://en.wikipedia.org/wiki/Mixin>`_
|
13 | 71 | implements the logic behind keyboard and touch
|
14 |
| - selection of selectable widgets managed by the derived widget. |
15 |
| - For example, it could be combined with a |
16 |
| - :class:`~kivy.uix.gridlayout.GridLayout` to add selection to the layout. |
17 |
| -
|
18 |
| - At its core, it keeps a dynamic list of widgets that can be selected. |
19 |
| - Then, as the touches and keyboard input are passed in, it selects one or |
20 |
| - more of the widgets based on these inputs. For example, it uses the mouse |
21 |
| - scroll and keyboard up/down buttons to scroll through the list of widgets. |
22 |
| - Multiselection can also be achieved using the keyboard shift and ctrl keys. |
23 |
| - Finally, in addition to the up/down type keyboard inputs, it can also |
24 |
| - accepts letters from the kayboard to be used to select nodes with |
25 |
| - associated strings that start with those letters, similar to how files |
26 |
| - are selected by a file browser. |
27 |
| -
|
28 |
| - When the controller needs to select a node it calls :meth:`select_node` and |
29 |
| - :meth:`deselect_node`. Therefore, they must be overwritten in order affect |
30 |
| - the selected nodes. By default, the class doesn't listen to keyboard and |
31 |
| - touch events, therefore, the derived widget must call |
32 |
| - :meth:`select_with_touch`, :meth:`select_with_key_down`, and |
33 |
| - :meth:`select_with_key_up` on events that it wants to pass on for selection |
34 |
| - purposes. |
35 |
| -
|
36 |
| - For example, to add selection to a grid layout which will contain |
37 |
| - :class:`~kivy.uix.Button` widgets:: |
38 |
| -
|
39 |
| - class SelectableGrid(CompoundSelectionBehavior, GridLayout): |
40 |
| -
|
41 |
| - def __init__(self, **kwargs): |
42 |
| - super(CompoundSelectionBehavior, self).__init__(**kwargs) |
43 |
| - keyboard = Window.request_keyboard(None, self) |
44 |
| - keyboard.bind(on_key_down=self.select_with_key_down, |
45 |
| - on_key_up=self.select_with_key_up) |
46 |
| -
|
47 |
| - def select_node(self, node): |
48 |
| - node.background_color = (1, 0, 0, 1) |
49 |
| - return super(CompoundSelectionBehavior, self).select_node(node) |
50 |
| -
|
51 |
| - def deselect_node(self, node): |
52 |
| - node.background_color = (1, 1, 1, 1) |
53 |
| - super(CompoundSelectionBehavior, self).deselect_node(node) |
54 |
| -
|
55 |
| - Then, for each button added to the layout, bind on_touch_down of the button |
56 |
| - to :meth:`select_with_touch` to pass on the touch events. |
| 72 | + selection of selectable widgets managed by the derived widget. Please see |
| 73 | + the :mod:`compound selection behaviors module |
| 74 | + <kivy.uix.behaviors.compoundselection>` documentation |
| 75 | + for more information. |
57 | 76 |
|
58 | 77 | .. versionadded:: 1.9.0
|
59 |
| -
|
60 |
| - .. warning:: |
61 |
| -
|
62 |
| - This code is still experimental, and its API is subject to change in a |
63 |
| - future version. |
64 | 78 | '''
|
65 | 79 |
|
66 | 80 | selected_nodes = ListProperty([])
|
|
0 commit comments