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
|
# (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!
""" A UI that allows the user to choose a view. """
from pyface.workbench.api import IView, WorkbenchWindow
from traits.api import Any, HasTraits, Instance, List, Str
from traits.api import TraitError, Undefined
from traitsui.api import Item, TreeEditor, TreeNode, View
from traitsui.menu import Action # fixme: Non-api import!
class Category(HasTraits):
""" A view category. """
# The name of the category.
name = Str()
# The views in the category.
views = List()
class WorkbenchWindowTreeNode(TreeNode):
""" A tree node for workbench windows that displays the window's views.
The views are grouped by their category.
"""
# 'TreeNode' interface -------------------------------------------------
# List of object classes that the node applies to.
node_for = [WorkbenchWindow]
# ------------------------------------------------------------------------
# 'TreeNode' interface.
# ------------------------------------------------------------------------
def get_children(self, object):
""" Get the object's children. """
# Collate the window's views into categories.
categories_by_name = self._get_categories_by_name(object)
categories = list(categories_by_name.values())
categories.sort(key=lambda category: category.name)
return categories
# ------------------------------------------------------------------------
# Private interface.
# ------------------------------------------------------------------------
def _get_categories_by_name(self, window):
""" Return a dictionary containing all categories keyed by name. """
categories_by_name = {}
for view in window.views:
category = categories_by_name.get(view.category)
if category is None:
category = Category(name=view.category)
categories_by_name[view.category] = category
category.views.append(view)
return categories_by_name
class IViewTreeNode(TreeNode):
""" A tree node for objects that implement the 'IView' interface.
This node does *not* recognise objects that can be *adapted* to the 'IView'
interface, only those that actually implement it. If we wanted to allow
for adaptation we would have to work out a way for the rest of the
'TreeNode' code to access the adapter, not the original object. We could,
of course override every method, but that seems a little, errr, tedious.
We could probably do with something like in the Pyface tree where there
is a method that returns the actual object that we want to manipulate.
"""
def is_node_for(self, obj):
""" Returns whether this is the node that handles a specified object.
"""
# By checking for 'is obj' here, we are *not* allowing adaptation (if
# we were allowing adaptation it would be 'is not None'). See the class
# doc string for details.
return IView(obj, Undefined) is obj
def get_icon(self, obj, is_expanded):
""" Returns the icon for a specified object. """
if obj.image is not None:
icon = obj.image
else:
# fixme: A bit of magic here! Is there a better way to say 'use
# the default leaf icon'?
icon = "<item>"
return icon
class ViewChooser(HasTraits):
""" Allow the user to choose a view.
This implementation shows views in a tree grouped by category.
"""
# The window that contains the views to choose from.
window = Instance("pyface.workbench.api.WorkbenchWindow")
# The currently selected tree item (at any point in time this might be
# either None, a view category, or a view).
selected = Any()
# The selected view (None if the selected item is not a view).
view = Instance(IView)
# Traits UI views -----------------------------------------------------#
traits_ui_view = View(
Item(
name="window",
editor=TreeEditor(
nodes=[
WorkbenchWindowTreeNode(
auto_open=True,
label="=Views",
rename=False,
copy=False,
delete=False,
insert=False,
menu=None,
),
TreeNode(
node_for=[Category],
auto_open=True,
children="views",
label="name",
rename=False,
copy=False,
delete=False,
insert=False,
menu=None,
),
IViewTreeNode(
auto_open=False,
label="name",
rename=False,
copy=False,
delete=False,
insert=False,
menu=None,
),
],
editable=False,
hide_root=True,
selected="selected",
show_icons=True,
),
show_label=False,
),
buttons=[Action(name="OK", enabled_when="view is not None"), "Cancel"],
resizable=True,
style="custom",
title="Show View",
width=0.2,
height=0.4,
)
# ------------------------------------------------------------------------
# 'ViewChooser' interface.
# ------------------------------------------------------------------------
def _selected_changed(self, old, new):
""" Static trait change handler. """
# If the assignment fails then the selected object does *not* implement
# the 'IView' interface.
try:
self.view = new
except TraitError:
self.view = None
return
|