Skip to content

Commit 656c642

Browse files
committed
Refactor out changes to ProgressBarBase
ProgressBar fully refactored VerticalProgressBar fully refactored
1 parent 91865d0 commit 656c642

File tree

4 files changed

+99
-129
lines changed

4 files changed

+99
-129
lines changed

adafruit_progressbar/__init__.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
import displayio
2626

2727

28+
# pylint: disable=too-few-public-methods
29+
class FillDirection(enumerate):
30+
"""Enums to define the direction in which the progressbar
31+
should fill"""
32+
33+
LEFT_TO_RIGHT = 0
34+
DEFAULT = LEFT_TO_RIGHT
35+
BOTTOM_UP = 1
36+
TOP_DOWN = 2
37+
RIGHT_TO_LEFT = 3
38+
39+
2840
class ProgressBarBase(displayio.TileGrid):
2941
"""The base class for dynamic progress bar widgets.
3042
@@ -181,7 +193,7 @@ def progress(self, value):
181193
:param float value: The new value which should be displayed by the progress
182194
bar. Must be between 0.0-1.0
183195
"""
184-
_old_value = self._progress
196+
_old_value = self.progress
185197
# If we're using floats, from 0.0 to 1.0, using 4 decimal places allows us to handle values
186198
# as precise as 0.23456, which evaluates to a percentage value of 23.45% (with rounding)
187199
self._progress = round(value, 4)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`horizontalprogressbar`
7+
================================================================================
8+
9+
Dynamic progress bar widget for CircuitPython displays
10+
11+
12+
"""
13+
14+
# import displayio
15+
from . import ProgressBarBase # , FillDirection
16+
17+
18+
class HorizontalProgressBar(ProgressBarBase):
19+
"""
20+
A progress bar that goes horizontally.
21+
"""
22+
23+
def __init__(self):
24+
"""
25+
Get stuff, horizontally!
26+
:param args:
27+
:param kwargs:
28+
"""
29+
30+
super().__init__((0, 0), (100, 20), 0.0)
31+
32+
def render(self, _previous_value, _new_value, _progress_value) -> None:
33+
"""
34+
The rendering mechanism to display the newly set value.
35+
36+
:param _previous_value: The value from which we are updating
37+
:type _previous_value: object
38+
:param _new_value: The value to which we are updating
39+
:type _new_value: object
40+
:param _progress_value: The value of the progress, or ratio between the new value and the
41+
maximum value
42+
:type _progress_value: float
43+
:rtype None:
44+
"""
45+
46+
if _previous_value == _new_value:
47+
return # Do nothing if there's nothing to update
48+
49+
if _previous_value > _new_value:
50+
# Remove color in range from width*value+margin to width-margin
51+
# from right to left
52+
_prev_pixel = max(2, int(self.widget_width * self.progress - 2))
53+
_new_pixel = max(int(self.widget_width * _new_value - 2), 2)
54+
for _w in range(_prev_pixel, _new_pixel - 1, -1):
55+
for _h in range(2, self.widget_height - 2):
56+
self._bitmap[_w, _h] = 0
57+
else:
58+
# fill from the previous x pixel to the new x pixel
59+
_prev_pixel = max(2, int(self.widget_width * self.progress - 3))
60+
_new_pixel = min(
61+
int(self.widget_width * _new_value - 2),
62+
int(self.widget_width * 1.0 - 3),
63+
)
64+
for _w in range(_prev_pixel, _new_pixel + 1):
65+
for _h in range(2, self.widget_height - 2):
66+
self._bitmap[_w, _h] = 2

adafruit_progressbar/progressbar.py

+3-28
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
"""
2323

2424
# imports
25-
from . import ProgressBarBase
25+
from horizontalprogressbar import HorizontalProgressBar
2626

2727

