-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagelist.py
315 lines (234 loc) · 7.75 KB
/
imagelist.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
"""
Components/ImageList
====================
.. seealso::
`Material Design spec, Image lists <https://material.io/components/image-lists>`_
.. rubric:: Image lists display a collection of images in an organized grid.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/image-list.png
:align: center
`KivyMD` provides the following tile classes for use:
- SmartTileWithStar_
- SmartTileWithLabel_
.. SmartTileWithStar:
SmartTileWithStar
-----------------
.. code-block::
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
<MyTile@SmartTileWithStar>
size_hint_y: None
height: "240dp"
ScrollView:
MDGridLayout:
cols: 3
adaptive_height: True
padding: dp(4), dp(4)
spacing: dp(4)
MyTile:
stars: 5
source: "cat-1.jpg"
MyTile:
stars: 5
source: "cat-2.jpg"
MyTile:
stars: 5
source: "cat-3.jpg"
'''
class MyApp(MDApp):
def build(self):
return Builder.load_string(KV)
MyApp().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/SmartTileWithStar.gif
:align: center
.. SmartTileWithLabel:
SmartTileWithLabel
------------------
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
<MyTile@SmartTileWithLabel>
size_hint_y: None
height: "240dp"
ScrollView:
MDGridLayout:
cols: 3
adaptive_height: True
padding: dp(4), dp(4)
spacing: dp(4)
MyTile:
source: "cat-1.jpg"
text: "[size=26]Cat 1[/size]\\n[size=14]cat-1.jpg[/size]"
MyTile:
source: "cat-2.jpg"
text: "[size=26]Cat 2[/size]\\n[size=14]cat-2.jpg[/size]"
tile_text_color: app.theme_cls.accent_color
MyTile:
source: "cat-3.jpg"
text: "[size=26][color=#ffffff]Cat 3[/color][/size]\\n[size=14]cat-3.jpg[/size]"
tile_text_color: app.theme_cls.accent_color
'''
class MyApp(MDApp):
def build(self):
return Builder.load_string(KV)
MyApp().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/SmartTileWithLabel.png
:align: center
"""
__all__ = ("SmartTile", "SmartTileWithLabel", "SmartTileWithStar")
from typing import NoReturn
from kivy.lang import Builder
from kivy.properties import (
BooleanProperty,
ColorProperty,
NumericProperty,
ObjectProperty,
OptionProperty,
StringProperty,
)
from kivy.uix.behaviors import ButtonBehavior
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import RectangularRippleBehavior
from kivymd.uix.button import MDIconButton
from kivymd.uix.floatlayout import MDFloatLayout
Builder.load_string(
"""
<SmartTile>
_img_widget: img
_img_overlay: img_overlay
_box_overlay: box
FitImage:
id: img
source: root.source
x: root.x
y: root.y if root.overlap or root.box_position == 'header' else box.top
BoxLayout:
id: img_overlay
size_hint: img.size_hint
size: img.size
pos: img.pos
MDBoxLayout:
id: box
md_bg_color: root.box_color
size_hint_y: None
height: "68dp" if root.lines == 2 else "48dp"
x: root.x
y: root.y if root.box_position == 'footer' else root.y + root.height - self.height
<SmartTileWithLabel>
_img_widget: img
_img_overlay: img_overlay
_box_overlay: box
_box_label: boxlabel
FitImage:
id: img
source: root.source
x: root.x
y: root.y if root.overlap or root.box_position == 'header' else box.top
BoxLayout:
id: img_overlay
size_hint: img.size_hint
size: img.size
pos: img.pos
MDBoxLayout:
id: box
padding: "5dp", 0, 0, 0
md_bg_color: root.box_color
adaptive_height: True
x: root.x
y: root.y if root.box_position == 'footer' else root.y + root.height - self.height
MDLabel:
id: boxlabel
font_style: root.font_style
size_hint_y: None
height: self.texture_size[1]
text: root.text
color: root.tile_text_color
markup: True
"""
)
class SmartTile(
ThemableBehavior, RectangularRippleBehavior, ButtonBehavior, MDFloatLayout
):
"""
A tile for more complex needs.
Includes an image, a container to place overlays and a box that can act
as a header or a footer, as described in the Material Design specs.
"""
box_color = ColorProperty((0, 0, 0, 0.5))
"""
Sets the color and opacity for the information box.
:attr:`box_color` is a :class:`~kivy.properties.ColorProperty`
and defaults to `(0, 0, 0, 0.5)`.
"""
box_position = OptionProperty("footer", options=["footer", "header"])
"""
Determines wether the information box acts as a header or footer to the
image. Available are options: `'footer'`, `'header'`.
:attr:`box_position` is a :class:`~kivy.properties.OptionProperty`
and defaults to `'footer'`.
"""
lines = OptionProperty(1, options=[1, 2])
"""
Number of lines in the `header/footer`. As per `Material Design specs`,
only 1 and 2 are valid values. Available are options: ``1``, ``2``.
:attr:`lines` is a :class:`~kivy.properties.OptionProperty`
and defaults to `1`.
"""
overlap = BooleanProperty(True)
"""
Determines if the `header/footer` overlaps on top of the image or not.
:attr:`overlap` is a :class:`~kivy.properties.BooleanProperty`
and defaults to `True`.
"""
source = StringProperty()
"""
Path to tile image. See :attr:`~kivy.uix.image.Image.source`.
:attr:`source` is a :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
_img_widget = ObjectProperty()
_img_overlay = ObjectProperty()
_box_overlay = ObjectProperty()
_box_label = ObjectProperty()
def reload(self):
self._img_widget.reload()
class SmartTileWithLabel(SmartTile):
font_style = StringProperty("Caption")
"""
Tile font style.
:attr:`font_style` is a :class:`~kivy.properties.StringProperty`
and defaults to `'Caption'`.
"""
tile_text_color = ColorProperty((1, 1, 1, 1))
"""
Tile text color in ``rgba`` format.
:attr:`tile_text_color` is a :class:`~kivy.properties.ColorProperty`
and defaults to `(1, 1, 1, 1)`.
"""
text = StringProperty()
"""
Determines the text for the box `footer/header`.
:attr:`text` is a :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
_box_label = ObjectProperty()
class SmartTileWithStar(SmartTileWithLabel):
stars = NumericProperty(1)
"""
Tile stars.
:attr:`stars` is a :class:`~kivy.properties.NumericProperty`
and defaults to `1`.
"""
def on_stars(self, *args) -> NoReturn:
for star in range(self.stars):
self.ids.box.add_widget(
_Star(
icon="star-outline",
theme_text_color="Custom",
text_color=(1, 1, 1, 1),
)
)
class _Star(MDIconButton):
def on_touch_down(self, touch):
return True