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