1 | """ |
---|
2 | Implement grid used to store data |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import numpy |
---|
6 | import sys |
---|
7 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
8 | import wx.grid as Grid |
---|
9 | import wx.aui |
---|
10 | from wx.aui import AuiNotebook as nb |
---|
11 | from sans.guiframe.panel_base import PanelBase |
---|
12 | import wx.lib.sheet as sheet |
---|
13 | |
---|
14 | from sans.guiframe.events import NewPlotEvent |
---|
15 | from sans.guiframe.events import StatusEvent |
---|
16 | from sans.guiframe.dataFitting import Data1D |
---|
17 | |
---|
18 | |
---|
19 | class GridPage(sheet.CSheet): |
---|
20 | def __init__(self, parent, panel=None): |
---|
21 | """ |
---|
22 | """ |
---|
23 | sheet.CSheet.__init__(self, parent) |
---|
24 | self.SetLabelBackgroundColour('#DBD4D4') |
---|
25 | self.panel = panel |
---|
26 | self.col_names = [] |
---|
27 | self._cols = 0 |
---|
28 | self._rows = 0 |
---|
29 | self.SetNumberRows(self._cols) |
---|
30 | self.SetNumberCols(self._rows) |
---|
31 | self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click) |
---|
32 | self.axis_value = [] |
---|
33 | self.axis_label = "" |
---|
34 | |
---|
35 | |
---|
36 | def on_right_click(self, event): |
---|
37 | print "on double click", event.GetRow(), event.GetCol() |
---|
38 | col = event.GetCol() |
---|
39 | if col != -1 and len(self.col_names) > col: |
---|
40 | label = self.col_names[int(col)] |
---|
41 | self.axis_label = label |
---|
42 | if label in self.data.keys(): |
---|
43 | col_val = self.data[label] |
---|
44 | self.axis_value = col_val |
---|
45 | # Slicer plot popup menu |
---|
46 | slicerpop = wx.Menu() |
---|
47 | id = wx.NewId() |
---|
48 | slicerpop.Append(id, '&Set X axis', 'Set X axis') |
---|
49 | wx.EVT_MENU(self, id, self.on_set_x_axis) |
---|
50 | |
---|
51 | |
---|
52 | id = wx.NewId() |
---|
53 | slicerpop.Append(id, '&Set Y axis', 'Set Y axis') |
---|
54 | wx.EVT_MENU(self, id, self.on_set_y_axis) |
---|
55 | pos = event.GetPosition() |
---|
56 | pos = self.ScreenToClient(pos) |
---|
57 | self.PopupMenu(slicerpop, pos) |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | def on_set_x_axis(self, event): |
---|
62 | self.panel.set_xaxis(x=self.axis_value, label=self.axis_label) |
---|
63 | |
---|
64 | def on_set_y_axis(self, event): |
---|
65 | self.panel.set_yaxis(y=self.axis_value, label=self.axis_label) |
---|
66 | |
---|
67 | def set_data(self, data): |
---|
68 | """ |
---|
69 | """ |
---|
70 | if data is None: |
---|
71 | data = {} |
---|
72 | if len(data) > 0: |
---|
73 | self.data = data |
---|
74 | self.col_names = data.keys() |
---|
75 | self.col_names.sort() |
---|
76 | self._cols = len(self.data.keys()) |
---|
77 | self._rows = max([len(v) for v in self.data.values()]) |
---|
78 | self.SetNumberRows(int(self._rows)) |
---|
79 | self.SetNumberCols(int(self._cols)) |
---|
80 | for index in range(len(self.col_names)): |
---|
81 | self.SetColLabelValue(index, str(self.col_names[index])) |
---|
82 | |
---|
83 | col = 0 |
---|
84 | for value_list in self.data.values(): |
---|
85 | for row in range(len(value_list)): |
---|
86 | self.SetCellValue(row, col, str(value_list[row])) |
---|
87 | col += 1 |
---|
88 | self.AutoSize() |
---|
89 | |
---|
90 | class Notebook(nb, PanelBase): |
---|
91 | """ |
---|
92 | ## Internal name for the AUI manager |
---|
93 | window_name = "Fit panel" |
---|
94 | ## Title to appear on top of the window |
---|
95 | """ |
---|
96 | window_caption = "Notebook " |
---|
97 | |
---|
98 | def __init__(self, parent, manager=None, data=None, *args, **kwargs): |
---|
99 | """ |
---|
100 | """ |
---|
101 | nb.__init__(self, parent, -1, |
---|
102 | style= wx.NB_BOTTOM) |
---|
103 | PanelBase.__init__(self, parent) |
---|
104 | |
---|
105 | self.parent = parent |
---|
106 | self.manager = manager |
---|
107 | self.data = data |
---|
108 | |
---|
109 | def set_data(self, data): |
---|
110 | if data is None: |
---|
111 | return |
---|
112 | |
---|
113 | grid = GridPage(self, panel=self.parent) |
---|
114 | grid.set_data(data) |
---|
115 | self.AddPage(grid, "Batch") |
---|
116 | |
---|
117 | |
---|
118 | class SPanel(ScrolledPanel): |
---|
119 | def __init__(self, parent, *args, **kwds): |
---|
120 | ScrolledPanel.__init__(self, parent , *args, **kwds) |
---|
121 | self.SetupScrolling() |
---|
122 | |
---|
123 | class GridPanel(SPanel): |
---|
124 | def __init__(self, parent,data=None, *args, **kwds): |
---|
125 | SPanel.__init__(self, parent , *args, **kwds) |
---|
126 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
127 | |
---|
128 | self.plotting_sizer = wx.FlexGridSizer(3, 5, 10, 5) |
---|
129 | w, h = self.GetSize() |
---|
130 | #self.panel_grid = SPanel(self, -1, size=(w, -1)) |
---|
131 | self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
132 | self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0), |
---|
133 | (wx.StaticLine(self, -1), 0, wx.EXPAND, 0), |
---|
134 | (self.plotting_sizer)]) |
---|
135 | self.parent = parent |
---|
136 | self._data = data |
---|
137 | self.x = [] |
---|
138 | self.y = [] |
---|
139 | |
---|
140 | self.x_axis_label = None |
---|
141 | self.y_axis_label = None |
---|
142 | self.x_axis_value = None |
---|
143 | self.y_axis_value = None |
---|
144 | self.x_axis_unit = None |
---|
145 | self.y_axis_unit = None |
---|
146 | self.plot_button = None |
---|
147 | self.grid = None |
---|
148 | self.layout_grid() |
---|
149 | self.layout_plotting_area() |
---|
150 | self.SetSizer(self.vbox) |
---|
151 | |
---|
152 | def set_data(self, data): |
---|
153 | """ |
---|
154 | """ |
---|
155 | if self.grid is not None: |
---|
156 | self.grid.set_data(data) |
---|
157 | |
---|
158 | def set_xaxis(self, label="", x=None) : |
---|
159 | if x is None: |
---|
160 | x = [] |
---|
161 | self.x = x |
---|
162 | self.x_axis_value.SetValue("%s[:]" % str(label)) |
---|
163 | self.x_axis_label.SetValue(str(label)) |
---|
164 | |
---|
165 | def set_yaxis(self, label="", y=None) : |
---|
166 | if y is None: |
---|
167 | y = [] |
---|
168 | self.y = y |
---|
169 | self.y_axis_value.SetValue("%s[:]" % str(label)) |
---|
170 | self.y_axis_label.SetValue(str(label)) |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | def on_plot(self, event): |
---|
175 | """ |
---|
176 | plotting |
---|
177 | """ |
---|
178 | new_plot = Data1D(x=self.x, y=self.y) |
---|
179 | new_plot.id = wx.NewId() |
---|
180 | new_plot.group_id = wx.NewId() |
---|
181 | title = "%s vs %s" % (self.y_axis_label.GetValue(), self.x_axis_label.GetValue()) |
---|
182 | new_plot.xaxis(self.x_axis_label.GetValue(), self.x_axis_unit.GetValue()) |
---|
183 | new_plot.yaxis(self.y_axis_label.GetValue(), self.y_axis_unit.GetValue()) |
---|
184 | wx.PostEvent(self.parent.parent, |
---|
185 | NewPlotEvent(plot=new_plot, group_id=str(new_plot.group_id), title ="batch")) |
---|
186 | def layout_grid(self): |
---|
187 | """ |
---|
188 | Draw the area related to the grid |
---|
189 | """ |
---|
190 | self.grid = Notebook(parent=self) |
---|
191 | self.grid.set_data(self._data) |
---|
192 | #self.grid = Table(parent=self, data=self._data) |
---|
193 | #vbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
194 | self.grid_sizer.Add(self.grid, 1, wx.EXPAND, 0) |
---|
195 | #self.panel_grid.SetSizer(vbox) |
---|
196 | |
---|
197 | def layout_grid1(self): |
---|
198 | """ |
---|
199 | Draw the area related to the grid |
---|
200 | """ |
---|
201 | self.grid = Table(parent=self.panel_grid, data=self._data) |
---|
202 | vbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
203 | vbox.Add(self.grid, 1, wx.EXPAND, 0) |
---|
204 | self.panel_grid.SetSizer(vbox) |
---|
205 | |
---|
206 | def layout_plotting_area(self): |
---|
207 | """ |
---|
208 | Draw area containing options to plot |
---|
209 | """ |
---|
210 | self.x_axis_label = wx.TextCtrl(self, -1) |
---|
211 | |
---|
212 | self.y_axis_label = wx.TextCtrl(self, -1) |
---|
213 | |
---|
214 | self.x_axis_value = wx.TextCtrl(self, -1) |
---|
215 | self.y_axis_value = wx.TextCtrl(self, -1) |
---|
216 | |
---|
217 | self.x_axis_unit = wx.TextCtrl(self, -1) |
---|
218 | self.y_axis_unit = wx.TextCtrl(self, -1) |
---|
219 | self.plot_button = wx.Button(self, -1, "Plot") |
---|
220 | wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot) |
---|
221 | self.plotting_sizer.AddMany([(wx.StaticText(self, -1, "x"), 1, wx.LEFT, 10), |
---|
222 | (self.x_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
223 | (self.x_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
224 | (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE|wx.EXPAND, 0), |
---|
225 | (self.x_axis_unit), |
---|
226 | (wx.StaticText(self, -1, "y"), 1, wx.LEFT, 10), |
---|
227 | (self.y_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
228 | (self.y_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
229 | (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE|wx.EXPAND, 0), |
---|
230 | (self.y_axis_unit), |
---|
231 | (-1, -1), |
---|
232 | (-1, -1), |
---|
233 | (-1, -1), |
---|
234 | (self.plot_button, 1, wx.LEFT, 30)]) |
---|
235 | |
---|
236 | |
---|
237 | def add_column(self): |
---|
238 | if self.grid is not None: |
---|
239 | self.grid.add_column() |
---|
240 | |
---|
241 | |
---|
242 | class GridFrame(wx.Frame): |
---|
243 | def __init__(self, parent=None, data=None, id=-1, title="Batch Results", size=(500, 400)): |
---|
244 | wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
245 | self.parent = parent |
---|
246 | self.panel = GridPanel(self, data) |
---|
247 | menubar = wx.MenuBar() |
---|
248 | edit = wx.Menu() |
---|
249 | id_col = wx.NewId() |
---|
250 | edit.Append(id_col, 'Edit', '' ) |
---|
251 | menubar.Append(edit, "&New column") |
---|
252 | self.SetMenuBar(menubar) |
---|
253 | wx.EVT_MENU(self, id_col, self.on_add_column) |
---|
254 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
255 | |
---|
256 | |
---|
257 | def on_close(self, event): |
---|
258 | """ |
---|
259 | """ |
---|
260 | self.Hide() |
---|
261 | |
---|
262 | def on_add_column(self, event): |
---|
263 | """ |
---|
264 | """ |
---|
265 | self.panel.add_column() |
---|
266 | |
---|
267 | def set_data(self, data): |
---|
268 | """ |
---|
269 | """ |
---|
270 | self.panel.set_data(data) |
---|
271 | |
---|
272 | |
---|
273 | if __name__ == "__main__": |
---|
274 | app = wx.App() |
---|
275 | |
---|
276 | try: |
---|
277 | data = {} |
---|
278 | j = 0 |
---|
279 | for i in range(4): |
---|
280 | j += 1 |
---|
281 | data["index"+str(i)] = [i/j, i*j, i, i+j] |
---|
282 | |
---|
283 | frame = TestFrame(data=data) |
---|
284 | frame.Show(True) |
---|
285 | except: |
---|
286 | print sys.exc_value |
---|
287 | |
---|
288 | app.MainLoop() |
---|