Skip to content

Commit

Permalink
Cleanup public API by making draw protected and renaming methods
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Yeaw <dan@yeaw.me>
  • Loading branch information
danyeaw committed Jun 12, 2018
1 parent da7a5d5 commit 368921e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions docs/reference/api/widgets/canvas.rst
Expand Up @@ -73,11 +73,11 @@ Main Interface
:members:
:undoc-members:
:inherited-members:
:exclude-members: canvas, redraw, add_drawing_object, draw
:exclude-members: canvas, add_draw_obj

Lower-Level Classes
^^^^^^^^^^^^^^^^^^^

.. automodule:: toga.widgets.canvas
:members:
:exclude-members: Canvas
:exclude-members: Canvas, add_draw_obj
84 changes: 42 additions & 42 deletions src/core/toga/widgets/canvas.py
Expand Up @@ -24,7 +24,7 @@ def __init__(self, *args, **kwargs): # kwargs used to support multiple inherita
def __repr__(self):
return "{}()".format(self.__class__.__name__)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw all drawing objects that are on the context or canvas.
Expand All @@ -34,7 +34,7 @@ def draw(self, impl, *args, **kwargs):
"""
for obj in self.drawing_objects:
obj.draw(impl, *args, **kwargs)
obj._draw(impl, *args, **kwargs)

###########################################################################
# Methods to keep track of the canvas, automatically redraw it
Expand All @@ -60,20 +60,20 @@ def canvas(self, value):
"""
self._canvas = value

def add_drawing_object(self, drawing_object):
def add_draw_obj(self, draw_obj):
"""A drawing object to add to the drawing object stack on a context
Args:
drawing_object: (:obj:`Drawing Object`): The drawing object to add
draw_obj: (:obj:`Drawing Object`): The drawing object to add
"""
self.drawing_objects.append(drawing_object)
self.drawing_objects.append(draw_obj)

# Only redraw if drawing to canvas directly
if self.canvas is self:
self.redraw()

return drawing_object
return draw_obj

def redraw(self):
"""Force a redraw of the Canvas
Expand Down Expand Up @@ -116,7 +116,7 @@ def context(self):
"""
context = Context()
self.add_drawing_object(context)
self.add_draw_obj(context)
context.canvas = self.canvas
yield context
self.redraw()
Expand Down Expand Up @@ -144,7 +144,7 @@ def fill(self, color=BLACK, fill_rule="nonzero", preserve=False):
else:
fill = Fill(color, "nonzero", preserve)
fill.canvas = self.canvas
yield self.add_drawing_object(fill)
yield self.add_draw_obj(fill)
self.redraw()

@contextmanager
Expand All @@ -162,7 +162,7 @@ def stroke(self, color=BLACK, line_width=2.0):
"""
stroke = Stroke(color, line_width)
stroke.canvas = self.canvas
yield self.add_drawing_object(stroke)
yield self.add_draw_obj(stroke)
self.redraw()

@contextmanager
Expand All @@ -180,7 +180,7 @@ def closed_path(self, x, y):
"""
closed_path = ClosedPath(x, y)
closed_path.canvas = self.canvas
yield self.add_drawing_object(closed_path)
yield self.add_draw_obj(closed_path)
self.redraw()

