| 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 |
|
| 21 |
class ToolPoints(AbstractClassicTool): |
| 22 |
__gtype_name__ = 'ToolPoints' |
| 23 |
|
| 24 |
def __init__(self, window, **kwargs): |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
super().__init__('points', _("Points"), 'tool-points-symbolic', window) |
| 31 |
|
| 32 |
self.add_tool_action_enum('points_type', 'cross') |
| 33 |
self.add_tool_action_boolean('points_number', False) |
| 34 |
self.add_tool_action_simple('points_reset_num', self._reset_number) |
| 35 |
self.add_tool_action_simple('points_inc_num', self._increment_number) |
| 36 |
self.add_tool_action_simple('points_dec_num', self._decrement_number) |
| 37 |
|
| 38 |
self._next_number = 1 |
| 39 |
self._set_active_type() |
| 40 |
|
| 41 |
def _set_active_type(self): |
| 42 |
state_as_string = self.get_option_value('points_type') |
| 43 |
if state_as_string == 'cross': |
| 44 |
self._shape_label = _("Cross") |
| 45 |
elif state_as_string == 'x-cross': |
| 46 |
self._shape_label = _("X-shaped cross") |
| 47 |
elif state_as_string == 'square': |
| 48 |
self._shape_label = _("Square") |
| 49 |
else: |
| 50 |
self._shape_label = _("Circle") |
| 51 |
self._points_type = state_as_string |
| 52 |
|
| 53 |
self._use_number = self.get_option_value('points_number') |
| 54 |
self.set_action_sensitivity('points_reset_num', self._use_number) |
| 55 |
self.set_action_sensitivity('points_inc_num', self._use_number) |
| 56 |
self.set_action_sensitivity('points_dec_num', self._use_number) |
| 57 |
|
| 58 |
def _reset_number(self, *args): |
| 59 |
self._next_number = 1 |
| 60 |
self.window.set_window_subtitles() |
| 61 |
|
| 62 |
def _increment_number(self, *args): |
| 63 |
self._next_number += 1 |
| 64 |
self.window.set_window_subtitles() |
| 65 |
|
| 66 |
def _decrement_number(self, *args): |
| 67 |
self._next_number -= 1 |
| 68 |
self.window.set_window_subtitles() |
| 69 |
|
| 70 |
def get_options_label(self): |
| 71 |
return _("Points options") |
| 72 |
|
| 73 |
def get_editing_tips(self): |
| 74 |
self._set_active_type() |
| 75 |
label = self.label + " - " + self._shape_label |
| 76 |
if self._use_number: |
| 77 |
label += " - " + _("Next number: %s") % self._next_number |
| 78 |
|
| 79 |
return [label] |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
def on_press_on_area(self, event, surface, event_x, event_y): |
| 84 |
self.set_common_values(event.button, event_x, event_y) |
| 85 |
|
| 86 |
def on_motion_on_area(self, event, surface, event_x, event_y, render=True): |
| 87 |
if render: |
| 88 |
operation = self.build_operation(event_x, event_y) |
| 89 |
self.do_tool_operation(operation) |
| 90 |
|
| 91 |
def on_release_on_area(self, event, surface, event_x, event_y): |
| 92 |
operation = self.build_operation(event_x, event_y) |
| 93 |
self.apply_operation(operation) |
| 94 |
if self._use_number: |
| 95 |
self._increment_number() |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
def build_operation(self, event_x, event_y): |
| 100 |
if self._use_number: |
| 101 |
number = self._next_number |
| 102 |
else: |
| 103 |
number = None |
| 104 |
operation = { |
| 105 |
'tool_id': self.id, |
| 106 |
'rgba': self.main_color, |
| 107 |
'rgba2': self.secondary_color, |
| 108 |
'antialias': self._use_antialias, |
| 109 |
'line_width': self.tool_width, |
| 110 |
'point_type': self._points_type, |
| 111 |
'number': number, |
| 112 |
'x': event_x, |
| 113 |
'y': event_y, |
| 114 |
} |
| 115 |
return operation |
| 116 |
|
| 117 |
def do_tool_operation(self, operation): |
| 118 |
cairo_context = self.start_tool_operation(operation) |
| 119 |
cairo_context.set_line_cap(cairo.LineCap.BUTT) |
| 120 |
cairo_context.set_source_rgba(*operation['rgba']) |
| 121 |
|
| 122 |
point_width = operation['line_width'] |
| 123 |
line_width = max(1, int(point_width / 4)) |
| 124 |
half_width = max(1, int(point_width / 2)) |
| 125 |
cairo_context.set_line_width(line_width) |
| 126 |
|
| 127 |
x = operation['x'] |
| 128 |
y = operation['y'] |
| 129 |
|
| 130 |
point_type = operation['point_type'] |
| 131 |
if point_type == 'circle': |
| 132 |
cairo_context.arc(x, y, half_width, 0.0, 2 * math.pi) |
| 133 |
cairo_context.fill() |
| 134 |
elif point_type == 'cross': |
| 135 |
|
| 136 |
cairo_context.move_to(x, y - half_width) |
| 137 |
cairo_context.line_to(x, y + half_width) |
| 138 |
cairo_context.move_to(x - half_width, y) |
| 139 |
cairo_context.line_to(x + half_width, y) |
| 140 |
elif point_type == 'x-cross': |
| 141 |
cairo_context.move_to(x - half_width, y - half_width) |
| 142 |
cairo_context.line_to(x + half_width, y + half_width) |
| 143 |
cairo_context.move_to(x + half_width, y - half_width) |
| 144 |
cairo_context.line_to(x - half_width, y + half_width) |
| 145 |
else: |
| 146 |
cairo_context.set_line_width(point_width) |
| 147 |
cairo_context.move_to(x, y - half_width) |
| 148 |
cairo_context.line_to(x, y + half_width) |
| 149 |
|
| 150 |
cairo_context.stroke() |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
number = operation['number'] |
| 155 |
if number is None: |
| 156 |
return |
| 157 |
|
| 158 |
|
| 159 |
cairo_context.set_font_size(max(1, int(point_width * 0.8))) |
| 160 |
|
| 161 |
|
| 162 |
if number < 10: |
| 163 |
num_x = x - line_width |
| 164 |
elif number < 100: |
| 165 |
num_x = x - line_width * 2 |
| 166 |
else: |
| 167 |
num_x = x - line_width * 3 |
| 168 |
|
| 169 |
num_y = y + line_width |
| 170 |
|
| 171 |
|
| 172 |
for dx in [-1, 1]: |
| 173 |
for dy in [-1, 1]: |
| 174 |
cairo_context.move_to(num_x + dx, num_y + dy) |
| 175 |
cairo_context.show_text(str(number)) |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
cairo_context.set_source_rgba(*operation['rgba2']) |
| 180 |
|
| 181 |
|
| 182 |
cairo_context.move_to(num_x, num_y) |
| 183 |
cairo_context.show_text(str(number)) |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|