| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import cairo, math |
| 19 |
from .abstract_classic_tool import AbstractClassicTool |
| 20 |
from .utilities_paths import utilities_smooth_path |
| 21 |
|
| 22 |
class ToolPencil(AbstractClassicTool): |
| 23 |
__gtype_name__ = 'ToolPencil' |
| 24 |
|
| 25 |
def __init__(self, window, **kwargs): |
| 26 |
super().__init__('pencil', _("Pencil"), 'tool-pencil-symbolic', window) |
| 27 |
self.use_operator = True |
| 28 |
|
| 29 |
self._path = None |
| 30 |
self._shape_label = _("Round") |
| 31 |
self._cap_id = cairo.LineCap.ROUND |
| 32 |
self._join_id = cairo.LineCap.ROUND |
| 33 |
self._dashes_type = 'none' |
| 34 |
self._use_outline = False |
| 35 |
|
| 36 |
self.add_tool_action_enum('line_shape', 'round') |
| 37 |
self.add_tool_action_enum('dashes-type', self._dashes_type) |
| 38 |
self.add_tool_action_boolean('pencil-outline', self._use_outline) |
| 39 |
|
| 40 |
def _set_active_shape(self, *args): |
| 41 |
state_as_string = self.get_option_value('line_shape') |
| 42 |
if state_as_string == 'thin': |
| 43 |
self._cap_id = cairo.LineCap.BUTT |
| 44 |
self._join_id = cairo.LineJoin.BEVEL |
| 45 |
self._shape_label = _("Square") |
| 46 |
else: |
| 47 |
self._cap_id = cairo.LineCap.ROUND |
| 48 |
self._join_id = cairo.LineJoin.ROUND |
| 49 |
self._shape_label = _("Round") |
| 50 |
|
| 51 |
def get_options_label(self): |
| 52 |
return _("Pencil options") |
| 53 |
|
| 54 |
def get_editing_tips(self): |
| 55 |
self._dashes_type = self.get_option_value('dashes-type') |
| 56 |
use_dashes = self._dashes_type != 'none' |
| 57 |
self._use_outline = self.get_option_value('pencil-outline') |
| 58 |
self._set_active_shape() |
| 59 |
|
| 60 |
if use_dashes: |
| 61 |
label_options = self.label + " - " + _("Dashed") |
| 62 |
else: |
| 63 |
label_options = None |
| 64 |
|
| 65 |
if self.get_image().get_mouse_is_pressed(): |
| 66 |
label_modifier_alt = None |
| 67 |
if label_options is None: |
| 68 |
label_options = self.label |
| 69 |
else: |
| 70 |
label_modifier_alt = self.label + " - " + \ |
| 71 |
_("Press <Alt> to toggle the 'outline' option") |
| 72 |
|
| 73 |
full_list = [label_options, label_modifier_alt] |
| 74 |
return list(filter(None, full_list)) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
def on_press_on_area(self, event, surface, event_x, event_y): |
| 79 |
self.set_common_values(event.button, event_x, event_y) |
| 80 |
self._path = None |
| 81 |
|
| 82 |
self.update_modifier_state(event.state) |
| 83 |
if 'ALT' in self._modifier_keys: |
| 84 |
self._use_outline = not self._use_outline |
| 85 |
|
| 86 |
def _add_point(self, event_x, event_y): |
| 87 |
cairo_context = self.get_context() |
| 88 |
if self._path is None: |
| 89 |
cairo_context.move_to(self.x_press, self.y_press) |
| 90 |
else: |
| 91 |
cairo_context.append_path(self._path) |
| 92 |
cairo_context.line_to(event_x, event_y) |
| 93 |
self._path = cairo_context.copy_path() |
| 94 |
|
| 95 |
def on_motion_on_area(self, event, surface, event_x, event_y, render=True): |
| 96 |
self._add_point(event_x, event_y) |
| 97 |
if not render: |
| 98 |
return |
| 99 |
operation = self.build_operation() |
| 100 |
self.do_tool_operation(operation) |
| 101 |
|
| 102 |
def on_release_on_area(self, event, surface, event_x, event_y): |
| 103 |
self._add_point(event_x, event_y) |
| 104 |
|
| 105 |
if self.x_press == event_x and self.y_press == event_y: |
| 106 |
|
| 107 |
|
| 108 |
float_x, float_y = self.get_image().get_event_coords(event, False) |
| 109 |
decimals_x = float_x - event_x |
| 110 |
if decimals_x > 0: |
| 111 |
delta_x = 0.20 |
| 112 |
else: |
| 113 |
delta_x = -0.20 |
| 114 |
decimals_y = float_y - event_y |
| 115 |
if decimals_y > 0: |
| 116 |
delta_y = 0.20 |
| 117 |
else: |
| 118 |
delta_y = -0.20 |
| 119 |
self._add_point(event_x + delta_x, event_y + delta_y) |
| 120 |
|
| 121 |
operation = self.build_operation() |
| 122 |
self.apply_operation(operation) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
def build_operation(self): |
| 127 |
operation = { |
| 128 |
'tool_id': self.id, |
| 129 |
'rgba': self.main_color, |
| 130 |
'rgba2': self.secondary_color, |
| 131 |
'antialias': self._use_antialias, |
| 132 |
'smooth': not self.get_image().is_zoomed_surface_sharp(), |
| 133 |
'outline': self._use_outline, |
| 134 |
'operator': self._operator, |
| 135 |
'line_width': self.tool_width, |
| 136 |
'line_cap': self._cap_id, |
| 137 |
'line_join': self._join_id, |
| 138 |
'dashes': self._dashes_type, |
| 139 |
'path': self._path |
| 140 |
} |
| 141 |
return operation |
| 142 |
|
| 143 |
def do_tool_operation(self, operation): |
| 144 |
if operation['path'] is None: |
| 145 |
return |
| 146 |
cairo_context = self.start_tool_operation(operation) |
| 147 |
|
| 148 |
cairo_context.set_operator(operation['operator']) |
| 149 |
line_width = operation['line_width'] |
| 150 |
self.set_dashes_and_cap(cairo_context, line_width, \ |
| 151 |
operation['dashes'], operation['line_cap']) |
| 152 |
cairo_context.set_line_join(operation['line_join']) |
| 153 |
|
| 154 |
if operation['smooth']: |
| 155 |
utilities_smooth_path(cairo_context, operation['path']) |
| 156 |
else: |
| 157 |
cairo_context.append_path(operation['path']) |
| 158 |
|
| 159 |
if operation['outline']: |
| 160 |
cairo_context.set_source_rgba(*operation['rgba2']) |
| 161 |
cairo_context.set_line_width(line_width * 1.2 + 2) |
| 162 |
cairo_context.stroke_preserve() |
| 163 |
|
| 164 |
cairo_context.set_source_rgba(*operation['rgba']) |
| 165 |
cairo_context.set_line_width(line_width) |
| 166 |
cairo_context.stroke() |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|