Gnumed.wxpython.gmFamilyHistoryWidgets

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.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 = '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'), '?'),
     61                          gmTools.coalesce(f['age_noted'], ''),
     62                          gmDateTime.format_interval (
     63                                  interval = f['age_of_death'],
     64                                  accuracy_wanted = gmDateTime.acc_years,
     65                                  none_string = ''
     66                          ),
     67                          gmTools.coalesce(f['name_relative'], ''),
     68                          gmTools.coalesce(f['dob_relative'], '', function4value = ('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  #----------------------------------------------------------------
     91 -def edit_family_history(parent=None, family_history=None, single_entry=True):
     92          ea = cFamilyHistoryEAPnl(parent, -1)
     93          ea.data = family_history
     94          ea.mode = gmTools.coalesce(family_history, 'new', 'edit')
     95          dlg = gmEditArea.cGenericEditAreaDlg2(parent, -1, edit_area = ea, single_entry = single_entry)
     96          dlg.SetTitle(gmTools.coalesce(family_history, _('Adding family history'), _('Editing family history')))
     97          if dlg.ShowModal() == wx.ID_OK:
     98                  dlg.DestroyLater()
     99                  return True
    100          dlg.DestroyLater()
    101          return False
    102
    103  #====================================================================
    104  from Gnumed.wxGladeWidgets import wxgFamilyHistoryEAPnl
    105
    106 -class cFamilyHistoryEAPnl(wxgFamilyHistoryEAPnl.wxgFamilyHistoryEAPnl, gmEditArea.cGenericEditAreaMixin):
    107
    108 -        def __init__(self, *args, **kwargs):
    109
    110                  try:
    111                          data = kwargs['family_history']
    112                          del kwargs['family_history']
    113                  except KeyError:
    114                          data = None
    115
    116                  wxgFamilyHistoryEAPnl.wxgFamilyHistoryEAPnl.__init__(self, *args, **kwargs)
    117                  gmEditArea.cGenericEditAreaMixin.__init__(self)
    118
    119                  self.mode = 'new'
    120                  self.data = data
    121                  if data is not None:
    122                          self.mode = 'edit'
    123
    124                  #self.__init_ui()
    125          #----------------------------------------------------------------
    126  #       def __init_ui(self):
    127  #               # adjust phrasewheels etc
    128          #----------------------------------------------------------------
    129          # generic Edit Area mixin API
    130          #----------------------------------------------------------------
    131 -        def _valid_for_save(self):
    132
    133                  validity = True
    134
    135                  if self._PRW_condition.GetValue().strip() == '':
    136                          validity = False
    137                          self._PRW_condition.display_as_valid(False)
    138                  else:
    139                          self._PRW_condition.display_as_valid(True)
    140
    141                  # make sure there's a relationship string
    142                  if self._PRW_relationship.GetValue().strip() == '':
    143                          validity = False
    144                          self._PRW_relationship.display_as_valid(False)
    145                  else:
    146                          self._PRW_relationship.display_as_valid(True)
    147
    148                  # make sure there's an episode name
    149                  if self._PRW_episode.GetValue().strip() == '':
    150                          self._PRW_episode.SetText(_('Family History'), None)
    151                          self._PRW_episode.display_as_valid(True)
    152
    153                  return validity
    154          #----------------------------------------------------------------
    155 -        def _save_as_new(self):
    156
    157                  pat = gmPerson.gmCurrentPatient()
    158                  emr = pat.emr
    159
    160                  data = emr.add_family_history (
    161                          episode = self._PRW_episode.GetData(can_create = True),
    162                          condition = self._PRW_condition.GetValue().strip(),
    163                          relation = self._PRW_relationship.GetData(can_create = True)
    164                  )
    165
    166                  data['age_noted'] = self._TCTRL_age_of_onset.GetValue().strip()
    167                  data['age_of_death'] = self._PRW_age_of_death.GetData()
    168                  data['contributed_to_death'] = self._PRW_died_of_this.GetData()
    169                  data['name_relative'] = self._TCTRL_name.GetValue().strip()
    170                  data['dob_relative'] = self._PRW_dob.GetData()
    171                  data['comment'] = self._TCTRL_comment.GetValue().strip()
    172                  data.save()
    173
    174                  data.generic_codes = [ c['data'] for c in self._PRW_codes.GetData() ]
    175
    176                  self.data = data
    177                  return True
    178          #----------------------------------------------------------------
    179 -        def _save_as_update(self):
    180
    181                  self.data['pk_episode'] = self._PRW_episode.GetData(can_create = True)
    182                  self.data['condition'] = self._PRW_condition.GetValue().strip()
    183                  self.data['pk_fhx_relation_type'] = self._PRW_relationship.GetData(can_create = True)
    184
    185                  self.data['age_noted'] = self._TCTRL_age_of_onset.GetValue().strip()
    186                  self.data['age_of_death'] = self._PRW_age_of_death.GetData()
    187                  self.data['contributed_to_death'] = self._PRW_died_of_this.GetData()
    188                  self.data['name_relative'] = self._TCTRL_name.GetValue().strip()
    189                  self.data['dob_relative'] = self._PRW_dob.GetData()
    190                  self.data['comment'] = self._TCTRL_comment.GetValue().strip()
    191
    192                  self.data.save()
    193                  self.data.generic_codes = [ c['data'] for c in self._PRW_codes.GetData() ]
    194
    195                  return True
    196          #----------------------------------------------------------------
    197 -        def _refresh_as_new(self):
    198                  self._PRW_relationship.SetText('', None)
    199                  self._PRW_condition.SetText('', None)
    200                  self._PRW_codes.SetText()
    201                  self._TCTRL_age_of_onset.SetValue('')
    202                  self._PRW_age_of_death.SetText('', None)
    203                  self._PRW_died_of_this.SetData(None)
    204                  self._PRW_episode.SetText('', None)
    205                  self._TCTRL_name.SetValue('')
    206                  self._PRW_dob.SetText('', None)
    207                  self._TCTRL_comment.SetValue('')
    208
    209                  self._PRW_relationship.SetFocus()
    210          #----------------------------------------------------------------
    211 -        def _refresh_as_new_from_existing(self):
    212                  self._refresh_as_new()
    213          #----------------------------------------------------------------
    214 -        def _refresh_from_existing(self):
    215                  self._PRW_relationship.SetText (
    216                          self.data['l10n_relation'],
    217                          self.data['pk_fhx_relation_type']
    218                  )
    219                  self._PRW_condition.SetText(self.data['condition'], None)
    220                  val, data = self._PRW_codes.generic_linked_codes2item_dict(self.data.generic_codes)
    221                  self._PRW_codes.SetText(val, data)
    222                  self._TCTRL_age_of_onset.SetValue(gmTools.coalesce(self.data['age_noted'], ''))
    223                  self._PRW_age_of_death.SetData(self.data['age_of_death'])
    224                  self._PRW_died_of_this.SetData(self.data['contributed_to_death'])
    225                  self._PRW_episode.SetText(self.data['episode'], self.data['pk_episode'])
    226                  self._TCTRL_name.SetValue(gmTools.coalesce(self.data['name_relative'], ''))
    227                  self._PRW_dob.SetData(self.data['dob_relative'])
    228                  self._TCTRL_comment.SetValue(gmTools.coalesce(self.data['comment'], ''))
    229
    230                  self._PRW_relationship.SetFocus()
    231  #================================================================
    232 -class cRelationshipTypePhraseWheel(gmPhraseWheel.cPhraseWheel):
    233
    234 -        def __init__(self, *args, **kwargs):
    235
    236                  super(cRelationshipTypePhraseWheel, self).__init__(*args, **kwargs)
    237
    238                  query = """
    239                          SELECT DISTINCT ON (list_label)
    240                                  pk as data,
    241                                  _(description) as field_label,
    242                                  _(description) as list_label
    243                          FROM
    244                                  clin.fhx_relation_type
    245                          WHERE
    246                                  description %(fragment_condition)s
    247                                          OR
    248                                  _(description) %(fragment_condition)s
    249                          ORDER BY list_label
    250                          LIMIT 30"""
    251
    252                  mp = gmMatchProvider.cMatchProvider_SQL2(queries = query)
    253                  mp.setThresholds(1, 2, 3)
    254                  self.matcher = mp
    255          #----------------------------------------------------------------
    256 -        def _create_data(self):
    257                  if self.GetData() is not None:
    258                          return
    259
    260                  val = self.GetValue().strip()
    261                  if val == '':
    262                          return
    263
    264                  self.SetText (
    265                          value = val,
    266                          data = gmFamilyHistory.create_relationship_type(relationship = val)
    267                  )
    268  #================================================================
    269  # main
    270  #----------------------------------------------------------------
    271  if __name__ == '__main__':
    272
    273          if len(sys.argv) < 2:
    274                  sys.exit()
    275
    276          if sys.argv[1] != 'test':
    277                  sys.exit()
    278
    279          from Gnumed.pycommon import gmI18N
    280          gmI18N.activate_locale()
    281          gmI18N.install_domain()
    282
    283          #--------------------------------------------------------
    284  #       def test_generic_codes_prw():
    285  #               gmPG2.get_connection()
    286  #               app = wx.PyWidgetTester(size = (500, 40))
    287  #               pw = cGenericCodesPhraseWheel(app.frame, -1)
    288  ##              #pw.set_context(context = u'zip', val = u'04318')
    289  #               app.frame.Show(True)
    290  #               app.MainLoop()
    291          #--------------------------------------------------------
    292  #       test_generic_codes_prw()
    293
    294  #================================================================
    295