#!/usr/bin/env python
# -*- coding: utf-8 -*-
import traceback
import wx
import wx.grid as gridlib
from library import i18n
_ = i18n.i18n()
class CustomDataTable(gridlib.PyGridTableBase):
def __init__(self):
gridlib.PyGridTableBase.__init__(self)
self.colLabels = [_("fac_lbl_gr_tag_name"), _("fac_lbl_gr_field_name"),
_("fac_lbl_gr_field_lenght"), _("fac_lbl_gr_read_only")]
self.dataTypes = [gridlib.GRID_VALUE_STRING, gridlib.GRID_VALUE_STRING,
gridlib.GRID_VALUE_STRING, gridlib.GRID_VALUE_BOOL]
self.data = []
self.attr_cell = {}
self.attr_col = {}
self.attr_row = {}
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.colLabels)
def IsEmptyCell(self, row, col):
try:
return not self.data[row][col]
except IndexError:
return True
# Get/Set values in the table. The Python version of these
# methods can handle any data-type, (as long as the Editor and
# Renderer understands the type too,) not just strings as in the
# C++ version.
def GetValue(self, row, col):
try:
return self.data[row][col]
except IndexError:
return ''
def SetValue(self, row, col, value):
try:
self.data[row][col] = value
except IndexError:
# add a new row
self.data.append([''] * (self.GetNumberCols() -1) + [0])
self.SetValue(row, col, value)
self.__notify_table_add()
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetTypeName(self, row, col):
return self.dataTypes[col]
def CanGetValueAs(self, row, col, typeName):
colType = self.dataTypes[col].split(':')[0]
if typeName == colType:
return True
else:
return False
def CanSetValueAs(self, row, col, typeName):
return self.CanGetValueAs(row, col, typeName)
def DeleteRows(self, pos=0, num=1, *args, **kw):
""" Delete some row(s)
"""
#self.data
for i in xrange(num):
self.data.pop(pos)
msg = wx.grid.GridTableMessage(self, gridlib.GRIDTABLE_NOTIFY_ROWS_DELETED,
len(self.data), num)
self.GetView().ProcessTableMessage(msg)
def AppendRows(self, num=1, *arg, **kw):
""" Append rows
"""
for num_rows in xrange(num):
self.data.append([''] * (self.GetNumberCols() -1) + [0])
self.__notify_table_add(num)
#
# Non default methods
#
def appendRowsData(self, data):
""" Append and set data
"""
if not isinstance(data, list):
data = list(data)
data = [list(x) for x in data]
self.data += data
self.__notify_table_add(len(data))
def moveRowDown(self, row):
"""
"""
self.data.insert(row, self.data.pop(row+1))
def moveRowUp(self, row):
"""
"""
self.data.insert(row-1, self.data.pop(row))
def __notify_table_add(self, how_many=1):
"""
"""
# tell the grid we've added a row
msg = gridlib.GridTableMessage(self,
gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED,
how_many )
self.GetView().ProcessTableMessage(msg)
#---------------------------------------------------------------------------
class CustTableGrid(gridlib.Grid):
def __init__(self, parent):
gridlib.Grid.__init__(self, parent, -1)
self._table = CustomDataTable()
self.SetTable(self._table, True)
self.SetRowLabelSize(15)
self.SetMargins(0,0)
self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnLeftDClick)
def OnLeftDClick(self, evt):
if self.CanEnableCellControl():
self.EnableCellEditControl()
#
# External interface
#
def appendRowsData(self, data):
self._table.appendRowsData(data)
def getValues(self):
""" Return the grid values
"""
data = []
rows = self._table.GetNumberRows()
cols = self._table.GetNumberCols()
for row in xrange(rows):
data.append( [str(self._table.GetValue(row, col)) for col in xrange(cols)] )
return data
def clear(self):
""" Clear the grid from its values
"""
self._table.DeleteRows(0, self._table.GetNumberRows())
def moveRowUp(self, rows=None):
"""
"""
rs = self._get_row_sel(rows)
if rs == -1 or rs == 0:
#no selected or the first
return
self._table.moveRowUp(rs)
self.SelectRow(rs-1, False)
wx.CallAfter(self.Refresh)
def moveRowDown(self, rows=None):
"""
"""
rs = self._get_row_sel(rows)
if rs == -1 or rs == self._table.GetNumberRows() -1:
#no selected or the last
return
self._table.moveRowDown(rs)
self.SelectRow(rs+1, False)
wx.CallAfter(self.Refresh)
def _get_row_sel(self, rows):
"""
"""
if not rows:
rows = self.GetSelectedRows()
if not rows:
return -1
else:
return rows[0]
def _get_value_(self, row, col):
return self._table.GetValue(row, col)
|