2828
# pylint: disable=too-many-arguments, too-few-public-methods
29-
class ProgressBar(ProgressBarBase):
29+
class ProgressBar(HorizontalProgressBar):
3030
"""A dynamic progress bar widget.
3131
3232
:param x: The x-position of the top left corner.
@@ -61,7 +61,7 @@ def __init__(
6161
stroke=1,
6262
):
6363

64-
# This needs to remain, since for backward compatibility, the default ProgressBar class
64+
# This needs to remain for backward compatibility, the default ProgressBar class
6565
# should only be able to handle values of type "float"
6666
assert isinstance(progress, float), "Progress must be a floating point value."
6767

@@ -79,31 +79,6 @@ def __init__(
7979

8080
# _outline_color: int # The colour used for the border of the widget
8181

82-
@property
83-
def outline_color(self):
84-
"""Returns the currently configured value for the color of the
85-
outline (border) of the widget."""
86-
return self._outline_color
87-
88-
@property
89-
def fill(self):
90-
"""The fill of the progress bar. Can be a hex value for a color or ``None`` for
91-
transparent.
92-
"""
93-
return self._palette[0]
94-
95-
@fill.setter
96-
def fill(self, color):
97-
"""Sets the fill of the progress bar. Can be a hex value for a color or ``None`` for
98-
transparent.
99-
"""
100-
if color is None:
101-
self._palette[2] = 0
102-
self._palette.make_transparent(0)
103-
else:
104-
self._palette[2] = color
105-
self._palette.make_opaque(0)
106-
10782
def render(self, _previous_value, _new_value, _progress_value) -> None:
10883
"""
10984
The rendering mechanism to display the newly set value.

adafruit_progressbar/verticalprogressbar.py

+17-100
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,7 @@
2424

2525
# imports
2626
import displayio
27-
from . import ProgressBarBase
28-
29-
30-
# pylint: disable=too-few-public-methods
31-
class FillDirection(enumerate):
32-
"""Enums to define the direction in which the progressbar
33-
should fill"""
34-
35-
LEFT_TO_RIGHT = 0
36-
DEFAULT = LEFT_TO_RIGHT
37-
BOTTOM_UP = 1
38-
TOP_DOWN = 2
39-
RIGHT_TO_LEFT = 3
27+
from . import ProgressBarBase, FillDirection
4028

4129

