Skip to content

Missing type annotations, refs #50 #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions adafruit_displayio_layout/layouts/grid_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""
try:
# Used only for typing
from typing import Tuple
from typing import Any, List, Optional, Tuple, Union
except ImportError:
pass

Expand Down Expand Up @@ -61,32 +61,32 @@ class GridLayout(displayio.Group):
# pylint: disable=too-many-instance-attributes
def __init__(
self,
x,
y,
width,
height,
grid_size,
cell_padding=0,
divider_lines=False,
h_divider_line_rows=None,
v_divider_line_cols=None,
divider_line_color=0xFFFFFF,
cell_anchor_point=(0.0, 0.0),
):
x: int,
y: int,
width: int,
height: int,
grid_size: tuple[int, int],
cell_padding: int = 0,
divider_lines: bool = False,
h_divider_line_rows: Union[Tuple[int, ...], List[int], None] = None,
v_divider_line_cols: Union[Tuple[int, ...], List[int], None] = None,
divider_line_color: int = 0xFFFFFF,
cell_anchor_point: Tuple[float, float] = (0.0, 0.0),
) -> None:
super().__init__(x=x, y=y)
self.x = x
self.y = y
self._width = width
self._height = height
self.grid_size = grid_size
self.cell_padding = cell_padding
self._cell_content_list = []
self._cell_content_list: List[dict[str, Any]] = []
self._cell_anchor_point = cell_anchor_point

self._divider_lines = []
self._divider_lines: List[dict[str, Any]] = []
self._divider_color = divider_line_color
self.h_divider_line_rows = h_divider_line_rows
self.v_divider_line_cols = v_divider_line_cols
self.h_divider_line_rows = h_divider_line_rows or tuple()
self.v_divider_line_cols = v_divider_line_cols or tuple()

self._divider_lines_enabled = (
(divider_lines is True)
Expand All @@ -95,27 +95,22 @@ def __init__(
)

if divider_lines:
if self.h_divider_line_rows is None:
if h_divider_line_rows is None:
self.h_divider_line_rows = []
for _y in range(self.grid_size[1] + 1):
self.h_divider_line_rows.append(_y)
if self.v_divider_line_cols is None:
if v_divider_line_cols is None:
self.v_divider_line_cols = []
for _x in range(self.grid_size[0] + 1):
self.v_divider_line_cols.append(_x)
else:
if not h_divider_line_rows:
self.h_divider_line_rows = tuple()
if not v_divider_line_cols:
self.v_divider_line_cols = tuple()

# use at least 1 padding so that content is inside the divider lines
if cell_padding == 0 and (
divider_lines or h_divider_line_rows or v_divider_line_cols
):
self.cell_padding = 1

def _layout_cells(self):
def _layout_cells(self) -> None:
# pylint: disable=too-many-locals, too-many-branches, too-many-statements
for cell in self._cell_content_list:
if cell["content"] not in self:
Expand Down Expand Up @@ -382,8 +377,12 @@ def _layout_cells(self):
self.append(line_obj["tilegrid"])

def add_content(
self, cell_content, grid_position, cell_size, cell_anchor_point=None
):
self,
cell_content: displayio.Group,
grid_position: Tuple[int, int],
cell_size: Tuple[int, int],
cell_anchor_point: Optional[Tuple[float, ...]] = None,
) -> None:
"""Add a child to the grid.

:param cell_content: the content to add to this cell e.g. label, button, etc...
Expand Down Expand Up @@ -412,7 +411,7 @@ def add_content(
self._cell_content_list.append(sub_view_obj)
self._layout_cells()

def get_cell(self, cell_coordinates):
def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
"""
Return a cells content based on the cell_coordinates. Raises
KeyError if coordinates were not found in the GridLayout.
Expand Down
35 changes: 19 additions & 16 deletions adafruit_displayio_layout/widgets/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
pass

try:
from typing import Tuple
from typing import Any, List, Optional, Tuple
except ImportError:
pass

Expand Down Expand Up @@ -178,15 +178,15 @@ def __init__(
tick_color: int = 0xFFFFFF,
major_tick_stroke: int = 1,
major_tick_length: int = 5,
tick_label_font=terminalio.FONT,
tick_label_font: terminalio.FONT = terminalio.FONT,
font_color: int = 0xFFFFFF,
pointer_radius: int = 1,
pointer_color: int = 0xFFFFFF,
subticks: bool = False,
nudge_x: int = 0,
nudge_y: int = 0,
verbose: bool = False,
**kwargs,
**kwargs: Any,
) -> None:

