~ubuntu-branches/ubuntu/utopic/python-apptools/utopic

« back to all changes in this revision

Viewing changes to apptools/template/test/scatter_plot_2.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 23:55:50 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110708235550-yz5u79ubeo4dhyfx
Tags: 4.0.0-1
* New upstream release
* Update debian/watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-------------------------------------------------------------------------------
 
2
#
 
3
#  A simple dual scatter-plot template defined as a test for the template
 
4
#  package.
 
5
#
 
6
#  Written by: David C. Morrill
 
7
#  (based on the original cp.plot geo_scatter_plot.py file)
 
8
#
 
9
#  Date: 08/01/2007
 
10
#
 
11
#  (c) Copy 2007 by Enthought, Inc.
 
12
#
 
13
#-------------------------------------------------------------------------------
 
14
 
 
15
""" A simple dual scatter-plot template defined as a test for the template
 
16
    package.
 
17
"""
 
18
 
 
19
#-------------------------------------------------------------------------------
 
20
#  Imports:
 
21
#-------------------------------------------------------------------------------
 
22
 
 
23
from traits.api \
 
24
    import Undefined
 
25
 
 
26
from traitsui.api \
 
27
    import View, VGroup, Item, Label, Theme, TextEditor
 
28
 
 
29
from traitsui.wx.themed_slider_editor \
 
30
    import ThemedSliderEditor
 
31
 
 
32
from traitsui.wx.themed_text_editor \
 
33
    import ThemedTextEditor
 
34
 
 
35
from enable.api \
 
36
    import ColorTrait
 
37
 
 
38
from chaco.api \
 
39
    import HPlotContainer
 
40
 
 
41
from chaco.scatter_markers \
 
42
    import marker_trait
 
43
 
 
44
from apptools.template.api \
 
45
    import Template, TRange, TStr, TInstance, TDerived
 
46
 
 
47
from enable_editor \
 
48
    import EnableEditor
 
49
 
 
50
from scatter_plot \
 
51
    import ScatterPlot
 
52
 
 
53
#-------------------------------------------------------------------------------
 
54
#  Trait definitions:
 
55
#-------------------------------------------------------------------------------
 
56
 
 
57
# Template color trait:
 
58
TColor = ColorTrait( template = 'copy' )
 
59
 
 
60
#-------------------------------------------------------------------------------
 
61
#  'ScatterPlot2' class:
 
62
#-------------------------------------------------------------------------------
 
63
 
 
64
class ScatterPlot2 ( Template ):
 
65
 
 
66
    #-- Template Traits --------------------------------------------------------
 
67
 
 
68
    # The title of the plot:
 
69
    title = TStr( 'Dual Scatter Plots' )
 
70
 
 
71
    # The type of marker to use.  This is a mapped trait using strings as the
 
72
    # keys:
 
73
    marker = marker_trait( template = 'copy', event = 'update' )
 
74
 
 
75
    # The pixel size of the marker (doesn't include the thickness of the
 
76
    # outline):
 
77
    marker_size = TRange( 1, 5, 1, event = 'update' )
 
78
 
 
79
    # The thickness, in pixels, of the outline to draw around the marker.  If
 
80
    # this is 0, no outline will be drawn.
 
81
    line_width = TRange( 0.0, 5.0, 1.0 )
 
82
 
 
83
    # The fill color of the marker:
 
84
    color = TColor( 'red', event = 'update' )
 
85
 
 
86
    # The color of the outline to draw around the marker
 
87
    outline_color = TColor( 'black', event = 'update' )
 
88
 
 
89
    # The amount of space between plots:
 
90
    spacing = TRange( 0.0, 20.0, 0.0 )
 
91
 
 
92
    # The contained scatter plots:
 
93
    scatter_plot_1 = TInstance( ScatterPlot, () )
 
94
    scatter_plot_2 = TInstance( ScatterPlot, () )
 
95
 
 
96
    #-- Derived Traits ---------------------------------------------------------
 
97
 
 
98
    plot = TDerived
 
