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.name = 'SLD' |
---|
72 | self.plotpanel.add_image(self.newplot) |
---|
73 | self.plotpanel.subplot.set_ylim(min(data_plot.y) - _Y_OFF , |
---|
74 | max(data_plot.y) + _Y_OFF) |
---|
75 | self.plotpanel.subplot.set_xlim(min(data_plot.x) - _X_OFF, |
---|
76 | max(data_plot.x) + _X_OFF) |
---|
77 | #self.Centre() |
---|
78 | self.Layout() |
---|
79 | |
---|
80 | def _set_dy_data(self): |
---|
81 | """ |
---|
82 | make fake dy data |
---|
83 | |
---|
84 | :return dy: |
---|
85 | """ |
---|
86 | # set dy as zero |
---|
87 | dy = [0 for y in self.data.y] |
---|
88 | return dy |
---|
89 | |
---|
90 | def _set_y_data(self): |
---|
91 | """ |
---|
92 | make y data unit Mega times |
---|
93 | |
---|
94 | :return y_value: |
---|
95 | """ |
---|
96 | # changes the unit |
---|
97 | y_value = [yvalue * 1e+006 for yvalue in self.data.y] |
---|
98 | |
---|
99 | return y_value |
---|
100 | |
---|
101 | def _setup_layout(self): |
---|
102 | """ |
---|
103 | Set up the layout |
---|
104 | """ |
---|
105 | # panel sizer |
---|
106 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
107 | sizer.Add(self.plotpanel, -1, wx.LEFT|wx.RIGHT, 5) |
---|
108 | sizer.Add((0, 10)) |
---|
109 | #-----Button------------1 |
---|
110 | id = wx.NewId() |
---|
111 | button_reset = wx.Button(self, id, "Close") |
---|
112 | button_reset.SetToolTipString("Close...") |
---|
113 | button_reset.Bind(wx.EVT_BUTTON, self._close, |
---|
114 | id=button_reset.GetId()) |
---|
115 | sizer.Add(button_reset, 0, wx.LEFT, _STATICBOX_WIDTH - 80) |
---|
116 | sizer.Add((0, 10)) |
---|
117 | self.SetSizerAndFit(sizer) |
---|
118 | self.Centre() |
---|
119 | self.Show(True) |
---|
120 | button_reset.SetFocus() |
---|
121 | |
---|
122 | def _close(self, event): |
---|
123 | """ |
---|
124 | Close the dialog |
---|
125 | """ |
---|
126 | self.Close(True) |
---|
127 | |
---|
128 | def _draw_model(self, event): |
---|
129 | """ |
---|
130 | on_close, update the model2d plot |
---|
131 | """ |
---|
132 | pass |
---|
133 | |
---|
134 | def get_context_menu(self, graph=None): |
---|
135 | """ |
---|
136 | When the context menu of a plot is rendered, the |
---|
137 | get_context_menu method will be called to give you a |
---|
138 | chance to add a menu item to the context menu. |
---|
139 | :param graph: the Graph object to which we attach the context menu |
---|
140 | |
---|
141 | :return: a list of menu items with call-back function |
---|
142 | """ |
---|
143 | return [] |
---|
144 | |
---|
145 | def set_schedule_full_draw(self, panel=None, func=None): |
---|
146 | """ |
---|
147 | Set_schedule for full draw |
---|
148 | """ |
---|
149 | # Not implemented |
---|
150 | pass |
---|
151 | |
---|
152 | def set_schedule(self, schedule=False): |
---|
153 | """ |
---|
154 | Set schedule for redraw |
---|
155 | """ |
---|
156 | # Not implemented |
---|
157 | pass |
---|
158 | |
---|
159 | class SLDplotpanel(PlotPanel): |
---|
160 | """ |
---|
161 | Panel |
---|
162 | """ |
---|
163 | def __init__(self, parent, axes=[], id=-1, color=None, dpi=None, |
---|
164 | **kwargs): |
---|
165 | """ |
---|
166 | """ |
---|
167 | PlotPanel.__init__(self, parent, id=id, xtransform='x', ytransform='y', |
---|
168 | color=color, dpi=dpi, |
---|
169 | size=(_STATICBOX_WIDTH, PANEL_SIZE-100), **kwargs) |
---|
170 | |
---|
171 | # Keep track of the parent Frame |
---|
172 | self.parent = parent |
---|
173 | # Internal list of plottable names (because graph |
---|
174 | # doesn't have a dictionary of handles for the plottables) |
---|
175 | self.plots = {} |
---|
176 | self.graph = Graph() |
---|
177 | self.axes_label = [] |
---|
178 | for idx in range(0, len(axes)): |
---|
179 | self.axes_label.append(axes[idx]) |
---|
180 | |
---|
181 | def add_image(self, plot): |
---|
182 | """ |
---|
183 | Add image(Theory1D) |
---|
184 | """ |
---|
185 | self.plots[plot.name] = plot |
---|
186 | #init graph |
---|
187 | #self.gaph = Graph() |
---|
188 | #add plot |
---|
189 | self.graph.add(plot) |
---|
190 | #add axes |
---|
191 | x1_label = self.axes_label[0] |
---|
192 | self.graph.xaxis('\\rm{%s} '% x1_label, 'A') |
---|
193 | self.graph.yaxis('\\rm{SLD} ', '10^{-6}A^{-2}') |
---|
194 | #draw |
---|
195 | self.graph.render(self) |
---|
196 | self.subplot.figure.canvas.draw_idle() |
---|
197 | |
---|
198 | # For latter scale changes |
---|
199 | self.plots[plot.name].xaxis('\\rm{%s} '% x1_label, 'A') |
---|
200 | self.plots[plot.name].yaxis('\\rm{SLD} ', '10^{-6}A^{-2}') |
---|
201 | |
---|
202 | def on_set_focus(self, event): |
---|
203 | """ |
---|
204 | send to the parenet the current panel on focus |
---|
205 | |
---|
206 | """ |
---|
207 | #Not implemented |
---|
208 | pass |
---|
209 | |
---|
210 | def on_kill_focus(self, event): |
---|
211 | """ |
---|
212 | reset the panel color |
---|
213 | |
---|
214 | """ |
---|
215 | #Not implemented |
---|
216 | pass |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | class ViewerFrame(wx.Frame): |
---|
221 | """ |
---|
222 | Add comment |
---|
223 | """ |
---|
224 | def __init__(self, parent, id, title): |
---|
225 | """ |
---|
226 | comment |
---|
227 | :param parent: parent panel/container |
---|
228 | """ |
---|
229 | # Initialize the Frame object |
---|
230 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
231 | wx.Size(_STATICBOX_WIDTH, PANEL_SIZE)) |
---|
232 | # Panel for 1D plot |
---|
233 | self.plotpanel = SLDplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
234 | |
---|
235 | class ViewApp(wx.App): |
---|
236 | def OnInit(self): |
---|
237 | frame = ViewerFrame(None, -1, 'testView') |
---|
238 | frame.Show(True) |
---|
239 | self.SetTopWindow(frame) |
---|
240 | |
---|
241 | return True |
---|
242 | |
---|
243 | if __name__ == "__main__": |
---|
244 | app = ViewApp(0) |
---|
245 | app.MainLoop() |
---|