File: tool_palette.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (212 lines) | stat: -rw-r--r-- 6,254 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
# 
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" View of an ActionManager drawn as a rectangle of buttons. """

import wx
from enthought.pyface.widget import Widget

from enthought.traits.api import Bool, Dict, Int, List, Tuple
# HTML templates.
# FIXME : Not quite the right color.
HTML = """

<html>
  <body bgcolor='#cccccc'>
    %s
  </body>
</html>

"""

PART = """<wxp module="wx" class="Panel"><param name="id" value="%s"><param name="size" value="%s"></wxp>"""


class ToolPalette(Widget):

    tools = List
    
    id_tool_map = Dict

    tool_id_to_button_map = Dict

    button_size = Tuple((25, 25), Int, Int)

    is_realized = Bool(False)

    tool_listeners = Dict

    # Maps a button id to its tool id.
    button_tool_map = Dict
    
    ###########################################################################
    # 'object' interface. 
    ###########################################################################

    def __init__(self, parent, **traits):
        """ Creates a new tool palette. """

        # Base class constructor.
        super(ToolPalette, self).__init__(**traits)

        # Create the toolkit-specific control that represents the widget.
        self.control = self._create_control(parent)

        return

    ###########################################################################
    # ToolPalette interface.
    ###########################################################################

    def add_tool(self, label, bmp, kind, tooltip, longtip):
        """ Add a tool with the specified properties to the palette.

        Return an id that can be used to reference this tool in the future.
        """

        wxid = wx.NewId()
        params = (wxid, label, bmp, kind, tooltip, longtip)
        self.tools.append(params)
        self.id_tool_map[wxid] = params

        if self.is_realized:
            self._reflow()

        return wxid

    def toggle_tool(self, id, checked):
        """ Toggle the tool identified by 'id' to the 'checked' state.

        If the button is a toggle or radio button, the button will be checked
        if the 'checked' parameter is True; unchecked otherwise.  If the button
        is a standard button, this method is a NOP.
        """
        
        button = self.tool_id_to_button_map.get(id, None)
        if button is not None and hasattr(button, 'SetToggle'):
            button.SetToggle(checked)

        return

    def enable_tool(self, id, enabled):
        """ Enable or disable the tool identified by 'id'. """
        
        button = self.tool_id_to_button_map.get(id, None)
        if button is not None:
            button.SetEnabled(enabled)

        return

    def on_tool_event(self, id, callback):
        """ Register a callback for events on the tool identified by 'id'. """

        callbacks = self.tool_listeners.setdefault(id, [])
        callbacks.append(callback)

        return

    def realize(self):
        """ Realize the control so that it can be displayed. """
        
        self.is_realized = True
        self._reflow()

        return

    def get_tool_state(self, id):
        """ Get the toggle state of the tool identified by 'id'. """

        button = self.tool_id_to_button_map.get(id, None)
        if hasattr(button, 'GetToggle'):
            if button.GetToggle():
                state = 1
            else:
                state = 0
        else:
            state = 0

        return state
            
    
    ###########################################################################
    # Private interface.
    ###########################################################################
    
    def _create_control(self, parent):

        html_window = wx.html.HtmlWindow(parent, -1, style=wx.CLIP_CHILDREN)
        
        return html_window


    def _reflow(self):
        """ Reflow the layout. """


        # Create a bit of html for each tool.
        parts = []
        for param in self.tools:
            parts.append(PART % (str(param[0]), self.button_size))

        # Create the entire html page.
        html = HTML % ''.join(parts)

        # Set the HTML on the widget.  This will create all of the buttons.
        self.control.SetPage(html)

        for param in self.tools:
            self._initialize_tool(param)

        return

    def _initialize_tool(self, param):
        """ Initialize the tool palette button. """

        wxid, label, bmp, kind, tooltip, longtip = param
        
        panel = self.control.FindWindowById(wxid)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(sizer)
        panel.SetAutoLayout(True)
        panel.SetWindowStyleFlag(wx.CLIP_CHILDREN)
        
        from wx.lib.buttons import GenBitmapToggleButton, GenBitmapButton

        if kind == 'radio':
            button = GenBitmapToggleButton(panel, -1, None, size=self.button_size)

        else:
            button = GenBitmapButton(panel, -1, None, size=self.button_size)

        self.button_tool_map[button.GetId()] = wxid
        self.tool_id_to_button_map[wxid] = button
        wx.EVT_BUTTON(panel, button.GetId(), self._on_button)
        button.SetBitmapLabel(bmp)
        button.SetToolTipString(label)
        sizer.Add(button, 0, wx.EXPAND)

        
        return

    def _on_button(self, event):
        
        button_id = event.GetId()
        tool_id = self.button_tool_map.get(button_id, None)
        if tool_id is not None:
            for listener in self.tool_listeners.get(tool_id, []):
                listener(event)

        return
        
#### EOF ######################################################################