| 1 |
|
| 2 |
|
| 3 |
import cairo, math |
| 4 |
from gi.repository import Gdk, GdkPixbuf |
| 5 |
from .selection_manager import NoSelectionPathException |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
def utilities_show_overlay_on_context(ccontext, cpath, thickness=1): |
| 11 |
"""Draw a blueish area on `ccontext`, for the selection.""" |
| 12 |
if cpath is None: |
| 13 |
raise NoSelectionPathException() |
| 14 |
ccontext.new_path() |
| 15 |
ccontext.set_antialias(cairo.Antialias.NONE) |
| 16 |
ccontext.set_line_width(thickness) |
| 17 |
ccontext.set_dash([thickness * 3, thickness * 3]) |
| 18 |
ccontext.append_path(cpath) |
| 19 |
ccontext.set_source_rgba(0.1, 0.1, 0.3, 0.2) |
| 20 |
ccontext.fill_preserve() |
| 21 |
ccontext.set_source_rgba(0.5, 0.5, 0.5, 0.5) |
| 22 |
ccontext.stroke() |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
def utilities_show_handles_on_context(cairo_context, x1, x2, y1, y2, thickness=1): |
| 28 |
"""Request the drawing of handles for a rectangle pixbuf having the provided |
| 29 |
coords. Handles are only decorative objects drawn on the surface to help the |
| 30 |
user understand the rationale of tools without relying on the mouse cursor.""" |
| 31 |
radius = _get_radius((x2 - x1), (y2 - y1), thickness) |
| 32 |
lateral_handles = True |
| 33 |
if radius < 4 * thickness: |
| 34 |
cairo_context.set_line_width(thickness) |
| 35 |
elif radius < 8 * thickness: |
| 36 |
cairo_context.set_line_width(2 * thickness) |
| 37 |
else: |
| 38 |
cairo_context.set_line_width(3 * thickness) |
| 39 |
|
| 40 |
_draw_arc_handle(cairo_context, x1, y1, radius, 'nw') |
| 41 |
_draw_arc_handle(cairo_context, x2, y1, radius, 'ne') |
| 42 |
_draw_arc_handle(cairo_context, x2, y2, radius, 'se') |
| 43 |
_draw_arc_handle(cairo_context, x1, y2, radius, 'sw') |
| 44 |
if lateral_handles: |
| 45 |
_draw_arc_handle(cairo_context, (x1+x2)/2, y1, radius, 'n') |
| 46 |
_draw_arc_handle(cairo_context, x2, (y1+y2)/2, radius, 'e') |
| 47 |
_draw_arc_handle(cairo_context, (x1+x2)/2, y2, radius, 's') |
| 48 |
_draw_arc_handle(cairo_context, x1, (y1+y2)/2, radius, 'w') |
| 49 |
|
| 50 |
cairo_context.move_to(x1, y1) |
| 51 |
cairo_context.line_to(x1, y2) |
| 52 |
cairo_context.line_to(x2, y2) |
| 53 |
cairo_context.line_to(x2, y1) |
| 54 |
cairo_context.close_path() |
| 55 |
|
| 56 |
cairo_context.set_line_width(thickness) |
| 57 |
cairo_context.set_dash([2 * thickness, 2 * thickness]) |
| 58 |
cairo_context.set_source_rgba(0.5, 0.5, 0.5, 0.5) |
| 59 |
cairo_context.stroke() |
| 60 |
|
| 61 |
def _draw_arc_handle(cairo_context, x, y, radius, orientation): |
| 62 |
"""Draw a moon-like shape with the given orientation and radius. The coords |
| 63 |
are the center of the shape. The orientation is an enumeration provided as a |
| 64 |
string with the stupid n/nw/w/sw/s/se/e/ne format.""" |
| 65 |
if orientation == 'nw': |
| 66 |
angle_1 = 0.5 * math.pi |
| 67 |
angle_2 = 2.0 * math.pi |
| 68 |
elif orientation == 'n': |
| 69 |
angle_1 = math.pi |
| 70 |
angle_2 = 0.0 |
| 71 |
elif orientation == 'ne': |
| 72 |
angle_1 = math.pi |
| 73 |
angle_2 = 0.5 * math.pi |
| 74 |
elif orientation == 'e': |
| 75 |
angle_1 = -0.5 * math.pi |
| 76 |
angle_2 = 0.5 * math.pi |
| 77 |
elif orientation == 'se': |
| 78 |
angle_1 = -0.5 * math.pi |
| 79 |
angle_2 = math.pi |
| 80 |
elif orientation == 's': |
| 81 |
angle_1 = 0.0 |
| 82 |
angle_2 = math.pi |
| 83 |
elif orientation == 'sw': |
| 84 |
angle_1 = 0.0 |
| 85 |
angle_2 = -0.5 * math.pi |
| 86 |
elif orientation == 'w': |
| 87 |
angle_1 = 0.5 * math.pi |
| 88 |
angle_2 = -0.5 * math.pi |
| 89 |
|
| 90 |
cairo_context.move_to(x, y) |
| 91 |
cairo_context.arc(x, y, radius, angle_1, angle_2) |
| 92 |
cairo_context.close_path() |
| 93 |
cairo_context.set_source_rgba(1.0, 1.0, 1.0, 1.0) |
| 94 |
cairo_context.fill_preserve() |
| 95 |
cairo_context.set_source_rgba(0.5, 0.5, 0.5, 0.5) |
| 96 |
cairo_context.stroke() |
| 97 |
|
| 98 |
def _get_radius(dx, dy, thickness): |
| 99 |
return min([dx/5, dy/5, 12 * thickness]) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
def utilities_generic_canvas_outline(cairo_context, zoom_level, w, h): |
| 105 |
cairo_context.set_source_rgba(0.0, 0.0, 0.0, 1.0) |
| 106 |
cairo_context.set_dash([]) |
| 107 |
size = max(1, int(1 / zoom_level)) |
| 108 |
cairo_context.set_line_width(size) |
| 109 |
cairo_context.move_to(w + size, 0) |
| 110 |
cairo_context.rel_line_to(0, h + size) |
| 111 |
cairo_context.line_to(0, h + size) |
| 112 |
cairo_context.stroke_preserve() |
| 113 |
|
| 114 |
cairo_context.set_source_rgba(1.0, 1.0, 1.0, 1.0) |
| 115 |
cairo_context.set_dash([2 * size, 2 * size]) |
| 116 |
cairo_context.stroke() |
| 117 |
|
| 118 |
|
| 119 |
|