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