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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since a27e8b8 was b40ad40, checked in by Mathieu Doucet <doucetm@…>, 10 years ago

pylint fixes

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