Debian Codegraph @

traitsui/examples/demo/Advanced/Auto_editable_readonly_table_cells.py

raw
1 # (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 """
12 This example shows how to define a read-only, auto-edit table column using a
13 custom pop-up view.
14
15 The example displays a list of integer values from 1 to n, where 'n' can be set
16 using the slider at the top of the view. Each entry in the list shows the value
17 of the integer and the number of unique factors it has.
18
19 Mousing over the number of factors for a particular integer displays a pop-up
20 list containing the unique factors for the integer. Mousing out of the cell
21 causes the pop-up list to be removed (and perhaps causes a new pop-up list to
22 be displayed, depending upon whether the mouse entered a new cell or not).
23
24 Creating the auto pop-up effect is achieved by setting the 'auto_editable' trait
25 of the associated ObjectColumn to True and also specifying a view to display
26 on mouse over as the value of the ObjectColumn's 'view' trait.
27
28 Note that this style of auto pop-up view can only be used with non-editable
29 table editor fields. If the field is editable, then setting 'auto_editable' to
30 True will cause the editor associated with the ObjectColumn to be automatically
31 activated on mouse over, rather than the pop-up view specified by the 'view'
32 trait.
33 """
34
35 # -- Imports --------------------------------------------------------------
36
37
38 from operator import attrgetter
39
40 from traits.api import (
41 HasTraits,
42 Int,
43 List,
44 Range,
45 Property,
46 cached_property,
47 )
48
49 from traitsui.api import View, VGroup, Item, TableEditor
50
51 from traitsui.table_column import ObjectColumn
52
53 # -- Integer Class --------------------------------------------------------
54
55
56 class Integer(HasTraits):
57
58 # The value:
59 n = Int()
60
61
62 # -- Factor Class ---------------------------------------------------------
63
64
65 class Factor(HasTraits):
66
67 # The number being factored:
68 n = Int()
69
70 # The list of factors of 'n':
71 factors = Property(List, observe='n')
72
73 @cached_property
74 def _get_factors(self):
75 n = self.n
76 i = 1
77 result = []
78
79 while (i * i) <= n:
80 j = n // i
81 if (i * j) == n:
82 result.append(Integer(n=i))
83 if i != j:
84 result.append(Integer(n=j))
85 i += 1
86
87 result.sort(key=attrgetter('n'))
88
89 return result
90
91
92 # -- The table editor used for the pop-up view ----------------------------
93
94 factor_table_editor = TableEditor(
95 columns=[
96 ObjectColumn(
97 name='n', width=1.0, editable=False, horizontal_alignment='center'
98 )
99 ],
100 sortable=False,
101 auto_size=False,
102 show_toolbar=False,
103 show_column_labels=False,
104 )
105
106 # -- The table editor used for the main view ------------------------------
107
108 factors_view = View(
109 Item(
110 'factors',
111 id='factors',
112 show_label=False,
113 editor=factor_table_editor,
114 ),
115 id='traits.examples.demo.Advanced.factors_view',
116 kind='info',
117 height=0.30,
118 )
119
120 factors_table_editor = TableEditor(
121 columns=[
122 ObjectColumn(
123 name='n', width=0.5, editable=False, horizontal_alignment='center'
124 ),
125 ObjectColumn(
126 name='factors',
127 width=0.5,
128 editable=False,
129 horizontal_alignment='center',
130 auto_editable=True,
131 format_func=lambda f: '%s factors' % len(f),
132 view=factors_view,
133 ),
134 ],
135 sortable=False,
136 auto_size=False,
137 show_toolbar=False,
138 )
139
140 # -- Factors Class --------------------------------------------------------
141
142
143 class Factors(HasTraits):
144
145 # The maximum number to include in the table:
146 max_n = Range(1, 1000, 20, mode='slider')
147
148 # The list of Factor objects:
149 factors = Property(List, observe='max_n')
150
151 # The view of the list of Factor objects:
152 view = View(
153 VGroup(
154 VGroup(
155 Item('max_n'),
156 show_labels=False,
157 show_border=True,
158 label='Maximum Number',
159 ),
160 VGroup(
161 Item('factors', show_label=False, editor=factors_table_editor),
162 ),
163 ),
164 title='List of numbers and their factors',
165 width=0.2,
166 height=0.4,
167 resizable=True,
168 )
169
170 @cached_property
171 def _get_factors(self):
172 return [Factor(n=i + 1) for i in range(self.max_n)]
173
174
175 # -- Create and run the demo ----------------------------------------------
176
177 # Create the demo:
178 demo = Factors()
179
180 # Run the demo (if invoked from the command line):
181 if __name__ == '__main__':
182 demo.configure_traits()