File: tool_bar_manager.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (220 lines) | stat: -rw-r--r-- 7,598 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
213
214
215
216
217
218
219
220
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in 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!
# (C) Copyright 2007 Riverbank Computing Limited
# This software is provided without warranty under the terms of the BSD license.
# However, when used with the GPL version of PyQt the additional terms described in the PyQt GPL exception also apply

# ------------------------------------------------------------------------------


from pyface.qt import QtCore, QtGui


from traits.api import Bool, Instance, List, Str, Tuple, provides


from pyface.image_cache import ImageCache
from pyface.action.action_manager import ActionManager
from pyface.action.i_tool_bar_manager import IToolBarManager
from pyface.ui_traits import Orientation


@provides(IToolBarManager)
class ToolBarManager(ActionManager):
    """ A tool bar manager realizes itself in errr, a tool bar control. """

    # 'ToolBarManager' interface -------------------------------------------

    # Is the tool bar enabled?
    enabled = Bool(True)

    # Is the tool bar visible?
    visible = Bool(True)

    # The size of tool images (width, height).
    image_size = Tuple((16, 16))

    # The toolbar name (used to distinguish multiple toolbars).
    name = Str("ToolBar")

    # The orientation of the toolbar.
    orientation = Orientation("horizontal")

    # Should we display the name of each tool bar tool under its image?
    show_tool_names = Bool(True)

    # Should we display the horizontal divider?
    show_divider = Bool(True)

    # Private interface ----------------------------------------------------

    # Cache of tool images (scaled to the appropriate size).
    _image_cache = Instance(ImageCache)

    #: Keep track of all created toolbars in order to properly dispose of them
    _toolbars = List()

    # ------------------------------------------------------------------------
    # 'object' interface.
    # ------------------------------------------------------------------------

    def __init__(self, *args, **traits):
        """ Creates a new tool bar manager. """

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

        # An image cache to make sure that we only load each image used in the
        # tool bar exactly once.
        self._image_cache = ImageCache(self.image_size[0], self.image_size[1])

        return

    # ------------------------------------------------------------------------
    # 'ToolBarManager' interface.
    # ------------------------------------------------------------------------

    def create_tool_bar(self, parent, controller=None):
        """ Creates a tool bar. """

        # If a controller is required it can either be set as a trait on the
        # tool bar manager (the trait is part of the 'ActionManager' API), or
        # passed in here (if one is passed in here it takes precedence over the
        # trait).
        if controller is None:
            controller = self.controller

        # Create the control.
        tool_bar = _ToolBar(self, parent)
        self._toolbars.append(tool_bar)
        tool_bar.setObjectName(self.id)
        tool_bar.setWindowTitle(self.name)

        if self.show_tool_names:
            tool_bar.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonTextUnderIcon)

        if self.orientation == "horizontal":
            tool_bar.setOrientation(QtCore.Qt.Orientation.Horizontal)
        else:
            tool_bar.setOrientation(QtCore.Qt.Orientation.Vertical)

        # We would normally leave it to the current style to determine the icon
        # size.
        w, h = self.image_size
        tool_bar.setIconSize(QtCore.QSize(w, h))

        # Add all of items in the manager's groups to the tool bar.
        self._qt4_add_tools(parent, tool_bar, controller)

        return tool_bar

    # ------------------------------------------------------------------------
    # 'ActionManager' interface.
    # ------------------------------------------------------------------------

    def destroy(self):
        while self._toolbars:
            toolbar = self._toolbars.pop()
            toolbar.dispose()

        super().destroy()

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    def _qt4_add_tools(self, parent, tool_bar, controller):
        """ Adds tools for all items in the list of groups. """

        previous_non_empty_group = None
        for group in self.groups:
            if len(group.items) > 0:
                # Is a separator required?
                if previous_non_empty_group is not None and group.separator:
                    separator = tool_bar.addSeparator()
                    group.observe(
                        self._separator_visibility_method(separator), "visible"
                    )

                previous_non_empty_group = group

                # Create a tool bar tool for each item in the group.
                for item in group.items:
                    item.add_to_toolbar(
                        parent,
                        tool_bar,
                        self._image_cache,
                        controller,
                        self.show_tool_names,
                    )

    def _separator_visibility_method(self, separator):
        """ Method to return closure to set visibility of group separators. """
        return lambda event: separator.setVisible(event.new)


class _ToolBar(QtGui.QToolBar):
    """ The toolkit-specific tool bar implementation. """

    # ------------------------------------------------------------------------
    # 'object' interface.
    # ------------------------------------------------------------------------

    def __init__(self, tool_bar_manager, parent):
        """ Constructor. """

        QtGui.QToolBar.__init__(self, parent)

        # List of tools
        self.tools = []

        # Listen for changes to the tool bar manager's enablement and
        # visibility.
        self.tool_bar_manager = tool_bar_manager

        self.tool_bar_manager.observe(
            self._on_tool_bar_manager_enabled_changed, "enabled"
        )

        self.tool_bar_manager.observe(
            self._on_tool_bar_manager_visible_changed, "visible"
        )

        return

    def dispose(self):
        self.tool_bar_manager.observe(
            self._on_tool_bar_manager_enabled_changed, "enabled", remove=True
        )
        self.tool_bar_manager.observe(
            self._on_tool_bar_manager_visible_changed, "visible", remove=True
        )
        # Removes event listeners from downstream tools and clears their
        # references
        for item in self.tools:
            item.dispose()

        self.tools = []

    # ------------------------------------------------------------------------
    # Trait change handlers.
    # ------------------------------------------------------------------------

    def _on_tool_bar_manager_enabled_changed(self, event):
        """ Dynamic trait change handler. """

        self.setEnabled(event.new)

    def _on_tool_bar_manager_visible_changed(self, event):
        """ Dynamic trait change handler. """

        self.setVisible(event.new)

        return