###################################################################### # Copyright (c) 2007, Petteri Aimonen # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice and this list of conditions. # * Redistributions in binary form must reproduce the above copyright # notice and this list of conditions in the documentation and/or other # materials provided with the distribution. '''Panel for per-animal information Allows editing group/active/notes ''' import wx from wx.lib.intctrl import IntCtrl, EVT_INT import events from pymisc import _ from mplutils import PlotPanel from multigraph import plot_animal from dataaccess import animalinfo, animalnotes, animal_getfeeds from database import db_conn # Graph #______________________________ # Notes: Group: ______ [X] Active # --------------------------- # | | # | | # |___________________________| # # Save Undo class AnimalPlotPanel(PlotPanel): def __init__(self, *args, **kwargs): PlotPanel.__init__(self, *args, **kwargs) self.animalid = None def draw(self): self.figure.clear() if self.animalid is None: return self.subplot = self.figure.add_subplot(1,1,1) plot_animal(self.subplot, self.animalid, self.fromdate, self.todate) self.canvas.draw() class AnimalView(wx.Panel): def __init__(self, parent, **kwargs): wx.Panel.__init__(self, parent, **kwargs) self.plotpanel = AnimalPlotPanel(self) self.notes = wx.TextCtrl(self, style = wx.TE_MULTILINE) self.group = IntCtrl(self) self.active = wx.CheckBox(self, label = _('Include animal in graphs')) self.savebutton = wx.Button(self, wx.ID_SAVE, label = _('Save')) self.undobutton = wx.Button(self, wx.ID_REVERT_TO_SAVED, label = _('Revert to saved')) self.subsizer = wx.BoxSizer(wx.HORIZONTAL) self.subsizer.Add(wx.StaticText(self, label = _('Notes:')), flag = wx.CENTER) self.subsizer.Add(wx.Panel(self), flag = wx.EXPAND, proportion = 1) self.subsizer.Add(wx.StaticText(self, label = _('Group:')), flag = wx.CENTER) self.subsizer.Add(self.group, flag = wx.LEFT | wx.RIGHT | wx.CENTER, border = 5) self.subsizer.Add(self.active, flag = wx.RIGHT | wx.CENTER, border = 5) self.buttonsizer = wx.BoxSizer(wx.HORIZONTAL) self.buttonsizer.Add(wx.Panel(self), flag = wx.EXPAND, proportion = 1) self.buttonsizer.Add(self.savebutton) self.buttonsizer.Add(self.undobutton) self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.plotpanel, flag = wx.EXPAND, proportion = 3) self.sizer.Add(wx.StaticLine(self), flag = wx.EXPAND) self.sizer.Add(self.subsizer, flag = wx.TOP | wx.LEFT | wx.EXPAND, border = 5) self.sizer.Add(self.notes, flag = wx.EXPAND | wx.LEFT, proportion = 1, border = 5) self.sizer.Add(self.buttonsizer, flag = wx.EXPAND) self.SetAutoLayout(True) self.SetSizer(self.sizer) self.Layout() self.notes.Bind(wx.EVT_TEXT, self.OnModify) self.group.Bind(EVT_INT, self.OnModify) self.active.Bind(wx.EVT_CHECKBOX, self.OnModify) self.savebutton.Bind(wx.EVT_BUTTON, self.savedata) self.undobutton.Bind(wx.EVT_BUTTON, self.restoredata) def showanimal(self, animalid, fromdate, todate): self.animalid = animalid self.fromdate = fromdate self.todate = todate self.restoredata() self.plot() def plot(self): '''Force graph redraw''' self.plotpanel.animalid = self.animalid self.plotpanel.fromdate = self.fromdate self.plotpanel.todate = self.todate self.plotpanel.draw() def restoredata(self, evt = None): '''Restore/load data from database''' groupid, active = animalinfo(self.animalid) self.group.SetValue(groupid) self.active.SetValue(active) notes = animalnotes(self.animalid) self.notes.SetValue(notes) self.setmodified(False) def savedata(self, evt = None): '''Save data''' oldgroupid, oldactive = animalinfo(self.animalid) cursor = db_conn.cursor() cursor.execute("UPDATE animals SET groupid=?, active=?, notes=? " \ "WHERE animalid=?", (self.group.GetValue(), int(self.active.GetValue()), self.notes.GetValue(), self.animalid)) db_conn.commit() self.restoredata() # Show the actual data stored, should be the same if (self.group.GetValue() != oldgroupid or self.active.GetValue() != oldactive): # Tree structure changed wx.PostEvent(self, events.TreeChangeEvent()) def setmodified(self, state): '''Enable or disable save/undo buttons''' self.savebutton.Enable(state) self.undobutton.Enable(state) def OnModify(self, evt = None): self.setmodified(True) if __name__ == '__main__': import datetime class MyApp(wx.App): def OnInit(self): wx.InitAllImageHandlers() todate = datetime.date(2007,07,27) fromdate = todate - datetime.timedelta(14) frame = wx.Frame(None, -1, "AnimalView unit testing") frame.SetSize((800, 700)) d = AnimalView(frame) d.showanimal(3404, fromdate, todate) frame.Show(True) self.SetTopWindow(frame) return True app = MyApp(0) app.MainLoop()