1 | |
---|
2 | |
---|
3 | |
---|
4 | import wx |
---|
5 | import sys |
---|
6 | from copy import deepcopy |
---|
7 | from danse.common.plottools.plottables import Graph |
---|
8 | from Plotter1D import ModelPanel1D as PlotPanel |
---|
9 | from sans.guiframe.dataFitting import Theory1D |
---|
10 | import pylab |
---|
11 | |
---|
12 | DEFAULT_CMAP = pylab.cm.jet |
---|
13 | _BOX_WIDTH = 76 |
---|
14 | _STATICBOX_WIDTH = 400 |
---|
15 | _SCALE = 1e-6 |
---|
16 | |
---|
17 | #SLD panel size |
---|
18 | if sys.platform.count("win32") > 0: |
---|
19 | _STATICBOX_WIDTH = 570 |
---|
20 | PANEL_SIZE = 475 |
---|
21 | FONT_VARIANT = 0 |
---|
22 | else: |
---|
23 | _STATICBOX_WIDTH = 610 |
---|
24 | PANEL_SIZE = 500 |
---|
25 | FONT_VARIANT = 1 |
---|
26 | |
---|
27 | |
---|
28 | class SLDPanel(wx.Dialog): |
---|
29 | """ |
---|
30 | Provides the SLD profile plot panel. |
---|
31 | """ |
---|
32 | ## Internal nickname for the window, used by the AUI manager |
---|
33 | window_name = "SLD Profile" |
---|
34 | ## Name to appear on the window title bar |
---|
35 | window_caption = "SLD Profile" |
---|
36 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
37 | CENTER_PANE = True |
---|
38 | def __init__(self, parent=None, base=None, data=None, axes =['Radius'], |
---|
39 | id=-1, *args, **kwds): |
---|
40 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
41 | kwds["size"] = wx.Size(_STATICBOX_WIDTH, PANEL_SIZE) |
---|
42 | wx.Dialog.__init__(self, parent, id=id, *args, **kwds) |
---|
43 | |
---|
44 | if data != None: |
---|
45 | #Font size |
---|
46 | kwds = [] |
---|
47 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
48 | self.SetTitle("Scattering Length Density Profile") |
---|
49 | self.parent = base |
---|
50 | self.data = data |
---|
51 | self.str = self.data.__str__() |
---|
52 | ## when 2 data have the same id override the 1 st plotted |
---|
53 | self.name = self.data.name |
---|
54 | # Panel for plot |
---|
55 | self.plotpanel = SLDplotpanel(self, axes, -1, |
---|
56 | style=wx.TRANSPARENT_WINDOW) |
---|
57 | self.cmap = DEFAULT_CMAP |
---|
58 | ## Create Artist and bind it |
---|
59 | self.subplot = self.plotpanel.subplot |
---|
60 | # layout |
---|
61 | self._setup_layout() |
---|
62 | # plot |
---|
63 | data_plot = deepcopy(self.data) |
---|
64 | data_plot.dy = self._set_dy_data() |
---|
65 | self.newplot = Theory1D(data_plot.x, data_plot.y, data_plot.dy) |
---|
66 | self.newplot.name = 'SLD' |
---|
67 | self.plotpanel.add_image(self.newplot) |
---|
68 | self.Centre() |
---|
69 | self.Layout() |
---|
70 | |
---|
71 | def _set_dy_data(self): |
---|
72 | """ |
---|
73 | make fake dy data |
---|
74 | |
---|
75 | :return dy: |
---|
76 | """ |
---|
77 | # set dy as zero |
---|
78 | dy = [0 for y in self.data.y] |
---|
79 | return dy |
---|
80 | |
---|
81 | def _setup_layout(self): |
---|
82 | """ |
---|
83 | Set up the layout |
---|
84 | """ |
---|
85 | # panel sizer |
---|
86 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
87 | sizer.Add(self.plotpanel, -1, wx.LEFT|wx.RIGHT, 5) |
---|
88 | sizer.Add((0, 10)) |
---|
89 | #-----Button------------1 |
---|
90 | id = wx.NewId() |
---|
91 | button_reset = wx.Button(self, id, "Close") |
---|
92 | button_reset.SetToolTipString("Close...") |
---|
93 | button_reset.Bind(wx.EVT_BUTTON, self._close, |
---|
94 | id=button_reset.GetId()) |
---|
95 | sizer.Add(button_reset, 0, wx.LEFT, _STATICBOX_WIDTH - 80) |
---|
96 | sizer.Add((0, 10)) |
---|
97 | self.SetSizerAndFit(sizer) |
---|
98 | self.Centre() |
---|
99 | self.Show(True) |
---|
100 | button_reset.SetFocus() |
---|
101 | |
---|
102 | def _close(self, event): |
---|
103 | """ |
---|
104 | Close the dialog |
---|
105 | """ |
---|
106 | self.Close(True) |
---|
107 | |
---|
108 | def _draw_model(self, event): |
---|
109 | """ |
---|
110 | on_close, update the model2d plot |
---|
111 | """ |
---|
112 | pass |
---|
113 | |
---|
114 | def get_context_menu(self, graph=None): |
---|
115 | """ |
---|
116 | When the context menu of a plot is rendered, the |
---|
117 | get_context_menu method will be called to give you a |
---|
118 | chance to add a menu item to the context menu. |
---|
119 | :param graph: the Graph object to which we attach the context menu |
---|
120 | |
---|
121 | :return: a list of menu items with call-back function |
---|
122 | """ |
---|
123 | return [] |
---|
124 | |
---|
125 | |
---|
126 | class SLDplotpanel(PlotPanel): |
---|
127 | """ |
---|
128 | Panel |
---|
129 | """ |
---|
130 | def __init__(self, parent, axes=[], id=-1, color=None, dpi=None, |
---|
131 | **kwargs): |
---|
132 | """ |
---|
133 | """ |
---|
134 | PlotPanel.__init__(self, parent, id=id, xtransform='x', ytransform='y', |
---|
135 | color=color, dpi=dpi, |
---|
136 | size=(_STATICBOX_WIDTH, PANEL_SIZE-100), **kwargs) |
---|
137 | |
---|
138 | # Keep track of the parent Frame |
---|
139 | self.parent = parent |
---|
140 | # Internal list of plottable names (because graph |
---|
141 | # doesn't have a dictionary of handles for the plottables) |
---|
142 | self.plots = {} |
---|
143 | self.graph = Graph() |
---|
144 | self.axes_label = [] |
---|
145 | for idx in range(0, len(axes)): |
---|
146 | self.axes_label.append(axes[idx]) |
---|
147 | |
---|
148 | def add_image(self, plot): |
---|
149 | """ |
---|
150 | Add image(Theory1D) |
---|
151 | """ |
---|
152 | self.plots[plot.name] = plot |
---|
153 | #init graph |
---|
154 | self.gaph = Graph() |
---|
155 | #add plot |
---|
156 | self.graph.add(plot) |
---|
157 | #add axes |
---|
158 | x1_label = self.axes_label[0] |
---|
159 | self.graph.xaxis('\\rm{%s} '% x1_label, 'A') |
---|
160 | self.graph.yaxis('\\rm{SLD} ', 'A^{-2}') |
---|
161 | #draw |
---|
162 | self.graph.render(self) |
---|
163 | self.subplot.figure.canvas.draw_idle() |
---|
164 | |
---|
165 | # For latter scale changes |
---|
166 | self.plots[plot.name].xaxis('\\rm{%s} '% x1_label, 'A') |
---|
167 | self.plots[plot.name].yaxis('\\rm{SLD} ', 'A^{-2}') |
---|
168 | |
---|
169 | def on_set_focus(self, event): |
---|
170 | """ |
---|
171 | send to the parenet the current panel on focus |
---|
172 | |
---|
173 | """ |
---|
174 | #Not implemented |
---|
175 | pass |
---|
176 | |
---|
177 | def on_kill_focus(self, event): |
---|
178 | """ |
---|
179 | reset the panel color |
---|
180 | |
---|
181 | """ |
---|
182 | #Not implemented |
---|
183 | pass |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | class ViewerFrame(wx.Frame): |
---|
188 | """ |
---|
189 | Add comment |
---|
190 | """ |
---|
191 | def __init__(self, parent, id, title): |
---|
192 | """ |
---|
193 | comment |
---|
194 | :param parent: parent panel/container |
---|
195 | """ |
---|
196 | # Initialize the Frame object |
---|
197 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
198 | wx.Size(_STATICBOX_WIDTH, PANEL_SIZE)) |
---|
199 | # Panel for 1D plot |
---|
200 | self.plotpanel = SLDplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
201 | |
---|
202 | class ViewApp(wx.App): |
---|
203 | def OnInit(self): |
---|
204 | frame = ViewerFrame(None, -1, 'testView') |
---|
205 | frame.Show(True) |
---|
206 | self.SetTopWindow(frame) |
---|
207 | |
---|
208 | return True |
---|
209 | |
---|
210 | if __name__ == "__main__": |
---|
211 | app = ViewApp(0) |
---|
212 | app.MainLoop() |
---|