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

# Source Code for Module Gnumed.wxpython.gmFamilyHistoryWidgets

      1  """GNUmed family history related widgets."""
      2  #================================================================
      3  __author__ = 'karsten.hilbert@gmx.net'
      4  __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
      5
      6  # stdlib
      7  import logging, sys
      8
      9
     10  # 3rd party
     11  import wx
     12
     13
     14  # GNUmed
     15  if __name__ == '__main__':
     16          sys.path.insert(0, '../../')
     17  from Gnumed.pycommon import gmTools
     18  from Gnumed.pycommon import gmDateTime
     19  from Gnumed.pycommon import gmMatchProvider
     20
     21  from Gnumed.business import gmPerson
     22  from Gnumed.business import gmFamilyHistory
     23
     24  from Gnumed.wxpython import gmListWidgets
     25  from Gnumed.wxpython import gmEditArea
     26  from Gnumed.wxpython import gmPhraseWheel
     27
     28
     29  _log = logging.getLogger('gm.ui')
     30
     31  #================================================================
     32 -def manage_family_history(parent=None):
     33
     34          pat = gmPerson.gmCurrentPatient()
     35          emr = pat.get_emr()
     36
     37          if parent is None:
     38                  parent = wx.GetApp().GetTopWindow()
     39
     40          #-----------------------------------------
     41          def edit(family_history=None):
     42                  return edit_family_history(parent = parent, family_history = family_history)
     43          #-----------------------------------------
     44          def delete(family_history=None):
     45                  if gmFamilyHistory.delete_family_history(pk_family_history = family_history['pk_family_history']):
     46                          return True
     47
     48                  gmDispatcher.send (
     49                          signal = u'statustext',
     50                          msg = _('Cannot delete family history item.'),
     51                          beep = True
     52                  )
     53                  return False
     54          #------------------------------------------------------------
     55          def refresh(lctrl):
     56                  fhx = emr.get_family_history()
     57                  items = [ [
     58                          f['l10n_relation'],
     59                          f['condition'],
     60                          gmTools.bool2subst(f['contributed_to_death'], _('yes'), _('no'), u'?'),
     61                          gmTools.coalesce(f['age_noted'], u''),
     62                          gmDateTime.format_interval (
     63                                  interval = f['age_of_death'],
     64                                  accuracy_wanted = gmDateTime.acc_years,
     65                                  none_string = u''
     66                          ),
     67                          gmTools.coalesce(f['name_relative'], u''),
     68                          gmTools.coalesce(f['dob_relative'], u'', function_initial = ('strftime', '%Y-%m-%d'))
     69                  ] for f in fhx ]
     70                  lctrl.set_string_items(items)
     71                  lctrl.set_data(fhx)
     72          #------------------------------------------------------------
     73          gmListWidgets.get_choices_from_list (
     74                  parent = parent,
     75                  msg = _('Family history of this patient.'),
     76                  caption = _('Showing family history.'),
     77                  columns = [ _('Relationship'), _('Condition'), _('Fatal'), _('Noted'), _('Died'), _('Name'), _('Born') ],
     78                  single_selection = True,
     79                  can_return_empty = True,
     80                  ignore_OK_button = True,
     81                  refresh_callback = refresh,
     82                  edit_callback = edit,
     83                  new_callback = edit,
     84                  delete_callback = delete
     85  #               left_extra_button=None,
     86  #               middle_extra_button=None,
     87  #               right_extra_button=None
     88          )
     89  #----------------------------------------------------------------
     90 -def edit_family_history(parent=None, family_history=None):
     91          ea = cFamilyHistoryEAPnl(parent = parent, id = -1)
     92          ea.data = family_history
     93          ea.mode = gmTools.coalesce(family_history, 'new', 'edit')
     94          dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = True)
     95          dlg.SetTitle(gmTools.coalesce(family_history, _('Adding family history'), _('Editing family history')))
     96          if dlg.ShowModal() == wx.ID_OK:
     97                  dlg.Destroy()
     98                  return True
     99          dlg.Destroy()
    100          return False
    101  #====================================================================
    102  from Gnumed.wxGladeWidgets import wxgFamilyHistoryEAPnl
    103
    104 -class cFamilyHistoryEAPnl(wxgFamilyHistoryEAPnl.wxgFamilyHistoryEAPnl, gmEditArea.cGenericEditAreaMixin):
    105
    106 -        def __init__(self, *args, **kwargs):
    107
    108                  try:
    109                          data = kwargs['family_history']
    110                          del kwargs['family_history']
    111                  except KeyError:
    112                          data = None
    113
    114                  wxgFamilyHistoryEAPnl.wxgFamilyHistoryEAPnl.__init__(self, *args, **kwargs)
    115                  gmEditArea.cGenericEditAreaMixin.__init__(self)
    116
    117                  self.mode = 'new'
    118                  self.data = data
    119                  if data is not None:
    120                          self.mode = 'edit'
    121
    122                  #self.__init_ui()
    123          #----------------------------------------------------------------
    124  #       def __init_ui(self):
    125  #               # adjust phrasewheels etc
    126          #----------------------------------------------------------------
    127          # generic Edit Area mixin API
    128          #----------------------------------------------------------------
    129 -        def _valid_for_save(self):
    130
    131                  validity = True
    132
    133                  if self._PRW_condition.GetValue().strip() == u'':
    134                          validity = False
    135                          self._PRW_condition.display_as_valid(False)
    136                  else:
    137                          self._PRW_condition.display_as_valid(True)
    138
    139                  # make sure there's a relationship string
    140                  if self._PRW_relationship.GetValue().strip() == u'':
    141                          validity = False
    142                          self._PRW_relationship.display_as_valid(False)
    143                  else:
    144                          self._PRW_relationship.display_as_valid(True)
    145
    146                  # make sure there's an episode name
    147                  if self._PRW_episode.GetValue().strip() == u'':
    148                          self._PRW_episode.SetText(_('Family History'), None)
    149                          self._PRW_episode.display_as_valid(True)
    150
    151                  return validity
    152          #----------------------------------------------------------------
    153 -        def _save_as_new(self):
    154
    155                  pat = gmPerson.gmCurrentPatient()
    156                  emr = pat.get_emr()
    157
    158                  data = emr.add_family_history (
    159                          episode = self._PRW_episode.GetData(can_create = True),
    160                          condition = self._PRW_condition.GetValue().strip(),
    161                          relation = self._PRW_relationship.GetData(can_create = True)
    162                  )
    163
    164                  data['age_noted'] = self._TCTRL_age_of_onset.GetValue().strip()
    165                  data['age_of_death'] = self._PRW_age_of_death.GetData()
    166                  data['contributed_to_death'] = self._PRW_died_of_this.GetData()
    167                  data['name_relative'] = self._TCTRL_name.GetValue().strip()
    168                  data['dob_relative'] = self._PRW_dob.GetData()
    169                  data['comment'] = self._TCTRL_comment.GetValue().strip()
    170                  data.save()
    171
    172                  data.generic_codes = [ c['data'] for c in self._PRW_codes.GetData() ]
    173
    174                  self.data = data
    175                  return True
    176          #----------------------------------------------------------------
    177 -        def _save_as_update(self):
    178
    179                  self.data['pk_episode'] = self._PRW_episode.GetData(can_create = True)
    180                  self.data['condition'] = self._PRW_condition.GetValue().strip()
    181                  self.data['pk_fhx_relation_type'] = self._PRW_relationship.GetData(can_create = True)
    182
    183                  self.data['age_noted'] = self._TCTRL_age_of_onset.GetValue().strip()
    184                  self.data['age_of_death'] = self._PRW_age_of_death.GetData()
    185                  self.data['contributed_to_death'] = self._PRW_died_of_this.GetData()
    186                  self.data['name_relative'] = self._TCTRL_name.GetValue().strip()
    187                  self.data['dob_relative'] = self._PRW_dob.GetData()
    188                  self.data['comment'] = self._TCTRL_comment.GetValue().strip()
    189
    190                  self.data.save()
    191                  self.data.generic_codes = [ c['data'] for c in self._PRW_codes.GetData() ]
    192
    193                  return True
    194          #----------------------------------------------------------------
    195 -        def _refresh_as_new(self):
    196                  self._PRW_relationship.SetText(u'', None)
    197                  self._PRW_condition.SetText(u'', None)
    198                  self._PRW_codes.SetText()
    199                  self._TCTRL_age_of_onset.SetValue(u'')
    200                  self._PRW_age_of_death.SetText(u'', None)
    201                  self._PRW_died_of_this.SetData(None)
    202                  self._PRW_episode.SetText(u'', None)
    203                  self._TCTRL_name.SetValue(u'')
    204                  self._PRW_dob.SetText(u'', None)
    205                  self._TCTRL_comment.SetValue(u'')
    206
    207                  self._PRW_relationship.SetFocus()
    208          #----------------------------------------------------------------
    209 -        def _refresh_as_new_from_existing(self):
    210                  self._refresh_as_new()
    211          #----------------------------------------------------------------
    212 -        def _refresh_from_existing(self):
    213                  self._PRW_relationship.SetText (
    214                          self.data['l10n_relation'],
    215                          self.data['pk_fhx_relation_type']
    216                  )
    217                  self._PRW_condition.SetText(self.data['condition'], None)
    218                  val, data = self._PRW_codes.generic_linked_codes2item_dict(self.data.generic_codes)
    219                  self._PRW_codes.SetText(val, data)
    220                  self._TCTRL_age_of_onset.SetValue(gmTools.coalesce(self.data['age_noted'], u''))
    221                  self._PRW_age_of_death.SetData(self.data['age_of_death'])
    222                  self._PRW_died_of_this.SetData(self.data['contributed_to_death'])
    223                  self._PRW_episode.SetText(self.data['episode'], self.data['pk_episode'])
    224                  self._TCTRL_name.SetValue(gmTools.coalesce(self.data['name_relative'], u''))
    225                  self._PRW_dob.SetData(self.data['dob_relative'])
    226                  self._TCTRL_comment.SetValue(gmTools.coalesce(self.data['comment'], u''))
    227
    228                  self._PRW_relationship.SetFocus()
    229  #================================================================
    230 -class cRelationshipTypePhraseWheel(gmPhraseWheel.cPhraseWheel):
    231
    232 -        def __init__(self, *args, **kwargs):
    233
    234                  super(cRelationshipTypePhraseWheel, self).__init__(*args, **kwargs)
    235
    236                  query = u"""
    237                          SELECT DISTINCT ON (list_label)
    238                                  pk as data,
    239                                  _(description) as field_label,
    240                                  _(description) as list_label
    241                          FROM
    242                                  clin.fhx_relation_type
    243                          WHERE
    244                                  description %(fragment_condition)s
    245                                          OR
    246                                  _(description) %(fragment_condition)s
    247                          ORDER BY list_label
    248                          LIMIT 30"""
    249
    250                  mp = gmMatchProvider.cMatchProvider_SQL2(queries = query)
    251                  mp.setThresholds(1, 2, 3)
    252                  self.matcher = mp
    253          #----------------------------------------------------------------
    254 -        def _create_data(self):
    255                  if self.GetData() is not None:
    256                          return
    257
    258                  val = self.GetValue().strip()
    259                  if val == u'':
    260                          return
    261
    262                  self.SetText (
    263                          value = val,
    264                          data = gmFamilyHistory.create_relationship_type(relationship = val)
    265                  )
    266  #================================================================
    267  # main
    268  #----------------------------------------------------------------
    269  if __name__ == '__main__':
    270
    271          if len(sys.argv) < 2:
    272                  sys.exit()
    273
    274          if sys.argv[1] != 'test':
    275                  sys.exit()
    276
    277          from Gnumed.pycommon import gmI18N
    278          gmI18N.activate_locale()
    279          gmI18N.install_domain()
    280
    281          #--------------------------------------------------------
    282  #       def test_generic_codes_prw():
    283  #               gmPG2.get_connection()
    284  #               app = wx.PyWidgetTester(size = (500, 40))
    285  #               pw = cGenericCodesPhraseWheel(app.frame, -1)
    286  ##              #pw.set_context(context = u'zip', val = u'04318')
    287  #               app.frame.Show(True)
    288  #               app.MainLoop()
    289          #--------------------------------------------------------
    290  #       test_generic_codes_prw()
    291
    292  #================================================================
    293