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

# Source Code for Module Gnumed.wxpython.gmContactWidgets

      1  """GNUmed generic contact 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
     18  from Gnumed.pycommon import gmPG2
     19  from Gnumed.pycommon import gmTools
     20  from Gnumed.pycommon import gmMatchProvider
     21  from Gnumed.pycommon import gmDispatcher
     22
     23  from Gnumed.business import gmDemographicRecord
     24
     25  from Gnumed.wxpython import gmListWidgets
     26  from Gnumed.wxpython import gmPhraseWheel
     27  from Gnumed.wxpython import gmEditArea
     28  from Gnumed.wxpython import gmGuiHelpers
     29
     30
     31  _log = logging.getLogger('gm.ui')
     32  #============================================================
     33  # communication channels related widgets
     34  #============================================================
     35 -def manage_comm_channel_types(parent=None):
     36
     37          if parent is None:
     38                  parent = wx.GetApp().GetTopWindow()
     39
     40          #------------------------------------------------------------
     41          def delete(channel=None):
     42                  return gmDemographicRecord.delete_comm_channel_type(pk_channel_type = channel['pk'])
     43          #------------------------------------------------------------
     44          def refresh(lctrl):
     45                  wx.BeginBusyCursor()
     46                  channel_types = gmDemographicRecord.get_comm_channel_types()
     47                  lctrl.set_string_items([ (ct['l10n_description'], ct['description'], ct['pk']) for ct in channel_types ])
     48                  lctrl.set_data(channel_types)
     49                  wx.EndBusyCursor()
     50          #------------------------------------------------------------
     51          msg = _('\nThis lists the communication channel types known to GNUmed.\n')
     52
     53          gmListWidgets.get_choices_from_list (
     54                  parent = parent,
     55                  msg = msg,
     56                  caption = _('Managing communication types ...'),
     57                  columns = [_('Channel'), _('System type'), '#'],
     58                  single_selection = True,
     59                  #new_callback = edit,
     60                  #edit_callback = edit,
     61                  delete_callback = delete,
     62                  refresh_callback = refresh
     63          )
     64
     65  #------------------------------------------------------------
     66 -class cCommChannelTypePhraseWheel(gmPhraseWheel.cPhraseWheel):
     67
     68 -        def __init__(self, *args, **kwargs):
     69
     70                  query = u"""
     71  SELECT
     72          data,
     73          field_label,
     74          list_label
     75  FROM (
     76          SELECT DISTINCT ON (field_label)
     77                  pk
     78                          AS data,
     79                  _(description)
     80                          AS field_label,
     81                  (_(description) || ' (' || description || ')')
     82                          AS list_label
     83          FROM dem.enum_comm_types
     84          WHERE
     85                  _(description) %(fragment_condition)s
     86                          OR
     87                  description %(fragment_condition)s
     88  ) AS ur
     89  ORDER BY
     90          ur.list_label
     91  """
     92                  mp = gmMatchProvider.cMatchProvider_SQL2(queries=query)
     93                  mp.setThresholds(1, 2, 4)
     94                  mp.word_separators = u'[ \t]+'
     95                  gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
     96                  self.matcher = mp
     97                  self.SetToolTipString(_('Select the type of communications channel.'))
     98                  self.selection_only = True
     99
    100  #================================================================
    101 -def edit_comm_channel(parent=None, comm_channel=None, channel_owner=None):
    102          if parent is None:
    103                  parent = wx.GetApp().GetTopWindow()
    104          ea = cCommChannelEditAreaPnl(parent, -1, comm_channel = comm_channel)
    105          ea.channel_owner = channel_owner
    106          dlg = gmEditArea.cGenericEditAreaDlg2(self, -1, edit_area = ea, single_entry = True)
    107          dlg.SetTitle(_('Editing communications channel'))
    108          if dlg.ShowModal() == wx.ID_OK:
    109                  return True
    110          return False
    111  #------------------------------------------------------------
    112  from Gnumed.wxGladeWidgets import wxgCommChannelEditAreaPnl
    113
    114 -class cCommChannelEditAreaPnl(wxgCommChannelEditAreaPnl.wxgCommChannelEditAreaPnl, gmEditArea.cGenericEditAreaMixin):
    115          """An edit area for editing/creating a comms channel.
    116
    117          Does NOT act on/listen to the current patient.
    118          """
    119 -        def __init__(self, *args, **kwargs):
    120                  try:
    121                          data = kwargs['comm_channel']
    122                          del kwargs['comm_channel']
    123                  except KeyError:
    124                          data = None
    125
    126                  self.channel_owner = None
    127
    128                  wxgCommChannelEditAreaPnl.wxgCommChannelEditAreaPnl.__init__(self, *args, **kwargs)
    129                  gmEditArea.cGenericEditAreaMixin.__init__(self)
    130
    131                  self.mode = 'new'
    132                  self.data = data
    133                  if data is not None:
    134                          self.mode = 'edit'
    135
    136                  #self.__init_ui()
    137          #----------------------------------------------------------------
    138          #def __init_ui(self):
    139          #----------------------------------------------------------------
    140          # generic Edit Area mixin API
    141          #----------------------------------------------------------------
    142 -        def _valid_for_save(self):
    143                  validity = True
    144
    145                  if self._TCTRL_url.GetValue().strip() == u'':
    146                          validity = False
    147                          self.display_tctrl_as_valid(tctrl = self._TCTRL_url, valid = False)
    148                          self._TCTRL_url.SetFocus()
    149                  else:
    150                          self.display_tctrl_as_valid(tctrl = self._TCTRL_url, valid = True)
    151
    152                  # do not check GetData() because comm
    153                  # types are created as needed
    154                  #if self._PRW_type.GetData() is None:
    155                  if self._PRW_type.GetValue().strip() == u'':
    156                          validity = False
    157                          self._PRW_type.display_as_valid(False)
    158                          self._PRW_type.SetFocus()
    159                  else:
    160                          self._PRW_type.display_as_valid(True)
    161
    162                  return validity
    163          #----------------------------------------------------------------
    164 -        def _save_as_new(self):
    165                  try:
    166                          data = self.channel_owner.link_comm_channel (
    167                                  comm_medium = self._PRW_type.GetValue().strip(),
    168                                  pk_channel_type = self._PRW_type.GetData(),
    169                                  url = self._TCTRL_url.GetValue().strip(),
    170                                  is_confidential = self._CHBOX_confidential.GetValue(),
    171                          )
    172                  except gmPG2.dbapi.IntegrityError:
    173                          _log.exception('error saving comm channel')
    174                          gmDispatcher.send(signal = u'statustext', msg = _('Cannot save (duplicate ?) communications channel.'), beep = True)
    175                          return False
    176
    177                  data['comment'] = self._TCTRL_comment.GetValue().strip()
    178                  data.save()
    179
    180                  self.data = data
    181                  return True
    182          #----------------------------------------------------------------
    183 -        def _save_as_update(self):
    184                  comm_type = self._PRW_type.GetValue().strip()
    185                  if comm_type != u'':
    186                          self.data['comm_type'] = comm_type
    187                  url = self._TCTRL_url.GetValue().strip()
    188                  if url != u'':
    189                          self.data['url'] = url
    190                  self.data['is_confidential'] = self._CHBOX_confidential.GetValue()
    191                  self.data['comment'] = self._TCTRL_comment.GetValue().strip()
    192
    193                  self.data.save()
    194                  return True
    195          #----------------------------------------------------------------
    196 -        def _refresh_as_new(self):
    197                  self._PRW_type.SetText(u'')
    198                  self._TCTRL_url.SetValue(u'')
    199                  self._CHBOX_confidential.SetValue(False)
    200                  self._TCTRL_comment.SetValue(u'')
    201
    202                  self._PRW_type.SetFocus()
    203          #----------------------------------------------------------------
    204 -        def _refresh_as_new_from_existing(self):
    205                  self._refresh_as_new()
    206          #----------------------------------------------------------------
    207 -        def _refresh_from_existing(self):
    208                  self._PRW_type.SetText(self.data['l10n_comm_type'])
    209                  self._TCTRL_url.SetValue(self.data['url'])
    210                  self._CHBOX_confidential.SetValue(self.data['is_confidential'])
    211                  self._TCTRL_comment.SetValue(gmTools.coalesce(self.data['comment'], u''))
    212
    213                  self._TCTRL_url.SetFocus()
    214  #------------------------------------------------------------
    215 -class cCommChannelsManagerPnl(gmListWidgets.cGenericListManagerPnl):
    216          """A list for managing a person's comm channels."""
    217 -        def __init__(self, *args, **kwargs):
    218
    219                  try:
    220                          self.__channel_owner = kwargs['identity']
    221                          del kwargs['identity']
    222                  except KeyError:
    223                          self.__channel_owner = None
    224
    225                  gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs)
    226
    227                  self.new_callback = self._add_comm
    228                  self.edit_callback = self._edit_comm
    229                  self.delete_callback = self._del_comm
    230                  self.refresh_callback = self.refresh
    231
    232                  self.__init_ui()
    233                  self.refresh()
    234          #--------------------------------------------------------
    235          # external API
    236          #--------------------------------------------------------
    237 -        def refresh(self, *args, **kwargs):
    238                  if self.__channel_owner is None:
    239                          self._LCTRL_items.set_string_items()
    240                          return
    241
    242                  comms = self.__channel_owner.get_comm_channels()
    243                  self._LCTRL_items.set_string_items (
    244                          items = [ [
    245                                  gmTools.bool2str(c['is_confidential'], u'X', u''),
    246                                  c['l10n_comm_type'],
    247                                  c['url'],
    248                                  gmTools.coalesce(c['comment'], u'')
    249                          ] for c in comms ]
    250                  )
    251                  self._LCTRL_items.set_column_widths()
    252                  self._LCTRL_items.set_data(data = comms)
    253          #--------------------------------------------------------
    254          # internal helpers
    255          #--------------------------------------------------------
    256 -        def __init_ui(self):
    257                  self._LCTRL_items.SetToolTipString(_('List of known communication channels.'))
    258                  self._LCTRL_items.set_columns(columns = [
    259                          _('confidential'),
    260                          _('Type'),
    261                          _('Value'),
    262                          _('Comment')
    263                  ])
    264          #--------------------------------------------------------
    265 -        def _add_comm(self):
    266                  ea = cCommChannelEditAreaPnl(self, -1)
    267                  ea.channel_owner = self.__channel_owner
    268                  dlg = gmEditArea.cGenericEditAreaDlg2(self, -1, edit_area = ea)
    269                  dlg.SetTitle(_('Adding new communications channel'))
    270                  if dlg.ShowModal() == wx.ID_OK:
    271                          return True
    272                  return False
    273          #--------------------------------------------------------
    274 -        def _edit_comm(self, comm_channel):
    275                  ea = cCommChannelEditAreaPnl(self, -1, comm_channel = comm_channel)
    276                  ea.channel_owner = self.__channel_owner
    277                  dlg = gmEditArea.cGenericEditAreaDlg2(self, -1, edit_area = ea, single_entry = True)
    278                  dlg.SetTitle(_('Editing communications channel'))
    279                  if dlg.ShowModal() == wx.ID_OK:
    280                          return True
    281                  return False
    282          #--------------------------------------------------------
    283 -        def _del_comm(self, comm):
    284                  go_ahead = gmGuiHelpers.gm_show_question (
    285                          _(      'Are you sure this communication channel\n'
    286                                  'can no longer be used ?'
    287                          ),
    288                          _('Removing communication channel')
    289                  )
    290                  if not go_ahead:
    291                          return False
    292                  self.__channel_owner.unlink_comm_channel(comm_channel = comm)
    293                  return True
    294          #--------------------------------------------------------
    295          # properties
    296          #--------------------------------------------------------
    297 -        def __get_channel_owner(self):
    298                  return self.__channel_owner
    299
    300 -        def __set_channel_owner(self, channel_owner):
    301                  self.__channel_owner = channel_owner
    302                  self.refresh()
    303
    304          channel_owner = property(__get_channel_owner, __set_channel_owner)
    305
    306  #================================================================
    307  # main
    308  #----------------------------------------------------------------
    309  if __name__ == '__main__':
    310
    311          if len(sys.argv) < 2:
    312                  sys.exit()
    313
    314          if sys.argv[1] != 'test':
    315                  sys.exit()
    316
    317          from Gnumed.pycommon import gmI18N
    318          gmI18N.activate_locale()
    319          gmI18N.install_domain()
    320          from Gnumed.business import gmPersonSearch
    321
    322          #--------------------------------------------------------
    323 -        def test_person_comms_pnl():
    324                  pat = gmPersonSearch.ask_for_patient()
    325                  app = wx.PyWidgetTester(size = (600, 400))
    326                  widget = cCommChannelsManagerPnl(app.frame, -1)
    327                  widget.identity = pat
    328                  app.frame.Show(True)
    329                  app.MainLoop()
    330          #--------------------------------------------------------
    331          test_person_comms_pnl()
    332
    333  #================================================================
    334