[6550b64] | 1 | """ |
---|
| 2 | Simple Plot Frame : supporting only copy, print, scale |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
[79492222] | 5 | from sas.guiframe.local_perspectives.plotting.Plotter2D import ModelPanel2D \ |
---|
[6550b64] | 6 | as PlotPanel |
---|
[79492222] | 7 | from sas.plottools.toolbar import NavigationToolBar |
---|
| 8 | from sas.plottools.plottables import Graph |
---|
| 9 | from sas.guiframe.utils import PanelMenu |
---|
[6550b64] | 10 | |
---|
| 11 | class SimplePlotPanel(PlotPanel): |
---|
| 12 | """ |
---|
| 13 | PlotPanel for 1d and 2d |
---|
| 14 | """ |
---|
| 15 | _window_caption = 'Simple Plot' |
---|
| 16 | def __init__(self, parent, id=-1, color = None, |
---|
| 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) |
---|
| 24 | |
---|
| 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 |
---|
| 29 | |
---|
| 30 | def draw(self): |
---|
| 31 | """ |
---|
| 32 | """ |
---|
| 33 | self.resizing = False |
---|
| 34 | self.canvas.set_resizing(self.resizing) |
---|
| 35 | self.canvas.draw() |
---|
| 36 | |
---|
| 37 | def add_toolbar(self): |
---|
| 38 | """ |
---|
| 39 | """ |
---|
| 40 | pass |
---|
| 41 | |
---|
| 42 | def onContextMenu(self, event): |
---|
| 43 | """ |
---|
| 44 | 2D plot context menu |
---|
| 45 | |
---|
| 46 | :param event: wx context event |
---|
| 47 | |
---|
| 48 | """ |
---|
| 49 | slicerpop = PanelMenu() |
---|
| 50 | slicerpop.set_plots(self.plots) |
---|
| 51 | slicerpop.set_graph(self.graph) |
---|
| 52 | |
---|
| 53 | id = wx.NewId() |
---|
| 54 | slicerpop.Append(id, '&Save Image') |
---|
| 55 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
| 56 | |
---|
| 57 | id = wx.NewId() |
---|
| 58 | slicerpop.Append(id,'&Print Image', 'Print image') |
---|
| 59 | wx.EVT_MENU(self, id, self.onPrint) |
---|
| 60 | |
---|
| 61 | id = wx.NewId() |
---|
| 62 | slicerpop.Append(id,'&Print Preview', 'Print preview') |
---|
| 63 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
| 64 | |
---|
| 65 | id = wx.NewId() |
---|
| 66 | slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
| 67 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
| 68 | |
---|
[fad6056] | 69 | if self.dimension != 3: |
---|
[6550b64] | 70 | slicerpop.AppendSeparator() |
---|
| 71 | id = wx.NewId() |
---|
| 72 | slicerpop.Append(id, '&Toggle Grid On/Off', 'Toggle Grid On/Off') |
---|
| 73 | wx.EVT_MENU(self, id, self.on_grid_onoff) |
---|
| 74 | |
---|
| 75 | if self.data.__class__.__name__ == 'Data1D': |
---|
| 76 | slicerpop.AppendSeparator() |
---|
| 77 | id = wx.NewId() |
---|
| 78 | slicerpop.Append(id, '&Change Scale') |
---|
| 79 | wx.EVT_MENU(self, id, self._onProperties) |
---|
| 80 | elif self.data2D.__class__.__name__ == 'Data2D': |
---|
| 81 | slicerpop.AppendSeparator() |
---|
| 82 | id = wx.NewId() |
---|
| 83 | slicerpop.Append(id, '&Toggle Linear/Log Scale') |
---|
| 84 | wx.EVT_MENU(self, id, self._onToggleScale) |
---|
| 85 | |
---|
| 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: |
---|
| 94 | self.parent.scale2d = self.scale |
---|
| 95 | |
---|
| 96 | def on_grid_onoff(self, event): |
---|
| 97 | """ |
---|
| 98 | On grid on/off |
---|
| 99 | """ |
---|
| 100 | switch = (not self.grid_on) |
---|
| 101 | self.onGridOnOff(switch) |
---|
| 102 | |
---|
| 103 | def onLeftDown(self, event): |
---|
| 104 | """ |
---|
| 105 | left button down and ready to drag |
---|
| 106 | |
---|
| 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: |
---|
| 122 | self.position = None |
---|
| 123 | |
---|
| 124 | def _OnReSize(self, event): |
---|
| 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 |
---|
| 136 | # Do not remove this Skip. |
---|
| 137 | # Otherwise it will get runtime error on wx>=2.9. |
---|
| 138 | event.Skip() |
---|
| 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) |
---|
| 145 | |
---|
| 146 | def on_set_focus(self, event): |
---|
| 147 | """ |
---|
| 148 | By pass default boundary blue color drawing |
---|
| 149 | """ |
---|
| 150 | pass |
---|
| 151 | |
---|
| 152 | def on_kill_focus(self, event): |
---|
| 153 | """ |
---|
| 154 | Reset the panel color |
---|
| 155 | """ |
---|
| 156 | pass |
---|
| 157 | |
---|
| 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) |
---|
| 184 | self.yaxis(_yaxis, _yunit) |
---|
| 185 | self.set_xscale(self.xscale) |
---|
| 186 | self.set_yscale(self.yscale) |
---|
| 187 | self.graph.render(self) |
---|
| 188 | |
---|
| 189 | class PlotFrame(wx.Frame): |
---|
| 190 | """ |
---|
| 191 | Frame for simple plot |
---|
| 192 | """ |
---|
| 193 | def __init__(self, parent, id, title, scale='log_{10}', |
---|
| 194 | size=wx.Size(550, 470)): |
---|
| 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) |
---|
[6550b64] | 201 | |
---|
| 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) |
---|
| 216 | save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR, |
---|
| 217 | tsize) |
---|
| 218 | quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR, |
---|
| 219 | tsize) |
---|
| 220 | print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR, |
---|
| 221 | tsize) |
---|
| 222 | preview_bmp = wx.ArtProvider.GetBitmap(wx.ART_REPORT_VIEW, wx.ART_TOOLBAR, |
---|
| 223 | tsize) |
---|
| 224 | copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR, |
---|
| 225 | tsize) |
---|
| 226 | menu_bar = wx.MenuBar() |
---|
| 227 | |
---|
| 228 | menu = wx.Menu() |
---|
| 229 | id = wx.NewId() |
---|
| 230 | item = wx.MenuItem(menu, id, "&Save Image") |
---|
| 231 | item.SetBitmap(save_bmp) |
---|
| 232 | menu.AppendItem(item) |
---|
| 233 | wx.EVT_MENU(self, id, self.on_save_file) |
---|
| 234 | |
---|
| 235 | id = wx.NewId() |
---|
| 236 | item = wx.MenuItem(menu, id, "&Print Image") |
---|
| 237 | item.SetBitmap(print_bmp) |
---|
| 238 | menu.AppendItem(item) |
---|
| 239 | wx.EVT_MENU(self, id, self.on_print_image) |
---|
| 240 | |
---|
| 241 | id = wx.NewId() |
---|
| 242 | item = wx.MenuItem(menu, id, "&Print Preview") |
---|
| 243 | item.SetBitmap(preview_bmp) |
---|
| 244 | menu.AppendItem(item) |
---|
| 245 | wx.EVT_MENU(self, id, self.on_print_preview) |
---|
| 246 | |
---|
| 247 | menu.AppendSeparator() |
---|
| 248 | id = wx.NewId() |
---|
| 249 | item = wx.MenuItem(menu, id, "&Quit") |
---|
| 250 | item.SetBitmap(quit_bmp) |
---|
| 251 | menu.AppendItem(item) |
---|
| 252 | |
---|
| 253 | menu_bar.Append(menu, "&File") |
---|
| 254 | wx.EVT_MENU(self, id, self.on_close) |
---|
| 255 | |
---|
| 256 | menu_edit = wx.Menu() |
---|
| 257 | id = wx.NewId() |
---|
| 258 | item = wx.MenuItem(menu_edit, id, "&Copy") |
---|
| 259 | item.SetBitmap(copy_bmp) |
---|
| 260 | menu_edit.AppendItem(item) |
---|
| 261 | wx.EVT_MENU(self, id, self.on_copy_image) |
---|
| 262 | |
---|
| 263 | menu_bar.Append(menu_edit, "&Edit") |
---|
| 264 | self.menu_bar = menu_bar |
---|
| 265 | self.SetMenuBar(self.menu_bar) |
---|
[6550b64] | 266 | |
---|
| 267 | def set_plot_unfocus(self): |
---|
| 268 | """ |
---|
| 269 | un focusing |
---|
| 270 | """ |
---|
| 271 | pass |
---|
| 272 | |
---|
| 273 | def add_plot(self, plot): |
---|
| 274 | """ |
---|
| 275 | Add Image |
---|
| 276 | """ |
---|
| 277 | plotpanel = self.plotpanel |
---|
| 278 | plotpanel.scale = self.scale |
---|
| 279 | plotpanel.show_plot(plot) |
---|
| 280 | |
---|
| 281 | def set_schedule_full_draw(self, panel, func): |
---|
| 282 | """ |
---|
| 283 | """ |
---|
| 284 | self.plotpanel.resizing = False |
---|
| 285 | |
---|
[fad6056] | 286 | def im_show(self, img): |
---|
| 287 | """ |
---|
| 288 | Show background image |
---|
| 289 | :Param img: [imread(path) from matplotlib.pyplot] |
---|
| 290 | """ |
---|
| 291 | self.plotpanel.subplot.imshow(img) |
---|
| 292 | |
---|
[6550b64] | 293 | def set_schedule(self, schedule=False): |
---|
| 294 | """ |
---|
| 295 | """ |
---|
| 296 | #Not implemented |
---|
| 297 | |
---|
| 298 | def disable_app_menu(self, panel): |
---|
| 299 | """ |
---|
| 300 | """ |
---|
| 301 | #Not implemented |
---|
| 302 | |
---|
| 303 | def get_current_context_menu(self, plotpanel): |
---|
| 304 | """ |
---|
| 305 | """ |
---|
| 306 | #Not implemented |
---|
[9e9e9a5] | 307 | |
---|
| 308 | def on_save_file(self, event): |
---|
| 309 | """ |
---|
| 310 | Save image |
---|
| 311 | """ |
---|
| 312 | self.plotpanel.onSaveImage(event) |
---|
| 313 | |
---|
| 314 | def on_print_image(self, event): |
---|
| 315 | """ |
---|
| 316 | Save image |
---|
| 317 | """ |
---|
| 318 | self.plotpanel.onPrint(event) |
---|
| 319 | |
---|
| 320 | def on_print_preview(self, event): |
---|
| 321 | """ |
---|
| 322 | Save image |
---|
| 323 | """ |
---|
| 324 | self.plotpanel.onPrinterPreview(event) |
---|
| 325 | |
---|
| 326 | def on_copy_image(self, event): |
---|
| 327 | """ |
---|
| 328 | Save image |
---|
| 329 | """ |
---|
| 330 | self.plotpanel.OnCopyFigureMenu(event) |
---|
| 331 | |
---|
[6550b64] | 332 | def on_close(self, event): |
---|
| 333 | """ |
---|
| 334 | On Close |
---|
| 335 | """ |
---|
| 336 | try: |
---|
| 337 | self.parent.set_scale2d(self.scale) |
---|
| 338 | self.parent.on_panel_close(event) |
---|
| 339 | except: |
---|
| 340 | self.Destroy() |
---|
| 341 | |
---|