##// END OF EJS Templates
Show the input object of the igrid browser as the window tile....
walter.doerwald -
Show More

The requested changes are too big and content was truncated. Show full diff

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