99
 
 
100
    #-- Traits UI Views --------------------------------------------------------
 
101
 
 
102
    # The scatter plot view:
 
103
    template_view = View(
 
104
        VGroup(
 
105
            Item( 'title',
 
106
                  show_label = False,
 
107
                  style      = 'readonly',
 
108
                  editor     = ThemedTextEditor(
 
109
                                 theme = Theme( '@GBB', alignment = 'center' ) )
 
110
            ),
 
111
            Item( 'plot',
 
112
                  show_label = False,
 
113
                  resizable  = True,
 
114
                  editor     = EnableEditor(),
 
115
                  item_theme = Theme( '@GF5', margins = 0 )
 
116
            )
 
117
        ),
 
118
        resizable = True
 
119
    )
 
120
 
 
121
    # The scatter plot options view:
 
122
    options_view = View(
 
123
        VGroup(
 
124
            VGroup(
 
125
                Label( 'Scatter Plot Options',
 
126
                       item_theme = Theme( '@GBB', alignment = 'center' ) ),
 
127
                show_labels = False
 
128
            ),
 
129
            VGroup(
 
130
                Item( 'title', editor = TextEditor() ),
 
131
                Item( 'marker' ),
 
132
                Item( 'marker_size', editor = ThemedSliderEditor() ),
 
133
                Item( 'line_width',
 
134
                      label  = 'Line Width',
 
135
                      editor = ThemedSliderEditor() ),
 
136
                Item( 'spacing', editor = ThemedSliderEditor() ),
 
137
                Item( 'color',         label = 'Fill Color' ),
 
138
                Item( 'outline_color', label = 'Outline Color' ),
 
139
                group_theme = Theme( '@GF5', margins = ( -5, -1 ) ),
 
140
                item_theme  = Theme( '@G0B', margins = 0 )
 
141
            )
 
142
        )
 
143
    )
 
144
 
 
145
    #-- ITemplate Interface Implementation -------------------------------------
 
146
 
 
147
    def activate_template ( self ):
 
148
        """ Converts all contained 'TDerived' objects to real objects using the
 
149
            template traits of the object. This method must be overridden in
 
150
            subclasses.
 
151
 
 
152
            Returns
 
153
            -------
 
154
            None
 
155
        """
 
156
        plots = [ p for p in [ self.scatter_plot_1.plot,
 
157
                               self.scatter_plot_2.plot ] if p is not None ]
 
158
        if len( plots ) == 2:
 
159
            self.plot = HPlotContainer( spacing = self.spacing )
 
160
            self.plot.add( *plots )
 
161
        elif len( plots ) == 1:
 
162
            self.plot = plots[0]
 
163
 
 
164
    #-- Default Values ---------------------------------------------------------
 
165
 
 
166
    def _scatter_plot_1_default ( self ):
 
167
        """ Returns the default value for the first scatter plot.
 
168
        """
 
169
        result = ScatterPlot()
 
170
        result.index.description  = 'Shared Plot Index'
 
171
        result.value.description += ' 1'
 
172
 
 
173
        return result
 
174
 
 
175
    def _scatter_plot_2_default ( self ):
 
176
        """ Returns the default value for the second scatter plot.
 
177
        """
 
178
        result = ScatterPlot( index = self.scatter_plot_1.index )
 
179
        result.value.description += ' 2'
 
180
        result.value.optional = True
 
181
 
 
182
        return result
 
183
 
 
184
    #-- Trait Event Handlers ---------------------------------------------------
 
185
 
 
186
    def _update_changed ( self, name, old, new ):
 
187
        """ Handles a plot option being changed.
 
188
        """
 
189
        setattr( self.scatter_plot_1, name, new )
 
190
        setattr( self.scatter_plot_2, name, new )
 
191
        self.plot = Undefined
 
192
 
 
193
    def _spacing_changed ( self, spacing ):
 
194
        """ Handles the spacing between plots being changed.
 
195
        """
 
196
        self.plot = Undefined
 
197