~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/basic/image_from_file.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""
3
 
Loads and saves RGB images from disk
4
 
 - Left-drag pans the plot.
5
 
 - Mousewheel up and down zooms the plot in and out.
6
 
 - Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
7
 
   region to zoom.  If you use a sequence of zoom boxes, pressing alt-left-arrow
8
 
   and alt-right-arrow moves you forwards and backwards through the "zoom
9
 
   history".
10
 
"""
11
 
 
12
 
# Standard library imports
13
 
import os, sys
14
 
 
15
 
# Major library imports
16
 
 
17
 
# Enthought library imports
18
 
from traits.util.resource import find_resource
19
 
from traits.api import File, HasTraits, Instance
20
 
from traitsui.api import Handler, Item, View
21
 
from traitsui.menu \
22
 
    import Action, CloseAction, Menu, MenuBar, OKCancelButtons, Separator
23
 
 
24
 
# Chaco imports
25
 
from chaco.api \
26
 
    import ArrayPlotData, ImageData, Plot, PlotGraphicsContext
27
 
from enable.component_editor import ComponentEditor
28
 
from chaco.tools.api import PanTool, ZoomTool
29
 
 
30
 
 
31
 
#-------------------------------------------------------------------------------
32
 
# Class 'DemoView'
33
 
#-------------------------------------------------------------------------------
34
 
 
35
 
class DemoView(HasTraits):
36
 
 
37
 
    ### Public Traits ##########################################################
38
 
 
39
 
    # A Plot Data object to hold our image data
40
 
    pd = Instance(ArrayPlotData, ())
41
 
 
42
 
    # A Plot object to plot our image data
43
 
    plot = Instance(Plot)
44
 
 
45
 
 
46
 
    ### Private Traits #########################################################
47
 
 
48
 
    # File name to load image from
49
 
    resource_path = os.path.join('examples','basic','capitol.jpg')
50
 
    alt_path = 'capitol.jpg'
51
 
    image_path = find_resource('Chaco', resource_path, alt_path=alt_path,
52
 
        return_path=True)
53
 
    _load_file = File(image_path)
54
 
 
55
 
    # File name to save image to
56
 
    _save_file = File
57
 
 
58
 
 
59
 
    ### Traits Views ###########################################################
60
 
 
61
 
    # This view is for a file dialog to select the 'load' filename
62
 
    load_file_view = View(
63
 
        Item('_load_file'),
64
 
        buttons=OKCancelButtons,
65
 
        kind='livemodal',  # NB must use livemodal, plot objects don't copy well
66
 
        width=400,
67
 
        resizable=True,
68
 
    )
69
 
 
70
 
    # This view is for a file dialog to select the 'save' filename
71
 
    save_file_view = View(
72
 
        Item('_save_file'),
73
 
        buttons=OKCancelButtons,
74
 
        kind='livemodal',  # NB must use livemodal, plot objects don't copy well
75
 
        width=400,
76
 
        resizable=True,
77
 
    )
78
 
 
79
 
    #---------------------------------------------------------------------------
80
 
    # Public 'DemoView' interface
81
 
    #---------------------------------------------------------------------------
82
 
 
83
 
    def __init__(self, *args, **kwargs):
84
 
        super(DemoView, self).__init__(*args, **kwargs)
85
 
 
86
 
        # Create the plot object, set some options, and add some tools
87
 
        plot = self.plot = Plot(self.pd, default_origin="top left")
88
 
        plot.x_axis.orientation = "top"
89
 
        plot.padding = 50
90
 
        plot.padding_top = 75
91
 
        plot.tools.append(PanTool(plot))
92
 
        zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
93
 
        plot.overlays.append(zoom)
94
 
 
95
 
        # Load the default image
96
 
        self._load()
97
 
 
98
 
        # Plot the image plot with this image
99
 
        self.plot.img_plot("imagedata")
100
 
 
101
 
    def default_traits_view(self):
102
 
        """ Returns the default view to use for this class.
