451 |
451 |
452 #------------------------------------------------------------------------------- |
452 #------------------------------------------------------------------------------- |
453 # Helper for VariablesGrid values |
453 # Helper for VariablesGrid values |
454 #------------------------------------------------------------------------------- |
454 #------------------------------------------------------------------------------- |
455 |
455 |
456 class VariablesTable(wx.grid.PyGridTableBase): |
456 class VariablesTable(CustomTable): |
457 |
457 |
458 """ |
|
459 A custom wxGrid Table using user supplied data |
|
460 """ |
|
461 def __init__(self, parent, data, colnames): |
|
462 # The base class must be initialized *first* |
|
463 wx.grid.PyGridTableBase.__init__(self) |
|
464 self.data = data |
|
465 self.colnames = colnames |
|
466 self.Parent = parent |
|
467 # XXX |
|
468 # we need to store the row length and collength to |
|
469 # see if the table has changed size |
|
470 self._rows = self.GetNumberRows() |
|
471 self._cols = self.GetNumberCols() |
|
472 |
|
473 def GetNumberCols(self): |
|
474 return len(self.colnames) |
|
475 |
|
476 def GetNumberRows(self): |
|
477 return len(self.data) |
|
478 |
|
479 def GetColLabelValue(self, col): |
|
480 if col < len(self.colnames): |
|
481 return self.colnames[col] |
|
482 |
|
483 def GetRowLabelValues(self, row): |
|
484 return row |
|
485 |
|
486 def GetValue(self, row, col): |
458 def GetValue(self, row, col): |
487 if row < self.GetNumberRows(): |
459 if row < self.GetNumberRows(): |
488 if col == 0: |
460 if col == 0: |
489 return row + 1 |
461 return row + 1 |
490 else: |
462 else: |
491 return str(self.data[row].get(self.GetColLabelValue(col), "")) |
463 return str(self.data[row].get(self.GetColLabelValue(col, False), "")) |
492 |
464 |
493 def GetValueByName(self, row, colname): |
|
494 if row < self.GetNumberRows(): |
|
495 return self.data[row].get(colname, None) |
|
496 return None |
|
497 |
|
498 def SetValue(self, row, col, value): |
|
499 if col < len(self.colnames): |
|
500 self.data[row][self.GetColLabelValue(col)] = value |
|
501 |
|
502 def SetValueByName(self, row, colname, value): |
|
503 if row < self.GetNumberRows(): |
|
504 self.data[row][colname] = value |
|
505 |
|
506 def ResetView(self, grid): |
|
507 """ |
|
508 (wxGrid) -> Reset the grid view. Call this to |
|
509 update the grid if rows and columns have been added or deleted |
|
510 """ |
|
511 grid.BeginBatch() |
|
512 for current, new, delmsg, addmsg in [ |
|
513 (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED), |
|
514 (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED), |
|
515 ]: |
|
516 if new < current: |
|
517 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new) |
|
518 grid.ProcessTableMessage(msg) |
|
519 elif new > current: |
|
520 msg = wx.grid.GridTableMessage(self,addmsg,new-current) |
|
521 grid.ProcessTableMessage(msg) |
|
522 self.UpdateValues(grid) |
|
523 grid.EndBatch() |
|
524 |
|
525 self._rows = self.GetNumberRows() |
|
526 self._cols = self.GetNumberCols() |
|
527 # update the column rendering scheme |
|
528 self._updateColAttrs(grid) |
|
529 |
|
530 # update the scrollbars and the displayed part of the grid |
|
531 grid.AdjustScrollbars() |
|
532 grid.ForceRefresh() |
|
533 |
|
534 def UpdateValues(self, grid): |
|
535 """Update all displayed values""" |
|
536 # This sends an event to the grid table to update all of the values |
|
537 msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES) |
|
538 grid.ProcessTableMessage(msg) |
|
539 |
|
540 def _updateColAttrs(self, grid): |
465 def _updateColAttrs(self, grid): |
541 """ |
466 """ |
542 wxGrid -> update the column attributes to add the |
467 wxGrid -> update the column attributes to add the |
543 appropriate renderer given the column name. |
468 appropriate renderer given the column name. |
544 |
469 |
566 |
491 |
567 grid.SetCellEditor(row, col, editor) |
492 grid.SetCellEditor(row, col, editor) |
568 grid.SetCellRenderer(row, col, renderer) |
493 grid.SetCellRenderer(row, col, renderer) |
569 |
494 |
570 grid.SetCellBackgroundColour(row, col, wx.WHITE) |
495 grid.SetCellBackgroundColour(row, col, wx.WHITE) |
571 |
496 self.ResizeRow(grid, row) |
572 def SetData(self, data): |
497 |
573 self.data = data |
|
574 |
|
575 def GetData(self): |
|
576 return self.data |
|
577 |
|
578 def GetCurrentIndex(self): |
|
579 return self.CurrentIndex |
|
580 |
|
581 def SetCurrentIndex(self, index): |
|
582 self.CurrentIndex = index |
|
583 |
|
584 def AppendRow(self, row_content): |
|
585 self.data.append(row_content) |
|
586 |
|
587 def InsertRow(self, row_index, row_content): |
|
588 self.data.insert(row_index, row_content) |
|
589 |
|
590 def RemoveRow(self, row_index): |
|
591 self.data.pop(row_index) |
|
592 |
|
593 def MoveRow(self, row_index, move): |
|
594 new_index = max(0, min(row_index + move, len(self.data) - 1)) |
|
595 if new_index != row_index: |
|
596 self.data.insert(new_index, self.data.pop(row_index)) |
|
597 return new_index |
|
598 |
|
599 def GetRow(self, row_index): |
|
600 return self.data[row_index] |
|
601 |
|
602 def Empty(self): |
|
603 self.data = [] |
|
604 self.editors = [] |
|
605 |
|
606 |
498 |
607 [ID_VARIABLESEDITOR, ID_VARIABLESEDITORVARIABLESGRID, |
499 [ID_VARIABLESEDITOR, ID_VARIABLESEDITORVARIABLESGRID, |
608 ID_VARIABLESEDITORADDVARIABLEBUTTON, ID_VARIABLESEDITORDELETEVARIABLEBUTTON, |
500 ID_VARIABLESEDITORADDVARIABLEBUTTON, ID_VARIABLESEDITORDELETEVARIABLEBUTTON, |
609 ID_VARIABLESEDITORUPVARIABLEBUTTON, ID_VARIABLESEDITORDOWNVARIABLEBUTTON |
501 ID_VARIABLESEDITORUPVARIABLEBUTTON, ID_VARIABLESEDITORDOWNVARIABLEBUTTON |
610 ] = [wx.NewId() for _init_ctrls in range(6)] |
502 ] = [wx.NewId() for _init_ctrls in range(6)] |