/
processor_element.py
117 lines (94 loc) · 3.3 KB
/
processor_element.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
clutter/processor_element.py -- clutter backend for processor elements
Copyright (c) Bill Gribble <grib@billgribble.com>
"""
from gi.repository import Clutter
import cairo
from mfp import log
from ..colordb import ColorDB
from .base_element import ClutterBaseElementBackend
from ..processor_element import (
ProcessorElement,
ProcessorElementImpl,
)
class ClutterProcessorElementImpl(ProcessorElement, ProcessorElementImpl, ClutterBaseElementBackend):
backend_name = "clutter"
def __init__(self, window, x, y):
super().__init__(window, x, y)
# create elements
self.texture = Clutter.Canvas.new()
self.texture.connect("draw", self.draw_cb)
self.group.set_content(self.texture)
self.group.set_reactive(True)
# resize widget when text gets longer
self.handler_id = self.label.signal_listen('text-changed', self.label_changed_cb)
self.width = 35
self.height = 25
self.texture.set_size(self.width, self.height)
self.group.set_size(self.width, self.height)
self.group.set_position(x, y)
self.redraw()
async def delete(self, **kwargs):
if self.texture:
self.group.set_content(None)
self.texture = None
if self.label:
self.label.signal_unlisten(self.handler_id)
await self.label.delete()
self.label = None
await super().delete(**kwargs)
def redraw(self):
super().redraw()
self.texture.invalidate()
async def set_size(self, width, height):
await super().set_size(width, height)
self.texture.set_size(width, height)
async def label_changed_cb(self, *args):
newtext = self.label.get_text()
if newtext != self.label_text:
self.label_text = newtext
await self.update()
def select(self):
super().select()
if self.label:
self.label.set_color(self.get_color('text-color'))
if self.texture:
self.texture.invalidate()
def unselect(self):
super().unselect()
if self.label:
self.label.set_color(self.get_color('text-color'))
if self.texture:
self.texture.invalidate()
def draw_cb(self, texture, ct, width, height):
lw = 2.0
w = width - lw
h = height - lw
# clear the drawing area
ct.save()
ct.set_operator(cairo.OPERATOR_CLEAR)
ct.paint()
ct.restore()
ct.set_line_width(lw)
ct.set_antialias(cairo.ANTIALIAS_NONE)
ct.translate(lw/2.0, lw/2.0)
ct.move_to(0, 0)
ct.line_to(0, h)
ct.line_to(w, h)
ct.line_to(w, 0)
ct.line_to(0, 0)
ct.close_path()
# fill to paint the background
color = ColorDB().normalize(self.get_color('fill-color'))
ct.set_source_rgba(color.red, color.green, color.blue, color.alpha)
ct.fill_preserve()
# stroke to draw the outline
color = ColorDB().normalize(self.get_color('stroke-color'))
ct.set_source_rgba(color.red, color.green, color.blue, color.alpha)
if self.obj_state == self.OBJ_COMPLETE:
ct.set_dash([])
else:
ct.set_dash([8, 4])
ct.set_line_width(lw)
ct.stroke()
return True