| 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 |
""" Traits Test case |
| 12 |
""" |
| 13 |
|
| 14 |
# ------------------------------------------------------------------------- |
| 15 |
# Imports: |
| 16 |
# ------------------------------------------------------------------------- |
| 17 |
|
| 18 |
import wx |
| 19 |
|
| 20 |
from traits.api import ( |
| 21 |
Trait, |
| 22 |
HasTraits, |
| 23 |
Str, |
| 24 |
Int, |
| 25 |
Range, |
| 26 |
List, |
| 27 |
Event, |
| 28 |
File, |
| 29 |
Directory, |
| 30 |
Bool, |
| 31 |
) |
| 32 |
|
| 33 |
from traitsui.api import ( |
| 34 |
View, |
| 35 |
Handler, |
| 36 |
Item, |
| 37 |
CheckListEditor, |
| 38 |
ButtonEditor, |
| 39 |
FileEditor, |
| 40 |
DirectoryEditor, |
| 41 |
ImageEnumEditor, |
| 42 |
Color, |
| 43 |
Font, |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
# ------------------------------------------------------------------------- |
| 48 |
# Constants: |
| 49 |
# ------------------------------------------------------------------------- |
| 50 |
|
| 51 |
origin_values = ['top left', 'top right', 'bottom left', 'bottom right'] |
| 52 |
|
| 53 |
# ------------------------------------------------------------------------- |
| 54 |
# 'Instance' class: |
| 55 |
# ------------------------------------------------------------------------- |
| 56 |
|
| 57 |
|
| 58 |
class Instance(HasTraits): |
| 59 |
|
| 60 |
# ------------------------------------------------------------------------- |
| 61 |
# Trait definitions: |
| 62 |
# ------------------------------------------------------------------------- |
| 63 |
|
| 64 |
integer_text = Int(1) |
| 65 |
enumeration = Trait('one', 'two', 'three', 'four', 'five', 'six', cols=3) |
| 66 |
float_range = Range(0.0, 10.0, 10.0) |
| 67 |
int_range = Range(1, 5) |
| 68 |
boolean = Bool(True) |
| 69 |
|
| 70 |
view = View( |
| 71 |
'integer_text', 'enumeration', 'float_range', 'int_range', 'boolean' |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
# ------------------------------------------------------------------------- |
| 76 |
# 'TraitsTestHandler' class: |
| 77 |
# ------------------------------------------------------------------------- |
| 78 |
|
| 79 |
|
| 80 |
class TraitsTestHandler(Handler): |
| 81 |
def object_enabled_changed(self, info): |
| 82 |
enabled = info.object.enabled |
| 83 |
for i in range(1, 63): |
| 84 |
getattr(info, 'f%d' % i).enabled = enabled |
| 85 |
|
| 86 |
|
| 87 |
# ------------------------------------------------------------------------- |
| 88 |
# 'TraitsTest' class |
| 89 |
# ------------------------------------------------------------------------- |
| 90 |
|
| 91 |
|
| 92 |
class TraitsTest(HasTraits): |
| 93 |
|
| 94 |
# ------------------------------------------------------------------------- |
| 95 |
# Trait definitions: |
| 96 |
# ------------------------------------------------------------------------- |
| 97 |
|
| 98 |
enabled = Bool(True) |
| 99 |
integer_text = Int(1) |
| 100 |
enumeration = Trait('one', 'two', 'three', 'four', 'five', 'six', cols=3) |
| 101 |
float_range = Range(0.0, 10.0, 10.0) |
| 102 |
int_range = Range(1, 6) |
| 103 |
int_range2 = Range(1, 50) |
| 104 |
compound = Trait( |
| 105 |
1, Range(1, 6), 'one', 'two', 'three', 'four', 'five', 'six' |
| 106 |
) |
| 107 |
boolean = Bool(True) |
| 108 |
instance = Trait(Instance()) |
| 109 |
color = Color('cyan') |
| 110 |
font = Font() |
| 111 |
check_list = List( |
| 112 |
editor=CheckListEditor(values=['one', 'two', 'three', 'four'], cols=4) |
| 113 |
) |
| 114 |
list = List( |
| 115 |
Str, ['East of Eden', 'The Grapes of Wrath', 'Of Mice and Men'] |
| 116 |
) |
| 117 |
button = Event(0, editor=ButtonEditor(label='Click')) |
| 118 |
file = File() |
| 119 |
directory = Directory() |
| 120 |
image_enum = Trait( |
| 121 |
editor=ImageEnumEditor( |
| 122 |
values=origin_values, suffix='_origin', cols=4, klass=Instance |
| 123 |
), |
| 124 |
*origin_values, |
| 125 |
) |
| 126 |
|
| 127 |
# ------------------------------------------------------------------------- |
| 128 |
# View definitions: |
| 129 |
# ------------------------------------------------------------------------- |
| 130 |
|
| 131 |
view = View( |
| 132 |
( |
| 133 |
'|{Enum}', |
| 134 |
('enabled',), |
| 135 |
( |
| 136 |
'|<[Enumeration]', |
| 137 |
'f1:enumeration[Simple]', |
| 138 |
'_', |
| 139 |
'f2:enumeration[Custom]@', |
| 140 |
'_', |
| 141 |
'f3:enumeration[Text]*', |
| 142 |
'_', |
| 143 |
'f4:enumeration[Readonly]~', |
| 144 |
), |
| 145 |
( |
| 146 |
'|<[Check List]', |
| 147 |
'f5:check_list[Simple]', |
| 148 |
'_', |
| 149 |
'f6:check_list[Custom]@', |
| 150 |
'_', |
| 151 |
'f7:check_list[Text]*', |
| 152 |
'_', |
| 153 |
'f8:check_list[Readonly]~', |
| 154 |
), |
| 155 |
), |
| 156 |
( |
| 157 |
'|{Range}', |
| 158 |
( |
| 159 |
'|<[Float Range]', |
| 160 |
'f9:float_range[Simple]', |
| 161 |
'_', |
| 162 |
'f10:float_range[Custom]@', |
| 163 |
'_', |
| 164 |
'f11:float_range[Text]*', |
| 165 |
'_', |
| 166 |
'f12:float_range[Readonly]~', |
| 167 |
), |
| 168 |
( |
| 169 |
'|<[Int Range]', |
| 170 |
'f13:int_range[Simple]', |
| 171 |
'_', |
| 172 |
'f14:int_range[Custom]@', |
| 173 |
'_', |
| 174 |
'f15:int_range[Text]*', |
| 175 |
'_', |
| 176 |
'f16:int_range[Readonly]~', |
| 177 |
), |
| 178 |
( |
| 179 |
'|<[Int Range 2]', |
| 180 |
'f17:int_range2[Simple]', |
| 181 |
'_', |
| 182 |
'f18:int_range2[Custom]@', |
| 183 |
'_', |
| 184 |
'f19:int_range2[Text]*', |
| 185 |
'_', |
| 186 |
'f20:int_range2[Readonly]~', |
| 187 |
), |
| 188 |
), |
| 189 |
( |
| 190 |
'|{Misc}', |
| 191 |
( |
| 192 |
'|<[Integer Text]', |
| 193 |
'f21:integer_text[Simple]', |
| 194 |
'_', |
| 195 |
'f22:integer_text[Custom]@', |
| 196 |
'_', |
| 197 |
'f23:integer_text[Text]*', |
| 198 |
'_', |
| 199 |
'f24:integer_text[Readonly]~', |
| 200 |
), |
| 201 |
( |
| 202 |
'|<[Compound]', |
| 203 |
'f25:compound[Simple]', |
| 204 |
'_', |
| 205 |
'f26:compound[Custom]@', |
| 206 |
'_', |
| 207 |
'f27:compound[Text]*', |
| 208 |
'_', |
| 209 |
'f28:compound[Readonly]~', |
| 210 |
), |
| 211 |
( |
| 212 |
'|<[Boolean]', |
| 213 |
'f29:boolean[Simple]', |
| 214 |
'_', |
| 215 |
'f30:boolean[Custom]@', |
| 216 |
'_', |
| 217 |
'f31:boolean[Text]*', |
| 218 |
'_', |
| 219 |
'f32:boolean[Readonly]~', |
| 220 |
), |
| 221 |
), |
| 222 |
( |
| 223 |
'|{Color/Font}', |
| 224 |
( |
| 225 |
'|<[Color]', |
| 226 |
'f33:color[Simple]', |
| 227 |
'_', |
| 228 |
'f34:color[Custom]@', |
| 229 |
'_', |
| 230 |
'f35:color[Text]*', |
| 231 |
'_', |
| 232 |
'f36:color[Readonly]~', |
| 233 |
), |
| 234 |
( |
| 235 |
'|<[Font]', |
| 236 |
'f37:font[Simple]', |
| 237 |
'_', |
| 238 |
'f38:font[Custom]@', |
| 239 |
'_', |
| 240 |
'f39:font[Text]*', |
| 241 |
'_', |
| 242 |
'f40:font[Readonly]~', |
| 243 |
), |
| 244 |
), |
| 245 |
( |
| 246 |
'|{List}', |
| 247 |
( |
| 248 |
'|<[List]', |
| 249 |
'f41:list[Simple]', |
| 250 |
'_', |
| 251 |
'f42:list[Custom]@', |
| 252 |
'_', |
| 253 |
'f43:list[Text]*', |
| 254 |
'_', |
| 255 |
'f44:list[Readonly]~', |
| 256 |
), |
| 257 |
), |
| 258 |
( |
| 259 |
'|{Button}', |
| 260 |
('|<[Button]', 'f45:button[Simple]', '_', 'f46:button[Custom]@'), |
| 261 |
# 'button[Text]*', |
| 262 |
# 'button[Readonly]~' ), |
| 263 |
( |
| 264 |
'|<[Image Enum]', |
| 265 |
'f47:image_enum[Simple]', |
| 266 |
'_', |
| 267 |
'f48:image_enum[Custom]@', |
| 268 |
'_', |
| 269 |
'f49:image_enum[Text]*', |
| 270 |
'_', |
| 271 |
'f50:image_enum[Readonly]~', |
| 272 |
), |
| 273 |
( |
| 274 |
'|<[Instance]', |
| 275 |
'f51:instance[Simple]', |
| 276 |
'_', |
| 277 |
'f52:instance[Custom]@', |
| 278 |
'_', |
| 279 |
'f53:instance[Text]*', |
| 280 |
'_', |
| 281 |
'f54:instance[Readonly]~', |
| 282 |
), |
| 283 |
), |
| 284 |
( |
| 285 |
'|{File}', |
| 286 |
( |
| 287 |
'|<[File]', |
| 288 |
'f55:file[Simple]', |
| 289 |
'_', |
| 290 |
'f56:file[Custom]@', |
| 291 |
'_', |
| 292 |
'f57:file[Text]*', |
| 293 |
'_', |
| 294 |
'f58:file[Readonly]~', |
| 295 |
), |
| 296 |
( |
| 297 |
'|<[Directory]', |
| 298 |
'f59:directory[Simple]', |
| 299 |
'_', |
| 300 |
'f60:directory[Custom]@', |
| 301 |
'_', |
| 302 |
'f61:directory[Text]*', |
| 303 |
'_', |
| 304 |
'f62:directory[Readonly]~', |
| 305 |
), |
| 306 |
), |
| 307 |
buttons=['Apply', 'Revert', 'Undo', 'OK'], |
| 308 |
handler=TraitsTestHandler(), |
| 309 |
) |
| 310 |
|
| 311 |
|
| 312 |
# ------------------------------------------------------------------------- |
| 313 |
# 'TraitSheetApp' class: |
| 314 |
# ------------------------------------------------------------------------- |
| 315 |
|
| 316 |
|
| 317 |
class TraitSheetApp(wx.App): |
| 318 |
|
| 319 |
# ------------------------------------------------------------------------- |
| 320 |
# Initialize the object: |
| 321 |
# ------------------------------------------------------------------------- |
| 322 |
|
| 323 |
def __init__(self, object): |
| 324 |
self.object = object |
| 325 |
wx.InitAllImageHandlers() |
| 326 |
wx.App.__init__(self, 1, 'debug.log') |
| 327 |
self.MainLoop() |
| 328 |
|
| 329 |
# ------------------------------------------------------------------------- |
| 330 |
# Handle application initialization: |
| 331 |
# ------------------------------------------------------------------------- |
| 332 |
|
| 333 |
def OnInit(self): |
| 334 |
ui = self.object.edit_traits(kind='live') |
| 335 |
ui = self.object.edit_traits(kind='modal') |
| 336 |
ui = self.object.edit_traits(kind='nonmodal') |
| 337 |
ui = self.object.edit_traits(kind='wizard') |
| 338 |
self.SetTopWindow(ui.control) |
| 339 |
return True |
| 340 |
|
| 341 |
|
| 342 |
# ------------------------------------------------------------------------- |
| 343 |
# Main program: |
| 344 |
# ------------------------------------------------------------------------- |
| 345 |
|
| 346 |
TraitSheetApp(TraitsTest()) |