[wxPython] wxGrid Cell Format

Sorry about that. Standalone Boa Frame below.

Lynndon

···

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Wednesday, 11 September 2002 2:56 AM
To: wxpython-users@lists.wxwindows.org
Subject: Re: [wxPython] wxGrid Cell Format

When I say "small sample" I mean something a lot less than 600 lines that is
completly standalone and has the minimum amount of windows/controls/data to
show the problem in question. If it takes me longer than five minutes to
understand your code before running it then I won't.

The reason I require this is that in 90% of the cases when you go through
the effort to do the above you will find and solve the problem yourself.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

#Boa:Frame:wxFrame2

from wxPython.wx import *
from wxPython.lib.anchors import LayoutAnchors
from wxPython.lib.bcrtl.user.StaticTextCtrl import *
from wxPython.grid import *
from wxPython.lib.buttons import *
from math import *
import string
global TotVol
global sPsmys
TotVol=0
SPsmys=1000000
ColHeaders=["ID","Len(m)","OD(mm)","WT(mm)","Gr","SMYS","Vol(m3)","Psmys"]
RowHeaders=["1","2"]
NCols=len(ColHeaders)
NRows=len(RowHeaders)
PipeGridData = [
            [1, 30000, 610.0, 9.53, "X-70", 483,0,0],
            [2, 10000, 610.0, 9.53, "X-70",483,0,0]
            ]
def GridUpdate(GridData):
    z=len(GridData)
    for i in range(0,z):
        GridData[i][0]=i+1
       # calc volume
        length = GridData[i][1]
        temp1 = GridData[i][3]
        temp1 = float(temp1) / 1000 # wall thickness in m
        idia = float(GridData[i][2]) / 1000
        idia = idia - 2 * temp1
        vol = length * pi/4
        vol = vol * idia * idia # in m3
        GridData[i][6] = vol
       # calc SMYS
        if GridData[i][4] == "B":
            GridData[i][5] = 241
        elif GridData[i][4][0:1] =="X":
            GridData[i][5] = int(float(GridData[i][4][2:4]) * 6.895)
    # calc Psmys
        Psmys = 2 * temp1 * GridData[i][5]
        if idia <> 0:
            Psmys = Psmys / idia # in MPa
        GridData[i][7] = Psmys

def CalcVol(GridData,Tvol):
    Tvol = 0
    z=len(GridData)
    for i in range(0,z):
        Tvol = Tvol + GridData[i][6]
    print "CalcVol ",Tvol
    return Tvol

def CalcPsmys(GridData,mPsmys):
    mPsmys = 1000000
    z=len(GridData)
    for i in range(0,z):
        if GridData[i][7] <> 0:
            mPsmys = min(mPsmys,GridData[i][7])
    print "CalcPsmys ",mPsmys
    return mPsmys
        
GridUpdate(PipeGridData)
TotVol = CalcVol(PipeGridData,TotVol)
SPsmys = CalcPsmys(PipeGridData,SPsmys)
#---------------------------------------------------------------------------
class PipesTable(wxPyGridTableBase):
    """
    """
    def __init__(self):
        wxPyGridTableBase.__init__(self)
        self.colLabels = ColHeaders
        NCols=len(self.colLabels)
        NRows=len(RowHeaders)
        self.dataTypes = [wxGRID_VALUE_NUMBER,
                          wxGRID_VALUE_NUMBER,
                          wxGRID_VALUE_CHOICE +
':60.3,88.9,114.3,168.3,219.1,273.1,323.9,355.6,406.4,457.0,508.0,559.0,610.
0,660.0,711.0,762.0,813.0,864.0,914.0,1067.0',
                          wxGRID_VALUE_FLOAT + ':4 , 2',
                          wxGRID_VALUE_CHOICE +
':B,X-42,X-48,X-52,X-56,X-65,X-70,X-80',
                          wxGRID_VALUE_NUMBER,
                          wxGRID_VALUE_FLOAT + ':6 , 3',
                          wxGRID_VALUE_FLOAT + ':6, 3']
        self.data = PipeGridData
    #--------------------------------------------------
    # required methods for the wxPyGridTableBase interface

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return len(self.data[0])

    def IsEmptyCell(self, row, col):
        return not self.data[row][col]

    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
            print "error"
    #--------------------------------------------------
    # Some optional methods
    # Called when the grid needs to display labels
    def GetColLabelValue(self, col):
        return self.colLabels[col]

    def GetTypeName(self, row, col):
        return self.dataTypes[col]

    def CanGetValueAs(self, row, col, typeName):
        colType = string.split(self.dataTypes[col], ':')[0]
        if typeName == colType:
            return true
        else:
            return false

    def CanSetValueAs(self, row, col, typeName):
        return self.CanGetValueAs(row, col, typeName)

# -----------------------------------------
class PipesGrid(wxGrid):
    def
__init__(self,parent,id=-1,pos=wxDefaultPosition,size=wxDefaultSize,style=wx
WANTS_CHARS,name="grid"):
        wxGrid.__init__(self, parent, id, pos, size, style, name)
        self.table = PipesTable()

        self.SetTable(self.table, true)

        self.SetRowLabelSize(0)
        self.SetMargins(0,0)
        self.SetColLabelSize(18)
        self.AutoSizeColumns(false)
        self.SetDefaultCellAlignment(wxALIGN_RIGHT,wxALIGN_CENTER)

# EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)

        for i in range(NCols):
            self.SetColLabelValue(i,ColHeaders[i])
            self.SetColSize(i,50)
        self.SetColSize(0,20)
        self.SetColSize(5,40)
        self.SetColSize(6,60)

    def SetData(self,data):
        for row in range(len(data)):
            for col in range(len(data[row])):
                self.SetCellValue(row,col,data[row][col])
            self.SetCellValue(row,1,row)
# ----------------------------------------------
def create(parent):
    return wxFrame2(parent)

[wxID_WXFRAME2, wxID_WXFRAME2ADD, wxID_WXFRAME2BUTTON2, wxID_WXFRAME2DELETE,
wxID_WXFRAME2GRID1, wxID_WXFRAME2PANEL2] = map(lambda _init_ctrls:
wxNewId(), range(6))

class wxFrame2(wxFrame):
    _custom_classes = {'wxGrid':['PipesGrid']}
    def _init_utils(self):
        pass

    def _init_ctrls(self, prnt):
        wxFrame.__init__(self, id = wxID_WXFRAME2, name = '', parent = prnt,
pos = wxPoint(205, 8), size = wxSize(588, 527), style =
wxDEFAULT_FRAME_STYLE, title = 'Test Section Calculation')
        self._init_utils()
        self.SetBackgroundColour(wxColour(192, 192, 192))
        self.SetClientSize(wxSize(580, 500))

        self.panel2 = wxPanel(id = wxID_WXFRAME2PANEL2, name = 'panel2',
parent = self, pos = wxPoint(0, 56), size = wxSize(576, 144), style =
wxSUNKEN_BORDER | wxTAB_TRAVERSAL)
        self.panel2.SetBackgroundColour(wxColour(128, 255, 255))
        self.panel2.SetToolTipString('Pipeline Test Section Physical
Properties')

        self.button2 = wxButton(id = wxID_WXFRAME2BUTTON2, label = '&Close',
name = 'button2', parent = self, pos = wxPoint(528, 24), size = wxSize(51,
23), style = 0)
        EVT_BUTTON(self.button2, wxID_WXFRAME2BUTTON2, self.OnButton2Button)

        self.grid1 = PipesGrid(id = wxID_WXFRAME2GRID1, name = 'grid1',
parent = self.panel2, pos = wxPoint(136, 0), size = wxSize(408, 88), style =
wxVSCROLL | wxDOUBLE_BORDER)
        self.grid1.SetToolTipString('Pipe data grid')
        EVT_GRID_CELL_CHANGE(self.grid1, self.OnCellChange)

        self.Add = wxButton(id = wxID_WXFRAME2ADD, label = '+', name =
'Add', parent = self.panel2, pos = wxPoint(552, 0), size = wxSize(16, 16),
style = 0)
        self.Add.SetToolTipString('Add a pipe. Dont forget to complete
length, dia, wall thickness and pipe grade.')
        EVT_BUTTON(self.Add, wxID_WXFRAME2ADD, self.OnAddButton)

        self.Delete = wxButton(id = wxID_WXFRAME2DELETE, label = '-', name =
'Delete', parent = self.panel2, pos = wxPoint(552, 24), size = wxSize(16,
16), style = 0)
        self.Delete.SetToolTipString('Delete a pipe')
        EVT_BUTTON(self.Delete, wxID_WXFRAME2DELETE, self.OnDeleteButton)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):
        pass

    def OnButton2Button(self, event):
        self.Close()

    def OnAddButton(self, event):
        table = self.grid1.table
        NRows = self.grid1.GetNumberRows()
        NRows = NRows + 1
        table.data.append([NRows, 0, 0, 0, "",0,0,0])
        msg = wxGridTableMessage(table, # the table
            wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
            1) # how many
        self.grid1.ProcessTableMessage(msg)

    def OnDeleteButton(self, event):
        DelRow = self.grid1.GetGridCursorRow()+1
        table = self.grid1.table
        global PipeGridData
        NewGridData =
PipeGridData[0:DelRow-1]+PipeGridData[DelRow:len(PipeGridData)]
        PipeGridData = NewGridData
        GridUpdate(PipeGridData)
        self.grid1.Destroy()
        self.grid1 = PipesGrid(id = wxID_WXFRAME2GRID1,
                             name = 'grid1',
                             parent = self.panel2,
                             pos = wxPoint(136, 0),
                             size = wxSize(408, 88),
                             style = wxVSCROLL | wxDOUBLE_BORDER)
        self.grid1.SetToolTipString('Pipe data grid')
        TotVol = 0
        SPsmys = 1000000
        TotVol = CalcVol(PipeGridData,TotVol)
        SPsmys = CalcPsmys(PipeGridData,SPsmys)
        self.TextNoPipes.SetLabel('%2.0f' % NRows)
        self.textSectVol.SetLabel('%9.3f' % TotVol)
        self.textPsmys.SetLabel('%9.3f' % SPsmys)
        EVT_GRID_CELL_CHANGE(self.grid1, self.OnCellChange)

    def OnCellChange(self, event):
        NRows = self.grid1.GetNumberRows()
        GridUpdate(PipeGridData)
        self.grid1.BeginBatch()
        self.grid1.EndBatch()
        TotVol = 0
        SPsmys = 1000000
        TotVol = CalcVol(PipeGridData,TotVol)
        SPsmys = CalcPsmys(PipeGridData,SPsmys)

    def OnWxframe2Activate(self, event):
        pass