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 = 570 |
---|
22 | PANEL_SIZE = 475 |
---|
23 | FONT_VARIANT = 0 |
---|
24 | else: |
---|
25 | _STATICBOX_WIDTH = 610 |
---|
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, -1, 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 | class SLDplotpanel(PlotPanel): |
---|
169 | """ |
---|
170 | Panel |
---|
171 | """ |
---|
172 | def __init__(self, parent, axes=[], id=-1, color=None, dpi=None, |
---|
173 | **kwargs): |
---|
174 | """ |
---|
175 | """ |
---|
176 | PlotPanel.__init__(self, parent, id=id, xtransform='x', ytransform='y', |
---|
177 | color=color, dpi=dpi, |
---|
178 | size=(_STATICBOX_WIDTH, PANEL_SIZE-100), **kwargs) |
---|
179 | |
---|
180 | # Keep track of the parent Frame |
---|
181 | self.parent = parent |
---|
182 | # Internal list of plottable names (because graph |
---|
183 | # doesn't have a dictionary of handles for the plottables) |
---|
184 | self.plots = {} |
---|
185 | self.graph = Graph() |
---|
186 | self.axes_label = [] |
---|
187 | for idx in range(0, len(axes)): |
---|
188 | self.axes_label.append(axes[idx]) |
---|
189 | |
---|
190 | def add_image(self, plot): |
---|
191 | """ |
---|
192 | Add image(Theory1D) |
---|
193 | """ |
---|
194 | self.plots[plot.name] = plot |
---|
195 | self.plots[plot.name].is_data = False |
---|
196 | #init graph |
---|
197 | #self.gaph = Graph() |
---|
198 | #add plot |
---|
199 | self.graph.add(plot) |
---|
200 | #add axes |
---|
201 | x1_label = self.axes_label[0] |
---|
202 | self.graph.xaxis('\\rm{%s} '% x1_label, 'A') |
---|
203 | self.graph.yaxis('\\rm{SLD} ', '10^{-6}A^{-2}') |
---|
204 | #draw |
---|
205 | self.graph.render(self) |
---|
206 | self.subplot.figure.canvas.draw_idle() |
---|
207 | |
---|
208 | # For latter scale changes |
---|
209 | self.plots[plot.name].xaxis('\\rm{%s} '% x1_label, 'A') |
---|
210 | self.plots[plot.name].yaxis('\\rm{SLD} ', '10^{-6}A^{-2}') |
---|
211 | |
---|
212 | def on_set_focus(self, event): |
---|
213 | """ |
---|
214 | send to the parenet the current panel on focus |
---|
215 | |
---|
216 | """ |
---|
217 | #Not implemented |
---|
218 | pass |
---|
219 | |
---|
220 | def on_kill_focus(self, event): |
---|
221 | """ |
---|
222 | reset the panel color |
---|
223 | |
---|
224 | """ |
---|
225 | #Not implemented |
---|
226 | pass |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | class ViewerFrame(wx.Frame): |
---|
231 | """ |
---|
232 | Add comment |
---|
233 | """ |
---|
234 | def __init__(self, parent, id, title): |
---|
235 | """ |
---|
236 | comment |
---|
237 | :param parent: parent panel/container |
---|
238 | """ |
---|
239 | # Initialize the Frame object |
---|
240 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
241 | wx.Size(_STATICBOX_WIDTH, PANEL_SIZE)) |
---|
242 | # Panel for 1D plot |
---|
243 | self.plotpanel = SLDplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
244 | |
---|
245 | class ViewApp(wx.App): |
---|
246 | def OnInit(self): |
---|
247 | frame = ViewerFrame(None, -1, 'testView') |
---|
248 | frame.Show(True) |
---|
249 | self.SetTopWindow(frame) |
---|
250 | |
---|
251 | return True |
---|
252 | |
---|
253 | if __name__ == "__main__": |
---|
254 | app = ViewApp(0) |
---|
255 | app.MainLoop() |
---|