source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/SimplePlot.py @ df1a6ed

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since df1a6ed was 25b9707a, checked in by lewis, 8 years ago

Remove menu icons from Image Viewer tool (references #368)

  • Property mode set to 100644
File size: 9.8 KB
RevLine 
[6550b64]1"""
2Simple Plot Frame : supporting only copy, print, scale
3"""
4import wx
[d85c194]5from sas.sasgui.guiframe.local_perspectives.plotting.Plotter2D import ModelPanel2D as PlotPanel
[d7bb526]6from sas.sasgui.plottools.toolbar import NavigationToolBar
7from sas.sasgui.plottools.plottables import Graph
[d85c194]8from sas.sasgui.guiframe.utils import PanelMenu
9from sas.sasgui.guiframe.events import StatusEvent
[6550b64]10
[b40ad40]11class SimplePlotPanel(PlotPanel):
[6550b64]12    """
13    PlotPanel for 1d and 2d
[b40ad40]14    """
[6550b64]15    _window_caption = 'Simple Plot'
[b40ad40]16    def __init__(self, parent, id=-1, color=None,
[6550b64]17                 dpi=None, style=wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):
18        """
19        Init
20        """
21        PlotPanel.__init__(self, parent, id=id, style=style, **kwargs)
[ae84427]22
[6550b64]23        self.SetColor(wx.WHITE)
[b40ad40]24
[6550b64]25        self.toolbar = NavigationToolBar(parent=self, canvas=self.canvas)
26        self.toolbar.Show(False)
27        self.scale = parent.scale
28        self.window_caption = self._window_caption
[b40ad40]29
[6550b64]30    def draw(self):
31        """
32        """
33        self.resizing = False
34        self.canvas.set_resizing(self.resizing)
35        self.canvas.draw()
[b40ad40]36
[6550b64]37    def add_toolbar(self):
38        """
39        """
[b40ad40]40        pass
41
[6550b64]42    def onContextMenu(self, event):
43        """
44        2D plot context menu
[b40ad40]45
[6550b64]46        :param event: wx context event
[b40ad40]47
[6550b64]48        """
49        slicerpop = PanelMenu()
50        slicerpop.set_plots(self.plots)
51        slicerpop.set_graph(self.graph)
52
[b40ad40]53        wx_id = wx.NewId()
54        slicerpop.Append(wx_id, '&Save Image')
55        wx.EVT_MENU(self, wx_id, self.onSaveImage)
56
57        wx_id = wx.NewId()
58        slicerpop.Append(wx_id, '&Print Image', 'Print image')
59        wx.EVT_MENU(self, wx_id, self.onPrint)
60
61        wx_id = wx.NewId()
62        slicerpop.Append(wx_id, '&Copy to Clipboard', 'Copy to the clipboard')
63        wx.EVT_MENU(self, wx_id, self.OnCopyFigureMenu)
[6550b64]64
[fad6056]65        if self.dimension != 3:
[6550b64]66            slicerpop.AppendSeparator()
[b40ad40]67            wx_id = wx.NewId()
68            slicerpop.Append(wx_id, '&Toggle Grid On/Off', 'Toggle Grid On/Off')
69            wx.EVT_MENU(self, wx_id, self.on_grid_onoff)
[6550b64]70
71        if self.data.__class__.__name__ == 'Data1D':
72            slicerpop.AppendSeparator()
[b40ad40]73            wx_id = wx.NewId()
74            slicerpop.Append(wx_id, '&Change Scale')
75            wx.EVT_MENU(self, wx_id, self._onProperties)
[6550b64]76        elif self.data2D.__class__.__name__ == 'Data2D':
77            slicerpop.AppendSeparator()
[b40ad40]78            wx_id = wx.NewId()
79            slicerpop.Append(wx_id, '&Toggle Linear/Log Scale')
80            wx.EVT_MENU(self, wx_id, self._onToggleScale)
81
[6550b64]82        try:
83            pos_evt = event.GetPosition()
84            pos = self.ScreenToClient(pos_evt)
85        except:
86            pos_x, pos_y = self.toolbar.GetPositionTuple()
87            pos = (pos_x, pos_y + 5)
88        self.PopupMenu(slicerpop, pos)
89        if self.scale != None:
[b40ad40]90            self.parent.scale2d = self.scale
91
[6550b64]92    def on_grid_onoff(self, event):
93        """
94        On grid on/off
95        """
96        switch = (not self.grid_on)
[b40ad40]97        self.onGridOnOff(switch)
98
[6550b64]99    def onLeftDown(self, event):
100        """
101        left button down and ready to drag
[b40ad40]102
[6550b64]103        """
104        # Check that the LEFT button was pressed
105        if event.button == 1:
106            self.leftdown = True
107            ax = event.inaxes
108            if ax != None:
109                self.xInit, self.yInit = event.xdata, event.ydata
110                try:
111                    pos_x = float(event.xdata)  # / size_x
112                    pos_y = float(event.ydata)  # / size_y
113                    pos_x = "%8.3g" % pos_x
114                    pos_y = "%8.3g" % pos_y
115                    self.position = str(pos_x), str(pos_y)
116                    wx.PostEvent(self.parent, StatusEvent(status=self.position))
117                except:
[b40ad40]118                    self.position = None
119
120    def _OnReSize(self, event):
[6550b64]121        """
122        On response of the resize of a panel, set axes_visiable False
123        """
124        self.resizing = False
125        if self.x_size != None:
126            if self.x_size == self.GetSize():
127                self.canvas.set_resizing(self.resizing)
128                return
129        self.x_size = self.GetSize()
130
131        # Ready for another event
[b40ad40]132        # Do not remove this Skip.
[6550b64]133        # Otherwise it will get runtime error on wx>=2.9.
[b40ad40]134        event.Skip()
[6550b64]135        # set the resizing flag
136        self.canvas.set_resizing(self.resizing)
137        pos_x, pos_y = self.GetPositionTuple()
138        if pos_x != 0 and pos_y != 0:
139            self.size, _ = self.GetClientSizeTuple()
140        self.SetSizer(self.sizer)
[b40ad40]141
[6550b64]142    def on_set_focus(self, event):
143        """
144        By pass default boundary blue color drawing
145        """
146        pass
[b40ad40]147
[6550b64]148    def on_kill_focus(self, event):
149        """
150        Reset the panel color
151        """
152        pass
[b40ad40]153
[6550b64]154    def show_plot(self, plot):
155        """
156        Show the plot
157        """
158        _yaxis, _yunit = plot.get_yaxis()
159        _xaxis, _xunit = plot.get_xaxis()
160        self.data = plot
161        self.plots[plot.name] = plot
162        # Axis scales
163        if plot.xtransform != None:
164            self.xLabel = plot.xtransform
165        if plot.ytransform != None:
166            self.yLabel = plot.ytransform
167        # Init graph
168        self.graph = Graph()
169        # Add plot: Handles both 1D and 2D
170        self.graph.add(self.data)
171        self.canvas.set_resizing(False)
172        if self.data.__class__.__name__ == 'Data2D':
173            self.data2D = plot
174        elif self.data.__class__.__name__ == 'Data1D':
175            self._onEVT_FUNC_PROPERTY(show=False)
176        # Axes
177        self.graph.xaxis(_xaxis, _xunit)
178        self.graph.yaxis(_yaxis, _yunit)
179        self.xaxis(_xaxis, _xunit)
[b40ad40]180        self.yaxis(_yaxis, _yunit)
[6550b64]181        self.set_xscale(self.xscale)
[b40ad40]182        self.set_yscale(self.yscale)
183        self.graph.render(self)
184
[6550b64]185class PlotFrame(wx.Frame):
186    """
187    Frame for simple plot
188    """
[b40ad40]189    def __init__(self, parent, id, title, scale='log_{10}',
[25b9707a]190                 size=wx.Size(550, 470), show_menu_icons=True):
[6550b64]191        """
192        comment
193        :param parent: parent panel/container
194        """
195        # Initialize the Frame object
[ae84427]196        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size)
[b40ad40]197
[6550b64]198        # Panel for 1D plot
199        self.parent = parent
200        self._mgr = None
[9e9e9a5]201        self.menu_bar = None
[6550b64]202        self._default_save_location = None
203        self.scale = scale
[25b9707a]204        self._show_menu_icons = show_menu_icons
[6550b64]205        self.plotpanel = SimplePlotPanel(self, -1)
[9e9e9a5]206        self._build_menubar()
207
208    def _build_menubar(self):
209        """
210        Build menubar
211        """
212        tsize = (13, 13)
[b40ad40]213        save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR, tsize)
214        quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR, tsize)
215        print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR, tsize)
216        copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR, tsize)
[9e9e9a5]217        menu_bar = wx.MenuBar()
[b40ad40]218
219        menu = wx.Menu()
[9e9e9a5]220        id = wx.NewId()
[25b9707a]221        save_item = wx.MenuItem(menu, id, "&Save Image")
222        menu.AppendItem(save_item)
[9e9e9a5]223        wx.EVT_MENU(self, id, self.on_save_file)
224
225        id = wx.NewId()
[25b9707a]226        print_item = wx.MenuItem(menu, id, "&Print Image")
227        menu.AppendItem(print_item)
[9e9e9a5]228        wx.EVT_MENU(self, id, self.on_print_image)
229
230        menu.AppendSeparator()
231        id = wx.NewId()
[25b9707a]232        quit_item = wx.MenuItem(menu, id, "&Quit")
233        menu.AppendItem(quit_item)
[9e9e9a5]234
235        menu_bar.Append(menu, "&File")
236        wx.EVT_MENU(self, id, self.on_close)
[b40ad40]237
[9e9e9a5]238        menu_edit = wx.Menu()
239        id = wx.NewId()
[25b9707a]240        copy_item = wx.MenuItem(menu_edit, id, "&Copy")
241        menu_edit.AppendItem(copy_item)
[9e9e9a5]242        wx.EVT_MENU(self, id, self.on_copy_image)
[b40ad40]243
[25b9707a]244        if self._show_menu_icons:
245            save_item.SetBitmap(save_bmp)
246            print_item.SetBitmap(print_bmp)
247            quit_item.SetBitmap(quit_bmp)
248            copy_item.SetBitmap(copy_bmp)
249
[9e9e9a5]250        menu_bar.Append(menu_edit, "&Edit")
251        self.menu_bar = menu_bar
252        self.SetMenuBar(self.menu_bar)
[6550b64]253
254    def set_plot_unfocus(self):
255        """
256        un focusing
257        """
[b40ad40]258        pass
259
[6550b64]260    def add_plot(self, plot):
261        """
262        Add Image
263        """
264        plotpanel = self.plotpanel
265        plotpanel.scale = self.scale
266        plotpanel.show_plot(plot)
[b40ad40]267
[6550b64]268    def set_schedule_full_draw(self, panel, func):
269        """
270        """
271        self.plotpanel.resizing = False
[b40ad40]272
[fad6056]273    def im_show(self, img):
274        """
275        Show background image
276        :Param img: [imread(path) from matplotlib.pyplot]
277        """
278        self.plotpanel.subplot.imshow(img)
[b40ad40]279
280    def set_schedule(self, schedule=False):
[6550b64]281        """
282        """
283        #Not implemented
[b40ad40]284
[6550b64]285    def disable_app_menu(self, panel):
286        """
287        """
[b40ad40]288        #Not implemented
289
290    def get_current_context_menu(self, plotpanel):
[6550b64]291        """
292        """
293        #Not implemented
[b40ad40]294
[9e9e9a5]295    def on_save_file(self, event):
296        """
297        Save image
298        """
299        self.plotpanel.onSaveImage(event)
300
301    def on_print_image(self, event):
302        """
303        Save image
304        """
305        self.plotpanel.onPrint(event)
[b40ad40]306
[9e9e9a5]307    def on_print_preview(self, event):
308        """
309        Save image
310        """
311        self.plotpanel.onPrinterPreview(event)
312
313    def on_copy_image(self, event):
314        """
315        Save image
316        """
317        self.plotpanel.OnCopyFigureMenu(event)
[b40ad40]318
[6550b64]319    def on_close(self, event):
320        """
[b40ad40]321        On Close
[6550b64]322        """
323        try:
324            self.parent.set_scale2d(self.scale)
325            self.parent.on_panel_close(event)
326        except:
327            self.Destroy()
Note: See TracBrowser for help on using the repository browser.