Skip to content

Commit bdbe480

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

File tree

3 files changed

+16
-114
lines changed

3 files changed

+16
-114
lines changed

adafruit_progressbar/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def progress(self, value):
181181
:param float value: The new value which should be displayed by the progress
182182
bar. Must be between 0.0-1.0
183183
"""
184-
_old_value = self._progress
184+
_old_value = self.progress
185185
# If we're using floats, from 0.0 to 1.0, using 4 decimal places allows us to handle values
186186
# as precise as 0.23456, which evaluates to a percentage value of 23.45% (with rounding)
187187
self._progress = round(value, 4)

adafruit_progressbar/progressbar.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ def __init__(
132132
self._bar_width = self._width
133133
self._bar_height = self._height
134134

135-
self._progress_val = 0.0
136-
self.progress = self._progress_val
137-
self.progress = progress
138-
139135
self._x = anchor_position[0]
140136
self._y = anchor_position[1]
141137

@@ -153,64 +149,24 @@ def __init__(
153149
value_range=(0.0, 1.0),
154150
)
155151

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-
180-
# 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):
152+
def render(self, _old_value, _new_value, _progress_value):
197153
"""
198154
Does the work of actually creating the graphical representation of
199155
the value (percentage, aka "progress") to be displayed.
200156
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
157+
:param _old_value: The previously displayed value
158+
:type _old_value: float
159+
:param _new_value: The new value to display
160+
:type _new_value: float
161+
:param _progress_value: The value to display, as a percentage, represented
206162
by a float from 0.0 to 1.0 (0% to 100%)
207-
:type progress: float
163+
:type _progress_value: float
208164
:return: None
209165
:rtype: None
210166
"""
211167
_padding = 0
212168

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

215171
if self._margin:
216172
_padding = 1
@@ -222,24 +178,24 @@ def render(self, old_value, new_value, progress):
222178
# in both directions (left-to-right and top-to-bottom)
223179

224180
_fill_width = (
225-
self.width - (2 * _padding) - _border_size
181+
self.widget_width - (2 * _padding) - _border_size
226182
) # Count padding on left and right
227183
_fill_height = (
228-
self.height - (2 * _padding) - _border_size
184+
self.widget_height - (2 * _padding) - _border_size
229185
) # Count padding on the top and bottom
230186

231-
_prev_value_size = int(old_value * _fill_height)
232-
_new_value_size = int(new_value * _fill_height)
187+
_prev_value_size = int(_old_value * _fill_height)
188+
_new_value_size = int(_new_value * _fill_height)
233189

234190
# If we have *ANY* value other than "zero" (minimum), we should
235191
# have at least one element showing
236-
if _new_value_size == 0 and new_value > self._min:
192+
if _new_value_size == 0 and _new_value > self._min:
237193
_new_value_size = 1
238194

239195
# Conversely, if we have *ANY* value other than 100% (maximum),
240196
# we should NOT show a full bar.
241197

242-
if _new_value_size == _fill_height and new_value < self._max:
198+
if _new_value_size == _fill_height and _new_value < self._max:
243199
_new_value_size -= 1
244200

245201
# Default values for increasing value
@@ -269,32 +225,3 @@ def render(self, old_value, new_value, progress):
269225
for h in range(_start, _end, _incr):
270226
for w in range(_start_offset, _fill_width):
271227
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)