1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import matplotlib |
---|
6 | matplotlib.use('GTK') |
---|
7 | |
---|
8 | from matplotlib.figure import Figure |
---|
9 | from matplotlib.axes import Subplot |
---|
10 | from matplotlib.backends.backend_gtk import FigureCanvasGTK, NavigationToolbar |
---|
11 | |
---|
12 | import usans |
---|
13 | |
---|
14 | |
---|
15 | try: |
---|
16 | import pygtk |
---|
17 | pygtk.require("2.0") |
---|
18 | |
---|
19 | except: |
---|
20 | pass |
---|
21 | |
---|
22 | try: |
---|
23 | import gtk |
---|
24 | import gtk.glade |
---|
25 | except: |
---|
26 | sys.exit(1) |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | class appGui: |
---|
31 | |
---|
32 | TARGETS = [('STRING', gtk.TARGET_SAME_APP, 0)] |
---|
33 | |
---|
34 | def __init__(self): |
---|
35 | |
---|
36 | gladefile = "bt5view.glade" |
---|
37 | self.windowname = "win_Main" |
---|
38 | self.wTree = gtk.glade.XML(gladefile, self.windowname) |
---|
39 | |
---|
40 | event_dic = {"on_win_Main_destroy" : gtk.main_quit, |
---|
41 | "on_quit1_activate" : gtk.main_quit, |
---|
42 | "on_set_data_dir1_activate" : self.setdatadir, |
---|
43 | "on_tv_plotlist_key_press_event" : self.handle_plotlist_keypress} |
---|
44 | |
---|
45 | |
---|
46 | self.wTree.signal_autoconnect(event_dic) |
---|
47 | |
---|
48 | # Set up file list |
---|
49 | self.filelistview = self.wTree.get_widget("tv_filelist") |
---|
50 | self.AddFileListColumn("Filename", 0) |
---|
51 | |
---|
52 | self.filelist = gtk.ListStore(str) |
---|
53 | self.filelistview.set_model(self.filelist) |
---|
54 | self.filelist.set_sort_column_id(0, gtk.SORT_ASCENDING) |
---|
55 | |
---|
56 | # Set up plot group list |
---|
57 | self.plotlistview = self.wTree.get_widget("tv_plotlist") |
---|
58 | #self.AddPlotListColumn("Dataset",0) |
---|
59 | #self.AddPlotListColumn("Plot",1) |
---|
60 | self.cellrendertoggle = gtk.CellRendererToggle() |
---|
61 | self.cellrendertoggle.set_property('activatable', True) |
---|
62 | self.AddPlotListColumns("Dataset") |
---|
63 | |
---|
64 | #Create TreeStore model - Name of dataset, is it plotted, loaded data for children, plottable data for parent |
---|
65 | self.plotlist = gtk.TreeStore(str, 'gboolean', object, object) |
---|
66 | self.plotlistview.set_model(self.plotlist) |
---|
67 | self.plotlist.set_sort_column_id(0, gtk.SORT_ASCENDING) |
---|
68 | |
---|
69 | #fill the file list |
---|
70 | self.FillFileList(self.GetDirList()) |
---|
71 | #self.plotlist.append(None,None) |
---|
72 | |
---|
73 | # Set up graphing widget to display xpeek data |
---|
74 | self.figure = Figure(figsize=(4, 4), dpi=72) |
---|
75 | self.axis = self.figure.add_subplot(111) |
---|
76 | self.axis.set_xlabel('Motor position') |
---|
77 | self.axis.set_ylabel('Counts') |
---|
78 | #self.axis.set_title('XPeek') |
---|
79 | self.axis.grid(True) |
---|
80 | |
---|
81 | self.canvas = FigureCanvasGTK(self.figure) |
---|
82 | self.canvas.show() |
---|
83 | |
---|
84 | self.plotView = self.wTree.get_widget("hbox1") |
---|
85 | self.plotView.pack_start(self.canvas, True, True) |
---|
86 | |
---|
87 | self.filelistview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, |
---|
88 | self.TARGETS, |
---|
89 | gtk.gdk.ACTION_COPY) |
---|
90 | self.plotlistview.enable_model_drag_dest(self.TARGETS, |
---|
91 | gtk.gdk.ACTION_COPY) |
---|
92 | |
---|
93 | self.filelistview.connect("drag_data_get", self.dnd_data_getdata) |
---|
94 | self.plotlistview.connect("drag_data_received", self.dnd_data_received) |
---|
95 | |
---|
96 | #Handle toggling of checkboxes in plot list |
---|
97 | self.cellrendertoggle.connect("toggled", self.handle_toggled, (self.plotlistview.get_model(), 1)) |
---|
98 | |
---|
99 | |
---|
100 | def AddFileListColumn(self, title, columnId): |
---|
101 | """This function adds a column to the list view. |
---|
102 | First it create the gtk.TreeViewColumn and then set |
---|
103 | some needed properties""" |
---|
104 | |
---|
105 | column = gtk.TreeViewColumn(title, gtk.CellRendererText() |
---|
106 | , text=columnId) |
---|
107 | column.set_resizable(True) |
---|
108 | column.set_sort_column_id(columnId) |
---|
109 | self.filelistview.append_column(column) |
---|
110 | return |
---|
111 | |
---|
112 | def AddPlotListColumns(self, title): |
---|
113 | column = gtk.TreeViewColumn(title, gtk.CellRendererText() |
---|
114 | , text=0) |
---|
115 | #column.set_resizable(True) |
---|
116 | column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) |
---|
117 | column.set_fixed_width(230) |
---|
118 | column.set_sort_column_id(0) |
---|
119 | #column.set_min_width(200) |
---|
120 | self.plotlistview.append_column(column) |
---|
121 | |
---|
122 | column = gtk.TreeViewColumn('', self.cellrendertoggle, active=1) |
---|
123 | column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) |
---|
124 | #column.set_min_width(20) |
---|
125 | #column.set_max_width(20) |
---|
126 | #column.set_fixed_width(20) |
---|
127 | #column.set_resizable(False) |
---|
128 | self.plotlistview.append_column(column) |
---|
129 | return |
---|
130 | |
---|
131 | def handle_toggled(self, cell, path, user_data): |
---|
132 | model, column = user_data |
---|
133 | model[path][column] = not model[path][column] |
---|
134 | |
---|
135 | return |
---|
136 | |
---|
137 | def GetDirList(self): |
---|
138 | dirlist = os.listdir(os.getcwd()) |
---|
139 | |
---|
140 | return dirlist |
---|
141 | |
---|
142 | |
---|
143 | def FillFileList(self, filenames): |
---|
144 | self.filelist.clear() |
---|
145 | for filename in filenames: |
---|
146 | self.filelist.append([filename]) |
---|
147 | return |
---|
148 | |
---|
149 | |
---|
150 | def dnd_data_received(self, treeview, context, x, y, selection, info, etime): |
---|
151 | model = treeview.get_model() |
---|
152 | data = selection.data |
---|
153 | #Get USANS data from file |
---|
154 | detdata = usans.getBT5DataFromFile(data) |
---|
155 | drop_info = treeview.get_dest_row_at_pos(x, y) |
---|
156 | if drop_info: |
---|
157 | #Other entries exist already |
---|
158 | path, position = drop_info |
---|
159 | iter = model.get_iter(path) |
---|
160 | if model.iter_depth(iter) > 0: |
---|
161 | #Adding to exisiting dataset |
---|
162 | parent = model.iter_parent(iter) |
---|
163 | if position == gtk.TREE_VIEW_DROP_INTO_OR_AFTER or position == gtk.TREE_VIEW_DROP_AFTER: |
---|
164 | titer = model.insert_after(parent, iter, [data, 0, detdata, 0]) |
---|
165 | elif position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE or position == gtk.TREE_VIEW_DROP_BEFORE: |
---|
166 | titer = model.insert_before(parent, iter, [data, 0, detdata, 0]) |
---|
167 | else: |
---|
168 | #New top level entry |
---|
169 | if position == gtk.TREE_VIEW_DROP_INTO_OR_AFTER or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE: |
---|
170 | titer = model.insert_after(iter, None, [data, 0, detdata, 0]) |
---|
171 | else: |
---|
172 | #Not sure that this should ever get called.... |
---|
173 | iter = model.append(None, [data, 0, 0, 0]) |
---|
174 | titer = model.append(iter, [data, 0, detdata, 0]) |
---|
175 | else: |
---|
176 | #First entry |
---|
177 | iter = model.append(None, [data[:5], 0, 0, 0]) |
---|
178 | titer = model.append(iter, [data, 0, detdata, 0]) |
---|
179 | |
---|
180 | print titer |
---|
181 | |
---|
182 | self.do_plot(model, titer, "total") |
---|
183 | |
---|
184 | return |
---|
185 | |
---|
186 | def do_plot(self, model, iter, type): |
---|
187 | #Handle plotting - ignore type for the moment, only plot total counts |
---|
188 | path = model.get_path(iter) |
---|
189 | #Always passing a child, but let's check anyway |
---|
190 | if model.iter_depth(iter) > 0: |
---|
191 | parent = model.iter_parent(iter) |
---|
192 | ppath = model.get_path(parent) |
---|
193 | #Check to see if parent is already plotted |
---|
194 | if (model[ppath][1]): |
---|
195 | print "Parent plotted" |
---|
196 | #If plotted, add new data to existing plotted dataset and set checkbox |
---|
197 | self.make_plottable_dataset(model, ppath, type) |
---|
198 | model[path][1] = 1 |
---|
199 | else: |
---|
200 | print "Parent not plotted" |
---|
201 | if(model.iter_n_children(iter) > 1): |
---|
202 | print "has siblings" |
---|
203 | #If not plotted and there are siblings, add new data to dataset and set checkbox but do not plot |
---|
204 | self.make_plottable_dataset(model, ppath, type) |
---|
205 | model[path][1] = 1 |
---|
206 | else: |
---|
207 | #If parent not plotted and there are no siblings, create plottable dataset, plot and set checkbox |
---|
208 | self.make_plottable_dataset(model, ppath, type) |
---|
209 | print model[ppath][3] |
---|
210 | self.axis.plot(model[ppath][3][0], model[ppath][3][1]) |
---|
211 | self.figure.draw() |
---|
212 | model[path][1] = 1 |
---|
213 | |
---|
214 | |
---|
215 | return |
---|
216 | |
---|
217 | def make_plottable_dataset(self, model, path, type): |
---|
218 | #Will use select here, but for now ignore type and just do total |
---|
219 | piter = model.get_iter(path) |
---|
220 | xdata = [] |
---|
221 | ydata = [] |
---|
222 | |
---|
223 | for iter in range(0, model.iter_n_children(piter)): |
---|
224 | #for each child that has the plot flag set add keys to xdata and summed values to ydata |
---|
225 | cpath = model.get_path(model.iter_nth_child(piter, iter)) |
---|
226 | data = model[cpath][2] |
---|
227 | mvals = data.keys() |
---|
228 | mvals.sort() |
---|
229 | for mval in mvals: |
---|
230 | xdata.append(mval) |
---|
231 | ydata.append(data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6]) |
---|
232 | |
---|
233 | print xdata |
---|
234 | print ydata |
---|
235 | |
---|
236 | model[path][3] = [xdata, ydata] |
---|
237 | return |
---|
238 | |
---|
239 | def dnd_data_getdata(self, treeview, context, selection, target_id, etime): |
---|
240 | treeselection = treeview.get_selection() |
---|
241 | model, iter = treeselection.get_selected() |
---|
242 | data = model.get_value(iter, 0) |
---|
243 | selection.set('STRING', 8, data) |
---|
244 | return |
---|
245 | |
---|
246 | def setdatadir(self, widget): |
---|
247 | chooser = gtk.FileChooserDialog(title="Select Data Directory", action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, |
---|
248 | buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) |
---|
249 | chooser.set_default_response(gtk.RESPONSE_OK) |
---|
250 | chooser.set_current_folder(os.getcwd()) |
---|
251 | response = chooser.run() |
---|
252 | if response == gtk.RESPONSE_OK: |
---|
253 | os.chdir(chooser.get_filename()) |
---|
254 | self.FillFileList(self.GetDirList()) |
---|
255 | chooser.destroy() |
---|
256 | |
---|
257 | def handle_plotlist_keypress(self, widget, event): |
---|
258 | keyname = gtk.gdk.keyval_name(event.keyval) |
---|
259 | print keyname |
---|
260 | if keyname in ["Delete", "BackSpace"]: |
---|
261 | self.deleteplotlistentry(widget) |
---|
262 | if keyname in ["Down"]: |
---|
263 | (path,), focus = widget.get_cursor() |
---|
264 | widget.set_cursor(path + 1) |
---|
265 | if keyname in ["Up"]: |
---|
266 | (path,), focus = widget.get_cursor() |
---|
267 | widget.set_cursor(path - 1) |
---|
268 | return True |
---|
269 | |
---|
270 | def deleteplotlistentry(self, treeview): |
---|
271 | treeselection = treeview.get_selection() |
---|
272 | model, iter = treeselection.get_selected() |
---|
273 | model.remove(iter) |
---|
274 | return |
---|
275 | |
---|
276 | app = appGui() |
---|
277 | gtk.main() |
---|