Skip to content

Commit

Permalink
more serious options handling for 'shape' and 'censor'
Browse files Browse the repository at this point in the history
related to #329: add support for operators to the 'shape' tool
  • Loading branch information
maoschanz committed Mar 5, 2021
1 parent 821db65 commit 6d2ccdf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 68 deletions.
40 changes: 20 additions & 20 deletions src/tools/classic_tools/tool_censor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,29 @@ def __init__(self, window, **kwargs):
# Context: a tool to hide things like text. You can translate it as
# "hide informations" if you think "censor" has a negative connotation
super().__init__('censor', _("Censor"), 'tool-censor-symbolic', window)
self.use_operator = True
self.use_operator = False
self.use_size = False
self.row.get_style_context().add_class('destructive-action')

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

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

def _set_options_attributes(self):
state_as_string = self.get_option_value('censor-type')
self._censor_type = state_as_string
if state_as_string == 'blur':
self._censor_label = _("Blur")
elif state_as_string == 'shuffle':
self._censor_label = _("Shuffle pixels")
elif state_as_string == 'mixed':
self._censor_label = _("Shuffle and blur")
elif state_as_string == 'mosaic':
self._censor_label = _("Mosaic")
else: # if state_as_string == 'solid':
self._censor_label = _("Solid color")
self._censor_type = self.get_option_value('censor-type')

def get_edition_status(self):
self._set_options_attributes()
return self.label + ' - ' + self._censor_label
censor_label = {
'blur': _("Blur"),
'shuffle': _("Shuffle pixels"),
'mixed': _("Shuffle and blur"),
'mosaic': _("Mosaic"),
'solid': _("Solid color"),
}[self._censor_type]
return self.label + ' - ' + censor_label

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

Expand Down Expand Up @@ -145,6 +142,9 @@ def _shuffle_pixels(self, surface, iterations):
w = surface.get_width()
h = surface.get_height()
channels = 4 # ARGB
if w <= 1 or h <= 1:
return surface

pixels = surface.get_data()

random.seed(1)
Expand All @@ -154,8 +154,8 @@ def _shuffle_pixels(self, surface, iterations):
return surface

def _shuffle_one_iteration(self, w, h, channels, pixels):
pix1_x = random.randint(0, w - 4)
pix1_y = random.randint(0, h - 4)
pix1_x = random.randint(0, w - 1)
pix1_y = random.randint(0, h - 1)

# Get data for a first pixel
cur_pixel = (pix1_y * w + pix1_x) * channels
Expand All @@ -164,8 +164,8 @@ def _shuffle_one_iteration(self, w, h, channels, pixels):
g1 = pixels[cur_pixel + 2]
b1 = pixels[cur_pixel + 3]

pix2_x = random.randint(0, w - 4)
pix2_y = random.randint(0, h - 4)
pix2_x = random.randint(0, w - 1)
pix2_y = random.randint(0, h - 1)

# Get data for a second pixel
cur_pixel = (pix2_y * w + pix2_x) * channels
Expand Down
84 changes: 36 additions & 48 deletions src/tools/classic_tools/tool_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ class ToolShape(AbstractClassicTool):

def __init__(self, window, **kwargs):
super().__init__('shape', _("Shape"), 'tool-freeshape-symbolic', window)
self.use_operator = True

self._reset_temp_points()

self.add_tool_action_simple('shape_close', self._force_close_shape)
self.set_action_sensitivity('shape_close', False)

self._shape_id = self.get_settings().get_string('last-active-shape')
self.add_tool_action_enum('shape_type', self._shape_id)
self._set_active_shape()
Expand All @@ -38,9 +43,6 @@ def __init__(self, window, **kwargs):
self._outline_label = _("Solid outline")
self.add_tool_action_enum('shape_outline', self._outline_id)

self.add_tool_action_simple('shape_close', self._force_close_shape)
self.set_action_sensitivity('shape_close', False)

def _reset_temp_points(self):
self._path = None
self.x_press = -1.0
Expand All @@ -49,53 +51,17 @@ def _reset_temp_points(self):
self.initial_y = -1.0

