File: annotater.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (77 lines) | stat: -rw-r--r-- 2,689 bytes parent folder | download | duplicates (3)
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
"""
Define an Annotater component that allows a user to annotate an underlying
component
"""

from __future__ import with_statement

from traits.api import Event, Trait, TraitPrefixList
from traitsui.api import Group, View

from enable.api import Component
from enable.colors import ColorTrait


class Annotater(Component):

    color = ColorTrait((0.0, 0.0, 0.0, 0.2 ))
    style = Trait("rectangular", TraitPrefixList(["rectangular", 'freehand']))
    annotation = Event

    traits_view = View(Group('<component>', id = 'component'),
                       Group('<links>', id = 'links'),
                       Group('color', 'style', id = 'annotater',
                             style = 'custom'))


    #---------------------------------------------------------------------------
    # Mouse event handlers
    #---------------------------------------------------------------------------

    def _left_down_changed(self, event):
        event.handled = True
        self.window.mouse_owner = self
        self._cur_x, self._cur_y = event.x, event.y
        self._start_x, self._start_y = event.x, event.y
        return

    def _left_up_changed(self, event):
        event.handled = True
        self.window.mouse_owner = None
        if self.xy_in_bounds(event):
            self.annotation = (min(self._start_x, event.x),
                               min(self._start_y, event.y),
                               abs(self._start_x - event.x),
                               abs(self._start_y - event.y))
        self._start_x = self._start_y = self._cur_x = self._cur_y = None
        self.redraw()
        return

    def _mouse_move_changed(self, event):
        event.handled = True
        if self._start_x is not None:
            x = max(min(event.x, self.right - 1.0), self.x)
            y = max(min(event.y, self.top - 1.0), self.y)
            if (x != self._cur_x) or (y != self._cur_y):
                self._cur_x, self._cur_y = x, y
                self.redraw()
        return

    #---------------------------------------------------------------------------
    # "Component" interface
    #---------------------------------------------------------------------------

    def _draw (self, gc):
        "Draw the contents of the control"
        if self._start_x is not None:
            with gc:
                gc.set_fill_color(self.color_)
                gc.begin_path()
                gc.rect(min(self._start_x, self._cur_x),
                         min(self._start_y, self._cur_y),
                         abs(self._start_x - self._cur_x),
                         abs(self._start_y - self._cur_y))
                gc.fill_path()
        return

# EOF