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 | |
---|
13 | try: |
---|
14 | import pygtk |
---|
15 | pygtk.require("2.0") |
---|
16 | |
---|
17 | except: |
---|
18 | pass |
---|
19 | |
---|
20 | try: |
---|
21 | import gtk |
---|
22 | import gtk.glade |
---|
23 | except: |
---|
24 | sys.exit(1) |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | class appGui: |
---|
29 | |
---|
30 | TARGETS = [('STRING', gtk.TARGET_SAME_APP, 0)] |
---|
31 | |
---|
32 | def __init__(self): |
---|
33 | |
---|
34 | gladefile = "bt5view.glade" |
---|
35 | self.windowname = "win_Main" |
---|
36 | self.wTree = gtk.glade.XML(gladefile, self.windowname) |
---|
37 | |
---|
38 | event_dic = {"on_win_Main_destroy" : gtk.main_quit, |
---|
39 | "on_quit1_activate" : gtk.main_quit, |
---|
40 | "on_set_data_dir1_activate" : self.setdatadir, |
---|
41 | "on_tv_plotlist_key_press_event" : self.handle_plotlist_keypress} |
---|
42 | |
---|
43 | |
---|
44 | self.wTree.signal_autoconnect(event_dic) |
---|
45 | |
---|
46 | # Set up file list |
---|
47 | self.filelistview = self.wTree.get_widget("tv_filelist") |
---|
48 | self.AddFileListColumn("Filename",0) |
---|
49 | |
---|
50 | self.filelist = gtk.ListStore(str) |
---|
51 | self.filelistview.set_model(self.filelist) |
---|
52 | |
---|
53 | # Set up plot group list |
---|
54 | self.plotlistview = self.wTree.get_widget("tv_plotlist") |
---|
55 | self.AddPlotListColumn("Dataset",0) |
---|
56 | |
---|
57 | self.plotlist = gtk.TreeStore(str) |
---|
58 | self.plotlistview.set_model(self.plotlist) |
---|
59 | |
---|
60 | #fill the file list |
---|
61 | self.FillFileList(self.GetDirList()) |
---|
62 | #self.plotlist.append(None,None) |
---|
63 | |
---|
64 | # Set up graphing widget to display xpeek data |
---|
65 | self.figure = Figure(figsize=(4,4), dpi=72) |
---|
66 | self.axis = self.figure.add_subplot(111) |
---|
67 | self.axis.set_xlabel('Motor position') |
---|
68 | self.axis.set_ylabel('Counts') |
---|
69 | #self.axis.set_title('XPeek') |
---|
70 | self.axis.grid(True) |
---|
71 | |
---|
72 | self.canvas = FigureCanvasGTK(self.figure) |
---|
73 | self.canvas.show() |
---|
74 | |
---|
75 | self.plotView = self.wTree.get_widget("hbox1") |
---|
76 | self.plotView.pack_start(self.canvas, True, True) |
---|
77 | |
---|
78 | self.filelistview.enable_model_drag_source( gtk.gdk.BUTTON1_MASK, |
---|
79 | self.TARGETS, |
---|
80 | gtk.gdk.ACTION_COPY) |
---|
81 | self.plotlistview.enable_model_drag_dest(self.TARGETS, |
---|
82 | gtk.gdk.ACTION_COPY) |
---|
83 | |
---|
84 | self.filelistview.connect("drag_data_get", self.dnd_data_getdata) |
---|
85 | self.plotlistview.connect("drag_data_received",self.dnd_data_received) |
---|
86 | |
---|
87 | |
---|
88 | def AddFileListColumn(self, title, columnId): |
---|
89 | """This function adds a column to the list view. |
---|
90 | First it create the gtk.TreeViewColumn and then set |
---|
91 | some needed properties""" |
---|
92 | |
---|
93 | column = gtk.TreeViewColumn(title, gtk.CellRendererText() |
---|
94 | , text=columnId) |
---|
95 | column.set_resizable(True) |
---|
96 | column.set_sort_column_id(columnId) |
---|
97 | self.filelistview.append_column(column) |
---|
98 | return |
---|
99 | |
---|
100 | def AddPlotListColumn(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.plotlistview.append_column(column) |
---|
110 | return |
---|
111 | |
---|
112 | def GetDirList(self): |
---|
113 | dirlist = os.listdir(os.getcwd()) |
---|
114 | |
---|
115 | return dirlist |
---|
116 | |
---|
117 | |
---|
118 | def FillFileList(self,filenames): |
---|
119 | self.filelist.clear() |
---|
120 | for filename in filenames: |
---|
121 | self.filelist.append([filename]) |
---|
122 | return |
---|
123 | |
---|
124 | |
---|
125 | def dnd_data_received(self, treeview, context, x, y, selection, info, etime): |
---|
126 | model = treeview.get_model() |
---|
127 | data = selection.data |
---|
128 | drop_info = treeview.get_dest_row_at_pos(x, y) |
---|
129 | if drop_info: |
---|
130 | path, position = drop_info |
---|
131 | iter = model.get_iter(path) |
---|
132 | if model.iter_depth(iter) > 0: |
---|
133 | parent = model.iter_parent(iter) |
---|
134 | if position == gtk.TREE_VIEW_DROP_INTO_OR_AFTER or position == gtk.TREE_VIEW_DROP_AFTER: |
---|
135 | model.insert_after(parent,iter,[data]) |
---|
136 | elif position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE or position == gtk.TREE_VIEW_DROP_BEFORE: |
---|
137 | model.insert_before(parent,iter,[data]) |
---|
138 | else: |
---|
139 | if position == gtk.TREE_VIEW_DROP_INTO_OR_AFTER or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE: |
---|
140 | model.insert_after(iter, None, [data]) |
---|
141 | else: |
---|
142 | iter = model.append(None,[data]) |
---|
143 | model.append(iter,[data]) |
---|
144 | else: |
---|
145 | iter = model.append(None,[data]) |
---|
146 | model.append(iter,[data]) |
---|
147 | return |
---|
148 | |
---|
149 | def dnd_data_getdata(self, treeview, context, selection, target_id, etime): |
---|
150 | treeselection = treeview.get_selection() |
---|
151 | model, iter = treeselection.get_selected() |
---|
152 | data = model.get_value(iter, 0) |
---|
153 | selection.set('STRING', 8, data) |
---|
154 | return |
---|
155 | |
---|
156 | def setdatadir(self, widget): |
---|
157 | chooser = gtk.FileChooserDialog(title="Select Data Directory",action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, |
---|
158 | buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) |
---|
159 | chooser.set_default_response(gtk.RESPONSE_OK) |
---|
160 | chooser.set_current_folder(os.getcwd()) |
---|
161 | response = chooser.run() |
---|
162 | if response == gtk.RESPONSE_OK: |
---|
163 | os.chdir(chooser.get_filename()) |
---|
164 | self.FillFileList(self.GetDirList()) |
---|
165 | chooser.destroy() |
---|
166 | |
---|
167 | def handle_plotlist_keypress(self,widget,event): |
---|
168 | keyname = gtk.gdk.keyval_name(event.keyval) |
---|
169 | print keyname |
---|
170 | if keyname in ["Delete", "BackSpace"]: |
---|
171 | self.deleteplotlistentry(widget) |
---|
172 | |
---|
173 | return True |
---|
174 | |
---|
175 | def deleteplotlistentry(self, treeview): |
---|
176 | treeselection = treeview.get_selection() |
---|
177 | model, iter = treeselection.get_selected() |
---|
178 | model.remove(iter) |
---|
179 | return |
---|
180 | |
---|
181 | |
---|
182 | app = appGui() |
---|
183 | gtk.main() |
---|