###########################################################################
Expand All @@ -195,7 +195,7 @@ def new_path(self):
"""
new_path = NewPath()
return self.add_drawing_object(new_path)
return self.add_draw_obj(new_path)

def move_to(self, x, y):
"""Constructs and returns a :class:`MoveTo <MoveTo>`.
Expand All @@ -209,7 +209,7 @@ def move_to(self, x, y):
"""
move_to = MoveTo(x, y)
return self.add_drawing_object(move_to)
return self.add_draw_obj(move_to)

def line_to(self, x, y):
"""Constructs and returns a :class:`LineTo <LineTo>`.
Expand All @@ -223,7 +223,7 @@ def line_to(self, x, y):
"""
line_to = LineTo(x, y)
return self.add_drawing_object(line_to)
return self.add_draw_obj(line_to)

def bezier_curve_to(self, cp1x, cp1y, cp2x, cp2y, x, y):
"""Constructs and returns a :class:`BezierCurveTo <BezierCurveTo>`.
Expand All @@ -241,7 +241,7 @@ def bezier_curve_to(self, cp1x, cp1y, cp2x, cp2y, x, y):
"""
bezier_curve_to = BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
return self.add_drawing_object(bezier_curve_to)
return self.add_draw_obj(bezier_curve_to)

def quadratic_curve_to(self, cpx, cpy, x, y):
"""Constructs and returns a :class:`QuadraticCurveTo <QuadraticCurveTo>`.
Expand All @@ -257,7 +257,7 @@ def quadratic_curve_to(self, cpx, cpy, x, y):
"""
quadratic_curve_to = QuadraticCurveTo(cpx, cpy, x, y)
return self.add_drawing_object(quadratic_curve_to)
return self.add_draw_obj(quadratic_curve_to)

def arc(self, x, y, radius, startangle=0.0, endangle=2 * pi, anticlockwise=False):
"""Constructs and returns a :class:`Arc <Arc>`.
Expand All @@ -280,7 +280,7 @@ def arc(self, x, y, radius, startangle=0.0, endangle=2 * pi, anticlockwise=False
"""
arc = Arc(x, y, radius, startangle, endangle, anticlockwise)
return self.add_drawing_object(arc)
return self.add_draw_obj(arc)

def ellipse(
self,
Expand Down Expand Up @@ -315,7 +315,7 @@ def ellipse(
ellipse = Ellipse(
x, y, radiusx, radiusy, rotation, startangle, endangle, anticlockwise
)
self.add_drawing_object(ellipse)
self.add_draw_obj(ellipse)
return ellipse

def rect(self, x, y, width, height):
Expand All @@ -332,7 +332,7 @@ def rect(self, x, y, width, height):
"""
rect = Rect(x, y, width, height)
return self.add_drawing_object(rect)
return self.add_draw_obj(rect)

###########################################################################
# Text drawing
Expand All @@ -358,7 +358,7 @@ def write_text(self, text, x=0, y=0, font=None):
if font is None:
font = Font(family=SYSTEM, size=self._canvas.style.font_size)
write_text = WriteText(text, x, y, font)
return self.add_drawing_object(write_text)
return self.add_draw_obj(write_text)


class Fill(Context):
Expand Down Expand Up @@ -387,13 +387,13 @@ def __repr__(self):
self.__class__.__name__, self.color, self.fill_rule, self.preserve
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Used by parent to draw all objects that are part of the context.
"""
impl.new_path(*args, **kwargs)
for obj in self.drawing_objects:
obj.draw(impl, *args, **kwargs)
obj._draw(impl, *args, **kwargs)
impl.fill(self.color, self.fill_rule, self.preserve, *args, **kwargs)

@property
Expand Down Expand Up @@ -432,12 +432,12 @@ def __repr__(self):
self.__class__.__name__, self.color, self.line_width
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Used by parent to draw all objects that are part of the context.
"""
for obj in self.drawing_objects:
obj.draw(impl, *args, **kwargs)
obj._draw(impl, *args, **kwargs)
impl.stroke(self.color, self.line_width, *args, **kwargs)

@property
Expand Down Expand Up @@ -472,13 +472,13 @@ def __init__(self, x, y):
def __repr__(self):
return "{}(x={}, y={})".format(self.__class__.__name__, self.x, self.y)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Used by parent to draw all objects that are part of the context.
"""
impl.move_to(self.x, self.y, *args, **kwargs)
for obj in self.drawing_objects:
obj.draw(impl, *args, **kwargs)
obj._draw(impl, *args, **kwargs)
impl.closed_path(self.x, self.y, *args, **kwargs)


Expand Down Expand Up @@ -516,7 +516,7 @@ def rotate(self, radians):
"""
rotate = Rotate(radians)
return self.add_drawing_object(rotate)
return self.add_draw_obj(rotate)

def scale(self, sx, sy):
"""Constructs and returns a :class:`Scale <Scale>`.
Expand All @@ -530,7 +530,7 @@ def scale(self, sx, sy):
"""
scale = Scale(sx, sy)
return self.add_drawing_object(scale)
return self.add_draw_obj(scale)

def translate(self, tx, ty):
"""Constructs and returns a :class:`Translate <Translate>`.
Expand All @@ -544,7 +544,7 @@ def translate(self, tx, ty):
"""
translate = Translate(tx, ty)
return self.add_drawing_object(translate)
return self.add_draw_obj(translate)

def reset_transform(self):
"""Constructs and returns a :class:`ResetTransform <ResetTransform>`.
Expand All @@ -554,7 +554,7 @@ def reset_transform(self):
"""
reset_transform = ResetTransform()
return self.add_drawing_object(reset_transform)
return self.add_draw_obj(reset_transform)


class MoveTo:
Expand All @@ -577,7 +577,7 @@ def __init__(self, x, y):
def __repr__(self):
return "{}(x={}, y={})".format(self.__class__.__name__, self.x, self.y)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand All @@ -604,7 +604,7 @@ def __init__(self, x, y):
def __repr__(self):
return "{}(x={}, y={})".format(self.__class__.__name__, self.x, self.y)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down Expand Up @@ -649,7 +649,7 @@ def __repr__(self):
self.y,
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down Expand Up @@ -686,7 +686,7 @@ def __repr__(self):
self.__class__.__name__, self.cpx, self.cpy, self.x, self.y
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down Expand Up @@ -748,7 +748,7 @@ def __repr__(self):
self.anticlockwise,
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down Expand Up @@ -809,7 +809,7 @@ def __repr__(self):
self.anticlockwise,
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down Expand Up @@ -852,7 +852,7 @@ def __repr__(self):
self.__class__.__name__, self.x, self.y, self.width, self.height
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand All @@ -878,7 +878,7 @@ def __init__(self, radians):
def __repr__(self):
return "{}(radians={})".format(self.__class__.__name__, self.radians)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand All @@ -903,7 +903,7 @@ def __init__(self, sx, sy):
def __repr__(self):
return "{}(sx={}, sy={})".format(self.__class__.__name__, self.sx, self.sy)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand All @@ -928,7 +928,7 @@ def __init__(self, tx, ty):
def __repr__(self):
return "{}(tx={}, ty={})".format(self.__class__.__name__, self.tx, self.ty)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand All @@ -947,7 +947,7 @@ class ResetTransform:
def __repr__(self):
return "{}()".format(self.__class__.__name__)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down Expand Up @@ -980,7 +980,7 @@ def __repr__(self):
self.__class__.__name__, self.text, self.x, self.y, self.font
)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand All @@ -995,7 +995,7 @@ class NewPath:
def __repr__(self):
return "{}()".format(self.__class__.__name__)

def draw(self, impl, *args, **kwargs):
def _draw(self, impl, *args, **kwargs):
"""Draw the drawing object using the implementation.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/dummy/toga_dummy/widgets/canvas.py
Expand Up @@ -7,7 +7,7 @@ def create(self):

def redraw(self):
self._action("redraw")
self.interface.draw(self)
self.interface._draw(self)

# Basic paths

Expand Down
2 changes: 1 addition & 1 deletion src/gtk/toga_gtk/widgets/canvas.py
Expand Up @@ -39,7 +39,7 @@ def gtk_draw_callback(self, canvas, gtk_context):
the draw method on the interface Canvas to draw the objects.
"""
self.interface.draw(self, draw_context=gtk_context)
self.interface._draw(self, draw_context=gtk_context)

def redraw(self):
pass
Expand Down

0 comments on commit 368921e

Please sign in to comment.