[Bio] / ModelEditor / DynParamsDialog.py Repository:
ViewVC logotype

View of /ModelEditor/DynParamsDialog.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Tue Jan 17 17:12:47 2006 UTC (13 years, 7 months ago) by efrank
Branch: MAIN
CVS Tags: lwc, HEAD
Changes since 1.2: +6 -2 lines
persistence for dynamics stuff
bunch of flux modelinig stuff including flux model building in modeleditor

from wxPython.wx import *
from wxPython.grid import *

from KahUtils.PyUtils import classFromName

class DynParamsDialog(wxDialog):
    #------------------------------------------------------------------------
    def __init__(self, parent, ID, title, rx, dyn=None,
                 pos=wxDefaultPosition, size=wxDefaultSize,
                 style=wxDEFAULT_DIALOG_STYLE):
    #------------------------------------------------------------------------
        wxDialog.__init__(self, parent, ID, title, pos, size, style)

        self.parent = parent
        self.rx = rx          #the Rx the dyn will be assoc. with
        self.dyn = dyn        #instance of Dynamics derivative, or None
        self.dynClass = None  #python class (so we can call ctor). init below
        
        dynParamPanel = wxPanel(self, -1)
        
        self.onSetClassId = wxNewId()
        
        self.okDialogBtn = wxButton(dynParamPanel, wxID_OK, "OK")
        self.cancelDialogBtn = wxButton(dynParamPanel, wxID_CANCEL, "Cancel")
        self.setClassBtn = wxButton(dynParamPanel, self.onSetClassId,
                                    " Set Dyn Class ")



        self.dynTypeBox = wxTextCtrl(dynParamPanel, -1)

        self.dynParamGrid = wxGrid(dynParamPanel, -1)
        self.dynParamGrid.CreateGrid(0, 3)
        self.dynParamGrid.SetColLabelValue(0, "Name")
        self.dynParamGrid.SetColLabelValue(1, "Type")
        self.dynParamGrid.SetColLabelValue(2, "Value")
        self.dynParamGrid.AutoSizeColumn(0)
        self.dynParamGrid.AutoSizeColumn(1)
        self.dynParamGrid.AutoSizeColumn(2)

        if (None==self.dyn):
            i = 0
            self.dynClass = None
            self.classLabel = wxStaticText(dynParamPanel, wxID_ANY,
                                           "Current Dyn:  None")
        else:
            self.dynClass = classFromName( self.dyn.getName() )
            self.classLabel = wxStaticText(dynParamPanel, wxID_ANY,
                                           "Current Dyn:  " +dyn.getName())
            i = 0;
            v = self.dyn.getParamValues()
            for param in self.dyn.listParams(self.rx):
                self.dynParamGrid.AppendRows(numRows=1)
                self.dynParamGrid.SetCellValue(i,0,param[0])
                self.dynParamGrid.SetCellValue(i,1,param[1])
                self.dynParamGrid.SetCellValue(i,2,str(v[i]))
                i = i + 1

        dynParamMainSizer = wxBoxSizer(wxVERTICAL)
        dynSetHSizer = wxBoxSizer(wxHORIZONTAL)
        dialogChoicesHSizer = wxBoxSizer(wxHORIZONTAL)

        dynSetHSizer.Add (self.dynTypeBox)
        dynSetHSizer.Add (self.setClassBtn)

        dialogChoicesHSizer.Add (self.okDialogBtn)
        dialogChoicesHSizer.Add (self.cancelDialogBtn)      

        dynParamMainSizer.Add(self.classLabel)
        dynParamMainSizer.Add(dynSetHSizer)
        dynParamMainSizer.Add (self.dynParamGrid, 3, wxEXPAND)        
        dynParamMainSizer.Add ((20,20), 0, wxEXPAND)
        dynParamMainSizer.Add(dialogChoicesHSizer)


        dynParamPanel.SetSizer(dynParamMainSizer)
        dynParamPanel.SetAutoLayout(true)

        EVT_BUTTON(self, self.cancelDialogBtn.GetId(), self.onDialogCancel)
        EVT_BUTTON(self, self.okDialogBtn.GetId(),     self.onDialogOK)       
        EVT_BUTTON(self, self.setClassBtn.GetId(), self.onSetClass)

        return

    #------------------------------------------------------------------------
    def onSetClass(self, evt):
    #------------------------------------------------------------------------
        if (self.dynParamGrid.GetNumberRows() > 0):
            self.dynParamGrid.DeleteRows(0, self.dynParamGrid.GetNumberRows())
             
        dynClassName = self.dynTypeBox.GetValue()
        if ( "" == dynClassName.strip()):
            self.parent.error( "Specify Package.Module, e.g., KahOM.MMDynamics")
            return

        try:
            self.dynClass = classFromName( dynClassName )  #eg KahOM.MMDynamics
        except ImportError:
            self.parent.error( "Could not find requested class" )
            return

        i = 0
        for param in self.dynClass.listParams(self.rx):
            self.dynParamGrid.AppendRows(numRows=1)
            self.dynParamGrid.SetCellValue(i,0,param[0])
            self.dynParamGrid.SetCellValue(i,1,param[1])
            self.dynParamGrid.SetCellValue(i,2,str(param[2]))
            i = i + 1

        return


    #------------------------------------------------------------------------
    def addRows(self, params):
    #------------------------------------------------------------------------
        i = 0
        for param in params:
            self.dynParamGrid.AppendRows(numRows=1)
            self.dynParamGrid.SetCellValue(i,0,param[0])
            self.dynParamGrid.SetCellValue(i,1,param[1])
            self.dynParamGrid.SetCellValue(i,2,str(param[2]))
            i = i + 1
        return
   
    #------------------------------------------------------------------------
    def onDialogCancel(self, evt):
    #------------------------------------------------------------------------
        self.EndModal(wxID_CANCEL)
        return

    #------------------------------------------------------------------------
    def onDialogOK(self, evt):
    #------------------------------------------------------------------------
        self.EndModal(wxID_OK)
        return

    #------------------------------------------------------------------------
    def GetValue(self):
    #------------------------------------------------------------------------
        vals = []

        for i in range(self.dynParamGrid.GetNumberRows()):
            if (self.dynParamGrid.GetCellValue(i,1) == "float"):
                vals.append(float(self.dynParamGrid.GetCellValue(i, 2)))
            elif (self.dynParamGrid.GetCellValue(i,1) == "int"):
                vals.append(str(self.dynParamGrid.GetCellValue(i, 2)))

        if (not None==self.dynClass):
            try:
                return self.dynClass(vals, self.rx)
            except ValueError:
                self.parent.error("Bad arguments for creating the dynamics")
        else:
            self.parent.error( "No dynamics class was set.")

        return None


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3