Skip to content

Commit

Permalink
create basic "censor" tool, as a replacement of the blur mode (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
maoschanz committed Feb 11, 2021
1 parent 68439da commit a7a5a66
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/drawing.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

<file>tools/ui/tool-arc.ui</file>
<file>tools/ui/tool-brush.ui</file>
<file>tools/ui/tool-censor.ui</file>
<file>tools/ui/tool-experiment.ui</file>
<file>tools/ui/tool-line.ui</file>
<file>tools/ui/tool-highlight.ui</file>
Expand All @@ -59,6 +60,7 @@
<!-- Tools icons -->
<file>tools/icons/tool-arc-symbolic.svg</file>
<file>tools/icons/tool-brush-symbolic.svg</file>
<file>tools/icons/tool-censor-symbolic.svg</file>
<file>tools/icons/tool-circle-symbolic.svg</file>
<file>tools/icons/tool-crop-symbolic.svg</file>
<file>tools/icons/tool-eraser-symbolic.svg</file>
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ drawing_sources = [
'tools/classic_tools/abstract_classic_tool.py',
'tools/classic_tools/tool_arc.py',
'tools/classic_tools/tool_brush.py',
'tools/classic_tools/tool_censor.py',
'tools/classic_tools/tool_eraser.py',
'tools/classic_tools/tool_experiment.py',
'tools/classic_tools/tool_highlight.py',
Expand Down
110 changes: 110 additions & 0 deletions src/tools/classic_tools/tool_censor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# tool_censor.py
#
# Copyright 2018-2021 Romain F. T.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import cairo
from .abstract_classic_tool import AbstractClassicTool
from .utilities_paths import utilities_add_arrow_triangle

class ToolCensor(AbstractClassicTool):
__gtype_name__ = 'ToolCensor'

def __init__(self, window, **kwargs):
super().__init__('censor', _("Censor"), 'tool-censor-symbolic', window)
self.use_operator = True
self.row.get_style_context().add_class('destructive-action')

self.add_tool_action_enum('censor-type', 'blur')
self._set_options_attributes() # Not optimal but more readable

def get_options_label(self):
return _("Censoring options")

def _set_options_attributes(self):
self._censor_type = self.get_option_value('censor-type')

def get_edition_status(self):
self._set_options_attributes()
return self.label

############################################################################

def on_press_on_area(self, event, surface, event_x, event_y):
self.set_common_values(event.button, event_x, event_y)

def on_motion_on_area(self, event, surface, event_x, event_y):
operation = self.build_operation(event_x, event_y, True)
self.do_tool_operation(operation)

def on_release_on_area(self, event, surface, event_x, event_y):
operation = self.build_operation(event_x, event_y, False)
self.apply_operation(operation)

############################################################################

def build_operation(self, event_x, event_y, is_preview):
operation = {
'tool_id': self.id,
'line_width': self.tool_width,
'censor-type': self._censor_type,
'is_preview': is_preview,
'x_release': event_x,
'y_release': event_y,
'x_press': self.x_press,
'y_press': self.y_press
}
return operation

def do_tool_operation(self, operation):
cairo_context = self.start_tool_operation(operation)
censor_type = operation['censor-type']
line_width = operation['line_width']
cairo_context.set_line_width(line_width)
x1 = operation['x_press']
y1 = operation['y_press']
x2 = operation['x_release']
y2 = operation['y_release']

# We don't memorize the path because all coords are here anyway for the
# surface clipping.
cairo_context.move_to(x1, y1)
cairo_context.line_to(x2, y2)

# self.stroke_with_operator(operation['operator'], cairo_context, \
# line_width, operation['is_preview'])
# FIXME TODO

# is_blur = (operator == cairo.Operator.DEST_IN)
# if is_blur and is_preview:
# context.set_operator(cairo.Operator.CLEAR)

# if is_blur and not is_preview:
# context.set_line_width(line_width)
# context.stroke_preserve()
# radius = int(line_width / 2)
# source_surface = self.get_surface()
# XXX using the whole surface is suboptimal
# blurred_surface = utilities_blur_surface(source_surface, radius, 3, 0)
# where 0 == BlurType.CAIRO_REPAINTS and 0 == BlurDirection.BOTH
# self.restore_pixbuf()
# context = self.get_context()
# context.set_operator(cairo.Operator.OVER)
# context.set_source_surface(blurred_surface, 0, 0)
# context.paint()

############################################################################
################################################################################

6 changes: 6 additions & 0 deletions src/tools/icons/tool-censor-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/tools/ui/tool-censor.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface domain="drawing">
<menu id="options-menu">
<section>
<attribute name="label" translatable="yes">Censor type</attribute>
<item>
<attribute name="label" translatable="yes">Blur</attribute>
<attribute name="action">win.censor-type</attribute>
<attribute name="target">blur</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Shuffle pixels</attribute>
<attribute name="action">win.censor-type</attribute>
<attribute name="target">shuffle</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Both</attribute>
<attribute name="action">win.censor-type</attribute>
<attribute name="target">both</attribute>
</item>
</section>
</menu>
</interface>

2 changes: 2 additions & 0 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# Import tools
from .tool_arc import ToolArc
from .tool_brush import ToolBrush
from .tool_censor import ToolCensor
from .tool_eraser import ToolEraser
from .tool_experiment import ToolExperiment
from .tool_highlight import ToolHighlighter
Expand Down Expand Up @@ -154,6 +155,7 @@ def _init_tools(self):
self._load_tool('pencil', ToolPencil, disabled_tools, dev)
if dev:
self._load_tool('brush', ToolBrush, disabled_tools, dev)
self._load_tool('censor', ToolCensor, disabled_tools, dev)
self._load_tool('eraser', ToolEraser, disabled_tools, dev)
self._load_tool('highlight', ToolHighlighter, disabled_tools, dev)
self._load_tool('text', ToolText, disabled_tools, dev)
Expand Down

0 comments on commit a7a5a66

Please sign in to comment.