~ubuntu-branches/ubuntu/oneiric/python-chaco/oneiric

« back to all changes in this revision

Viewing changes to chaco/selectable_legend.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 20:38:02 UTC
  • mfrom: (7.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110708203802-5t32e0ldv441yh90
Tags: 4.0.0-1
* New upstream release
* debian/control:
  - Depend on python-traitsui (Closes: #633604)
  - Bump Standards-Version to 3.9.2
* Update debian/watch file
* Remove debian/patches/* -- no longer needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from chaco.tools.api import SelectTool
 
3
from traits.api import List
 
4
 
 
5
from legend import Legend
 
6
 
 
7
class SelectableLegend(Legend, SelectTool):
 
8
 
 
9
    # A list of indices into self._cached_labels that indicates which labels
 
10
    # should be rendered in the "selected" style
 
11
    selections = List
 
12
 
 
13
    # A cached list of tuples (x,y,w,h) of each label's geometry
 
14
    _cached_label_dims = List
 
15
 
 
16
    #------------------------------------------------------------------------
 
17
    # Legend methods
 
18
    #------------------------------------------------------------------------
 
19
 
 
20
    def _do_layout(self):
 
21
        Legend._do_layout(self)
 
22
        self._compute_label_dims()
 
23
 
 
24
    def _compute_label_dims(self):
 
25
        dims = []
 
26
        edge_space = self.border_width + self.border_padding
 
27
        icon_width, icon_height = self.icon_bounds
 
28
 
 
29
        icon_x = self.x + edge_space
 
30
        text_x = icon_x + icon_width + self.icon_spacing
 
31
        y = self.y2 - edge_space
 
32
        for i, label_name in enumerate(self._cached_label_names):
 
33
            label_width, label_height = self._cached_label_sizes[i]
 
34
            y -= label_height
 
35
            icon_y = y + (label_height - icon_height) / 2
 
36
            dims.append((icon_x, icon_y, icon_width + self.icon_spacing + label_width,
 
37
                         label_height))
 
38
            y -= self.line_spacing
 
39
        self._cached_label_dims = dims
 
40
 
 
41
    #------------------------------------------------------------------------
 
42
    # SelectTool interface
 
43
    #------------------------------------------------------------------------
 
44
 
 
45
    def _get_selection_state(self, event):
 
46
        for ndx, dims in enumerate(self._cached_label_dims):
 
47
            x, y, w, h = dims
 
48
            if (x <= event.x <= x+w) and (y <= event.y <= y+h):
 
49
                return (ndx in self.selections), True
 
50
        else:
 
51
            if len(self._cached_label_dims) > 0:
 
52
                return (ndx in self.selections), False
 
53
            else:
 
54
                return False, False
 
55
 
 
56
    def _get_selection_token(self, event):
 
57
        for ndx, dims in enumerate(self._cached_label_dims):
 
58
            x, y, w, h = dims
 
59
            if (x <= event.x <= x+w) and (y <= event.y <= y+h):
 
60
                return ndx
 
61
        else:
 
62
            return None
 
63
 
 
64
    def _select(self, index, append=True):
 
65
        if append:
 
66
            self.selections.append(index)
 
67
        else:
 
68
            self.selections = [index]
 
69
        return
 
70
 
 
71
    def _deselect(self, index=None):
 
72
        if index in self.selections:
 
73
            self.selections.remove(index)
 
74
        return
 
75