1 | import wx |
---|
2 | from PlotPanel import PlotPanel |
---|
3 | from plottables import Plottable, Graph, Data1D, Theory1D |
---|
4 | import sys |
---|
5 | import numpy |
---|
6 | import random, math |
---|
7 | |
---|
8 | class SANSplotpanel(PlotPanel): |
---|
9 | |
---|
10 | def __init__(self, parent, id = -1, color = None,\ |
---|
11 | dpi = None, **kwargs): |
---|
12 | PlotPanel.__init__(self, parent, id=id, color=color, dpi=dpi, **kwargs) |
---|
13 | |
---|
14 | # Keep track of the parent Frame |
---|
15 | self.parent = parent |
---|
16 | |
---|
17 | # Internal list of plottable names (because graph |
---|
18 | # doesn't have a dictionary of handles for the plottables) |
---|
19 | self.plots = {} |
---|
20 | |
---|
21 | def add_plottable(self, plot): |
---|
22 | self.plots[plot.name] = plot |
---|
23 | |
---|
24 | self.graph.add(plot) |
---|
25 | self.graph.xaxis('\\rm{q} ', 'A^{-1}') |
---|
26 | self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
27 | self.onResetGraph(None) |
---|
28 | |
---|
29 | def plottable_selected(self, id): |
---|
30 | PlotPanel.plottable_selected(self, id) |
---|
31 | if id is not None: |
---|
32 | self.parent.SetStatusText("Hovering over %s" % self.graph.selected_plottable) |
---|
33 | else: |
---|
34 | self.parent.SetStatusText("") |
---|
35 | |
---|
36 | |
---|
37 | def onContextMenu(self, event): |
---|
38 | """ |
---|
39 | Default context menu for a plot panel |
---|
40 | """ |
---|
41 | # Slicer plot popup menu |
---|
42 | id = wx.NewId() |
---|
43 | slicerpop = wx.Menu() |
---|
44 | slicerpop.Append(id,'&Save image', 'Save image as PNG') |
---|
45 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
46 | |
---|
47 | slicerpop.AppendSeparator() |
---|
48 | id = wx.NewId() |
---|
49 | slicerpop.Append(id, '&Change scale') |
---|
50 | wx.EVT_MENU(self, id, self._onProperties) |
---|
51 | |
---|
52 | id = wx.NewId() |
---|
53 | slicerpop.Append(id, '&Reset Graph') |
---|
54 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
55 | |
---|
56 | if self.graph.selected_plottable in self.plots: |
---|
57 | # Careful here: Message to status bar |
---|
58 | self.parent.SetStatusText("Fit a line to %s" % self.graph.selected_plottable) |
---|
59 | id = wx.NewId() |
---|
60 | slicerpop.AppendSeparator() |
---|
61 | slicerpop.Append(id, '&Linear Fit to %s' % self.graph.selected_plottable ) |
---|
62 | wx.EVT_MENU(self, id, self.onFitting) |
---|
63 | |
---|
64 | pos = event.GetPosition() |
---|
65 | pos = self.ScreenToClient(pos) |
---|
66 | self.PopupMenu(slicerpop, pos) |
---|
67 | |
---|
68 | def onFitting(self, event): |
---|
69 | if self.graph.selected_plottable is not None: |
---|
70 | if self.plots[self.graph.selected_plottable].__class__.__name__ == 'Data1D': |
---|
71 | PlotPanel.onFitting(self, event) |
---|
72 | else: |
---|
73 | self.parent.SetStatusText("Can't fit a theory curve") |
---|
74 | |
---|
75 | class ViewerFrame(wx.Frame): |
---|
76 | """ |
---|
77 | Add comment |
---|
78 | """ |
---|
79 | def __init__(self, parent, id, title): |
---|
80 | """ |
---|
81 | comment |
---|
82 | @param parent: parent panel/container |
---|
83 | """ |
---|
84 | # Initialize the Frame object |
---|
85 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(950,850)) |
---|
86 | |
---|
87 | # Panel for 1D plot |
---|
88 | self.plotpanel = SANSplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
89 | |
---|
90 | # Set up the menu |
---|
91 | self._setup_menus() |
---|
92 | |
---|
93 | # Set up the layout |
---|
94 | self._setup_layout() |
---|
95 | |
---|
96 | # Register the close event so it calls our own method |
---|
97 | wx.EVT_CLOSE(self, self._onClose) |
---|
98 | |
---|
99 | |
---|
100 | def _setup_layout(self): |
---|
101 | """ |
---|
102 | Set up the layout |
---|
103 | """ |
---|
104 | # Status bar |
---|
105 | self.sb = self.CreateStatusBar() |
---|
106 | self.SetStatusText("This is a data viewer") |
---|
107 | |
---|
108 | # Two columns of panels |
---|
109 | sizer = wx.GridBagSizer(0,0) |
---|
110 | |
---|
111 | sizer.Add(self.plotpanel, (0,0), (1,1), flag=wx.EXPAND | wx.ALL, border=0) |
---|
112 | sizer.AddGrowableRow(0) |
---|
113 | sizer.AddGrowableCol(0) |
---|
114 | |
---|
115 | self.SetSizer(sizer) |
---|
116 | self.Centre() |
---|
117 | |
---|
118 | def _setup_menus(self): |
---|
119 | """ |
---|
120 | Set up the application menus |
---|
121 | """ |
---|
122 | |
---|
123 | # Menu |
---|
124 | menubar = wx.MenuBar() |
---|
125 | # File menu |
---|
126 | filemenu = wx.Menu() |
---|
127 | |
---|
128 | # Quit |
---|
129 | id = wx.NewId() |
---|
130 | filemenu.Append(id,'&Quit', 'Exit') |
---|
131 | wx.EVT_MENU(self, id, self.Close) |
---|
132 | |
---|
133 | # New plot |
---|
134 | id = wx.NewId() |
---|
135 | filemenu.Append(id,'&Add data', 'Add a new curve to the plot') |
---|
136 | wx.EVT_MENU(self, id, self._add_data) |
---|
137 | |
---|
138 | # Add sub menus |
---|
139 | menubar.Append(filemenu, '&File') |
---|
140 | |
---|
141 | self.SetMenuBar(menubar) |
---|
142 | |
---|
143 | def _onClose(self, event): |
---|
144 | """ |
---|
145 | """ |
---|
146 | wx.Exit() |
---|
147 | sys.exit() |
---|
148 | |
---|
149 | def _add_data(self, event): |
---|
150 | data_len = 25 |
---|
151 | x = numpy.zeros(data_len) |
---|
152 | y = numpy.zeros(data_len) |
---|
153 | x2 = numpy.zeros(data_len) |
---|
154 | y2 = numpy.zeros(data_len) |
---|
155 | dy2 = numpy.zeros(data_len) |
---|
156 | x3 = numpy.zeros(data_len) |
---|
157 | y3 = numpy.zeros(data_len) |
---|
158 | dy3 = numpy.zeros(data_len) |
---|
159 | for i in range(len(x)): |
---|
160 | x[i] = i |
---|
161 | x2[i] = i-0.1+0.2*random.random() |
---|
162 | x3[i] = i-0.1+0.2*random.random() |
---|
163 | y[i] = 0.5*(i+random.random()) |
---|
164 | y2[i] = i+random.random() |
---|
165 | dy2[i] = math.sqrt(y2[i]) |
---|
166 | y3[i] = 0.3*(i+random.random()) |
---|
167 | dy3[i] = math.sqrt(y3[i]) |
---|
168 | |
---|
169 | newplot = Theory1D(x=x, y=y) |
---|
170 | newplot.name = "Theory curve" |
---|
171 | self.plotpanel.add_plottable(newplot) |
---|
172 | |
---|
173 | newplot = Data1D(x=x2, y=y2, dy=dy2) |
---|
174 | newplot.name = "Data set 1" |
---|
175 | self.plotpanel.add_plottable(newplot) |
---|
176 | |
---|
177 | newplot = Data1D(x=x3, y=y3, dy=dy3) |
---|
178 | newplot.name = "Data set 2" |
---|
179 | self.plotpanel.add_plottable(newplot) |
---|
180 | |
---|
181 | |
---|
182 | class ViewApp(wx.App): |
---|
183 | def OnInit(self): |
---|
184 | frame = ViewerFrame(None, -1, 'testView') |
---|
185 | frame.Show(True) |
---|
186 | self.SetTopWindow(frame) |
---|
187 | |
---|
188 | return True |
---|
189 | |
---|
190 | |
---|
191 | if __name__ == "__main__": |
---|
192 | app = ViewApp(0) |
---|
193 | app.MainLoop() |
---|
194 | |
---|