Skip to content

Commit

Permalink
Use alpha-0 colors to erase previous scribbles
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimbali committed Oct 26, 2020
1 parent a180222 commit 1ffcedc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 20 deletions.
44 changes: 42 additions & 2 deletions pympress/scribble.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Scribbler(builder.Builder):
scribbling_mode = False
#: `list` of scribbles to be drawn, as tuples of color :class:`~Gdk.RGBA`, width `int`, and a `list` of points.
scribble_list = []
#: `list` of undone scribbles to possibly redo
scribble_redo_list = []
#: Whether the current mouse movements are drawing strokes or should be ignored
scribble_drawing = False
#: :class:`~Gdk.RGBA` current color of the scribbling tool
Expand All @@ -66,6 +68,13 @@ class Scribbler(builder.Builder):
#: :class:`~Gtk.AspectFrame` for the slide in the Presenter's highlight mode
scribble_p_frame = None

#: :class:`~Gtk.Button` for removing the last drawn scribble
scribble_undo = None
#: :class:`~Gtk.Button` for drawing the last removed scribble
scribble_redo = None
#: :class:`~Gtk.Button` for removing all drawn scribbles
scribble_clear = None

#: A :class:`~Gtk.OffscreenWindow` where we render the scribbling interface when it's not shown
off_render = None
#: :class:`~Gtk.Box` in the Presenter window, where we insert scribbling.
Expand Down Expand Up @@ -141,6 +150,8 @@ def nav_scribble(self, name, ctrl_pressed, command = None):
return False
elif command == 'undo_scribble':
self.pop_scribble()
elif command == 'redo_scribble':
self.pop_scribble()
elif command == 'cancel':
self.disable_scribbling()
else:
Expand All @@ -160,7 +171,9 @@ def track_scribble(self, widget, event):
"""
if self.scribble_drawing:
self.scribble_list[-1][2].append(self.get_slide_point(widget, event))
self.scribble_redo_list.clear()

self.adjust_buttons()
self.redraw_current_slide()
return True
else:
Expand Down Expand Up @@ -201,19 +214,26 @@ def draw_scribble(self, widget, cairo_context):
"""
ww, wh = widget.get_allocated_width(), widget.get_allocated_height()

cairo_context.push_group()
cairo_context.set_line_cap(cairo.LINE_CAP_ROUND)

for color, width, points in self.scribble_list:
points = [(p[0] * ww, p[1] * wh) for p in points]

# alpha == 0 -> Eraser mode
cairo_context.set_operator(cairo.OPERATOR_OVER if color.alpha else cairo.OPERATOR_CLEAR)
cairo_context.set_source_rgba(*color)
cairo_context.set_line_width(width)

cairo_context.move_to(*points[0])

for p in points[1:]:
cairo_context.line_to(*p)
cairo_context.stroke()

cairo_context.pop_group_to_source()
cairo_context.paint()


def update_color(self, widget):
""" Callback for the color chooser button, to set scribbling color.
Expand All @@ -237,20 +257,40 @@ def update_width(self, widget, event, value):
self.config.set('scribble', 'width', str(self.scribble_width))


def adjust_buttons(self):
""" Properly enable and disable buttons based on scribblings lists.
"""
self.scribble_undo.set_sensitive(bool(self.scribble_list))
self.scribble_clear.set_sensitive(bool(self.scribble_list))
self.scribble_redo.set_sensitive(bool(self.scribble_redo_list))


def clear_scribble(self, *args):
""" Callback for the scribble clear button, to remove all scribbles.
"""
del self.scribble_list[:]
self.scribble_list.clear()

self.redraw_current_slide()
self.adjust_buttons()


def pop_scribble(self, *args):
""" Callback for the scribble undo button, to undo the last scribble.
"""
if self.scribble_list:
self.scribble_list.pop()
self.scribble_redo_list.append(self.scribble_list.pop())

self.adjust_buttons()
self.redraw_current_slide()


def redo_scribble(self, *args):
""" Callback for the scribble undo button, to undo the last scribble.
"""
if self.scribble_redo_list:
self.scribble_list.append(self.scribble_redo_list.pop())

self.adjust_buttons()
self.redraw_current_slide()


Expand Down
1 change: 1 addition & 0 deletions pympress/share/defaults.conf
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@ unzoom = u
validate = Return KP_Enter
cancel = Escape
undo_scribble = <ctrl>z
redo_scribble = <ctrl>r
toggle_pointermode = l
68 changes: 51 additions & 17 deletions pympress/share/xml/highlight.glade
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<property name="width_request">80</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="always_show_image">True</property>
Expand All @@ -87,6 +88,7 @@
<property name="width_request">80</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="always_show_image">True</property>
Expand All @@ -100,16 +102,17 @@
</packing>
</child>
<child>
<object class="GtkColorButton" id="scribble_color">
<object class="GtkButton" id="scribble_redo">
<property name="label">gtk-redo</property>
<property name="width_request">80</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<property name="use_alpha">True</property>
<signal name="color-set" handler="update_color" swapped="no"/>
<signal name="clicked" handler="redo_scribble" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
Expand All @@ -118,21 +121,52 @@
</packing>
</child>
<child>
<object class="GtkScale" id="scribble_width">
<property name="width_request">80</property>
<property name="height_request">160</property>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<property name="orientation">vertical</property>
<property name="adjustment">scribble_width_adjust</property>
<property name="inverted">True</property>
<property name="digits">0</property>
<property name="draw_value">False</property>
<property name="value_pos">bottom</property>
<signal name="change-value" handler="update_width" swapped="no"/>
<child>
<object class="GtkColorButton" id="scribble_color">
<property name="width_request">80</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<property name="use_alpha">True</property>
<signal name="color-set" handler="update_color" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scribble_width">
<property name="width_request">80</property>
<property name="height_request">160</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="orientation">vertical</property>
<property name="adjustment">scribble_width_adjust</property>
<property name="inverted">True</property>
<property name="digits">0</property>
<property name="draw_value">False</property>
<property name="value_pos">bottom</property>
<signal name="change-value" handler="update_width" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
Expand All @@ -154,7 +188,7 @@
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">5</property>
<property name="position">7</property>
</packing>
</child>
<child>
Expand All @@ -173,14 +207,14 @@
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">6</property>
<property name="position">8</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
</object>
Expand Down
2 changes: 1 addition & 1 deletion pympress/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ def on_navigation(self, widget, event):
elif command == 'toggle_pointermode':
self.laser.toggle_pointermode()
else:
if command and command not in {'cancel', 'validate', 'undo_scribble'}:
if command and command not in {'cancel', 'validate', 'undo_scribble', 'redo_scribble'}:
logger.error('ERROR: missing command "{}" for {}{}{}{}'
.format(command, 'ctrl + ' if ctrl_pressed else '', 'shift + ' if shift_pressed else '',
'meta + ' if meta_pressed else '', name))
Expand Down

0 comments on commit 1ffcedc

Please sign in to comment.