##// END OF EJS Templates
Make a few igrid attributes private....
walter.doerwald -
Show More

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

@@ -1,769 +1,752 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 value = self.table.displayattrs[col].value(self.table.items[row])
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 self.hiddenattrs = []
127 self.attrs = attrs
128 self.displayattrs = []
129 self.fetch(1)
130 self.sizing = False
126 self.attrs = [ipipe.upgradexattr(attr) for attr in attrs]
127 self._displayattrs = self.attrs[:]
128 self._displayattrset = set(self.attrs)
129 self._sizing = False
131 130 self.fontsize = fontsize
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 return len(self.displayattrs)
142 return len(self._displayattrs)
143 143
144 144 def GetColLabelValue(self, col):
145 if col < len(self.displayattrs):
146 return self.displayattrs[col].name()
145 if col < len(self._displayattrs):
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 def fetch(self, count):
156 def _append(self, item):
157 self.items.append(item)
158 # Nothing to do if the set of attributes has been fixed by the user
159 if not self.attrs:
160 for attr in ipipe.xattrs(item):
161 attr = ipipe.upgradexattr(attr)
162 if attr not in self._displayattrset:
163 self._displayattrs.append(attr)
164 self._displayattrset.add(attr)
165
166 def _fetch(self, count):
157 167 # Try to fill ``self.items`` with at least ``count`` objects.
158 168 have = len(self.items)
159 work = False
160 169 while self.iterator is not None and have < count:
161 170 try:
162 171 item = self.iterator.next()
163 172 except StopIteration:
164 173 self.iterator = None
165 174 break
166 175 except (KeyboardInterrupt, SystemExit):
167 176 raise
168 177 except Exception, exc:
169 178 have += 1
170 self.items.append(exc)
171 work = True
179 self._append(item)
172 180 self.iterator = None
173 181 break
174 182 else:
175 183 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)
184 self._append(item)
202 185
203 186 def GetValue(self, row, col):
204 187 # some kind of dummy-function: does not return anything but "";
205 188 # (The value isn't use anyway)
206 189 # its main task is to trigger the fetch of new objects
207 had_cols = self.displayattrs[:]
190 had_cols = self._displayattrs[:]
208 191 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)
192 if row == had_rows - 1 and self.iterator is not None and not self._sizing:
193 self._fetch(row + 20)
211 194 have_rows = len(self.items)
212 have_cols = len(self.displayattrs)
195 have_cols = len(self._displayattrs)
213 196 if have_rows > had_rows:
214 197 msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, have_rows - had_rows)
215 198 self.GetView().ProcessTableMessage(msg)
216 self.sizing = True
199 self._sizing = True
217 200 self.GetView().AutoSizeColumns(False)
218 self.sizing = False
201 self._sizing = False
219 202 if row >= have_rows:
220 203 return ""
221 if self.displayattrs != had_cols:
204 if self._displayattrs != had_cols:
222 205 msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED, have_cols - len(had_cols))
223 206 self.GetView().ProcessTableMessage(msg)
224 207 return ""
225 208
226 209 def SetValue(self, row, col, value):
227 210 pass
228 211
229 212
230 213 class IGridGrid(wx.grid.Grid):
231 214 # The actual grid
232 215 # all methods for selecting/sorting/picking/... data are implemented here
233 216 def __init__(self, panel, input, *attrs):
234 217 wx.grid.Grid.__init__(self, panel)
235 218 fontsize = 9
236 219 self.input = input
237 220 self.table = IGridTable(self.input, fontsize, *attrs)
238 221 self.SetTable(self.table, True)
239 222 self.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
240 223 self.SetDefaultRenderer(IGridRenderer(self.table))
241 224 self.EnableEditing(False)
242 225 self.Bind(wx.EVT_KEY_DOWN, self.key_pressed)
243 226 self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.cell_doubleclicked)
244 227 self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_DCLICK, self.label_doubleclicked)
245 228 self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_label_leftclick)
246 229 self.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self._on_selected_range)
247 230 self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self._on_selected_cell)
248 231 self.current_selection = set()
249 232 self.maxchars = 200
250 233
251 234 def on_label_leftclick(self, event):
252 235 event.Skip()
253 236
254 237 def error_output(self, text):
255 238 wx.Bell()
256 239 frame = self.GetParent().GetParent().GetParent()
257 240 frame.SetStatusText(text)
258 241
259 242 def _on_selected_range(self, event):
260 243 # Internal update to the selection tracking lists
261 244 if event.Selecting():
262 245 # adding to the list...
263 246 self.current_selection.update(xrange(event.GetTopRow(), event.GetBottomRow()+1))
264 247 else:
265 248 # removal from list
266 249 for index in xrange( event.GetTopRow(), event.GetBottomRow()+1):
267 250 self.current_selection.discard(index)
268 251 event.Skip()
269 252
270 253 def _on_selected_cell(self, event):
271 254 # Internal update to the selection tracking list
272 255 self.current_selection = set([event.GetRow()])
273 256 event.Skip()
274 257
275 258 def sort(self, key, reverse=False):
276 259 """
277 260 Sort the current list of items using the key function ``key``. If
278 261 ``reverse`` is true the sort order is reversed.
279 262 """
280 263 row = self.GetGridCursorRow()
281 264 col = self.GetGridCursorCol()
282 265 curitem = self.table.items[row] # Remember where the cursor is now
283 266 # Sort items
284 267 def realkey(item):
285 268 return key(item)
286 269 try:
287 270 self.table.items = ipipe.deque(sorted(self.table.items, key=realkey, reverse=reverse))
288 271 except TypeError, exc:
289 272 self.error_output("Exception encountered: %s" % exc)
290 273 return
291 274 # Find out where the object under the cursor went
292 275 for (i, item) in enumerate(self.table.items):
293 276 if item is curitem:
294 277 self.SetGridCursor(i,col)
295 278 self.MakeCellVisible(i,col)
296 279 self.Refresh()
297 280
298 281 def sortattrasc(self):
299 282 """
300 283 Sort in ascending order; sorting criteria is the current attribute
301 284 """
302 285 col = self.GetGridCursorCol()
303 attr = self.table.displayattrs[col]
286 attr = self.table._displayattrs[col]
304 287 frame = self.GetParent().GetParent().GetParent()
305 288 if attr is ipipe.noitem:
306 289 self.error_output("no column under cursor")
307 290 return
308 291 frame.SetStatusText("sort by %s (ascending)" % attr.name())
309 292 def key(item):
310 293 try:
311 294 return attr.value(item)
312 295 except (KeyboardInterrupt, SystemExit):
313 296 raise
314 297 except Exception:
315 298 return None
316 299 self.sort(key)
317 300
318 301 def sortattrdesc(self):
319 302 """
320 303 Sort in descending order; sorting criteria is the current attribute
321 304 """
322 305 col = self.GetGridCursorCol()
323 attr = self.table.displayattrs[col]
306 attr = self.table._displayattrs[col]
324 307 frame = self.GetParent().GetParent().GetParent()
325 308 if attr is ipipe.noitem:
326 309 self.error_output("no column under cursor")
327 310 return
328 311 frame.SetStatusText("sort by %s (descending)" % attr.name())
329 312 def key(item):
330 313 try:
331 314 return attr.value(item)
332 315 except (KeyboardInterrupt, SystemExit):
333 316 raise
334 317 except Exception:
335 318 return None
336 319 self.sort(key, reverse=True)
337 320
338 321 def label_doubleclicked(self, event):
339 322 row = event.GetRow()
340 323 col = event.GetCol()
341 324 if col == -1:
342 325 self.enter(row)
343 326
344 327 def _getvalue(self, row, col):
345 328 """
346 329 Gets the text which is displayed at ``(row, col)``
347 330 """
348 331 try:
349 value = self.table.displayattrs[col].value(self.table.items[row])
332 value = self.table._displayattrs[col].value(self.table.items[row])
350 333 (align, width, text) = ipipe.xformat(value, "cell", self.maxchars)
351 334 except IndexError:
352 335 raise IndexError
353 336 except Exception, exc:
354 337 (align, width, text) = ipipe.xformat(exc, "cell", self.maxchars)
355 338 return text
356 339
357 340 def search(self, searchtext, startrow=0, startcol=0, search_forward=True):
358 341 """
359 342 search for ``searchtext``, starting in ``(startrow, startcol)``;
360 343 if ``search_forward`` is true the direction is "forward"
361 344 """
362 345 row = startrow
363 346 searchtext = searchtext.lower()
364 347 if search_forward:
365 348 while True:
366 349 for col in xrange(startcol, self.table.GetNumberCols()):
367 350 try:
368 351 foo = self.table.GetValue(row, col)
369 352 text = self._getvalue(row, col)
370 353 if searchtext in text.string().lower():
371 354 self.SetGridCursor(row, col)
372 355 self.MakeCellVisible(row, col)
373 356 return
374 357 except IndexError:
375 358 return
376 359 startcol = 0
377 360 row += 1
378 361 else:
379 362 while True:
380 363 for col in xrange(startcol, -1, -1):
381 364 try:
382 365 foo = self.table.GetValue(row, col)
383 366 text = self._getvalue(row, col)
384 367 if searchtext in text.string().lower():
385 368 self.SetGridCursor(row, col)
386 369 self.MakeCellVisible(row, col)
387 370 return
388 371 except IndexError:
389 372 return
390 373 startcol = self.table.GetNumberCols()-1
391 374 row -= 1
392 375
393 376 def key_pressed(self, event):
394 377 """
395 378 Maps pressed keys to functions
396 379 """
397 380 frame = self.GetParent().GetParent().GetParent()
398 381 frame.SetStatusText("")
399 382 sh = event.ShiftDown()
400 383 ctrl = event.ControlDown()
401 384
402 385 keycode = event.GetKeyCode()
403 386 if keycode == ord("P"):
404 387 row = self.GetGridCursorRow()
405 388 if event.ShiftDown():
406 389 col = self.GetGridCursorCol()
407 390 self.pickattr(row, col)
408 391 else:
409 392 self.pick(row)
410 393 elif keycode == ord("M"):
411 394 if ctrl:
412 395 col = self.GetGridCursorCol()
413 396 self.pickrowsattr(sorted(self.current_selection), col)
414 397 else:
415 398 self.pickrows(sorted(self.current_selection))
416 399 elif keycode in (wx.WXK_BACK, wx.WXK_DELETE, ord("X")) and not (ctrl or sh):
417 400 self.delete_current_notebook()
418 401 elif keycode == ord("E") and not (ctrl or sh):
419 402 row = self.GetGridCursorRow()
420 403 self.enter(row)
421 404 elif keycode == ord("E") and sh and not ctrl:
422 405 row = self.GetGridCursorRow()
423 406 col = self.GetGridCursorCol()
424 407 self.enterattr(row, col)
425 408 elif keycode == ord("E") and ctrl:
426 409 row = self.GetGridCursorRow()
427 410 self.SetGridCursor(row, self.GetNumberCols()-1)
428 411 elif keycode == wx.WXK_HOME or (keycode == ord("A") and ctrl):
429 412 row = self.GetGridCursorRow()
430 413 self.SetGridCursor(row, 0)
431 414 elif keycode == ord("C") and sh:
432 415 col = self.GetGridCursorCol()
433 attr = self.table.displayattrs[col]
416 attr = self.table._displayattrs[col]
434 417 returnobj = []
435 418 for i in xrange(self.GetNumberRows()):
436 returnobj.append(self.table.displayattrs[col].value(self.table.items[i]))
419 returnobj.append(self.table._displayattrs[col].value(self.table.items[i]))
437 420 self.quit(returnobj)
438 421 elif keycode in (wx.WXK_ESCAPE, ord("Q")) and not (ctrl or sh):
439 422 self.quit()
440 423 elif keycode == ord("<"):
441 424 row = self.GetGridCursorRow()
442 425 col = self.GetGridCursorCol()
443 426 if not event.ShiftDown():
444 427 newcol = col - 1
445 428 if newcol >= 0:
446 429 self.SetGridCursor(row, col - 1)
447 430 else:
448 431 newcol = col + 1
449 432 if newcol < self.GetNumberCols():
450 433 self.SetGridCursor(row, col + 1)
451 434 elif keycode == ord("D"):
452 435 col = self.GetGridCursorCol()
453 436 row = self.GetGridCursorRow()
454 437 if not sh:
455 438 self.detail(row, col)
456 439 else:
457 440 self.detail_attr(row, col)
458 441 elif keycode == ord("F") and ctrl:
459 442 frame.enter_searchtext(event)
460 443 elif keycode == wx.WXK_F3:
461 444 if sh:
462 445 frame.find_previous(event)
463 446 else:
464 447 frame.find_next(event)
465 448 elif keycode == ord("V"):
466 449 if sh:
467 450 self.sortattrdesc()
468 451 else:
469 452 self.sortattrasc()
470 453 else:
471 454 event.Skip()
472 455
473 456 def delete_current_notebook(self):
474 457 """
475 458 deletes the current notebook tab
476 459 """
477 460 panel = self.GetParent()
478 461 nb = panel.GetParent()
479 462 current = nb.GetSelection()
480 463 count = nb.GetPageCount()
481 464 if count > 1:
482 465 for i in xrange(count-1, current-1, -1):
483 466 nb.DeletePage(i)
484 467 nb.GetCurrentPage().grid.SetFocus()
485 468 else:
486 469 frame = nb.GetParent()
487 470 frame.SetStatusText("This is the last level!")
488 471
489 472 def _doenter(self, value, *attrs):
490 473 """
491 474 "enter" a special item resulting in a new notebook tab
492 475 """
493 476 panel = self.GetParent()
494 477 nb = panel.GetParent()
495 478 frame = nb.GetParent()
496 479 current = nb.GetSelection()
497 480 count = nb.GetPageCount()
498 481 try: # if we want to enter something non-iterable, e.g. a function
499 482 if current + 1 == count and value is not self.input: # we have an event in the last tab
500 483 frame._add_notebook(value, *attrs)
501 484 elif value != self.input: # we have to delete all tabs newer than [panel] first
502 485 for i in xrange(count-1, current, -1): # some tabs don't close if we don't close in *reverse* order
503 486 nb.DeletePage(i)
504 487 frame._add_notebook(value)
505 488 except TypeError, exc:
506 489 if exc.__class__.__module__ == "exceptions":
507 490 msg = "%s: %s" % (exc.__class__.__name__, exc)
508 491 else:
509 492 msg = "%s.%s: %s" % (exc.__class__.__module__, exc.__class__.__name__, exc)
510 493 frame.SetStatusText(msg)
511 494
512 495 def enterattr(self, row, col):
513 496 try:
514 attr = self.table.displayattrs[col]
497 attr = self.table._displayattrs[col]
515 498 value = attr.value(self.table.items[row])
516 499 except Exception, exc:
517 500 self.error_output(str(exc))
518 501 else:
519 502 self._doenter(value)
520 503
521 504 def enter(self, row):
522 505 try:
523 506 value = self.table.items[row]
524 507 except Exception, exc:
525 508 self.error_output(str(exc))
526 509 else:
527 510 self._doenter(value)
528 511
529 512 def detail(self, row, col):
530 513 """
531 514 shows a detail-view of the current cell
532 515 """
533 516 try:
534 attr = self.table.displayattrs[col]
517 attr = self.table._displayattrs[col]
535 518 item = self.table.items[row]
536 519 except Exception, exc:
537 520 self.error_output(str(exc))
538 521 else:
539 522 attrs = [ipipe.AttributeDetail(item, attr) for attr in ipipe.xattrs(item, "detail")]
540 523 self._doenter(attrs)
541 524
542 525 def detail_attr(self, row, col):
543 526 try:
544 attr = self.table.displayattrs[col]
527 attr = self.table._displayattrs[col]
545 528 item = attr.value(self.table.items[row])
546 529 except Exception, exc:
547 530 self.error_output(str(exc))
548 531 else:
549 532 attrs = [ipipe.AttributeDetail(item, attr) for attr in ipipe.xattrs(item, "detail")]
550 533 self._doenter(attrs)
551 534
552 535 def quit(self, returnobj=None):
553 536 """
554 537 quit
555 538 """
556 539 frame = self.GetParent().GetParent().GetParent()
557 540 if frame.helpdialog:
558 541 frame.helpdialog.Destroy()
559 542 frame.parent.returnobj = returnobj
560 543 frame.Close()
561 544 frame.Destroy()
562 545
563 546 def cell_doubleclicked(self, event):
564 547 self.enterattr(event.GetRow(), event.GetCol())
565 548
566 549 def pick(self, row):
567 550 """
568 551 pick a single row and return to the IPython prompt
569 552 """
570 553 try:
571 554 value = self.table.items[row]
572 555 except Exception, exc:
573 556 self.error_output(str(exc))
574 557 else:
575 558 self.quit(value)
576 559
577 560 def pickrows(self, rows):
578 561 """
579 562 pick multiple rows and return to the IPython prompt
580 563 """
581 564 try:
582 565 value = [self.table.items[row] for row in rows]
583 566 except Exception, exc:
584 567 self.error_output(str(exc))
585 568 else:
586 569 self.quit(value)
587 570
588 571 def pickrowsattr(self, rows, col):
589 572 """"
590 573 pick one column from multiple rows
591 574 """
592 575 values = []
593 576 try:
594 attr = self.table.displayattrs[col]
577 attr = self.table._displayattrs[col]
595 578 for row in rows:
596 579 try:
597 580 values.append(attr.value(self.table.items[row]))
598 581 except (SystemExit, KeyboardInterrupt):
599 582 raise
600 583 except Exception:
601 584 raise #pass
602 585 except Exception, exc:
603 586 self.error_output(str(exc))
604 587 else:
605 588 self.quit(values)
606 589
607 590 def pickattr(self, row, col):
608 591 try:
609 attr = self.table.displayattrs[col]
592 attr = self.table._displayattrs[col]
610 593 value = attr.value(self.table.items[row])
611 594 except Exception, exc:
612 595 self.error_output(str(exc))
613 596 else:
614 597 self.quit(value)
615 598
616 599
617 600 class IGridPanel(wx.Panel):
618 601 # Each IGridPanel contains an IGridGrid
619 602 def __init__(self, parent, input, *attrs):
620 603 wx.Panel.__init__(self, parent, -1)
621 604 self.grid = IGridGrid(self, input, *attrs)
622 605 sizer = wx.BoxSizer(wx.VERTICAL)
623 606 sizer.Add(self.grid, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
624 607 self.SetSizer(sizer)
625 608 sizer.Fit(self)
626 609 sizer.SetSizeHints(self)
627 610
628 611
629 612 class IGridHTMLHelp(wx.Frame):
630 613 def __init__(self, parent, title, filename, size):
631 614 wx.Frame.__init__(self, parent, -1, title, size=size)
632 615 html = wx.html.HtmlWindow(self)
633 616 if "gtk2" in wx.PlatformInfo:
634 617 html.SetStandardFonts()
635 618 html.LoadFile(filename)
636 619
637 620
638 621 class IGridFrame(wx.Frame):
639 622 maxtitlelen = 30
640 623
641 624 def __init__(self, parent, input):
642 625 wx.Frame.__init__(self, None, title="IGrid", size=(640, 480))
643 626 self.menubar = wx.MenuBar()
644 627 self.menucounter = 100
645 628 self.m_help = wx.Menu()
646 629 self.m_search = wx.Menu()
647 630 self.m_sort = wx.Menu()
648 631 self.notebook = wx.Notebook(self, -1, style=0)
649 632 self.statusbar = self.CreateStatusBar(1, wx.ST_SIZEGRIP)
650 633 self.parent = parent
651 634 self._add_notebook(input)
652 635 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
653 636 self.makemenu(self.m_sort, "&Sort (asc)", "Sort ascending", self.sortasc)
654 637 self.makemenu(self.m_sort, "Sort (&desc)", "Sort descending", self.sortdesc)
655 638 self.makemenu(self.m_help, "&Help", "Help", self.display_help)
656 639 self.makemenu(self.m_help, "&Show help in browser", "Show help in browser", self.display_help_in_browser)
657 640 self.makemenu(self.m_search, "&Find text", "Find text", self.enter_searchtext)
658 641 self.makemenu(self.m_search, "Find by &expression", "Find by expression", self.enter_searchexpression)
659 642 self.makemenu(self.m_search, "Find &next", "Find next", self.find_next)
660 643 self.makemenu(self.m_search, "Find &previous", "Find previous", self.find_previous)
661 644 self.menubar.Append(self.m_search, "&Find")
662 645 self.menubar.Append(self.m_sort, "&Sort")
663 646 self.menubar.Append(self.m_help, "&Help")
664 647 self.SetMenuBar(self.menubar)
665 648 self.searchtext = ""
666 649 self.helpdialog = None
667 650
668 651 def sortasc(self, event):
669 652 grid = self.notebook.GetPage(self.notebook.GetSelection()).grid
670 653 grid.sortattrasc()
671 654
672 655 def sortdesc(self, event):
673 656 grid = self.notebook.GetPage(self.notebook.GetSelection()).grid
674 657 grid.sortattrdesc()
675 658
676 659 def find_previous(self, event):
677 660 """
678 661 find previous occurrences
679 662 """
680 663 if self.searchtext:
681 664 grid = self.notebook.GetPage(self.notebook.GetSelection()).grid
682 665 row = grid.GetGridCursorRow()
683 666 col = grid.GetGridCursorCol()
684 667 if col-1 >= 0:
685 668 grid.search(self.searchtext, row, col-1, False)
686 669 else:
687 670 grid.search(self.searchtext, row-1, grid.table.GetNumberCols()-1, False)
688 671 else:
689 672 self.enter_searchtext(event)
690 673
691 674 def find_next(self, event):
692 675 """
693 676 find the next occurrence
694 677 """
695 678 if self.searchtext:
696 679 grid = self.notebook.GetPage(self.notebook.GetSelection()).grid
697 680 row = grid.GetGridCursorRow()
698 681 col = grid.GetGridCursorCol()
699 682 if col+1 < grid.table.GetNumberCols():
700 683 grid.search(self.searchtext, row, col+1)
701 684 else:
702 685 grid.search(self.searchtext, row+1, 0)
703 686 else:
704 687 self.enter_searchtext(event)
705 688
706 689 def display_help(self, event):
707 690 """
708 691 Display a help dialog
709 692 """
710 693 if self.helpdialog:
711 694 self.helpdialog.Destroy()
712 695 filename = os.path.join(os.path.dirname(__file__), "igrid_help.html")
713 696 self.helpdialog = IGridHTMLHelp(None, title="Help", filename=filename, size=wx.Size(600,400))
714 697 self.helpdialog.Show()
715 698
716 699 def display_help_in_browser(self, event):
717 700 """
718 701 Show the help-HTML in a browser (as a ``HtmlWindow`` does not understand
719 702 CSS this looks better)
720 703 """
721 704 filename = urllib.pathname2url(os.path.abspath(os.path.join(os.path.dirname(__file__), "igrid_help.html")))
722 705 if not filename.startswith("file"):
723 706 filename = "file:" + filename
724 707 webbrowser.open(filename, new=1, autoraise=True)
725 708
726 709 def enter_searchexpression(self, event):
727 710 pass
728 711
729 712 def makemenu(self, menu, label, help, cmd):
730 713 menu.Append(self.menucounter, label, help)
731 714 self.Bind(wx.EVT_MENU, cmd, id=self.menucounter)
732 715 self.menucounter += 1
733 716
734 717 def _add_notebook(self, input, *attrs):
735 718 # Adds another notebook which has the starting object ``input``
736 719 panel = IGridPanel(self.notebook, input, *attrs)
737 720 text = str(ipipe.xformat(input, "header", self.maxtitlelen)[2])
738 721 if len(text) >= self.maxtitlelen:
739 722 text = text[:self.maxtitlelen].rstrip(".") + "..."
740 723 self.notebook.AddPage(panel, text, True)
741 724 panel.grid.SetFocus()
742 725 self.Layout()
743 726
744 727 def OnCloseWindow(self, event):
745 728 self.Destroy()
746 729
747 730 def enter_searchtext(self, event):
748 731 # Displays a dialog asking for the searchtext
749 732 dlg = wx.TextEntryDialog(self, "Find:", "Find in list")
750 733 if dlg.ShowModal() == wx.ID_OK:
751 734 self.searchtext = dlg.GetValue()
752 735 self.notebook.GetPage(self.notebook.GetSelection()).grid.search(self.searchtext, 0, 0)
753 736 dlg.Destroy()
754 737
755 738
756 739 class igrid(ipipe.Display):
757 740 """
758 741 This is a wx-based display object that can be used instead of ``ibrowse``
759 742 (which is curses-based) or ``idump`` (which simply does a print).
760 743 """
761 744 def display(self):
762 745 self.returnobj = None
763 746 app = wx.App()
764 747 self.frame = IGridFrame(self, self.input)
765 748 self.frame.Show()
766 749 app.SetTopWindow(self.frame)
767 750 self.frame.Raise()
768 751 app.MainLoop()
769 752 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