Show More
This diff has been collapsed as it changes many lines, (764 lines changed) Show them Hide them | |||||
@@ -0,0 +1,764 b'' | |||||
|
1 | # -*- coding: iso-8859-1 -*- | |||
|
2 | ||||
|
3 | import ipipe, os, webbrowser, urllib | |||
|
4 | import wx | |||
|
5 | import wx.grid, wx.html | |||
|
6 | ||||
|
7 | try: | |||
|
8 | sorted | |||
|
9 | except NameError: | |||
|
10 | from ipipe import sorted | |||
|
11 | ||||
|
12 | ||||
|
13 | __all__ = ["igrid"] | |||
|
14 | ||||
|
15 | ||||
|
16 | class IGridRenderer(wx.grid.PyGridCellRenderer): | |||
|
17 | """ | |||
|
18 | This is a custom renderer for our IGridGrid | |||
|
19 | """ | |||
|
20 | def __init__(self, table): | |||
|
21 | self.maxchars = 200 | |||
|
22 | self.table = table | |||
|
23 | self.colormap = ( | |||
|
24 | ( 0, 0, 0), | |||
|
25 | (174, 0, 0), | |||
|
26 | ( 0, 174, 0), | |||
|
27 | (174, 174, 0), | |||
|
28 | ( 0, 0, 174), | |||
|
29 | (174, 0, 174), | |||
|
30 | ( 0, 174, 174), | |||
|
31 | ( 64, 64, 64) | |||
|
32 | ) | |||
|
33 | ||||
|
34 | wx.grid.PyGridCellRenderer.__init__(self) | |||
|
35 | ||||
|
36 | def _getvalue(self, row, col): | |||
|
37 | try: | |||
|
38 | value = self.table.displayattrs[col].value(self.table.items[row]) | |||
|
39 | (align, width, text) = ipipe.xformat(value, "cell", self.maxchars) | |||
|
40 | except Exception, exc: | |||
|
41 | (align, width, text) = ipipe.xformat(exc, "cell", self.maxchars) | |||
|
42 | return (align, text) | |||
|
43 | ||||
|
44 | def GetBestSize(self, grid, attr, dc, row, col): | |||
|
45 | text = grid.GetCellValue(row, col) | |||
|
46 | (align, text) = self._getvalue(row, col) | |||
|
47 | dc.SetFont(attr.GetFont()) | |||
|
48 | (w, h) = dc.GetTextExtent(str(text)) | |||
|
49 | return wx.Size(min(w+2, 600), h+2) # add border | |||
|
50 | ||||
|
51 | def Draw(self, grid, attr, dc, rect, row, col, isSelected): | |||
|
52 | """ | |||
|
53 | Takes care of drawing everything in the cell; aligns the text | |||
|
54 | """ | |||
|
55 | text = grid.GetCellValue(row, col) | |||
|
56 | (align, text) = self._getvalue(row, col) | |||
|
57 | if isSelected: | |||
|
58 | bg = grid.GetSelectionBackground() | |||
|
59 | else: | |||
|
60 | bg = ["white", (240, 240, 240)][row%2] | |||
|
61 | dc.SetTextBackground(bg) | |||
|
62 | dc.SetBrush(wx.Brush(bg, wx.SOLID)) | |||
|
63 | dc.SetPen(wx.TRANSPARENT_PEN) | |||
|
64 | dc.SetFont(attr.GetFont()) | |||
|
65 | dc.DrawRectangleRect(rect) | |||
|
66 | dc.SetClippingRect(rect) | |||
|
67 | # Format the text | |||
|
68 | if align == -1: # left alignment | |||
|
69 | (width, height) = dc.GetTextExtent(str(text)) | |||
|
70 | x = rect[0]+1 | |||
|
71 | y = rect[1]+0.5*(rect[3]-height) | |||
|
72 | ||||
|
73 | for (style, part) in text: | |||
|
74 | if isSelected: | |||
|
75 | fg = grid.GetSelectionForeground() | |||
|
76 | else: | |||
|
77 | fg = self.colormap[style.fg] | |||
|
78 | dc.SetTextForeground(fg) | |||
|
79 | (w, h) = dc.GetTextExtent(part) | |||
|
80 | dc.DrawText(part, x, y) | |||
|
81 | x += w | |||
|
82 | elif align == 0: # center alignment | |||
|
83 | (width, height) = dc.GetTextExtent(str(text)) | |||
|
84 | x = rect[0]+0.5*(rect[2]-width) | |||
|
85 | y = rect[1]+0.5*(rect[3]-height) | |||
|
86 | for (style, part) in text: | |||
|
87 | if isSelected: | |||
|
88 | fg = grid.GetSelectionForeground() | |||
|
89 | else: | |||
|
90 | fg = self.colormap[style.fg] | |||
|
91 | dc.SetTextForeground(fg) | |||
|
92 | (w, h) = dc.GetTextExtent(part) | |||
|
93 | dc.DrawText(part, x, y) | |||
|
94 | x += w | |||
|
95 | else: # right alignment | |||
|
96 | (width, height) = dc.GetTextExtent(str(text)) | |||
|
97 | x = rect[0]+rect[2]-1 | |||
|
98 | y = rect[1]+0.5*(rect[3]-height) | |||
|
99 | for (style, part) in reversed(text): | |||
|
100 | (w, h) = dc.GetTextExtent(part) | |||
|
101 | x -= w | |||
|
102 | if isSelected: | |||
|
103 | fg = grid.GetSelectionForeground() | |||
|
104 | else: | |||
|
105 | fg = self.colormap[style.fg] | |||
|
106 | dc.SetTextForeground(fg) | |||
|
107 | dc.DrawText(part, x, y) | |||
|
108 | dc.DestroyClippingRegion() | |||
|
109 | ||||
|
110 | def Clone(self): | |||
|
111 | return IGridRenderer(self.table) | |||
|
112 | ||||
|
113 | ||||
|
114 | class IGridTable(wx.grid.PyGridTableBase): | |||
|
115 | # The data table for the ``IGridGrid``. Some dirty tricks were used here: | |||
|
116 | # ``GetValue()`` does not get any values (or at least it does not return | |||
|
117 | # anything, accessing the values is done by the renderer) | |||
|
118 | # but rather tries to fetch the objects which were requested into the table. | |||
|
119 | # General behaviour is: Fetch the first X objects. If the user scrolls down | |||
|
120 | # to the last object another bunch of X objects is fetched (if possible) | |||
|
121 | def __init__(self, input, fontsize, *attrs): | |||
|
122 | wx.grid.PyGridTableBase.__init__(self) | |||
|
123 | self.input = input | |||
|
124 | self.iterator = ipipe.xiter(input) | |||
|
125 | self.items = [] | |||
|
126 | self.hiddenattrs = [] | |||
|
127 | self.attrs = attrs | |||
|
128 | self.displayattrs = [] | |||
|
129 | self.fetch(1) | |||
|
130 | self.sizing = False | |||
|
131 | self.fontsize = fontsize | |||
|
132 | ||||
|
133 | def GetAttr(self, *args): | |||
|
134 | attr = wx.grid.GridCellAttr() | |||
|
135 | attr.SetFont(wx.Font(self.fontsize, wx.TELETYPE, wx.NORMAL, wx.NORMAL)) | |||
|
136 | return attr | |||
|
137 | ||||
|
138 | def GetNumberRows(self): | |||
|
139 | return len(self.items) | |||
|
140 | ||||
|
141 | def GetNumberCols(self): | |||
|
142 | return len(self.displayattrs) | |||
|
143 | ||||
|
144 | def GetColLabelValue(self, col): | |||
|
145 | if col < len(self.displayattrs): | |||
|
146 | return self.displayattrs[col].name() | |||
|
147 | else: | |||
|
148 | return "" | |||
|
149 | ||||
|
150 | def GetRowLabelValue(self, row): | |||
|
151 | return str(row) | |||
|
152 | ||||
|
153 | def IsEmptyCell(self, row, col): | |||
|
154 | return False | |||
|
155 | ||||
|
156 | def fetch(self, count): | |||
|
157 | # Try to fill ``self.items`` with at least ``count`` objects. | |||
|
158 | have = len(self.items) | |||
|
159 | work = False | |||
|
160 | while self.iterator is not None and have < count: | |||
|
161 | try: | |||
|
162 | item = self.iterator.next() | |||
|
163 | except StopIteration: | |||
|
164 | self.iterator = None | |||
|
165 | break | |||
|
166 | except (KeyboardInterrupt, SystemExit): | |||
|
167 | raise | |||
|
168 | except Exception, exc: | |||
|
169 | have += 1 | |||
|
170 | self.items.append(exc) | |||
|
171 | work = True | |||
|
172 | self.iterator = None | |||
|
173 | break | |||
|
174 | else: | |||
|
175 | have += 1 | |||
|
176 | self.items.append(item) | |||
|
177 | work = True | |||
|
178 | if work: | |||
|
179 | self.calcdisplayattrs() | |||
|
180 | ||||
|
181 | def calcdisplayattrs(self): | |||
|
182 | # Calculate which attributes are available from the objects that are | |||
|
183 | # currently visible on screen (and store it in ``self.displayattrs``) | |||
|
184 | attrs = set() | |||
|
185 | self.displayattrs = [] | |||
|
186 | if self.attrs: | |||
|
187 | # If the browser object specifies a fixed list of attributes, | |||
|
188 | # simply use it (removing hidden attributes). | |||
|
189 | for attr in self.attrs: | |||
|
190 | attr = ipipe.upgradexattr(attr) | |||
|
191 | if attr not in attrs and attr not in self.hiddenattrs: | |||
|
192 | self.displayattrs.append(attr) | |||
|
193 | attrs.add(attr) | |||
|
194 | else: | |||
|
195 | endy = len(self.items) | |||
|
196 | for i in xrange(endy): | |||
|
197 | for attr in ipipe.xattrs(self.items[i]): | |||
|
198 | attr = ipipe.upgradexattr(attr) | |||
|
199 | if attr not in attrs and attr not in self.hiddenattrs: | |||
|
200 | self.displayattrs.append(attr) | |||
|
201 | attrs.add(attr) | |||
|
202 | ||||
|
203 | def GetValue(self, row, col): | |||
|
204 | # some kind of dummy-function: does not return anything but ""; | |||
|
205 | # (The value isn't use anyway) | |||
|
206 | # its main task is to trigger the fetch of new objects | |||
|
207 | had_cols = self.displayattrs[:] | |||
|
208 | had_rows = len(self.items) | |||
|
209 | if row == had_rows - 1 and self.iterator is not None and not self.sizing: | |||
|
210 | self.fetch(row + 20) | |||
|
211 | have_rows = len(self.items) | |||
|
212 | have_cols = len(self.displayattrs) | |||
|
213 | if have_rows > had_rows: | |||
|
214 | msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, have_rows - had_rows) | |||
|
215 | self.GetView().ProcessTableMessage(msg) | |||
|
216 | self.sizing = True | |||
|
217 | self.GetView().AutoSizeColumns(False) | |||
|
218 | self.sizing = False | |||
|
219 | if row >= have_rows: | |||
|
220 | return "" | |||
|
221 | if self.displayattrs != had_cols: | |||
|
222 | msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED, have_cols - len(had_cols)) | |||
|
223 | self.GetView().ProcessTableMessage(msg) | |||
|
224 | return "" | |||
|
225 | ||||
|
226 | def SetValue(self, row, col, value): | |||
|
227 | pass | |||
|
228 | ||||
|
229 | ||||
|
230 | class IGridGrid(wx.grid.Grid): | |||
|
231 | # The actual grid | |||
|
232 | # all methods for selecting/sorting/picking/... data are implemented here | |||
|
233 | def __init__(self, panel, input, *attrs): | |||
|
234 | wx.grid.Grid.__init__(self, panel) | |||
|
235 | fontsize = 9 | |||
|
236 | self.input = input | |||
|
237 | self.table = IGridTable(self.input, fontsize, *attrs) | |||
|
238 | self.SetTable(self.table, True) | |||
|
239 | self.SetSelectionMode(wx.grid.Grid.wxGridSelectRows) | |||
|
240 | self.SetDefaultRenderer(IGridRenderer(self.table)) | |||
|
241 | self.EnableEditing(False) | |||
|
242 | self.Bind(wx.EVT_KEY_DOWN, self.key_pressed) | |||
|
243 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.cell_doubleclicked) | |||
|
244 | self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_DCLICK, self.label_doubleclicked) | |||
|
245 | self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_label_leftclick) | |||
|
246 | self.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self._on_selected_range) | |||
|
247 | self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self._on_selected_cell) | |||
|
248 | self.current_selection = set() | |||
|
249 | self.maxchars = 200 | |||
|
250 | ||||
|
251 | def on_label_leftclick(self, event): | |||
|
252 | event.Skip() | |||
|
253 | ||||
|
254 | def error_output(self, text): | |||
|
255 | wx.Bell() | |||
|
256 | frame = self.GetParent().GetParent().GetParent() | |||
|
257 | frame.SetStatusText(text) | |||
|
258 | ||||
|
259 | def _on_selected_range(self, event): | |||
|
260 | # Internal update to the selection tracking lists | |||
|
261 | if event.Selecting(): | |||
|
262 | # adding to the list... | |||
|
263 | self.current_selection.update(xrange(event.GetTopRow(), event.GetBottomRow()+1)) | |||
|
264 | else: | |||
|
265 | # removal from list | |||
|
266 | for index in xrange( event.GetTopRow(), event.GetBottomRow()+1): | |||
|
267 | self.current_selection.discard(index) | |||
|
268 | event.Skip() | |||
|
269 | ||||
|
270 | def _on_selected_cell(self, event): | |||
|
271 | # Internal update to the selection tracking list | |||
|
272 | self.current_selection = set([event.GetRow()]) | |||
|
273 | event.Skip() | |||
|
274 | ||||
|
275 | def sort(self, key, reverse=False): | |||
|
276 | """ | |||
|
277 | Sort the current list of items using the key function ``key``. If | |||
|
278 | ``reverse`` is true the sort order is reversed. | |||
|
279 | """ | |||
|
280 | row = self.GetGridCursorRow() | |||
|
281 | col = self.GetGridCursorCol() | |||
|
282 | curitem = self.table.items[row] # Remember where the cursor is now | |||
|
283 | # Sort items | |||
|
284 | def realkey(item): | |||
|
285 | return key(item) | |||
|
286 | try: | |||
|
287 | self.table.items = ipipe.deque(sorted(self.table.items, key=realkey, reverse=reverse)) | |||
|
288 | except TypeError, exc: | |||
|
289 | self.error_output("Exception encountered: %s" % exc) | |||
|
290 | return | |||
|
291 | # Find out where the object under the cursor went | |||
|
292 | for (i, item) in enumerate(self.table.items): | |||
|
293 | if item is curitem: | |||
|
294 | self.SetGridCursor(i,col) | |||
|
295 | self.MakeCellVisible(i,col) | |||
|
296 | self.Refresh() | |||
|
297 | ||||
|
298 | def sortattrasc(self): | |||
|
299 | """ | |||
|
300 | Sort in ascending order; sorting criteria is the current attribute | |||
|
301 | """ | |||
|
302 | col = self.GetGridCursorCol() | |||
|
303 | attr = self.table.displayattrs[col] | |||
|
304 | frame = self.GetParent().GetParent().GetParent() | |||
|
305 | if attr is ipipe.noitem: | |||
|
306 | self.error_output("no column under cursor") | |||
|
307 | return | |||
|
308 | frame.SetStatusText("sort by %s (ascending)" % attr.name()) | |||
|
309 | def key(item): | |||
|
310 | try: | |||
|
311 | return attr.value(item) | |||
|
312 | except (KeyboardInterrupt, SystemExit): | |||
|
313 | raise | |||
|
314 | except Exception: | |||
|
315 | return None | |||
|
316 | self.sort(key) | |||
|
317 | ||||
|
318 | def sortattrdesc(self): | |||
|
319 | """ | |||
|
320 | Sort in descending order; sorting criteria is the current attribute | |||
|
321 | """ | |||
|
322 | col = self.GetGridCursorCol() | |||
|
323 | attr = self.table.displayattrs[col] | |||
|
324 | frame = self.GetParent().GetParent().GetParent() | |||
|
325 | if attr is ipipe.noitem: | |||
|
326 | self.error_output("no column under cursor") | |||
|
327 | return | |||
|
328 | frame.SetStatusText("sort by %s (descending)" % attr.name()) | |||
|
329 | def key(item): | |||
|
330 | try: | |||
|
331 | return attr.value(item) | |||
|
332 | except (KeyboardInterrupt, SystemExit): | |||
|
333 | raise | |||
|
334 | except Exception: | |||
|
335 | return None | |||
|
336 | self.sort(key, reverse=True) | |||
|
337 | ||||
|
338 | def label_doubleclicked(self, event): | |||
|
339 | row = event.GetRow() | |||
|
340 | col = event.GetCol() | |||
|
341 | if col == -1: | |||
|
342 | self.enter(row) | |||
|
343 | ||||
|
344 | def _getvalue(self, row, col): | |||
|
345 | """ | |||
|
346 | Gets the text which is displayed at ``(row, col)`` | |||
|
347 | """ | |||
|
348 | try: | |||
|
349 | value = self.table.displayattrs[col].value(self.table.items[row]) | |||
|
350 | (align, width, text) = ipipe.xformat(value, "cell", self.maxchars) | |||
|
351 | except IndexError: | |||
|
352 | raise IndexError | |||
|
353 | except Exception, exc: | |||
|
354 | (align, width, text) = ipipe.xformat(exc, "cell", self.maxchars) | |||
|
355 | return text | |||
|
356 | ||||
|
357 | def search(self, searchtext, startrow=0, startcol=0, search_forward=True): | |||
|
358 | """ | |||
|
359 | search for ``searchtext``, starting in ``(startrow, startcol)``; | |||
|
360 | if ``search_forward`` is true the direction is "forward" | |||
|
361 | """ | |||
|
362 | row = startrow | |||
|
363 | searchtext = searchtext.lower() | |||
|
364 | if search_forward: | |||
|
365 | while True: | |||
|
366 | for col in xrange(startcol, self.table.GetNumberCols()): | |||
|
367 | try: | |||
|
368 | foo = self.table.GetValue(row, col) | |||
|
369 | text = self._getvalue(row, col) | |||
|
370 | if searchtext in text.string().lower(): | |||
|
371 | self.SetGridCursor(row, col) | |||
|
372 | self.MakeCellVisible(row, col) | |||
|
373 | return | |||
|
374 | except IndexError: | |||
|
375 | return | |||
|
376 | startcol = 0 | |||
|
377 | row += 1 | |||
|
378 | else: | |||
|
379 | while True: | |||
|
380 | for col in xrange(startcol, -1, -1): | |||
|
381 | try: | |||
|
382 | foo = self.table.GetValue(row, col) | |||
|
383 | text = self._getvalue(row, col) | |||
|
384 | if searchtext in text.string().lower(): | |||
|
385 | self.SetGridCursor(row, col) | |||
|
386 | self.MakeCellVisible(row, col) | |||
|
387 | return | |||
|
388 | except IndexError: | |||
|
389 | return | |||
|
390 | startcol = self.table.GetNumberCols()-1 | |||
|
391 | row -= 1 | |||
|
392 | ||||
|
393 | def key_pressed(self, event): | |||
|
394 | """ | |||
|
395 | Maps pressed keys to functions | |||
|
396 | """ | |||
|
397 | frame = self.GetParent().GetParent().GetParent() | |||
|
398 | frame.SetStatusText("") | |||
|
399 | sh = event.ShiftDown() | |||
|
400 | ctrl = event.ControlDown() | |||
|
401 | ||||
|
402 | keycode = event.GetKeyCode() | |||
|
403 | if keycode == ord("P"): | |||
|
404 | row = self.GetGridCursorRow() | |||
|
405 | if event.ShiftDown(): | |||
|
406 | col = self.GetGridCursorCol() | |||
|
407 | self.pickattr(row, col) | |||
|
408 | else: | |||
|
409 | self.pick(row) | |||
|
410 | elif keycode == ord("M"): | |||
|
411 | if ctrl: | |||
|
412 | col = self.GetGridCursorCol() | |||
|
413 | self.pickrowsattr(sorted(self.current_selection), col) | |||
|
414 | else: | |||
|
415 | self.pickrows(sorted(self.current_selection)) | |||
|
416 | elif keycode in (wx.WXK_BACK, wx.WXK_DELETE, ord("X")) and not (ctrl or sh): | |||
|
417 | self.delete_current_notebook() | |||
|
418 | elif keycode == ord("E") and not (ctrl or sh): | |||
|
419 | row = self.GetGridCursorRow() | |||
|
420 | self.enter(row) | |||
|
421 | elif keycode == ord("E") and sh and not ctrl: | |||
|
422 | row = self.GetGridCursorRow() | |||
|
423 | col = self.GetGridCursorCol() | |||
|
424 | self.enterattr(row, col) | |||
|
425 | elif keycode == ord("E") and ctrl: | |||
|
426 | row = self.GetGridCursorRow() | |||
|
427 | self.SetGridCursor(row, self.GetNumberCols()-1) | |||
|
428 | elif keycode == wx.WXK_HOME or (keycode == ord("A") and ctrl): | |||
|
429 | row = self.GetGridCursorRow() | |||
|
430 | self.SetGridCursor(row, 0) | |||
|
431 | elif keycode == ord("C") and sh: | |||
|
432 | col = self.GetGridCursorCol() | |||
|
433 | attr = self.table.displayattrs[col] | |||
|
434 | returnobj = [] | |||
|
435 | for i in xrange(self.GetNumberRows()): | |||
|
436 | returnobj.append(self.table.displayattrs[col].value(self.table.items[i])) | |||
|
437 | self.quit(returnobj) | |||
|
438 | elif keycode in (wx.WXK_ESCAPE, ord("Q")) and not (ctrl or sh): | |||
|
439 | self.quit() | |||
|
440 | elif keycode == ord("<"): | |||
|
441 | row = self.GetGridCursorRow() | |||
|
442 | col = self.GetGridCursorCol() | |||
|
443 | if not event.ShiftDown(): | |||
|
444 | newcol = col - 1 | |||
|
445 | if newcol >= 0: | |||
|
446 | self.SetGridCursor(row, col - 1) | |||
|
447 | else: | |||
|
448 | newcol = col + 1 | |||
|
449 | if newcol < self.GetNumberCols(): | |||
|
450 | self.SetGridCursor(row, col + 1) | |||
|
451 | elif keycode == ord("D"): | |||
|
452 | col = self.GetGridCursorCol() | |||
|
453 | row = self.GetGridCursorRow() | |||
|
454 | if not sh: | |||
|
455 | self.detail(row, col) | |||
|
456 | else: | |||
|
457 | self.detail_attr(row, col) | |||
|
458 | elif keycode == ord("F") and ctrl: | |||
|
459 | frame.enter_searchtext(event) | |||
|
460 | elif keycode == wx.WXK_F3: | |||
|
461 | if sh: | |||
|
462 | frame.find_previous(event) | |||
|
463 | else: | |||
|
464 | frame.find_next(event) | |||
|
465 | elif keycode == ord("V"): | |||
|
466 | if sh: | |||
|
467 | self.sortattrdesc() | |||
|
468 | else: | |||
|
469 | self.sortattrasc() | |||
|
470 | else: | |||
|
471 | event.Skip() | |||
|
472 | ||||
|
473 | def delete_current_notebook(self): | |||
|
474 | """ | |||
|
475 | deletes the current notebook tab | |||
|
476 | """ | |||
|
477 | panel = self.GetParent() | |||
|
478 | nb = panel.GetParent() | |||
|
479 | current = nb.GetSelection() | |||
|
480 | count = nb.GetPageCount() | |||
|
481 | if count > 1: | |||
|
482 | for i in xrange(count-1, current-1, -1): | |||
|
483 | nb.DeletePage(i) | |||
|
484 | nb.GetCurrentPage().grid.SetFocus() | |||
|
485 | else: | |||
|
486 | frame = nb.GetParent() | |||
|
487 | frame.SetStatusText("This is the last level!") | |||
|
488 | ||||
|
489 | def _doenter(self, value, *attrs): | |||
|
490 | """ | |||
|
491 | "enter" a special item resulting in a new notebook tab | |||
|
492 | """ | |||
|
493 | panel = self.GetParent() | |||
|
494 | nb = panel.GetParent() | |||
|
495 | frame = nb.GetParent() | |||
|
496 | current = nb.GetSelection() | |||
|
497 | count = nb.GetPageCount() | |||
|
498 | try: # if we want to enter something non-iterable, e.g. a function | |||
|
499 | if current + 1 == count and value is not self.input: # we have an event in the last tab | |||
|
500 | frame._add_notebook(value, *attrs) | |||
|
501 | elif value != self.input: # we have to delete all tabs newer than [panel] first | |||
|
502 | for i in xrange(count-1, current, -1): # some tabs don't close if we don't close in *reverse* order | |||
|
503 | nb.DeletePage(i) | |||
|
504 | frame._add_notebook(value) | |||
|
505 | except TypeError, exc: | |||
|
506 | if exc.__class__.__module__ == "exceptions": | |||
|
507 | msg = "%s: %s" % (exc.__class__.__name__, exc) | |||
|
508 | else: | |||
|
509 | msg = "%s.%s: %s" % (exc.__class__.__module__, exc.__class__.__name__, exc) | |||
|
510 | frame.SetStatusText(msg) | |||
|
511 | ||||
|
512 | def enterattr(self, row, col): | |||
|
513 | try: | |||
|
514 | attr = self.table.displayattrs[col] | |||
|
515 | value = attr.value(self.table.items[row]) | |||
|
516 | except Exception, exc: | |||
|
517 | self.error_output(str(exc)) | |||
|
518 | else: | |||
|
519 | self._doenter(value) | |||
|
520 | ||||
|
521 | def enter(self, row): | |||
|
522 | try: | |||
|
523 | value = self.table.items[row] | |||
|
524 | except Exception, exc: | |||
|
525 | self.error_output(str(exc)) | |||
|
526 | else: | |||
|
527 | self._doenter(value) | |||
|
528 | ||||
|
529 | def detail(self, row, col): | |||
|
530 | """ | |||
|
531 | shows a detail-view of the current cell | |||
|
532 | """ | |||
|
533 | try: | |||
|
534 | attr = self.table.displayattrs[col] | |||
|
535 | item = self.table.items[row] | |||
|
536 | except Exception, exc: | |||
|
537 | self.error_output(str(exc)) | |||
|
538 | else: | |||
|
539 | attrs = [ipipe.AttributeDetail(item, attr) for attr in ipipe.xattrs(item, "detail")] | |||
|
540 | self._doenter(attrs) | |||
|
541 | ||||
|
542 | def detail_attr(self, row, col): | |||
|
543 | try: | |||
|
544 | attr = self.table.displayattrs[col] | |||
|
545 | item = attr.value(self.table.items[row]) | |||
|
546 | except Exception, exc: | |||
|
547 | self.error_output(str(exc)) | |||
|
548 | else: | |||
|
549 | attrs = [ipipe.AttributeDetail(item, attr) for attr in ipipe.xattrs(item, "detail")] | |||
|
550 | self._doenter(attrs) | |||
|
551 | ||||
|
552 | def quit(self, returnobj=None): | |||
|
553 | """ | |||
|
554 | quit | |||
|
555 | """ | |||
|
556 | frame = self.GetParent().GetParent().GetParent() | |||
|
557 | frame.parent.returnobj = returnobj | |||
|
558 | frame.Close() | |||
|
559 | frame.Destroy() | |||
|
560 | ||||
|
561 | def cell_doubleclicked(self, event): | |||
|
562 | self.enterattr(event.GetRow(), event.GetCol()) | |||
|
563 | ||||
|
564 | def pick(self, row): | |||
|
565 | """ | |||
|
566 | pick a single row and return to the IPython prompt | |||
|
567 | """ | |||
|
568 | try: | |||
|
569 | value = self.table.items[row] | |||
|
570 | except Exception, exc: | |||
|
571 | self.error_output(str(exc)) | |||
|
572 | else: | |||
|
573 | self.quit(value) | |||
|
574 | ||||
|
575 | def pickrows(self, rows): | |||
|
576 | """ | |||
|
577 | pick multiple rows and return to the IPython prompt | |||
|
578 | """ | |||
|
579 | try: | |||
|
580 | value = [self.table.items[row] for row in rows] | |||
|
581 | except Exception, exc: | |||
|
582 | self.error_output(str(exc)) | |||
|
583 | else: | |||
|
584 | self.quit(value) | |||
|
585 | ||||
|
586 | def pickrowsattr(self, rows, col): | |||
|
587 | """" | |||
|
588 | pick one column from multiple rows | |||
|
589 | """ | |||
|
590 | values = [] | |||
|
591 | try: | |||
|
592 | attr = self.table.displayattrs[col] | |||
|
593 | for row in rows: | |||
|
594 | try: | |||
|
595 | values.append(attr.value(self.table.items[row])) | |||
|
596 | except (SystemExit, KeyboardInterrupt): | |||
|
597 | raise | |||
|
598 | except Exception: | |||
|
599 | raise #pass | |||
|
600 | except Exception, exc: | |||
|
601 | self.error_output(str(exc)) | |||
|
602 | else: | |||
|
603 | self.quit(values) | |||
|
604 | ||||
|
605 | def pickattr(self, row, col): | |||
|
606 | try: | |||
|
607 | attr = self.table.displayattrs[col] | |||
|
608 | value = attr.value(self.table.items[row]) | |||
|
609 | except Exception, exc: | |||
|
610 | self.error_output(str(exc)) | |||
|
611 | else: | |||
|
612 | self.quit(value) | |||
|
613 | ||||
|
614 | ||||
|
615 | class IGridPanel(wx.Panel): | |||
|
616 | # Each IGridPanel contains an IGridGrid | |||
|
617 | def __init__(self, parent, input, *attrs): | |||
|
618 | wx.Panel.__init__(self, parent, -1) | |||
|
619 | self.grid = IGridGrid(self, input, *attrs) | |||
|
620 | sizer = wx.BoxSizer(wx.VERTICAL) | |||
|
621 | sizer.Add(self.grid, proportion=1, flag=wx.EXPAND | wx.ALL, border=10) | |||
|
622 | self.SetSizer(sizer) | |||
|
623 | sizer.Fit(self) | |||
|
624 | sizer.SetSizeHints(self) | |||
|
625 | ||||
|
626 | ||||
|
627 | class IGridHTMLHelp(wx.Frame): | |||
|
628 | def __init__(self, parent, title, filename, size): | |||
|
629 | wx.Frame.__init__(self, parent, -1, title, size=size) | |||
|
630 | html = wx.html.HtmlWindow(self) | |||
|
631 | if "gtk2" in wx.PlatformInfo: | |||
|
632 | html.SetStandardFonts() | |||
|
633 | html.LoadFile(filename) | |||
|
634 | ||||
|
635 | ||||
|
636 | class IGridFrame(wx.Frame): | |||
|
637 | maxtitlelen = 30 | |||
|
638 | ||||
|
639 | def __init__(self, parent, input): | |||
|
640 | wx.Frame.__init__(self, None, title="IGrid", size=(640, 480)) | |||
|
641 | self.menubar = wx.MenuBar() | |||
|
642 | self.menucounter = 100 | |||
|
643 | self.m_help = wx.Menu() | |||
|
644 | self.m_search = wx.Menu() | |||
|
645 | self.m_sort = wx.Menu() | |||
|
646 | self.notebook = wx.Notebook(self, -1, style=0) | |||
|
647 | self.statusbar = self.CreateStatusBar(1, wx.ST_SIZEGRIP) | |||
|
648 | self.parent = parent | |||
|
649 | self._add_notebook(input) | |||
|
650 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) | |||
|
651 | self.makemenu(self.m_sort, "&Sort (asc)", "Sort ascending", self.sortasc) | |||
|
652 | self.makemenu(self.m_sort, "Sort (&desc)", "Sort descending", self.sortdesc) | |||
|
653 | self.makemenu(self.m_help, "&Help", "Help", self.display_help) | |||
|
654 | self.makemenu(self.m_help, "&Show help in browser", "Show help in browser", self.display_help_in_browser) | |||
|
655 | self.makemenu(self.m_search, "&Find text", "Find text", self.enter_searchtext) | |||
|
656 | self.makemenu(self.m_search, "Find by &expression", "Find by expression", self.enter_searchexpression) | |||
|
657 | self.makemenu(self.m_search, "Find &next", "Find next", self.find_next) | |||
|
658 | self.makemenu(self.m_search, "Find &previous", "Find previous", self.find_previous) | |||
|
659 | self.menubar.Append(self.m_search, "&Find") | |||
|
660 | self.menubar.Append(self.m_sort, "&Sort") | |||
|
661 | self.menubar.Append(self.m_help, "&Help") | |||
|
662 | self.SetMenuBar(self.menubar) | |||
|
663 | self.searchtext = "" | |||
|
664 | ||||
|
665 | def sortasc(self, event): | |||
|
666 | grid = self.notebook.GetPage(self.notebook.GetSelection()).grid | |||
|
667 | grid.sortattrasc() | |||
|
668 | ||||
|
669 | def sortdesc(self, event): | |||
|
670 | grid = self.notebook.GetPage(self.notebook.GetSelection()).grid | |||
|
671 | grid.sortattrdesc() | |||
|
672 | ||||
|
673 | def find_previous(self, event): | |||
|
674 | """ | |||
|
675 | find previous occurrences | |||
|
676 | """ | |||
|
677 | if self.searchtext: | |||
|
678 | grid = self.notebook.GetPage(self.notebook.GetSelection()).grid | |||
|
679 | row = grid.GetGridCursorRow() | |||
|
680 | col = grid.GetGridCursorCol() | |||
|
681 | if col-1 >= 0: | |||
|
682 | grid.search(self.searchtext, row, col-1, False) | |||
|
683 | else: | |||
|
684 | grid.search(self.searchtext, row-1, grid.table.GetNumberCols()-1, False) | |||
|
685 | else: | |||
|
686 | self.enter_searchtext(event) | |||
|
687 | ||||
|
688 | def find_next(self, event): | |||
|
689 | """ | |||
|
690 | find the next occurrence | |||
|
691 | """ | |||
|
692 | if self.searchtext: | |||
|
693 | grid = self.notebook.GetPage(self.notebook.GetSelection()).grid | |||
|
694 | row = grid.GetGridCursorRow() | |||
|
695 | col = grid.GetGridCursorCol() | |||
|
696 | if col+1 < grid.table.GetNumberCols(): | |||
|
697 | grid.search(self.searchtext, row, col+1) | |||
|
698 | else: | |||
|
699 | grid.search(self.searchtext, row+1, 0) | |||
|
700 | else: | |||
|
701 | self.enter_searchtext(event) | |||
|
702 | ||||
|
703 | def display_help(self, event): | |||
|
704 | """ | |||
|
705 | Display a help dialog | |||
|
706 | """ | |||
|
707 | filename = os.path.join(os.path.dirname(__file__), "help.html") | |||
|
708 | frm = IGridHTMLHelp(None, title="Help", filename=filename, size=wx.Size(600,400)) | |||
|
709 | frm.Show() | |||
|
710 | ||||
|
711 | def display_help_in_browser(self, event): | |||
|
712 | """ | |||
|
713 | Show the help-HTML in a browser (as a ``HtmlWindow`` does not understand | |||
|
714 | CSS this looks better) | |||
|
715 | """ | |||
|
716 | filename = urllib.pathname2url(os.path.abspath(os.path.join(os.path.dirname(__file__), "help.html"))) | |||
|
717 | if not filename.startswith("file"): | |||
|
718 | filename = "file:" + filename | |||
|
719 | webbrowser.open(filename, new=1, autoraise=True) | |||
|
720 | ||||
|
721 | def enter_searchexpression(self, event): | |||
|
722 | pass | |||
|
723 | ||||
|
724 | def makemenu(self, menu, label, help, cmd): | |||
|
725 | menu.Append(self.menucounter, label, help) | |||
|
726 | self.Bind(wx.EVT_MENU, cmd, id=self.menucounter) | |||
|
727 | self.menucounter += 1 | |||
|
728 | ||||
|
729 | def _add_notebook(self, input, *attrs): | |||
|
730 | # Adds another notebook which has the starting object ``input`` | |||
|
731 | panel = IGridPanel(self.notebook, input, *attrs) | |||
|
732 | text = str(ipipe.xformat(input, "header", self.maxtitlelen)[2]) | |||
|
733 | if len(text) >= self.maxtitlelen: | |||
|
734 | text = text[:self.maxtitlelen].rstrip(".") + "..." | |||
|
735 | self.notebook.AddPage(panel, text, True) | |||
|
736 | panel.grid.SetFocus() | |||
|
737 | self.Layout() | |||
|
738 | ||||
|
739 | def OnCloseWindow(self, event): | |||
|
740 | self.Destroy() | |||
|
741 | ||||
|
742 | def enter_searchtext(self, event): | |||
|
743 | # Displays a dialog asking for the searchtext | |||
|
744 | dlg = wx.TextEntryDialog(self, "Find:", "Find in list") | |||
|
745 | if dlg.ShowModal() == wx.ID_OK: | |||
|
746 | self.searchtext = dlg.GetValue() | |||
|
747 | self.notebook.GetPage(self.notebook.GetSelection()).grid.search(self.searchtext, 0, 0) | |||
|
748 | dlg.Destroy() | |||
|
749 | ||||
|
750 | ||||
|
751 | class igrid(ipipe.Display): | |||
|
752 | """ | |||
|
753 | This is a wx-based display object that can be used instead of ``ibrowse`` | |||
|
754 | (which is curses-based) or ``idump`` (which simply does a print). | |||
|
755 | """ | |||
|
756 | def display(self): | |||
|
757 | self.returnobj = None | |||
|
758 | app = wx.App() | |||
|
759 | self.frame = IGridFrame(self, self.input) | |||
|
760 | self.frame.Show() | |||
|
761 | app.SetTopWindow(self.frame) | |||
|
762 | self.frame.Raise() | |||
|
763 | app.MainLoop() | |||
|
764 | return self.returnobj |
@@ -2100,13 +2100,20 b' class AttributeDetail(Table):' | |||||
2100 |
|
2100 | |||
2101 |
|
2101 | |||
2102 | try: |
|
2102 | try: | |
2103 |
from i |
|
2103 | from igrid import igrid | |
2104 | except ImportError: |
|
2104 | except ImportError: | |
2105 | # No curses (probably Windows) => use ``idump`` as the default display. |
|
2105 | # no wx | |
2106 | defaultdisplay = idump |
|
2106 | try: | |
|
2107 | from ibrowse import ibrowse | |||
|
2108 | except ImportError: | |||
|
2109 | # No curses (probably Windows) => use ``idump`` as the default display. | |||
|
2110 | defaultdisplay = idump | |||
|
2111 | else: | |||
|
2112 | defaultdisplay = ibrowse | |||
|
2113 | __all__.append("ibrowse") | |||
2107 | else: |
|
2114 | else: | |
2108 |
defaultdisplay = i |
|
2115 | defaultdisplay = igrid | |
2109 |
__all__.append("i |
|
2116 | __all__.append("igrid") | |
2110 |
|
2117 | |||
2111 |
|
2118 | |||
2112 | # If we're running under IPython, install an IPython displayhook that |
|
2119 | # If we're running under IPython, install an IPython displayhook that |
@@ -1,3 +1,11 b'' | |||||
|
1 | 2007-03-02 Walter Doerwald <walter@livinglogic.de> | |||
|
2 | ||||
|
3 | * IPython/Extensions/igrid.py: Add Nik Tautenhahns igrid extension. | |||
|
4 | igrid is a wxPython-based display object for ipipe. If your system has | |||
|
5 | wx installed igrid will be the default display. Without wx ipipe falls | |||
|
6 | back to ibrowse (which needs curses). If no curses is installed ipipe | |||
|
7 | falls back to idump. | |||
|
8 | ||||
1 | 2007-03-01 Fernando Perez <Fernando.Perez@colorado.edu> |
|
9 | 2007-03-01 Fernando Perez <Fernando.Perez@colorado.edu> | |
2 |
|
10 | |||
3 | * IPython/iplib.py (split_user_inputBROKEN): temporarily disable |
|
11 | * IPython/iplib.py (split_user_inputBROKEN): temporarily disable |
General Comments 0
You need to be logged in to leave comments.
Login now