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

# Source Code for Module Gnumed.wxpython.gmPregWidgets

      1  """GNUmed pregnancy related dates widgets.
      2  """
      3  #====================================================================
      4  __author__ = "M. Bonert, R. Terry, I. Haywood, K.Hilbert"
      5  __licence__ = "GPL v2 or later"
      6
      7
      8  import sys
      9  import datetime as pydt
     10
     11
     12  import wx
     13
     14
     15  from Gnumed.pycommon import gmDateTime
     16  from Gnumed.pycommon import gmTools
     17  from Gnumed.business import gmClinicalCalculator
     18  from Gnumed.wxpython import gmGuiHelpers
     19
     20  #====================================================================
     21 -def calculate_edc(parent=None, patient=None):
     22
     23          if parent is None:
     24                  parent = wx.GetApp().GetTopWindow()
     25
     26          dlg = cEdcCalculatorDlg(parent, -1)
     27          prev_edc = None
     28          if patient.connected:
     29                  prev_edc = patient.emr.EDC
     30                  dlg.patient = patient
     31                  dlg.EDC = prev_edc
     32          action = dlg.ShowModal()
     33          new_edc = dlg.EDC
     34          dlg.DestroyLater()
     35
     36          # cancelled ?
     37          if action != wx.ID_SAVE:
     38                  return
     39
     40          # but have we got a patient ?
     41          if not patient.connected:
     42                  return
     43
     44          # ignore setting EDC from NULL to NULL
     45          if (new_edc is None) and (prev_edc is None):
     46                  return
     47
     48          # setting from X to NULL -> is a deletion intended ?
     49          # (prev_edc MUST be not None due to the previous condition)
     50          if new_edc is None:
     51                  delete_edc = gmGuiHelpers.gm_show_question (
     52                          title = _('Editing EDC'),
     53                          question = _(
     54                                  'Really delete the EDC documented previously ?\n'
     55                                  '\n'
     56                                  ' EDC: %s'
     57                          ) % gmDateTime.pydt_strftime(prev_edc, '%Y %b %d'),
     58                          cancel_button = False
     59                  )
     60                  if not delete_edc:
     61                          return
     62
     63          patient.emr.EDC = new_edc
     64
     65  #====================================================================
     66  from Gnumed.wxGladeWidgets import wxgEdcCalculatorDlg
     67
     68 -class cEdcCalculatorDlg(wxgEdcCalculatorDlg.wxgEdcCalculatorDlg):
     69
     70 -        def __init__(self, *args, **kwds):
     71                  wxgEdcCalculatorDlg.wxgEdcCalculatorDlg.__init__(self, *args, **kwds)
     72                  self.__calc = gmClinicalCalculator.cClinicalCalculator()
     73                  self.__init_ui()
     74          #----------------------------------------------------------------
     75 -        def __init_ui(self):
     76                  edc = self.__calc.get_EDC(lmp = None, nullipara = self._CHBOX_first_pregnancy.GetValue())
     77                  txt = _(
     78                          'Algorithm: %s\n'
     79                          '\n'
     80                          'Source: %s'
     81                  ) % (
     82                          edc.formula_name,
     83                          edc.formula_source
     84                  )
     85                  self._TCTRL_algo.SetValue(txt)
     86                  self._PRW_lmp.add_callback_on_selection(self._on_lmp_selected)
     87                  self._PRW_edc.add_callback_on_modified(self._on_edc_modified)
     88
     89                  self._PRW_lmp.SetFocus()
     90          #----------------------------------------------------------------
     91 -        def _on_lmp_selected(self, data):
     92                  self.__recalculate()
     93          #----------------------------------------------------------------
     94 -        def _on_edc_modified(self):
     95                  self._PRW_lmp.SetData(None)
     96                  self._TCTRL_details.SetValue('')
     97          #----------------------------------------------------------------
     98 -        def _on_first_pregnancy_toggled(self, event):
     99                  event.Skip()
    100                  self.__recalculate()
    101          #----------------------------------------------------------------
    102 -        def _on_lmp_picked_in_calendar(self, event):
    103                  event.Skip()
    104                  self._PRW_lmp.SetData(gmDateTime.wxDate2py_dt(wxDate = self._CALCTRL.Date))
    105                  self.__recalculate()
    106          #----------------------------------------------------------------
    107 -        def _on_save_button_pressed(self, event):
    108                  event.Skip()
    109                  if self.__calc.patient is None:
    110                          return False
    111                  if self._PRW_edc.is_valid_timestamp(empty_is_valid = True):
    112                          self.EndModal(wx.ID_SAVE)
    113                          return True
    114                  return False
    115          #----------------------------------------------------------------
    116 -        def __recalculate(self):
    117                  lmp = self._PRW_lmp.date
    118                  if lmp is None:
    119                          self._PRW_edc.SetData(None)
    120                          self._TCTRL_details.SetValue('')
    121                          return
    122
    123                  edc = self.__calc.get_EDC(lmp = lmp, nullipara = self._CHBOX_first_pregnancy.GetValue())
    124
    125                  self._PRW_edc.SetData(edc.numeric_value)
    126                  details = ''
    127                  now = gmDateTime.pydt_now_here()
    128                  # Beulah Hunter, 375 days (http://www.reference.com/motif/health/longest-human-pregnancy-on-record)
    129                  if (lmp < now) and (edc.numeric_value > (now + pydt.timedelta(days = 380))):
    130                          age = now - lmp
    131                          weeks, days = divmod(age.days, 7)
    132                          week = weeks
    133                          if days > 0:
    134                                  week = weeks + 1
    135                          month, tmp = divmod(age.days, 28)
    136                          if days > 0:
    137                                  month += 1
    138                          details += _(
    139                                  'Current age of pregnancy (%s):\n'
    140                                  ' day %s = %s weeks %s days = week %s = month %s\n\n'
    141                          ) % (
    142                                  gmDateTime.pydt_strftime(now, '%Y %b %d'),
    143                                  age.days,
    144                                  int(weeks),
    145                                  int(days),
    146                                  week,
    147                                  month
    148                          )
    149
    150                  details += edc.format (
    151                          left_margin = 1,
    152                          width = 50,
    153                          with_formula = False,
    154                          with_warnings = True,
    155                          with_variables = True,
    156                          with_sub_results = True,
    157                          return_list = False
    158                  )
    159                  self._TCTRL_details.SetValue(details)
    160          #----------------------------------------------------------------
    161          # properties
    162          #----------------------------------------------------------------
    163 -        def _get_EDC(self):
    164                  return self._PRW_edc.date
    165
    166 -        def _set_EDC(self, edc):
    167                  self._PRW_edc.SetData(edc)
    168                  self._PRW_lmp.SetData(None)
    169                  self._TCTRL_details.SetValue('')
    170
    171          EDC = property(_get_EDC, _set_EDC)
    172          #----------------------------------------------------------------
    173 -        def _set_patient(self, patient):
    174                  self.__calc.patient = patient
    175
    176          patient = property(lambda x:x, _set_patient)
    177
    178  #====================================================================
    179  #====================================================================
    180  # old code
    181  #====================================================================
    182  #====================================================================
    183  #====================================================================
    184  import math, zlib, random, string, os.path
    185
    186
    187  import wx.lib.rcsizer
    188
    189  """
    190  Calculates from LMP:
    191   - EDC
    192   - 18th week ultrasound scan
    193
    194  Naegele's rule is easy for manual calculation, but a pain to code
    195  Enter Haywood's rule ;-), human gestation is defined as 24192000 seconds.
    196  (Ian, can you please explain a bit more ?)
    197
    198  TODO:
    199  ideally, tool should query backend for parity, race, etc. for exact measurement
    200  """
    201
    202
    203  LMP_FIELD = 0
    204  US_FIELD = 1
    205
    206  ID_LMP = wx.NewId()
    207  ID_DUE = wx.NewId()
    208  ID_DAY = wx.NewId()
    209  ID_WEEK = wx.NewId()
    210  ID_MENU = wx.NewId()
    211
    212  GESTATION = 24192000
    213  WEEK = 604800
    214  DAY = 86400
    215  US18_52 = 10886400      # 18 weeks in seconds (for 18/52 Ultrasound)
    216
    217  #====================================================================
    218 -class cPregCalcFrame (wx.Frame):
    219          """
    220          The new pregnancy calculator.
    221          """
    222
    223          #TODO
    224          # IMPROVE removal of time from txt_lmp, txtedc, txtdue (?)
    225          #       see: def PurgeTime(self, date):
    226          #
    227          # explore idea of 'status bar' across bottom -- something to make
    228          #       clear how to use the calculator
    229          #
    230          # change the set-up of the RowColSizer(), shrink the size of the 'Weeks' & 'Days' fields
    231          #       make column on right side of preg. calc. more compact
    232          #
    233          # clean-up the names of the variables (some could be named more descriptively)
    234          #
    235          # add ability to type in LMP and Scan Date with keyboard (as opposed to only clicking on calendar)
    236          #       make movement between fields possible with 'tab' & 'enter'
    237
    238 -        def __init__ (self, parent):
    239                  myStyle = wx.MINIMIZE_BOX | wx.CAPTION | wx.ALIGN_CENTER | \
    240                          wx.ALIGN_CENTER_VERTICAL | wx.TAB_TRAVERSAL | wx.STAY_ON_TOP
    241                  wx.Frame.__init__(self, parent, -1, _("Pregnancy Calculator"), style=myStyle)
    242
    243                  # initialization of variables used in the control & calculation
    244                  self.xfer_cal_date_to=LMP_FIELD # controls which variable (LMP or Ultrasound) a calendar event changes
    245                                          # (if == 0): {calendar selection modifies LMP}
    246                                          # (if == 1): {calendar selection modifies Ultrasound Date}
    247
    248                  self.ustxt=wx.DateTime_Today()  # avoids problem - one would have if the user clicked on
    249                                                  # ultrasound date
    250                                                  # BONUS: makes the preg. calculator useful for EDC calcs given
    251                                                  #       a date and the gestation time
    252
    253                  # get icon
    254                  if __name__ == '__main__':
    255                          png_fname = os.path.join('..', 'bitmaps', 'preg_calculator.png')
    256                  else:
    257                          from Gnumed.pycommon import gmGuiBroker
    258                          gb = gmGuiBroker.GuiBroker()
    259                          png_fname = os.path.join(gb['gnumed_dir'], 'bitmaps', 'preg_calculator.png')
    260                  icon = wx.Icon()
    261                  icon.LoadFile(png_fname, wx.BITMAP_TYPE_PNG)
    262                  self.SetIcon(icon)
    263
    264                  szr_rc = wx.lib.rcsizer.RowColSizer()
    265
    266                  #------------------------------
    267                  # sizer holding the 'LMP' stuff
    268                  #------------------------------
    269                  label = wx.StaticText(self,-1,_("LMP"),size = (50,20))  #label Lmp
    270                  label.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    271                  label.SetForegroundColour(wx.Colour(0,0,0))
    272
    273                  self.txt_lmp = wx.TextCtrl(self,-1,"",size=(100,20))    # text for lmp
    274                  self.txt_lmp.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    275                  self.txt_lmp.SetToolTip(wx.ToolTip(_("Click on calendar to enter the last menstrual period date")))
    276                  tiplmp=self.txt_lmp.GetToolTip()
    277
    278                  szr_row1 = wx.BoxSizer(wx.HORIZONTAL)
    279                  szr_row1.Add(self.txt_lmp,1,wx.EXPAND|wx.ALL,2)
    280                  wx.EVT_SET_FOCUS(self.txt_lmp, self.OnSetFocus_lmp)
    281
    282                  szr_lmp = wx.BoxSizer(wx.HORIZONTAL)
    283                  szr_lmp.Add(label, 1, 0, 0)
    284                  szr_lmp.Add((10,1),0,0)
    285                  szr_rc.Add(szr_lmp, flag=wx.EXPAND, row=0, col=1)
    286                  szr_rc.Add(szr_row1, flag=wx.EXPAND, row=0, col=2, colspan=5)
    287                  #------------------------------
    288                  # sizer holding the 'Gest.' stuff
    289                  #------------------------------
    290                  label = wx.StaticText(self,-1,_("Gest."),size = (50,20))
    291                  label.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    292                  label.SetForegroundColour(wx.Colour(0,0,0))
    293
    294                  self.txtgest = wx.TextCtrl(self,-1,"",size=(100,20))
    295                  self.txtgest.Enable(False)
    296                  self.txtgest.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    297                  self.txtgest_szr  = wx.BoxSizer(wx.HORIZONTAL)
    298                  self.txtgest_szr.Add(self.txtgest,1,wx.EXPAND|wx.ALL,2)
    299
    300                  szr_gest = wx.BoxSizer(wx.HORIZONTAL)
    301                  szr_gest.Add(label, 1, 0, 0)
    302                  szr_gest.Add((10,1),0,0)
    303                  szr_rc.Add(szr_gest, flag=wx.EXPAND, row=1, col=1)
    304                  szr_rc.Add(self.txtgest_szr, flag=wx.EXPAND, row=1, col=2, colspan=5)
    305
    306                  #------------------------------
    307                  # sizer holding the 'EDC' stuff
    308                  #------------------------------
    309                  label = wx.StaticText(self,-1,_("EDC"),size = (50,20))
    310                  label.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    311                  label.SetForegroundColour(wx.Colour(0,0,0))
    312
    313                  self.txtedc = wx.TextCtrl(self,-1,"",size=(100,20))
    314                  self.txtedc.Enable(False)
    315                  self.txtedc.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    316                  szr_txtedc = wx.BoxSizer(wx.HORIZONTAL)
    317                  szr_txtedc.Add(self.txtedc,1,wx.EXPAND|wx.ALL,2)
    318                  szr_edc = wx.BoxSizer(wx.HORIZONTAL)
    319                  szr_edc.Add(label,1,0,0)
    320                  szr_edc.Add((10,1),0,0)
    321                  szr_rc.Add(szr_edc, flag=wx.EXPAND, row=2, col=1)
    322                  szr_rc.Add(szr_txtedc, flag=wx.EXPAND, row=2, col=2, colspan=5)
    323
    324                  #------------------------------
    325                  # "Ultrasound Scan" label
    326                  #------------------------------
    327                  us_label = wx.StaticText(self,-1,_("18 Week Ultrasound Scan"),size = (200,20))
    328                  us_label.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,False,''))
    329                  us_label.SetForegroundColour(wx.Colour(50,50,204))
    330                  szr_backgrnd_18WkScanDue = wx.BoxSizer(wx.VERTICAL)
    331                  szr_backgrnd_18WkScanDue.Add((1,3), 0)
    332                  szr_backgrnd_18WkScanDue.Add(us_label,1,wx.EXPAND,1)
    333                  szr_rc.Add(szr_backgrnd_18WkScanDue, flag=wx.ALIGN_CENTRE_HORIZONTAL, row=3, col=2, colspan=5)
    334                  #------------------------------
    335                  # sizer holding the 'Due' stuff
    336                  #------------------------------
    337                  label = wx.StaticText(self,-1,_("Due"),size = (100,20))
    338                  label.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    339                  label.SetForegroundColour(wx.Colour(0,0,0))
    340
    341                  self.txtdue = wx.TextCtrl(self,-1,"",size=(100,20))
    342                  self.txtdue.Enable(False)
    343                  self.txtdue.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    344                  self.szr_txtdue  = wx.BoxSizer(wx.HORIZONTAL)
    345                  self.szr_txtdue.Add(self.txtdue,1,wx.EXPAND|wx.ALL,2)
    346                  szr_due = wx.BoxSizer(wx.HORIZONTAL)
    347                  szr_due.Add(label,1,0,0)
    348                  szr_due.Add((10,1),0,0)
    349                  szr_rc.Add(szr_due, flag=wx.EXPAND, row=4, col=1)
    350                  szr_rc.Add(self.szr_txtdue, flag=wx.EXPAND, row=4, col=2, colspan=5)
    351
    352                  #------------------------------
    353                  # "Ultrasound Scan - Revised EDC" label
    354                  #------------------------------
    355                  rev_edc_label = wx.StaticText(self,-1,_("Ultrasound Scan - Revised EDC"),size = (300,20))
    356                  rev_edc_label.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,False,''))
    357                  rev_edc_label.SetForegroundColour(wx.Colour(50,50,204))
    358                  szr_backgrnd_RevEDCLabel = wx.BoxSizer(wx.VERTICAL)
    359                  szr_backgrnd_RevEDCLabel.Add((1,3), 0)
    360                  szr_backgrnd_RevEDCLabel.Add(rev_edc_label,1,wx.EXPAND,1)
    361                  szr_rc.Add(szr_backgrnd_RevEDCLabel, flag=wx.ALIGN_CENTRE_HORIZONTAL, row=5, col=2, colspan=5)
    362
    363                  #------------------------------
    364                  # sizer holding the 'newedc' stuff
    365                  #------------------------------
    366                  label1 = wx.StaticText(self,-1,_("Scan Date"),size = (25,20))
    367                  label1.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    368                  label1.SetForegroundColour(wx.Colour(0,0,0))
    369                  self.txtdate = wx.TextCtrl(self,-1,"",size=(25,20))
    370                  self.txtdate.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    371                  self.txtdate.SetToolTip(wx.ToolTip(_("Click on this field and then the ultrasound scan date on the calendar")))
    372                  tipdue=self.txtdate.GetToolTip()
    373                  wx.ToolTip_Enable(1)
    374                  self.szr_txtdate  = wx.BoxSizer(wx.HORIZONTAL)
    375                  self.szr_txtdate.Add(self.txtdate,1,wx.EXPAND|wx.ALL,2)
    376                  wx.EVT_SET_FOCUS(self.txtdate, self.OnSetFocus_USDate)
    377
    378                  szr_label1 = wx.BoxSizer(wx.HORIZONTAL)
    379                  szr_label1.Add(label1,1,0,0)
    380                  szr_label1.Add((10,1),0,0)
    381                  szr_rc.Add(szr_label1, flag=wx.EXPAND, row=6, col=1)
    382                  szr_rc.Add(self.szr_txtdate, flag=wx.EXPAND, row=6, col=2, colspan=5)
    383
    384                  #------------------------------
    385
    386                  label2 = wx.StaticText(self,-1,_("Weeks"),size = (25,20))
    387                  label2.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    388                  label2.SetForegroundColour(wx.Colour(0,0,0))
    389                  self.txtweeks = wx.SpinCtrl (self, -1, value = "0", min = 0, max = 42)
    390                  wx.EVT_SPINCTRL (self.txtweeks ,self.txtweeks.GetId(), self.EvtText_calcnewedc)
    391                  self.txtweeks.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    392                  self.szr_txtweeks  = wx.BoxSizer(wx.HORIZONTAL)
    393                  self.szr_txtweeks.Add(self.txtweeks,1,wx.EXPAND|wx.ALL,2)
    394
    395                  label3 = wx.StaticText(self,-1,_("Days"),size = (25,20))
    396                  label3.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    397                  label3.SetForegroundColour(wx.Colour(0,0,0))
    398                  self.txtdays = wx.SpinCtrl (self, -1, value = "0", min = 0, max = 6)
    399                  wx.EVT_SPINCTRL (self.txtdays ,self.txtdays.GetId(), self.EvtText_calcnewedc)
    400                  self.txtdays.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    401                  self.szr_txtdays  = wx.BoxSizer(wx.HORIZONTAL)
    402                  self.szr_txtdays.Add(self.txtdays,1,wx.EXPAND|wx.ALL,2)
    403
    404                  szr_label2 = wx.BoxSizer(wx.HORIZONTAL)
    405                  szr_label2.Add(label2,1,wx.ALIGN_CENTRE_VERTICAL,0)
    406                  szr_label2.Add((10,1),0,0)
    407                  szr_label3 = wx.BoxSizer(wx.HORIZONTAL)
    408                  szr_label3.Add((10,1),0,0)
    409                  szr_label3.Add(label3,1,wx.ALIGN_CENTRE_VERTICAL,0)
    410                  szr_label3.Add((10,1),0,0)
    411                  szr_rc.Add(szr_label2, flag=wx.EXPAND, row=7, col=1)
    412                  szr_rc.Add(self.szr_txtweeks, flag=wx.EXPAND, row=7, col=2, colspan=2)
    413                  szr_rc.Add(szr_label3, flag=wx.EXPAND, row=7, col=4)
    414                  szr_rc.Add(self.szr_txtdays, flag=wx.EXPAND, row=7, col=5, colspan=2)
    415
    416                  #------------------------------
    417                  # sizer holding the new (or revised) 'EDC' stuff
    418                  #------------------------------
    419                  label = wx.StaticText(self,-1,_("Rev EDC"),size = (100,20))
    420                  label.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    421                  label.SetForegroundColour(wx.Colour(0,0,0))
    422
    423                  self.txtnewedc = wx.TextCtrl(self,-1,"",size=(100,20))
    424                  self.txtnewedc.Enable(False)
    425                  self.txtnewedc.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.NORMAL,False,''))
    426                  self.szr_txtnewedc  = wx.BoxSizer(wx.HORIZONTAL)
    427                  self.szr_txtnewedc.Add(self.txtnewedc,1,wx.EXPAND|wx.ALL,2)
    428                  szr_label=wx.BoxSizer(wx.HORIZONTAL)
    429                  szr_label.Add(label,1,0,0)
    430                  szr_label.Add((10,1),0,0)
    431                  szr_rc.Add(szr_label, flag=wx.EXPAND, row=8, col=1)
    432                  szr_rc.Add(self.szr_txtnewedc, flag=wx.EXPAND, row=8, col=2, colspan=5)
    433                  self.btnPrint = wx.Button(self,1011,_('&Print'))
    434                  self.btnSave = wx.Button(self,1011,_('&Save'))
    435                  szr_buttons = wx.BoxSizer(wx.HORIZONTAL)
    436                  szr_buttons.Add(self.btnPrint,0,wx.EXPAND)
    437                  szr_buttons.Add(self.btnSave,0,wx.EXPAND)
    438                  szr_rc.Add(szr_buttons, flag=wx.EXPAND,row=9, col=3, colspan=4)
    439                  #------------------------------
    440                  # Sizer holding stuff on the right
    441                  #------------------------------
    442                  szr_main_rt = wx.BoxSizer(wx.VERTICAL)
    443                  szr_main_rt.Add(szr_rc)
    444                  wx.EVT_BUTTON(self,1010,self.EvtReset)
    445                  wx.EVT_BUTTON(self,1011,self.EvtPrint)
    446                  wx.EVT_BUTTON(self,1012,self.EvtSave)
    447                  #------------------------------
    448                  # Add calendar (stuff on the left)
    449                  #------------------------------
    450                  self.lmp_cal = wx.calendar.CalendarCtrl (self, ID_LMP,style = wx.RAISED_BORDER)
    451                  wx.calendar.EVT_CALENDAR_SEL_CHANGED(self.lmp_cal, ID_LMP, self.OnCalcByLMP)
    452
    453                  szr_main_lf = wx.BoxSizer(wx.VERTICAL)
    454                  szr_main_lf.Add(self.lmp_cal,0,wx.ALIGN_CENTRE_HORIZONTAL)
    455                  btn_reset = wx.Button(self, 1010, _('&Reset'))
    456                  #szr_main_lf.Add(5,0,5)
    457                  szr_main_lf.Add(btn_reset,0,wx.EXPAND)
    458
    459                  #--------------------------------------
    460                  # Super-sizer holds all the stuff above
    461                  #--------------------------------------
    462                  szr_main_top= wx.BoxSizer(wx.HORIZONTAL)
    463                  szr_main_top.Add(szr_main_lf,0,0)
    464                  szr_main_top.Add((15,0),0,0)
    465                  szr_main_top.Add(szr_main_rt,0,0)
    466                  #szr_main_top.Add(15,1,0,0)
    467
    468                  #------------------------------
    469                  # Put everything together in one big sizer
    470                  #------------------------------
    471                  szr_main= wx.BoxSizer(wx.HORIZONTAL)
    472                  szr_main.Add(szr_main_top,1,wx.EXPAND|wx.ALL,10)
    473                  self.SetSizer (szr_main)
    474                  self.SetAutoLayout (1)
    475                  szr_main.Fit (self)
    476
    477                  wx.EVT_CLOSE (self, self.OnClose )
    478
    479          #-----------------------------------------
    480 -        def OnCalcByLMP (self, event):
    481
    482                  if(self.xfer_cal_date_to==LMP_FIELD):
    483                          # we do this the "UNIX Way" -- all dates are converted into seconds
    484                          # since midnight 1 Jan, 1970.
    485                          # .GetDate().GetTicks() returns time at 5AM.  The -18000 second
    486                          #       correction adjusts LMP to 12AM ??? NOT NEEDED
    487                          #       is it possible there is a BUG in the wxPython
    488                          #       Day Light Savings/Standard Time Calc?
    489
    490                          #LMP = self.lmp_cal.GetDate ().GetTicks () - 18000      # Standard Time Fix (?)
    491                          self.lmp = self.lmp_cal.GetDate ().GetTicks ()          # Correct for Day Light Saving Time
    492                          today = wx.DateTime_Today().GetTicks()
    493                          due = self.lmp + GESTATION
    494                          gest = today - self.lmp
    495                          self.ultrasound18_52 = self.lmp + US18_52
    496
    497                          # -----------------
    498                          # FIXME: use gmDateInput in gmDateTimeInput.py
    499                          lmp_txt = wx.DateTime()                 # FIXME? - change format of date (?)
    500                          lmp_txt.SetTimeT(self.lmp)
    501                          self.txt_lmp.SetValue(self.PurgeTime(lmp_txt))
    502
    503                          # -----------------
    504                          gest_week = gest / WEEK
    505                          gest_day = (gest % WEEK) / DAY
    506                          if(gest_day==1):
    507                                  days_label=_('day')
    508                          else:
    509                                  days_label=_('days')
    510                          if(gest_week==1):
    511                                  weeks_label=_('week')
    512                          else:
    513                                  weeks_label=_('weeks')
    514  #                       txtgest_str = "%s %s, %s %s" % (gest_week, weeks_label, gest_day, days_label)
    515                          txtgest_str=str(gest_week)+" "+weeks_label+", "+str(gest_day)+" "+days_label
    516                          self.txtgest.SetValue(txtgest_str)
    517
    518                          # -----------------
    519                          edctxt = wx.DateTime()
    520                          edctxt.SetTimeT(due)
    521                          self.txtedc.SetValue(self.PurgeTime(edctxt))
    522
    523                          # -----------------
    524                          self.ustxt = wx.DateTime()
    525                          self.ustxt.SetTimeT(self.ultrasound18_52)
    526                          self.txtdue.SetValue(self.PurgeTime(self.ustxt))
    527
    528                  else:
    529                          # set Ultrasound Date
    530                          self.usdate = self.lmp_cal.GetDate ().GetTicks ()
    531                          usdatetxt = wx.DateTime()       # FIXME? - change format of date
    532                          usdatetxt.SetTimeT(self.usdate)
    533                          self.txtdate.SetValue(self.PurgeTime(usdatetxt))
    534
    535                          # recalculate 'Rev EDC' if Ultrasound Scan Date is changed
    536                          if( self.txtnewedc.GetValue() !=""):
    537                                  self.EvtText_calcnewedc(self)
    538
    539          #-----------------------------------------
    540 -        def EvtText_calcnewedc (self, event):
    541                  try:
    542                          weeks=self.txtweeks.GetValue()
    543                          days=self.txtdays.GetValue()
    544
    545                          # get date of ultrasound
    546                          newedc=self.usdate+GESTATION-WEEK*weeks-DAY*days
    547
    548                          wx.D=wx.DateTime()
    549                          wx.D.SetTimeT(newedc)
    550                          self.txtnewedc.SetValue(self.PurgeTime(wx.D))
    551                  except:
    552                          pass    # error handling - FIXME is 'try' statement necessary (?)
    553
    554          #-----------------------------------------
    555 -        def EvtReset(self, event):
    556                  # reset variables
    557                  self.txt_lmp.SetValue("")
    558                  self.txtgest.SetValue("")
    559                  self.txtedc.SetValue("")
    560                  self.txtdue.SetValue("")
    561
    562                  self.txtdate.SetValue("")
    563                  self.ustxt=wx.DateTime_Today()
    564
    565                  self.txtweeks.SetValue(0)                       # FIXME - MAKE IT RESET TO BLANK?
    566                  self.txtdays.SetValue(0)
    567                  self.txtnewedc.SetValue("")
    568
    569                  self.xfer_cal_date_to=LMP_FIELD
    570                  self.lmp_cal.SetDate(wx.DateTime_Today())       # reset Calendar to current date
    571
    572          #-----------------------------------------
    573 -        def EvtPrint(self, event):
    574                  pass                                    # TODO
    575          #-----------------------------------------
    576 -        def EvtSave(self, event):
    577                  pass                                    # TODO
    578          #-----------------------------------------
    579          #def EvtHandout(self, event):
    580          #       pass                                    # TODO
    581          #-------------------------------------------
    582 -        def OnClose (self, event):
    583                  self.Destroy ()
    584
    585          #-------------------------------------------
    586 -        def PurgeTime(self, date):                      # a not so elegant way of removing the time
    587                  time_loc=string.find(str(date),":00:00")
    588                  date_str=str(date)
    589                  return date_str[:(time_loc-3)]
    590
    591          #-------------------------------------------
    592 -        def OnSetFocus_lmp (self, event):
    593                  self.xfer_cal_date_to=LMP_FIELD
    594                  event.Skip()                            # required so wxTextCtrl box is selected
    595
    596          #-------------------------------------------
    597 -        def OnSetFocus_USDate (self, event):
    598                  self.lmp_cal.SetDate(self.ustxt)        # flip calendar to 18/52 date
    599                  self.xfer_cal_date_to=US_FIELD
    600                  event.Skip()
    601
    602
    603
    604  #====================================================================
    605  # Main
    606  #====================================================================
    607  if __name__ == '__main__':
    608          # set up dummy app
    609 -        class TestApp (wx.App):
    610 -                def OnInit (self):
    611                          frame = cPregCalcFrame(None)
    612                          frame.Show(1)
    613                          return 1
    614          #---------------------
    615          import gettext
    616          _ = gettext.gettext
    617          gettext.textdomain ('gnumed')
    618          app = TestApp()
    619          app.MainLoop()
    620
    621  #=====================================================================
    622