def _set_filling_style(self):
state_as_string = self.get_option_value('shape_filling')
self._filling_id = state_as_string
if state_as_string == 'empty':
self._filling_label = _("Empty shape")
elif state_as_string == 'filled':
# Context: fill a shape with the color of the left click
self._filling_label = _("Main color")
elif state_as_string == 'h-gradient':
self._filling_label = _("Horizontal gradient")
elif state_as_string == 'v-gradient':
self._filling_label = _("Vertical gradient")
elif state_as_string == 'r-gradient':
self._filling_label = _("Radial gradient")
else: # if state_as_string == 'secondary':
pass
# Context: fill a shape with the color of the right click
self._filling_label = _("Secondary color")
self._filling_id = self.get_option_value('shape_filling')

def _set_outline_style(self):
state_as_string = self.get_option_value('shape_outline')
self._outline_id = state_as_string
if state_as_string == 'solid':
self._outline_label = _("Solid outline")
elif state_as_string == 'dashed':
self._outline_label = _("Dashed outline")
else: # if state_as_string == 'none':
self._outline_label = _("No outline")
self._outline_id = self.get_option_value('shape_outline')

def _set_active_shape(self, *args):
self._shape_id = self.get_option_value('shape_type')
if self._shape_id == 'rectangle':
self._shape_label = _("Rectangle")
if self._shape_id == 'rectangle' or self._shape_id == 'polygon':
self._join_id = cairo.LineJoin.MITER
elif self._shape_id == 'roundedrect':
self._shape_label = _("Rounded rectangle")
self._join_id = cairo.LineJoin.ROUND
elif self._shape_id == 'oval':
self._shape_label = _("Oval")
self._join_id = cairo.LineJoin.ROUND
elif self._shape_id == 'circle':
self._shape_label = _("Circle")
self._join_id = cairo.LineJoin.ROUND
elif self._shape_id == 'polygon':
self._shape_label = _("Polygon")
self._join_id = cairo.LineJoin.MITER # BEVEL ?
# maybe BEVEL for polygon?
else:
self._shape_label = _("Free shape")
self._join_id = cairo.LineJoin.ROUND

def get_options_label(self):
Expand All @@ -106,11 +72,33 @@ def get_edition_status(self):
self._set_outline_style()
self._set_active_shape()

label = self._shape_label
if self._filling_id != 'empty':
label += ' - ' + self._filling_label
label = {
'rectangle': _("Rectangle"),
'roundedrect': _("Rounded rectangle"),
'oval': _("Oval"),
'circle': _("Circle"),
'polygon': _("Polygon"),
'freeshape': _("Free shape"),
}[self._shape_id]

if self._outline_id != 'solid':
label += ' - ' + self._outline_label
label += ' - ' + {
'solid': _("Solid outline"),
'dashed': _("Dashed outline"),
'none': _("No outline"),
}[self._outline_id]

if self._filling_id != 'empty':
label += ' - ' + {
'empty': _("Empty shape"),
# Context: fill a shape with the color of the left click
'filled': _("Filled with main color"),
# Context: fill a shape with the color of the right click
'secondary': _("Filled with secondary color"),
'h-gradient': _("Horizontal gradient"),
'v-gradient': _("Vertical gradient"),
'r-gradient': _("Radial gradient"),
}[self._filling_id]

if self._shape_id == 'polygon' or self._shape_id == 'freeshape':
instruction = _("Click on the shape's first point to close it.")
Expand Down Expand Up @@ -259,7 +247,7 @@ def build_operation(self, cairo_path):
'rgba_main': self.main_color,
'rgba_secd': self.secondary_color,
'antialias': self._use_antialias,
'operator': cairo.Operator.OVER, # self._operator, # XXX ne marcherait pas avec le blur
'operator': self._operator,
'line_join': self._join_id,
'line_width': self.tool_width,
'filling': self._filling_id,
Expand Down

0 comments on commit 6d2ccdf

Please sign in to comment.