super().__init__(**kwargs)
Expand Down Expand Up @@ -317,12 +317,12 @@ def __init__(
self.append(self._screen_tilegrid)
self.append(self._corner_tilegrid)

self._pointer = None
self._circle_palette = None
self.plot_line_point = None
self._pointer: Optional[vectorio.Circle] = None
self._circle_palette: Optional[displayio.Palette] = None
self.plot_line_point: List[Tuple[int, int]] = []

@staticmethod
def _get_font_height(font, scale: int) -> Tuple[int, int]:
def _get_font_height(font: terminalio.FONT, scale: int) -> Tuple[int, int]:
if hasattr(font, "get_bounding_box"):
font_height = int(scale * font.get_bounding_box()[1])
font_width = int(scale * font.get_bounding_box()[0])
Expand Down Expand Up @@ -448,14 +448,15 @@ def _draw_ticks(self) -> None:
def _draw_pointers(self, x: int, y: int) -> None:

self._circle_palette = displayio.Palette(1)

self._circle_palette[0] = self._pointer_color
self._pointer = vectorio.Circle(
radius=self._pointer_radius, x=x, y=y, pixel_shader=self._circle_palette
)

self.append(self._pointer)

def _calc_local_xy(self, x: int, y: int) -> (int, int):
def _calc_local_xy(self, x: int, y: int) -> Tuple[int, int]:
local_x = (
int((x - self._xrange[0]) * self._factorx * self._valuex) + self._nudge_x
)
Expand All @@ -469,24 +470,24 @@ def _calc_local_xy(self, x: int, y: int) -> (int, int):
)
return (local_x, local_y)

def _check_local_x_in_range(self, local_x):
def _check_local_x_in_range(self, local_x: int) -> bool:
return 0 <= local_x < self.width

def _check_local_y_in_range(self, local_y):
def _check_local_y_in_range(self, local_y: int) -> bool:
return 0 <= local_y < self.height

def _check_local_xy_in_range(self, local_x, local_y):
def _check_local_xy_in_range(self, local_x: int, local_y: int) -> bool:
return self._check_local_x_in_range(local_x) and self._check_local_y_in_range(
local_y
)

def _check_x_in_range(self, x):
def _check_x_in_range(self, x: int) -> bool:
return self._xrange[0] <= x <= self._xrange[1]

def _check_y_in_range(self, y):
def _check_y_in_range(self, y: int) -> bool:
return self._yrange[0] <= y <= self._yrange[1]

def _check_xy_in_range(self, x, y):
def _check_xy_in_range(self, x: int, y: int) -> bool:
return self._check_x_in_range(x) and self._check_y_in_range(y)

def _add_point(self, x: int, y: int) -> None:
Expand Down Expand Up @@ -587,6 +588,7 @@ def update_pointer(self, x: int, y: int) -> None:
:return: None
rtype: None
"""

self._add_point(x, y)
if not self._pointer:
self._draw_pointers(
Expand All @@ -609,6 +611,7 @@ def add_plot_line(self, x: int, y: int) -> None:

rtype: None
"""

self._add_point(x, y)
if len(self.plot_line_point) > 1:
bitmaptools.draw_line(
Expand All @@ -620,7 +623,7 @@ def add_plot_line(self, x: int, y: int) -> None:
1,
)

def clear_plot_lines(self, palette_index=5):
def clear_plot_lines(self, palette_index: int = 5) -> None:
"""clear_plot_lines function.

clear all added lines
Expand All @@ -631,5 +634,5 @@ def clear_plot_lines(self, palette_index=5):

rtype: None
"""
self.plot_line_point = None
self.plot_line_point = []
self._plot_bitmap.fill(palette_index)
27 changes: 19 additions & 8 deletions adafruit_displayio_layout/widgets/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

"""

try:
from typing import Optional, Tuple
except ImportError:
pass


__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_Layout.git"

Expand All @@ -46,20 +52,24 @@ class Control:

def __init__(
self,
):
) -> None:
self.touch_boundary = (
None # `self.touch_boundary` should be updated by the subclass
0,
0,
0,
0, # `self.touch_boundary` should be updated by the subclass
)
# Tuple of [x, y, width, height]: [int, int, int, int] all in pixel units
# where x,y define the upper left corner
# and width and height define the size of the `touch_boundary`

def contains(self, touch_point):
def contains(self, touch_point: Tuple[int, int, Optional[int]]) -> bool:
"""Checks if the Control was touched. Returns True if the touch_point is within the
Control's touch_boundary.

:param touch_point: x,y location of the screen, converted to local coordinates.
:type touch_point: Tuple[x,y]
:param touch_point: x, y, p location of the screen, converted to local coordinates, plus
an optional pressure value for screens that support it.
:type touch_point: Tuple[int, int, Optional[int]]
:return: Boolean

"""
Expand All @@ -82,11 +92,12 @@ def contains(self, touch_point):
return False

# place holder touch_handler response functions
def selected(self, touch_point):
def selected(self, touch_point: Tuple[int, int, Optional[int]]) -> None:
"""Response function when Control is selected. Should be overridden by subclass.

:param touch_point: x,y location of the screen, converted to local coordinates.
:type touch_point: Tuple[x,y]
:param touch_point: x, y, p location of the screen, converted to local coordinates, plus
an optional pressure value for screens that support it.
:type touch_point: Tuple[int, int, Optional[int]]
:return: None

"""
Expand Down
Loading