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