| 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 |
Tiny application: convert length measurements from one unit system to another |
| 13 |
|
| 14 |
Select the input and output units using the drop down combo-boxes in the |
| 15 |
*Input:* and *Output:* sections respectively. Type the input quantity |
| 16 |
to convert into the left most text box. The output value corresponding to the |
| 17 |
current input value will automatically be updated in the *Output:* |
| 18 |
section. |
| 19 |
|
| 20 |
Use the *Undo* and *ReDo* buttons to undo and redo changes you |
| 21 |
have made to any of the input fields. |
| 22 |
|
| 23 |
Note that other than the 'output_amount' property implementation, the rest |
| 24 |
of the code is simply declarative. |
| 25 |
""" |
| 26 |
|
| 27 |
# FIXME: Help is broken in QT |
| 28 |
|
| 29 |
from traits.api import HasStrictTraits, Trait, CFloat, Property |
| 30 |
from traitsui.api import View, VGroup, HGroup, Item |
| 31 |
|
| 32 |
# Help text: |
| 33 |
ViewHelp = """ |
| 34 |
This program converts length measurements from one unit system to another. |
| 35 |
|
| 36 |
Select the input and output units using the drop down combo-boxes in the |
| 37 |
*Input:* and *Output:* sections respectively. Type the input quantity |
| 38 |
to convert into the left most text box. The output value corresponding to the |
| 39 |
current input value will automatically be updated in the *Output:* |
| 40 |
section. |
| 41 |
|
| 42 |
Use the *Undo* and *ReDo* buttons to undo and redo changes you |
| 43 |
have made to any of the input fields. |
| 44 |
""" |
| 45 |
|
| 46 |
# Units trait maps all units to centimeters: |
| 47 |
Units = Trait( |
| 48 |
'inches', |
| 49 |
{ |
| 50 |
'inches': 2.54, |
| 51 |
'feet': (12 * 2.54), |
| 52 |
'yards': (36 * 2.54), |
| 53 |
'miles': (5280 * 12 * 2.54), |
| 54 |
'millimeters': 0.1, |
| 55 |
'centimeters': 1.0, |
| 56 |
'meters': 100.0, |
| 57 |
'kilometers': 100000.0, |
| 58 |
}, |
| 59 |
) |
| 60 |
|
| 61 |
# Converter Class: |
| 62 |
|
| 63 |
|
| 64 |
class Converter(HasStrictTraits): |
| 65 |
|
| 66 |
# Trait definitions: |
| 67 |
input_amount = CFloat(12.0, desc="the input quantity") |
| 68 |
input_units = Units('inches', desc="the input quantity's units") |
| 69 |
output_amount = Property( |
| 70 |
observe=['input_amount', 'input_units', 'output_units'], |
| 71 |
desc="the output quantity", |
| 72 |
) |
| 73 |
output_units = Units('feet', desc="the output quantity's units") |
| 74 |
|
| 75 |
# User interface views: |
| 76 |
traits_view = View( |
| 77 |
VGroup( |
| 78 |
HGroup( |
| 79 |
Item('input_amount', springy=True), |
| 80 |
Item('input_units', show_label=False), |
| 81 |
label='Input', |
| 82 |
show_border=True, |
| 83 |
), |
| 84 |
HGroup( |
| 85 |
Item('output_amount', style='readonly', springy=True), |
| 86 |
Item('output_units', show_label=False), |
| 87 |
label='Output', |
| 88 |
show_border=True, |
| 89 |
), |
| 90 |
help=ViewHelp, |
| 91 |
), |
| 92 |
title='Units Converter', |
| 93 |
buttons=['Undo', 'OK', 'Help'], |
| 94 |
) |
| 95 |
|
| 96 |
# Property implementations |
| 97 |
def _get_output_amount(self): |
| 98 |
return (self.input_amount * self.input_units_) / self.output_units_ |
| 99 |
|
| 100 |
|
| 101 |
# Create the demo: |
| 102 |
popup = Converter() |
| 103 |
|
| 104 |
# Run the demo (if invoked from the command line): |
| 105 |
if __name__ == '__main__': |
| 106 |
popup.configure_traits() |