grid_fields.py :  » Business-Application » hylaPEx » hylapex » library » options » opt_db » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » hylaPEx 
hylaPEx » hylapex » library » options » opt_db » grid_fields.py
#!/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)
 
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.