Package Gnumed :: Package wxpython :: Module gmDermTool |
[frames] | no frames]

# Source Code for Module Gnumed.wxpython.gmDermTool

      1  #!/usr/bin/env python
      2  # -*- coding: UTF-8 -*-
      3  # generated by wxGlade 0.4 on Sat Oct 29 16:44:23 2005
      4
      5  # standard library
      6  import os.path
      7
      8  # 3rd party
      9  try:
     10          import wxversion
     11          import wx
     12          import wx.html
     13  except ImportError:
     14          from wxPython import wx
     15
     16  # GNUmed modules
     17  from Gnumed.pycommon import gmI18N
     18
     19  #===================================================================
     20 -class DermToolDialog(wx.Dialog):
     21 -    def __init__(self, *args, **kwds):
     22          # begin wxGlade: DermToolDialog.__init__
     23          kwds["style"] = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.THICK_FRAME
     24          wx.Dialog.__init__(self, *args, **kwds)
     25          self.population = wx.RadioBox(self, -1, _("Population"), choices=[_("Child"), _("Adult"), _("Elderly")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     26          self.distribution = wx.RadioBox(self, -1, _("Distribution"), choices=[_("Truncal"), _("Generalised"), _("Face"), _("Hands"), _("Flexural"), _("Sun-exposed")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     27          self.colouring = wx.RadioBox(self, -1, _("Colouring"), choices=[_("Normal"), _("Pigmented"), _("Depigmented"), _("Erythema"), _("Purpura")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     28          self.pattern = wx.RadioBox(self, -1, _("Pattern"), choices=[_("Confluent"), _("Macular"),  _("Maculo-Papular"), _("Papular"), _("Bullous")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     29          self.surface = wx.RadioBox(self, -1, _("Surface"), choices=[_("Skin level"), _("Eroded"), _("Crust"), _("Scale"), _("Exudate")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     30          self.sensation = wx.RadioBox(self, -1, _("Sensation"), choices=[_("Normal"), _("Itchy"), _("Painful")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     31          self.time_course = wx.RadioBox(self, -1, _("Occurrence"), choices=[_("Evolving"), _("Sudden")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
     32          self.disease_name = wx.TextCtrl(self, -1, "")
     33          self.disease_list = wx.ListBox(self, -1, choices=[])
     34          self.image = wx.Panel(self, -1)
     35          self.image_credit = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY)
     36          self.disease_text = wx.html.HtmlWindow(self, -1)
     37          self.btn_save = wx.Button(self, -1, _("Save"))
     38          self.btn_close = wx.Button(self, -1, _("Close"))
     39
     40          self.__set_properties()
     41          self.__do_layout()
     42
     43          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.population)
     44          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.distribution)
     45          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.colouring)
     46          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.pattern)
     47          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.surface)
     48          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.sensation)
     49          self.Bind(wx.EVT_RADIOBOX, self.search_type, self.time_course)
     50          self.Bind(wx.EVT_TEXT, self.search_text, self.disease_name)
     51          self.Bind(wx.EVT_LISTBOX, self.open_disease, self.disease_list)
     52          self.Bind(wx.EVT_BUTTON, self.save, self.btn_save)
     53          self.Bind(wx.EVT_BUTTON, self.close, self.btn_close)
     54          # end wxGlade
     55          self.Bind(wx.EVT_CLOSE, self.close, self)
     56          self.Bind(wx.EVT_SIZE, self.bind_image, self.image)
     57          self.diseases = []
     58          self.path = os.path.join (self.gb['resource dir'], 'data', 'derm')
     59          for l in file (os.path.join (self.path, 'INDEX')).readlines ():
     60              row = l.split ()
     61              d = {}
     62              lang = gmI18N.system_locale_level['language']
     63              n = 0
     64              for i in ['basename', 'code', 'population', 'distribution', 'colouring', 'pattern', 'surface', 'sensation','time_course']:
     65                  d[i] = row[n]
     66                  n += 1
     67              try:
     68                  txt = file (os.path.join (self.gb['resource dir'], 'data', 'derm', '%s.%s.txt' % (row[0], lang)))
     69              except IOError:
     70                  txt = file (os.path.join (self.gb['resource dir'], 'data', 'derm', '%s.en.txt' % row[0]))
     71                          # FIXME: should not this be done by Cheetah ?
     72              d['title'] =  txt.readline ()
     73              d['source'], d['text'] = tuple (txt.read ().split ('\n\n', 1))
     74              d['text'] = d['text'].replace ('&', '&amp;')
     75              d['text'] = d['text'].replace ('<', '&lt;')
     76              d['text'] = d['text'].replace ('>', '&gt;')
     77              d['text'] = d['text'].replace ('\n\n', '<p>')
     78              d['source'] = d['source'].split (',')[0]
     79              self.diseases.append (d)
     80
     81
     82 -    def __set_properties(self):
     83          # begin wxGlade: DermToolDialog.__set_properties
     84          self.SetTitle("DermTool")
     85          self.population.SetSelection(1)
     86          self.distribution.SetSelection(0)
     87          self.colouring.SetSelection(0)
     88          self.pattern.SetSelection(0)
     89          self.surface.SetSelection(0)
     90          self.sensation.SetSelection(0)
     91          self.time_course.SetSelection(0)
     92          self.disease_name.SetToolTipString(_("Type name of disease"))
     93          self.btn_save.SetDefault()
     94          # end wxGlade
     95
     96 -    def __do_layout(self):
     97          # begin wxGlade: DermToolDialog.__do_layout
     98          sizer_1 = wx.BoxSizer(wx.VERTICAL)
     99          sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
    100          sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
    101          sizer_7 = wx.BoxSizer(wx.VERTICAL)
    102          sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
    103          sizer_5 = wx.BoxSizer(wx.VERTICAL)
    104          sizer_4 = wx.BoxSizer(wx.VERTICAL)
    105          sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
    106          sizer_2.Add(self.population, 1, 0, 0)
    107          sizer_2.Add(self.distribution, 1, wx.ADJUST_MINSIZE, 0)
    108          sizer_2.Add(self.colouring, 1, wx.ADJUST_MINSIZE, 0)
    109          sizer_2.Add(self.pattern, 1, wx.ADJUST_MINSIZE, 0)
    110          sizer_2.Add(self.surface, 1, wx.ADJUST_MINSIZE, 0)
    111          sizer_2.Add(self.sensation, 1, wx.ADJUST_MINSIZE, 0)
    112          sizer_2.Add(self.time_course, 1, wx.ADJUST_MINSIZE, 0)
    113          sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
    114          sizer_4.Add(self.disease_name, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    115          sizer_4.Add(self.disease_list, 1, wx.ALL|wx.EXPAND, 0)
    116          sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
    117          sizer_5.Add(self.image, 1, wx.EXPAND, 0)
    118          sizer_5.Add(self.image_credit, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
    119          sizer_3.Add(sizer_5, 3, wx.EXPAND, 0)
    120          sizer_7.Add(self.disease_text, 1, wx.EXPAND, 0)
    121          sizer_8.Add(self.btn_save, 0, wx.ADJUST_MINSIZE, 0)
    122          sizer_8.Add(self.btn_close, 0, wx.ADJUST_MINSIZE, 0)
    123          sizer_7.Add(sizer_8, 0, wx.EXPAND, 0)
    124          sizer_6.Add(sizer_7, 2, wx.EXPAND, 0)
    125          sizer_3.Add(sizer_6, 2, wx.EXPAND, 0)
    126          sizer_1.Add(sizer_3, 2, wx.EXPAND, 0)
    127          self.SetAutoLayout(True)
    128          self.SetSizer(sizer_1)
    129          sizer_1.Fit(self)
    130          sizer_1.SetSizeHints(self)
    131          self.Layout()
    132          # end wxGlade
    133
    134 -    def search_type(self, event): # wxGlade: DermToolDialog.<event_handler>
    135          self.disease_name.Clear ()
    136          maps = [('population', ['P', 'A', 'E']), ('distribution', ['T', 'G', 'F', 'H', 'L', 'S']), ('colouring', ['N', 'P', 'D', 'E', 'P']), ('pattern', ['C', 'M', 'MP', 'P', 'B']), ('surface', ['F', 'E', 'C', 'S', 'X']), ('sensation', ['N', 'I', 'P']), ('time_course', ['E', 'S'])]
    137          vals = dict ([(i,j[getattr (self, i).GetSelection ()]) for i, j in maps])
    138          for i in self.diseases:
    139              n = 0
    140              for j in vals:
    141                  if vals[j] == i[j]:
    142                      n += 1
    143              i['matches'] = n
    144          self.diseases.sort (lambda x,y: cmp (x['matches'], y['matches']))
    145          self.disease_list.Clear ()
    146          for i in self.diseases[:10]:
    147              if i['matches'] > 4:
    148                  self.disease_list.Append (i['title'], i)
    149
    150
    151 -    def open_disease(self, event): # wxGlade: DermToolDialog.<event_handler>
    152          data = self.disease_list.GetClientData (self.disease_list.GetSelection ())
    153          self.disease_text.SetPage (data['text'])
    154          self.image_credit.SetValue (data['source'])
    155          fname = os.path.join (self.path, data['basename'] + '.jpg')
    156          if os.path.exists (fname):
    157              self.jpeg = wx.Image (fname)
    158              self.bind_image ()
    159          else:
    160              print "No image available"
    161
    162 -    def bind_image (self, event = None):
    163          jpg_width = self.jpeg.GetWidth ()
    164          jpg_height = self.jpeg.GetHeight ()
    165          pnl_width,pnl_height = self.image.GetSizeTuple ()
    166          self.image.DestroyChildren ()
    167          jpg_aspect = jpg_width/float (jpg_height)
    168          pnl_aspect = pnl_width/float (pnl_height)
    169          if jpg_aspect > pnl_aspect:
    170              desired_width = pnl_width
    171              desired_height = int (pnl_width/jpg_aspect)
    172              x = 0
    173              y = (pnl_height-desired_height)/2
    174          else:
    175              desired_height = pnl_height
    176              desired_width = int (pnl_height*jpg_aspect)
    177              x = (pnl_width-desired_width)/2
    178              y = 0
    179          njpeg = self.jpeg.Scale (desired_width, desired_height)
    180          bmp = wx.BitmapFromImage (njpeg)
    181          sbmp = wx.StaticBitmap (self.image, -1, bmp, pos=wx.Point (x, y))
    182          sbmp.Show ()
    183
    184 -    def save(self, event): # wxGlade: DermToolDialog.<event_handler>
    185          print "Event handler `save' not implemented"
    186          event.Skip()
    187
    188 -    def close(self, event): # wxGlade: DermToolDialog.<event_handler>
    189          self.Destroy ()
    190
    191 -    def search_text(self, event): # wxGlade: DermToolDialog.<event_handler>
    192          self.disease_list.Clear ()
    193          s = self.disease_name.GetValue ().upper ()
    194          if len (s) > 3:
    195              for i in self.diseases:
    196                  if s in i['title'].upper ():
    197                      self.disease_list.Append (i['title'], i)
    198
    199
    200  # end of class DermToolDialog
    201
    202
    203      #== if run as standalone =======================================================
    204  if __name__ == '__main__':
    205          # set up dummy app
    206 -        class TestApp (wx.App):
    207 -                def OnInit (self):
    208                          frame = DermToolDialog(None, -1)
    209                          frame.Show(True)
    210                          return True
    211          #---------------------
    212          wx.InitAllImageHandlers()
    213          app = TestApp()
    214          app.MainLoop()
    215