-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressbar.py
303 lines (251 loc) · 8.17 KB
/
progressbar.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
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# New ProgressBar widget
import kivy
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle, Line
from kivy.utils import get_color_from_hex
from os.path import join, dirname, abspath
Builder.load_file(join(dirname(abspath(__file__)),'./progressbar.kv'))
class NewProgressBar(BoxLayout):
"""My custom progressbar class"""
def __init__(self, **kwargs):
super(NewProgressBar, self).__init__(**kwargs)
## Get children
self.line_bar = self.children[1]
self.start_border = self.children[2]
self.finish_border = self.children[0]
## Set redraw binds
self.bind(pos=self.redraw_widget)
self.bind(size=self.redraw_widget)
## Create variables
self._background_color = '#222222'
@property
def min(self):
"""
The property that sets and
returns minimum value.
type: float
Example
"""
return self.line_bar.min
@min.setter
def min(self, value):
if value < self.line_bar.max:
self.line_bar.min = value
self.line_bar.redraw_widget()
else:
print "ERROR: min > max! Try again."
@property
def max(self):
"""
The property that sets and
returns maximum value.
type: float
"""
return self.line_bar.max
@max.setter
def max(self, value):
if value > self.line_bar.min:
self.line_bar.max = value
else:
print "ERROR: max < min! Try again."
@property
def bar_value(self):
"""
The property that sets and
returns progress bar value.
type: float
"""
return self.line_bar.bar_value
@bar_value.setter
def bar_value(self, value):
if self.min<=value<=self.max:
self.bar_value_percent = self._convert_to_percent(value)
else:
print 'ERROR: value < min OR value > max. Try Again.'
@property
def bar_value_percent(self):
"""
The property that sets and
returns progressbar value in percent.
type: float
"""
return self.line_bar.bar_value_percent
@bar_value_percent.setter
def bar_value_percent(self, value):
if (value >= 0) and (value <= 100):
self.line_bar.bar_value = self._convert_to_value(value)
self.line_bar.bar_value_percent = value
self.line_bar.redraw_widget()
else:
print "ERROR: value < 0 OR value > 100. Try again."
@property
def color(self):
"""
The property that sets and
returns progress bar color.
type: str (hex color)
"""
return self.line_bar.color
@color.setter
def color(self, hex_color):
self.line_bar.color = hex_color
self.line_bar.redraw_widget()
@property
def background_color(self):
"""
The property that sets and
returns progress bar background color.
type: str (hex color)
"""
return self._background_color
@background_color.setter
def background_color(self, color):
self._background_color = color
self.redraw_widget()
@property
def border_color(self):
"""
The property that sets and
returns borders color.
type: str (hex color)
"""
return self.start_border.color
@border_color.setter
def border_color(self, hex_color):
self.start_border.color = hex_color
self.finish_border.color = hex_color
self.start_border.redraw_widget()
self.finish_border.redraw_widget()
@property
def height_widget(self):
"""
The property that sets and
returns progressbar value.
type: int
"""
return self.height
@height_widget.setter
def height_widget(self, value):
self.height = value
self.start_border.redraw_widget()
self.finish_border.redraw_widget()
self.line_bar.redraw_widget()
@property
def padding_widget(self):
"""
The property that sets and
returns progressbar padding value.
type: int
"""
return self.padding
@padding_widget.setter
def padding_widget(self, value):
self.padding = value
# In order to save bar height we will add two values to height
self.height_widget = self.height+value*2
@property
def spacing_widget(self):
"""
The property that sets and
returns spacing value between elements.
type: int
"""
return self.spacing
@spacing_widget.setter
def spacing_widget(self, value):
self.spacing = value
def add_value(self, value=1):
"""
Add/subtract value to line bar.
Before setting value, you need to set min and max.
value type: float
"""
self.add_value_percent(self._convert_to_percent(value))
def add_value_percent(self, value=1):
"""
Add/subtract percent value to line bar.
Not obligatory set min and max.
value type: float
"""
percent = self.line_bar.bar_value_percent
if percent+value <= 0:
self.line_bar.bar_value_percent = 0
self.line_bar.bar_value = self.min
elif percent+value >= 100:
self.line_bar.bar_value_percent = 100
self.line_bar.bar_value = self.max
else:
self.line_bar.bar_value_percent+=value
self.line_bar.bar_value = self._convert_to_value(
self.line_bar.bar_value_percent)
self.line_bar.redraw_widget()
def _convert_to_percent(self, value):
"""
Convert value to percent.
value type: float
"""
min = self.line_bar.min
max = self.line_bar.max
full_length = max-min
percent_value = 100*value/float(full_length)
return percent_value
def _convert_to_value(self, percent_value):
"""
Convert percent to value.
value type: float
"""
min = self.line_bar.min
max = self.line_bar.max
full_length = max-min
value = float(full_length)*percent_value/100
return value
def redraw_widget(self, *args):
""" Method of redraw this widget """
with self.canvas.before:
self.canvas.before.clear()
Color(*get_color_from_hex(self.background_color))
Rectangle(pos=(self.x, self.y+1), size=self.size)
class ProgressLine(Widget):
"""Percent line class"""
min = 0
max = 10
bar_value = 0
bar_value_percent = 30
color = '#565656'
def __init__(self, **kwargs):
super(ProgressLine, self).__init__(**kwargs)
self.bind(pos=self.redraw_widget)
self.bind(size=self.redraw_widget)
def redraw_widget(self, *args):
""" Method of redraw this widget """
print 'POS_PERCENT:',self.bar_value_percent
with self.canvas:
self.canvas.clear()
line_width = float(self.height)/2+1
new_y = self.y+line_width
new_x = self.x+self.width*self.bar_value_percent/100
Color(*get_color_from_hex(self.color))
Line(points=[self.x, new_y, new_x, new_y],
width=line_width, cap='none')
class BorderLine(Widget):
"""Border line for progress line"""
color = '#9a9a9a'
def __init__(self, **kwargs):
super(BorderLine, self).__init__(**kwargs)
self.bind(pos=self.redraw_widget)
self.bind(size=self.redraw_widget)
def redraw_widget(self, *args):
""" Method of redraw this widget """
with self.canvas:
self.canvas.clear()
line_width = float(self.height)/2+1
new_y = self.y+line_width
new_x = self.x+4
Color(*get_color_from_hex(self.color))
Line(points=[self.x, new_y, new_x, new_y],
width=line_width, cap='none')