Debian Codegraph @

src/packages/garden/progressspinner/__init__.py

raw symbols SCIP
1 '''
2 ProgressSpinner
3 ===============
4
5 Android Lollipop style progress spinner.
6 '''
7
8 from kivy.lang import Builder
9 from kivy.core.image import Image as CoreImage
10 from kivy.properties import NumericProperty, ListProperty, BoundedNumericProperty, StringProperty, ObjectProperty
11 from kivy.animation import Animation
12 from kivy.clock import Clock
13 from kivy.properties import BooleanProperty
14 from kivy.uix.widget import Widget
15
16 Builder.load_string('''
17 <ProgressSpinnerBase>:
18 # TODO: make robust fix than to hard-code this
19 # _size: min(self.height, self.width)
20 _size: 70
21 _rsize: self._size / 2.
22 _stroke: max(0.1, self._rsize / 20. if self.stroke_width is None else self.stroke_width)
23 _radius: self._rsize - self._stroke * 2.
24
25 <ProgressSpinner>:
26 canvas:
27 Color:
28 rgba: self.color
29 Line:
30 circle:
31 (self.center_x, self.center_y, self._radius,
32 self._angle_center + self._angle_start,
33 self._angle_center + self._angle_end)
34 width: self._stroke
35 cap: 'none'
36
37 <TextureProgressSpinner>:
38 canvas:
39 StencilPush
40 Color:
41 rgba: 1, 1, 1, 1
42 Line:
43 circle:
44 (self.center_x, self.center_y, self._radius,
45 self._angle_center + self._angle_start,
46 self._angle_center + self._angle_end)
47 width: self._stroke
48 cap: 'none'
49 StencilUse
50
51 Color:
52 rgba: self.color
53 Rectangle:
54 pos: self.center_x - self._rsize, self.center_y - self._rsize
55 size: self._size, self._size
56 texture: self.texture
57
58 StencilUnUse
59 Color:
60 rgba: 1, 1, 1, 1
61 Line:
62 circle:
63 (self.center_x, self.center_y, self._radius,
64 self._angle_center + self._angle_start,
65 self._angle_center + self._angle_end)
66 width: self._stroke
67 cap: 'none'
68 StencilPop
69
70 <RotatingTextureProgressSpinner>:
71 canvas:
72 PushMatrix
73 Rotate:
74 angle: -self._angle_center
75 origin: self.center
76
77 StencilPush
78 Color:
79 rgba: 1, 1, 1, 1
80 Line:
81 circle:
82 (self.center_x, self.center_y, self._radius,
83 self._angle_start, self._angle_end)
84 width: self._stroke
85 cap: 'none'
86 StencilUse
87
88 Color:
89 rgba: self.color
90 Rectangle:
91 pos: self.center_x - self._rsize, self.center_y - self._rsize
92 size: self._size, self._size
93 texture: self.texture
94
95 StencilUnUse
96 Color:
97 rgba: 1, 1, 1, 1
98 Line:
99 circle:
100 (self.center_x, self.center_y, self._radius,
101 self._angle_start, self._angle_end)
102 width: self._stroke
103 cap: 'none'
104 StencilPop
105
106 PopMatrix
107
108 ''')
109
110
111 class ProgressSpinnerBase(Widget):
112 '''ProgressSpinnerBase - base class for progress spinner widgets
113 '''
114
115 color = ListProperty([1, 1, 1, 1])
116 '''Color to render the spinner.
117
118 :attr:`color` is a :class:`~kivy.properties.ListProperty` and defaults
119 to [1, 1, 1, 1] (white, full opacity).
120 '''
121
122 speed = BoundedNumericProperty(1, min=0.1)
123 '''Speed coefficient of the spinner. This value is multiplied by the
124 base speed of 90 degrees per second.
125
126 :attr:`speed` is a :class:`~kivy.properties.BoundedNumericProperty` and
127 defaults to 1.
128 '''
129
130 stroke_length = BoundedNumericProperty(25., min=1, max=180)
131 '''Base length of the stroke in degrees.
132
133 :attr:`stroke_length` is a :class:`~kivy.properties.BoundedNumericProperty`
134 and defaults to 25.
135 '''
136
137 stroke_width = NumericProperty(None, allownone=True)
138 '''Width of the stroke in pixels. If set to None, the width will be
139 calculated automatically as 1/20th of the radius.
140
141 :attr:`stroke_width` is a :class:`~kivy.properties.NumericProperty` and
142 defaults to None.
143 '''
144
145 auto_start = BooleanProperty(True)
146 '''Whether to automatically start spinning.
147
148 :attr:`auto_start` is a :class:`~kivy.properties.BooleanProperty` and
149 defaults to True.
150 '''
151
152 # internal properties
153 _angle_center = NumericProperty(0)
154 _angle_start = NumericProperty()
155 _angle_end = NumericProperty()
156 _size = NumericProperty()
157 _rsize = NumericProperty()
158 _stroke = NumericProperty(1)
159 _radius = NumericProperty(50)
160
161 def __init__(self, **kwargs):
162 super(ProgressSpinnerBase, self).__init__(**kwargs)
163 self._state = 'wait1'
164 self._next = None
165 self._spinning = False
166
167 if self.auto_start:
168 self.start_spinning()
169
170 def start_spinning(self, *args):
171 '''Start spinning the progress spinner. Ignores all positional args
172 for easy binding.
173 '''
174 if not self._spinning:
175 self._state = 'wait1'
176 self._next = None
177 self._angle_center = 0.
178 self._angle_start = 360.
179 self._angle_end = 360. + self.stroke_length
180 Clock.schedule_interval(self._update, 0)
181 Clock.schedule_once(self._rotate, 0.3)
182 self._spinning = True
183
184 def stop_spinning(self, *args):
185 '''Stop spinning the progress spinner. Ignores all positional args
186 for easy binding.
187
188 If you intend to keep the spinner around, you should stop it when
189 not using it and restart it when needed again.
190 '''
191 if self._spinning:
192 if self._next:
193 if isinstance(self._next, Animation):
194 self._next.cancel(self)
195 else:
196 self._next.cancel()
197 Clock.unschedule(self._update)
198 Clock.unschedule(self._rotate)
199 self._angle_start = self._angle_end = 0
200 self._spinning = False
201
202 def _update(self, dt):
203 angle_speed = 90. * self.speed
204 self._angle_center += dt * angle_speed
205
206 if self._angle_center > 360:
207 self._angle_center -= 360.
208
209 def _rotate(self, *args):
210 if not self._spinning:
211 return
212
213 rotate_speed = 0.6 / self.speed
214 wait_speed = 0.3 / self.speed
215 if self._state == 'wait1':
216 self._state = 'rotate1'
217 self._next = Animation(_angle_end=self._angle_start + 360. - self.stroke_length, d=rotate_speed,
218 t='in_quad')
219 self._next.bind(on_complete=self._rotate)
220 self._next.start(self)
221 elif self._state == 'rotate1':
222 self._state = 'wait2'
223 self._next = Clock.schedule_once(self._rotate, wait_speed)
224 elif self._state == 'wait2':
225 self._state = 'rotate2'
226 self._next = Animation(_angle_start=self._angle_end - self.stroke_length, d=rotate_speed, t='in_quad')
227 self._next.bind(on_complete=self._rotate)
228 self._next.start(self)
229 elif self._state == 'rotate2':
230 self._state = 'wait1'
231 self._next = Clock.schedule_once(self._rotate, wait_speed)
232 while self._angle_end > 720.:
233 self._angle_start -= 360.
234 self._angle_end -= 360.
235
236
237 class ProgressSpinner(ProgressSpinnerBase):
238 '''ProgressSpinner class. Android Lollipop style progress spinner.
239 '''
240 pass
241
242
243 class TextureProgressSpinnerBase(ProgressSpinnerBase):
244 texture = ObjectProperty()
245 '''Texture to render for the spinner.
246
247 :attr:`texture` is a :class:`~kivy.properties.ObjectProperty` and
248 defaults to None.
249 '''
250
251 source = StringProperty('')
252 '''Source image to render for the spinner.
253
254 :attr:`source` is a :class:`~kivy.properties.StringProperty` and
255 defaults to an empty string.
256 '''
257
258 def on_source(self, inst, value):
259 if value:
260 self.texture = CoreImage(value).texture
261
262
263 class TextureProgressSpinner(TextureProgressSpinnerBase):
264 '''TextureProgressSpinner class.
265
266 Same as ProgressSpinner, but with a texture/image instead of a solid color.
267 '''
268 pass
269
270
271 class RotatingTextureProgressSpinner(TextureProgressSpinnerBase):
272 '''RotatingTextureProgressSpinner class.
273
274 Same as TextureProgressSpinner, but the texture/image rotates along with
275 the spinner.
276 '''
277 pass
278
279
280 if __name__ == '__main__':
281 from kivy.app import App
282 from kivy.graphics.texture import Texture
283 from textwrap import dedent
284
285
286 class TestApp(App):
287 texture = ObjectProperty()
288
289 def blittex(self, *args):
290 rgbpixels = [(x, 0, y, 255) for x in range(256) for y in range(256)]
291 pixels = b''.join((b''.join(map(bytearray, pix)) for pix in rgbpixels))
292 self.texture = Texture.create(size=(256, 256))
293 self.texture.blit_buffer(pixels, colorfmt='rgba', bufferfmt='ubyte')
294
295 def build(self):
296 Clock.schedule_once(self.blittex, -1)
297
298 return Builder.load_string(dedent('''\
299 <ProgressSpinnerBase>:
300 on_touch_down: self.stop_spinning() if self._spinning else self.start_spinning()
301
302 <TTextureProgressSpinner@TextureProgressSpinner>:
303 texture: app.texture
304
305 <TRotatingTextureProgressSpinner@RotatingTextureProgressSpinner>:
306 texture: app.texture
307
308 <ITextureProgressSpinner@TextureProgressSpinner>:
309 source: 'demoimage.jpg'
310
311 <IRotatingTextureProgressSpinner@RotatingTextureProgressSpinner>:
312 source: 'demoimage.jpg'
313
314 BoxLayout:
315 BoxLayout:
316 orientation: 'vertical'
317
318 ProgressSpinner
319
320 TTextureProgressSpinner
321
322 TRotatingTextureProgressSpinner
323
324 BoxLayout:
325 orientation: 'vertical'
326
327 BoxLayout:
328 ProgressSpinner:
329 color: 0.3, 0.3, 1, 1
330 stroke_width: 1
331
332 ProgressSpinner:
333 speed: 0.5
334 color: 1, 0, 0, 1
335
336 ProgressSpinner:
337 speed: 2
338 color: 0, 1, 0, 1
339
340 BoxLayout:
341 TTextureProgressSpinner:
342 color: 1, 0, 0, 1
343
344 ITextureProgressSpinner:
345 stroke_width: 10
346
347 ITextureProgressSpinner:
348 stroke_length: 20
349
350 BoxLayout:
351 TRotatingTextureProgressSpinner:
352 color: 1, 0, 0, 1
353
354 IRotatingTextureProgressSpinner:
355 stroke_width: 10
356
357 IRotatingTextureProgressSpinner:
358 stroke_length: 20
359 '''))
360
361
362 TestApp().run()