4230
# pylint: disable=too-many-arguments, too-few-public-methods, too-many-instance-attributes
@@ -100,6 +88,7 @@ def __init__(
10088
progress=0.0,
10189
bar_color=0x00FF00,
10290
outline_color=0xFFFFFF,
91+
fill_color=None,
10392
stroke=1,
10493
margin=False,
10594
direction=FillDirection.DEFAULT,
@@ -132,10 +121,6 @@ def __init__(
132121
self._bar_width = self._width
133122
self._bar_height = self._height
134123

135-
self._progress_val = 0.0
136-
self.progress = self._progress_val
137-
self.progress = progress
138-
139124
self._x = anchor_position[0]
140125
self._y = anchor_position[1]
141126

@@ -147,70 +132,31 @@ def __init__(
147132
progress,
148133
bar_color,
149134
outline_color,
150-
0x444444,
135+
fill_color,
151136
border_thickness=stroke,
152137
show_margin=True,
153138
value_range=(0.0, 1.0),
154139
)
155140

156-
self._draw_outline()
157-
158-
def _draw_outline(self):
159-
"""
160-
Draws the outline (border) of the widget.
161-
"""
162-
163-
# draw outline rectangle
164-
for _w in range(self._width):
165-
for line in range(self._stroke):
166-
self._bitmap[_w, line] = 1
167-
self._bitmap[_w, self._height - 1 - line] = 1
168-
for _h in range(self._height):
169-
for line in range(self._stroke):
170-
self._bitmap[line, _h] = 1
171-
self._bitmap[self._width - 1 - line, _h] = 1
172-
173-
@property
174-
def progress(self):
175-
"""The percentage of the progress bar expressed as a
176-
floating point number.
177-
"""
178-
return self._progress_val
179-
180141
# pylint: disable=too-many-locals
181-
@progress.setter
182-
def progress(self, value):
183-
"""Draws the progress bar
184-
185-
:param value: Progress bar value.
186-
:type value: float
187-
"""
188-
assert value <= self._max, "Progress value may not be > maximum value"
189-
assert value >= self._min, "Progress value may not be < minimum value"
190-
191-
_old_value = self._progress_val
192-
_new_value = round(value / self._max, 2)
193-
self._progress_val = _new_value
194-
self.render(_old_value, _new_value, value)
195-
196-
def render(self, old_value, new_value, progress):
142+
def render(self, _old_value, _new_value, _progress_value):
197143
"""
198144
Does the work of actually creating the graphical representation of
199145
the value (percentage, aka "progress") to be displayed.
200146
201-
:param old_value: The previously displayed value
202-
:type old_value: float
203-
:param new_value: The new value to display
204-
:type new_value: float
205-
:param progress: The value to display, as a percentage, represented
147+
:param _old_value: The previously displayed value
148+
:type _old_value: float
149+
:param _new_value: The new value to display
150+
:type _new_value: float
151+
:param _progress_value: The value to display, as a percentage, represented
206152
by a float from 0.0 to 1.0 (0% to 100%)
207-
:type progress: float
153+
:type _progress_value: float
208154
:return: None
209155
:rtype: None
210156
"""
211157
_padding = 0
212158

213-
print(f"Drawing a visual of progress value {progress}")
159+
print(f"Drawing a visual of progress value {_progress_value}")
214160

215161
if self._margin:
216162
_padding = 1
@@ -222,24 +168,24 @@ def render(self, old_value, new_value, progress):
222168
# in both directions (left-to-right and top-to-bottom)
223169

224170
_fill_width = (
225-
self.width - (2 * _padding) - _border_size
171+
self.widget_width - (2 * _padding) - _border_size
226172
) # Count padding on left and right
227173
_fill_height = (
228-
self.height - (2 * _padding) - _border_size
174+
self.widget_height - (2 * _padding) - _border_size
229175
) # Count padding on the top and bottom
230176

231-
_prev_value_size = int(old_value * _fill_height)
232-
_new_value_size = int(new_value * _fill_height)
177+
_prev_value_size = int(_old_value * _fill_height)
178+
_new_value_size = int(_new_value * _fill_height)
233179

234180
# If we have *ANY* value other than "zero" (minimum), we should
235181
# have at least one element showing
236-
if _new_value_size == 0 and new_value > self._min:
182+
if _new_value_size == 0 and _new_value > self._min:
237183
_new_value_size = 1
238184

239185
# Conversely, if we have *ANY* value other than 100% (maximum),
240186
# we should NOT show a full bar.
241187

242-
if _new_value_size == _fill_height and new_value < self._max:
188+
if _new_value_size == _fill_height and _new_value < self._max:
243189
_new_value_size -= 1
244190

245191
# Default values for increasing value
@@ -269,32 +215,3 @@ def render(self, old_value, new_value, progress):
269215
for h in range(_start, _end, _incr):
270216
for w in range(_start_offset, _fill_width):
271217
self._bitmap[w, h] = _color
272-
273-
@property
274-
def fill(self):
275-
"""The fill of the progress bar. Can be a hex value for a color or
276-
``None`` for transparent.
277-
"""
278-
return self._palette[0]
279-
280-
@property
281-
def width(self):
282-
"""The width of the progress bar. In pixels, includes the border."""
283-
return self._bar_width
284-
285-
@property
286-
def height(self):
287-
"""The height of the progress bar. In pixels, includes the border."""
288-
return self._bar_height
289-
290-
@fill.setter
291-
def fill(self, color):
292-
"""Sets the fill of the progress bar. Can be a hex value for a color or
293-
``None`` for transparent.
294-
"""
295-
if color is None:
296-
self._palette[2] = 0
297-
self._palette.make_transparent(0)
298-
else:
299-
self._palette[2] = color
300-
self._palette.make_opaque(0)

0 commit comments

Comments
 (0)