[959eb01] | 1 | """ |
---|
| 2 | Simple Plot Frame : supporting only copy, print, scale |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
| 5 | from sas.sasgui.guiframe.local_perspectives.plotting.Plotter2D import ModelPanel2D as PlotPanel |
---|
| 6 | from sas.sasgui.plottools.toolbar import NavigationToolBar |
---|
| 7 | from sas.sasgui.plottools.plottables import Graph |
---|
| 8 | from sas.sasgui.guiframe.utils import PanelMenu |
---|
| 9 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 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) |
---|
| 22 | |
---|
| 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 | 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) |
---|
| 64 | |
---|
| 65 | if self.dimension != 3: |
---|
| 66 | slicerpop.AppendSeparator() |
---|
| 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) |
---|
| 70 | |
---|
| 71 | if self.data.__class__.__name__ == 'Data1D': |
---|
| 72 | slicerpop.AppendSeparator() |
---|
| 73 | wx_id = wx.NewId() |
---|
| 74 | slicerpop.Append(wx_id, '&Change Scale') |
---|
| 75 | wx.EVT_MENU(self, wx_id, self._onProperties) |
---|
| 76 | elif self.data2D.__class__.__name__ == 'Data2D': |
---|
| 77 | slicerpop.AppendSeparator() |
---|
| 78 | wx_id = wx.NewId() |
---|
| 79 | slicerpop.Append(wx_id, '&Toggle Linear/Log Scale') |
---|
| 80 | wx.EVT_MENU(self, wx_id, self._onToggleScale) |
---|
| 81 | |
---|
| 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) |
---|
[7432acb] | 89 | if self.scale is not None: |
---|
[959eb01] | 90 | self.parent.scale2d = self.scale |
---|
| 91 | |
---|
| 92 | def on_grid_onoff(self, event): |
---|
| 93 | """ |
---|
| 94 | On grid on/off |
---|
| 95 | """ |
---|
| 96 | switch = (not self.grid_on) |
---|
| 97 | self.onGridOnOff(switch) |
---|
| 98 | |
---|
| 99 | def onLeftDown(self, event): |
---|
| 100 | """ |
---|
| 101 | left button down and ready to drag |
---|
| 102 | |
---|
| 103 | """ |
---|
| 104 | # Check that the LEFT button was pressed |
---|
| 105 | if event.button == 1: |
---|
| 106 | self.leftdown = True |
---|
| 107 | ax = event.inaxes |
---|
[7432acb] | 108 | if ax is not None: |
---|
[959eb01] | 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: |
---|
| 118 | self.position = None |
---|
| 119 | |
---|
| 120 | def _OnReSize(self, event): |
---|
| 121 | """ |
---|
| 122 | On response of the resize of a panel, set axes_visiable False |
---|
| 123 | """ |
---|
| 124 | self.resizing = False |
---|
[7432acb] | 125 | if self.x_size is not None: |
---|
[959eb01] | 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 |
---|
| 132 | # Do not remove this Skip. |
---|
| 133 | # Otherwise it will get runtime error on wx>=2.9. |
---|
| 134 | event.Skip() |
---|
| 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) |
---|
| 141 | |
---|
| 142 | def on_set_focus(self, event): |
---|
| 143 | """ |
---|
| 144 | By pass default boundary blue color drawing |
---|
| 145 | """ |
---|
| 146 | pass |
---|
| 147 | |
---|
| 148 | def on_kill_focus(self, event): |
---|
| 149 | """ |
---|
| 150 | Reset the panel color |
---|
| 151 | """ |
---|
| 152 | pass |
---|
| 153 | |
---|
| 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 |
---|
[7432acb] | 163 | if plot.xtransform is not None: |
---|
[959eb01] | 164 | self.xLabel = plot.xtransform |
---|
[7432acb] | 165 | if plot.ytransform is not None: |
---|
[959eb01] | 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) |
---|
| 180 | self.yaxis(_yaxis, _yunit) |
---|
| 181 | self.set_xscale(self.xscale) |
---|
| 182 | self.set_yscale(self.yscale) |
---|
| 183 | self.graph.render(self) |
---|
| 184 | |
---|
| 185 | class PlotFrame(wx.Frame): |
---|
| 186 | """ |
---|
| 187 | Frame for simple plot |
---|
| 188 | """ |
---|
| 189 | def __init__(self, parent, id, title, scale='log_{10}', |
---|
| 190 | size=wx.Size(550, 470), show_menu_icons=True): |
---|
| 191 | """ |
---|
| 192 | comment |
---|
| 193 | :param parent: parent panel/container |
---|
| 194 | """ |
---|
| 195 | # Initialize the Frame object |
---|
| 196 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size) |
---|
| 197 | |
---|
| 198 | # Panel for 1D plot |
---|
| 199 | self.parent = parent |
---|
| 200 | self._mgr = None |
---|
| 201 | self.menu_bar = None |
---|
| 202 | self._default_save_location = None |
---|
| 203 | self.scale = scale |
---|
| 204 | self._show_menu_icons = show_menu_icons |
---|
| 205 | self.plotpanel = SimplePlotPanel(self, -1) |
---|
| 206 | self._build_menubar() |
---|
| 207 | |
---|
| 208 | def _build_menubar(self): |
---|
| 209 | """ |
---|
| 210 | Build menubar |
---|
| 211 | """ |
---|
| 212 | tsize = (13, 13) |
---|
| 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) |
---|
| 217 | menu_bar = wx.MenuBar() |
---|
| 218 | |
---|
| 219 | menu = wx.Menu() |
---|
| 220 | id = wx.NewId() |
---|
| 221 | save_item = wx.MenuItem(menu, id, "&Save Image") |
---|
| 222 | menu.AppendItem(save_item) |
---|
| 223 | wx.EVT_MENU(self, id, self.on_save_file) |
---|
| 224 | |
---|
| 225 | id = wx.NewId() |
---|
| 226 | print_item = wx.MenuItem(menu, id, "&Print Image") |
---|
| 227 | menu.AppendItem(print_item) |
---|
| 228 | wx.EVT_MENU(self, id, self.on_print_image) |
---|
| 229 | |
---|
| 230 | menu.AppendSeparator() |
---|
| 231 | id = wx.NewId() |
---|
| 232 | quit_item = wx.MenuItem(menu, id, "&Quit") |
---|
| 233 | menu.AppendItem(quit_item) |
---|
| 234 | |
---|
| 235 | menu_bar.Append(menu, "&File") |
---|
| 236 | wx.EVT_MENU(self, id, self.on_close) |
---|
| 237 | |
---|
| 238 | menu_edit = wx.Menu() |
---|
| 239 | id = wx.NewId() |
---|
| 240 | copy_item = wx.MenuItem(menu_edit, id, "&Copy") |
---|
| 241 | menu_edit.AppendItem(copy_item) |
---|
| 242 | wx.EVT_MENU(self, id, self.on_copy_image) |
---|
| 243 | |
---|
| 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 | |
---|
| 250 | menu_bar.Append(menu_edit, "&Edit") |
---|
| 251 | self.menu_bar = menu_bar |
---|
| 252 | self.SetMenuBar(self.menu_bar) |
---|
| 253 | |
---|
| 254 | def set_plot_unfocus(self): |
---|
| 255 | """ |
---|
| 256 | un focusing |
---|
| 257 | """ |
---|
| 258 | pass |
---|
| 259 | |
---|
| 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) |
---|
| 267 | |
---|
| 268 | def set_schedule_full_draw(self, panel, func): |
---|
| 269 | """ |
---|
| 270 | """ |
---|
| 271 | self.plotpanel.resizing = False |
---|
| 272 | |
---|
| 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) |
---|
| 279 | |
---|
| 280 | def set_schedule(self, schedule=False): |
---|
| 281 | """ |
---|
| 282 | """ |
---|
| 283 | #Not implemented |
---|
| 284 | |
---|
| 285 | def disable_app_menu(self, panel): |
---|
| 286 | """ |
---|
| 287 | """ |
---|
| 288 | #Not implemented |
---|
| 289 | |
---|
| 290 | def get_current_context_menu(self, plotpanel): |
---|
| 291 | """ |
---|
| 292 | """ |
---|
| 293 | #Not implemented |
---|
| 294 | |
---|
| 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) |
---|
| 306 | |
---|
| 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) |
---|
| 318 | |
---|
| 319 | def on_close(self, event): |
---|
| 320 | """ |
---|
| 321 | On Close |
---|
| 322 | """ |
---|
| 323 | try: |
---|
| 324 | self.parent.set_scale2d(self.scale) |
---|
| 325 | self.parent.on_panel_close(event) |
---|
| 326 | except: |
---|
| 327 | self.Destroy() |
---|