File: demoitem.py

package info (click to toggle)
diacanvas2 0.14.4-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,640 kB
  • ctags: 2,553
  • sloc: sh: 157,554; ansic: 21,714; xml: 8,167; python: 668; makefile: 427
file content (106 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (2)
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
# <![CDATA[
"""Demo item for DiaCanvas2.
"""
# vim:sw=4:et

# generators can be used to iterate shapes and child objects
from __future__ import generators

import gobject
import diacanvas
import diacanvas.shape as shape

class DemoItem(diacanvas.CanvasElement, diacanvas.CanvasAbstractGroup):

    def __init__(self):
        self.__gobject_init__()

        # create our line shapes (a red and a green line)
        self.top_line = shape.Path()
        self.top_line.set_color(diacanvas.color(255, 0, 0))
        self.top_line.set_line_width(2)
        self.bottom_line = shape.Path()
        self.bottom_line.set_color(diacanvas.color(0, 255, 0))
        self.bottom_line.set_line_width(2)
        
        # create a text object (CanvasText is a composite object)
        self.text = diacanvas.CanvasText()
        # make the text a child of this canvas item
        self.add_construction(self.text)
        
    def on_update(self, affine):
        # create a line on the top
        # (line() takes one argument: a list of points)
        self.top_line.line([(0, 0), (self.width, 0)])

        # create a line on the bottom
        self.bottom_line.line([(0, self.height), (self.width, self.height)])

        # give the text the same width and height as out object
        self.text.set(width=self.width, height=self.height)
        
        # update the text
        self.update_child(self.text, affine)

        # update the parent
        diacanvas.CanvasElement.on_update(self, affine)

        # expand the boundries of this canvas item so the lines (with
        # width 2.0) are inside the boundries of this canvas item.
        self.expand_bounds(1.0)

    def on_shape_iter(self):
        # an iterator 
        yield self.top_line
        yield self.bottom_line
        # alternative:
        #return iter([self.top_line, self.bottom_line])

    def on_event(self, event):
        # make sure key events are send to our text object
        if event.type == diacanvas.EVENT_KEY_PRESS:
            self.text.focus()
            return self.text.on_event(event)
        else:
            return diacanvas.CanvasElement.on_event(self, event)

    # Groupable

    def on_groupable_add(self, item):
        """Add a new item. This is not allowed in this case.
        """
        return 0

    def on_groupable_remove(self, item):
        """Do not allow the text to be removed.
        """
        return 1

    def on_groupable_iter(self):
        """Return an iterator that can be used to traverse the children.
        """
        yield self.text
        # alternative:
        # return iter([self.text])

    def on_groupable_length(self):
        """Return the number of child objects, we have just the text object.
        """
        return 1

    def on_groupable_pos(self, item):
        """Return the position of the item wrt other child objects.
        (we have only one child).
        """
        if item == self.text:
            return 0
        else:
            return -1

gobject.type_register(DemoItem)
# add CanvasItem callbacks
diacanvas.set_callbacks(DemoItem)
# make the item groupable
diacanvas.set_groupable(DemoItem)

# end-of-file ]]>