103
 
        """
104
 
        # NOTE: I moved the view to this method so we can declare a handler
105
 
        # for the view. Alternatively, we could move the DemoController class
106
 
        # to the top and declare view=Instance(HasTraits) instead.
107
 
        traits_view = View(
108
 
            Item('plot',
109
 
                 editor=ComponentEditor(),
110
 
                 show_label=False,
111
 
            ),
112
 
            menubar=MenuBar(
113
 
                Menu(Action(name="Save Plot", action="save"), # see Controller for
114
 
                     Action(name="Load Plot", action="load"), # these callbacks
115
 
                     Separator(),
116
 
                     CloseAction,
117
 
                     name="File",
118
 
                ),
119
 
            ),
120
 
            width=600,
121
 
            height=600,
122
 
            resizable=True,
123
 
            handler=DemoController
124
 
        )
125
 
        return traits_view
126
 
 
127
 
    #---------------------------------------------------------------------------
128
 
    # Private 'DemoView' interface
129
 
    #---------------------------------------------------------------------------
130
 
 
131
 
    def _save(self):
132
 
        # Create a graphics context of the right size
133
 
        win_size = self.plot.outer_bounds
134
 
        plot_gc = PlotGraphicsContext(win_size)
135
 
 
136
 
        # Have the plot component into it
137
 
        plot_gc.render_component(self.plot)
138
 
 
139
 
        # Save out to the user supplied filename
140
 
        plot_gc.save(self._save_file)
141
 
 
142
 
    def _load(self):
143
 
        try:
144
 
            # Load the image with the user supplied filename
145
 
            image = ImageData.fromfile(self._load_file)
146
 
 
147
 
            # Update the plot data. NB we must extract _date from the image
148
 
            # for the time being, until ImageData is made more friendly
149
 
            self.pd.set_data("imagedata", image._data)
150
 
 
151
 
            # Set the title and redraw
152
 
            self.plot.title = os.path.basename(self._load_file)
153
 
            self.plot.request_redraw()
154
 
        except:
155
 
            # If loading fails, simply do nothing
156
 
            pass
157
 
 
158
 
 
159
 
#-------------------------------------------------------------------------------
160
 
# Class 'DemoController'
161
 
#-------------------------------------------------------------------------------
162
 
 
163
 
class DemoController(Handler):
164
 
 
165
 
    # The HasTraits object we are a controller for
166
 
    view = Instance(DemoView)
167
 
 
168
 
    #---------------------------------------------------------------------------
169
 
    # Public 'DemoController' interface
170
 
    #---------------------------------------------------------------------------
171
 
 
172
 
    def init(self, info):
173
 
        """ Initializes the controls of a user interface.
174
 
        Overridden here to assign the 'view' trait.
175
 
        """
176
 
        self.view = info.object
177
 
 
178
 
    def save(self, ui_info):
179
 
        """
180
 
        Callback for the 'Save Image' menu option.
181
 
        """
182
 
        ui = self.view.edit_traits(view='save_file_view')
183
 
        if ui.result == True:
184
 
            self.view._save()
185
 
 
186
 
    def load(self, ui_info):
187
 
        """
188
 
        Callback for the 'Load Image' menu option.
189
 
        """
190
 
        ui = self.view.edit_traits(view='load_file_view')
191
 
        if ui.result == True:
192
 
            self.view._load()
193
 
 
194
 
 
195
 
#===============================================================================
196
 
# # popup object that is used by the demo.py application.
197
 
#===============================================================================
198
 
# Note: we declare a 'popup' rather than a 'demo' since the menubar doesn't seem
199
 
# to show up in a 'panel' mode.
200
 
popup = DemoView()
201
 
 
202
 
#-------------------------------------------------------------------------------
203
 
# Function 'main'
204
 
#-------------------------------------------------------------------------------
205
 
 
206
 
def main(argv=None):
207
 
    view = DemoView()
208
 
    view.configure_traits()
209
 
 
210
 
 
211
 
#-------------------------------------------------------------------------------
212
 
 
213
 
if __name__ == "__main__":
214
 
    sys.exit(main())