Skip to content

Commit 91865d0

Browse files
committed
Continue refactor to include vertical progressbar
1 parent abf1765 commit 91865d0

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

adafruit_progressbar/__init__.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,31 @@ def y(self):
139139
"""The vertical (y) position of the top-left corner of the widget."""
140140
return self._position[1]
141141

142+
@property
143+
def outline_color(self):
144+
"""Returns the currently configured value for the color of the
145+
outline (border) of the widget."""
146+
return self._palette[1]
147+
148+
@property
149+
def fill(self):
150+
"""The fill of the progress bar. Can be a hex value for a color or ``None`` for
151+
transparent.
152+
"""
153+
return self._palette[0]
154+
155+
@fill.setter
156+
def fill(self, color):
157+
"""Sets the fill of the progress bar. Can be a hex value for a color or ``None`` for
158+
transparent.
159+
"""
160+
if color is None:
161+
self._palette[2] = 0
162+
self._palette.make_transparent(0)
163+
else:
164+
self._palette[2] = color
165+
self._palette.make_opaque(0)
166+
142167
@property
143168
def progress(self):
144169
"""Gets the current displayed value of the widget."""
@@ -165,7 +190,7 @@ def progress(self, value):
165190

166191
@property
167192
def range(self):
168-
"""The range which can be handled as a tuple (min,max)"""
193+
"""The range which can be handled as a Tuple(min,max)"""
169194
return self._range
170195

171196
@property

adafruit_progressbar/progressbar.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ def render(self, _previous_value, _new_value, _progress_value) -> None:
117117
:type _progress_value: float
118118
:rtype None:
119119
"""
120-
# print("Calling 'super().render()' before our own code")
121-
# super().render()
122-
# print("Calling own 'render()' code")
120+
121+
if _previous_value == _new_value:
122+
return # Do nothing if there's nothing to update
123123

124124
if _previous_value > _new_value:
125-
# uncolorize range from width*value+margin to width-margin
125+
# Remove color in range from width*value+margin to width-margin
126126
# from right to left
127127
_prev_pixel = max(2, int(self.widget_width * self.progress - 2))
128128
_new_pixel = max(int(self.widget_width * _new_value - 2), 2)

adafruit_progressbar/verticalprogressbar.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Copyright (C) 2021 Hugo Dahl for Adafruit Industries
2-
# Copyright (c) 2020 Brent Rubell for Adafruit Industries
1+
# SPDX-FileCopyrightText: Copyright (c) 2020-2021 Brent Rubell for Adafruit Industries, Hugo Dahl
32
#
43
# SPDX-License-Identifier: MIT
54

@@ -175,7 +174,6 @@ def _draw_outline(self):
175174
def progress(self):
176175
"""The percentage of the progress bar expressed as a
177176
floating point number.
178-
179177
"""
180178
return self._progress_val
181179

@@ -210,7 +208,7 @@ def render(self, old_value, new_value, progress):
210208
:return: None
211209
:rtype: None
212210
"""
213-
_padding = 1
211+
_padding = 0
214212

215213
print(f"Drawing a visual of progress value {progress}")
216214

@@ -230,8 +228,8 @@ def render(self, old_value, new_value, progress):
230228
self.height - (2 * _padding) - _border_size
231229
) # Count padding on the top and bottom
232230

233-
_prev_value_size = int(old_value * _fill_width)
234-
_new_value_size = int(new_value * _fill_width)
231+
_prev_value_size = int(old_value * _fill_height)
232+
_new_value_size = int(new_value * _fill_height)
235233

236234
# If we have *ANY* value other than "zero" (minimum), we should
237235
# have at least one element showing
@@ -241,7 +239,7 @@ def render(self, old_value, new_value, progress):
241239
# Conversely, if we have *ANY* value other than 100% (maximum),
242240
# we should NOT show a full bar.
243241

244-
if _new_value_size == _fill_width and new_value < self._max:
242+
if _new_value_size == _fill_height and new_value < self._max:
245243
_new_value_size -= 1
246244

247245
# Default values for increasing value
@@ -259,23 +257,23 @@ def render(self, old_value, new_value, progress):
259257
_start = max(_prev_value_size, _start_offset)
260258
_end = max(_new_value_size, _start_offset)
261259
elif _prev_value_size == _new_value_size:
262-
return # No action to take. Return
260+
return # The pre-defined values above the start
261+
# of the if block are already correct.
263262
else:
264-
pass
263+
pass # No value change. Return.
265264

266265
# Because range() is ( from-include, to-exclude )...
267266
_vert_start = _border_thickness + _padding
268267
_vert_end = _vert_start + _fill_height
269268

270-
for h in range(_vert_start, _vert_end):
271-
for w in range(_start, _end, _incr):
269+
for h in range(_start, _end, _incr):
270+
for w in range(_start_offset, _fill_width):
272271
self._bitmap[w, h] = _color
273272

274273
@property
275274
def fill(self):
276-
"""The fill of the progress bar. Can be a hex value for a color or ``None`` for
277-
transparent.
278-
275+
"""The fill of the progress bar. Can be a hex value for a color or
276+
``None`` for transparent.
279277
"""
280278
return self._palette[0]
281279

@@ -291,9 +289,8 @@ def height(self):
291289

292290
@fill.setter
293291
def fill(self, color):
294-
"""Sets the fill of the progress bar. Can be a hex value for a color or ``None`` for
295-
transparent.
296-
292+
"""Sets the fill of the progress bar. Can be a hex value for a color or
293+
``None`` for transparent.
297294
"""
298295
if color is None:
299296
self._palette[2] = 0

0 commit comments

Comments
 (0)