[6550b64] | 1 | """ |
---|
| 2 | Simple Plot Frame : supporting only copy, print, scale |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
[b40ad40] | 5 | from sas.guiframe.local_perspectives.plotting.Plotter2D import ModelPanel2D as PlotPanel |
---|
[79492222] | 6 | from sas.plottools.toolbar import NavigationToolBar |
---|
| 7 | from sas.plottools.plottables import Graph |
---|
| 8 | from sas.guiframe.utils import PanelMenu |
---|
[b40ad40] | 9 | from sas.guiframe.events import StatusEvent |
---|
[6550b64] | 10 | |
---|
[b40ad40] | 11 | class 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] | 185 | class PlotFrame(wx.Frame): |
---|
| 186 | """ |
---|
| 187 | Frame for simple plot |
---|
| 188 | """ |
---|
[b40ad40] | 189 | def __init__(self, parent, id, title, scale='log_{10}', |
---|
| 190 | size=wx.Size(550, 470)): |
---|
[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 |
---|
| 204 | self.plotpanel = SimplePlotPanel(self, -1) |
---|
[9e9e9a5] | 205 | self._build_menubar() |
---|
| 206 | |
---|
| 207 | def _build_menubar(self): |
---|
| 208 | """ |
---|
| 209 | Build menubar |
---|
| 210 | """ |
---|
| 211 | tsize = (13, 13) |
---|
[b40ad40] | 212 | save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR, tsize) |
---|
| 213 | quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR, tsize) |
---|
| 214 | print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR, tsize) |
---|
| 215 | preview_bmp = wx.ArtProvider.GetBitmap(wx.ART_REPORT_VIEW, 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() |
---|
| 221 | item = wx.MenuItem(menu, id, "&Save Image") |
---|
| 222 | item.SetBitmap(save_bmp) |
---|
| 223 | menu.AppendItem(item) |
---|
| 224 | wx.EVT_MENU(self, id, self.on_save_file) |
---|
| 225 | |
---|
| 226 | id = wx.NewId() |
---|
| 227 | item = wx.MenuItem(menu, id, "&Print Image") |
---|
| 228 | item.SetBitmap(print_bmp) |
---|
| 229 | menu.AppendItem(item) |
---|
| 230 | wx.EVT_MENU(self, id, self.on_print_image) |
---|
| 231 | |
---|
| 232 | menu.AppendSeparator() |
---|
| 233 | id = wx.NewId() |
---|
| 234 | item = wx.MenuItem(menu, id, "&Quit") |
---|
| 235 | item.SetBitmap(quit_bmp) |
---|
| 236 | menu.AppendItem(item) |
---|
| 237 | |
---|
| 238 | menu_bar.Append(menu, "&File") |
---|
| 239 | wx.EVT_MENU(self, id, self.on_close) |
---|
[b40ad40] | 240 | |
---|
[9e9e9a5] | 241 | menu_edit = wx.Menu() |
---|
| 242 | id = wx.NewId() |
---|
| 243 | item = wx.MenuItem(menu_edit, id, "&Copy") |
---|
| 244 | item.SetBitmap(copy_bmp) |
---|
| 245 | menu_edit.AppendItem(item) |
---|
| 246 | wx.EVT_MENU(self, id, self.on_copy_image) |
---|
[b40ad40] | 247 | |
---|
[9e9e9a5] | 248 | menu_bar.Append(menu_edit, "&Edit") |
---|
| 249 | self.menu_bar = menu_bar |
---|
| 250 | self.SetMenuBar(self.menu_bar) |
---|
[6550b64] | 251 | |
---|
| 252 | def set_plot_unfocus(self): |
---|
| 253 | """ |
---|
| 254 | un focusing |
---|
| 255 | """ |
---|
[b40ad40] | 256 | pass |
---|
| 257 | |
---|
[6550b64] | 258 | def add_plot(self, plot): |
---|
| 259 | """ |
---|
| 260 | Add Image |
---|
| 261 | """ |
---|
| 262 | plotpanel = self.plotpanel |
---|
| 263 | plotpanel.scale = self.scale |
---|
| 264 | plotpanel.show_plot(plot) |
---|
[b40ad40] | 265 | |
---|
[6550b64] | 266 | def set_schedule_full_draw(self, panel, func): |
---|
| 267 | """ |
---|
| 268 | """ |
---|
| 269 | self.plotpanel.resizing = False |
---|
[b40ad40] | 270 | |
---|
[fad6056] | 271 | def im_show(self, img): |
---|
| 272 | """ |
---|
| 273 | Show background image |
---|
| 274 | :Param img: [imread(path) from matplotlib.pyplot] |
---|
| 275 | """ |
---|
| 276 | self.plotpanel.subplot.imshow(img) |
---|
[b40ad40] | 277 | |
---|
| 278 | def set_schedule(self, schedule=False): |
---|
[6550b64] | 279 | """ |
---|
| 280 | """ |
---|
| 281 | #Not implemented |
---|
[b40ad40] | 282 | |
---|
[6550b64] | 283 | def disable_app_menu(self, panel): |
---|
| 284 | """ |
---|
| 285 | """ |
---|
[b40ad40] | 286 | #Not implemented |
---|
| 287 | |
---|
| 288 | def get_current_context_menu(self, plotpanel): |
---|
[6550b64] | 289 | """ |
---|
| 290 | """ |
---|
| 291 | #Not implemented |
---|
[b40ad40] | 292 | |
---|
[9e9e9a5] | 293 | def on_save_file(self, event): |
---|
| 294 | """ |
---|
| 295 | Save image |
---|
| 296 | """ |
---|
| 297 | self.plotpanel.onSaveImage(event) |
---|
| 298 | |
---|
| 299 | def on_print_image(self, event): |
---|
| 300 | """ |
---|
| 301 | Save image |
---|
| 302 | """ |
---|
| 303 | self.plotpanel.onPrint(event) |
---|
[b40ad40] | 304 | |
---|
[9e9e9a5] | 305 | def on_print_preview(self, event): |
---|
| 306 | """ |
---|
| 307 | Save image |
---|
| 308 | """ |
---|
| 309 | self.plotpanel.onPrinterPreview(event) |
---|
| 310 | |
---|
| 311 | def on_copy_image(self, event): |
---|
| 312 | """ |
---|
| 313 | Save image |
---|
| 314 | """ |
---|
| 315 | self.plotpanel.OnCopyFigureMenu(event) |
---|
[b40ad40] | 316 | |
---|
[6550b64] | 317 | def on_close(self, event): |
---|
| 318 | """ |
---|
[b40ad40] | 319 | On Close |
---|
[6550b64] | 320 | """ |
---|
| 321 | try: |
---|
| 322 | self.parent.set_scale2d(self.scale) |
---|
| 323 | self.parent.on_panel_close(event) |
---|
| 324 | except: |
---|
| 325 | self.Destroy() |
---|
[b40ad40] | 326 | |
---|