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

# Source Code for Module Gnumed.wxpython.gmPersonContactWidgets

      1  """Widgets dealing with address/contact information."""
      2  #============================================================
      3  __author__ = "R.Terry, SJ Tan, I Haywood, Carlos Moro <cfmoro1976@yahoo.es>"
      4  __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
      5
      6  # standard library
      7  import sys
      8  import logging
      9
     10
     11  import wx
     12
     13
     14  # GNUmed specific
     15  if __name__ == '__main__':
     16          sys.path.insert(0, '../../')
     17  from Gnumed.pycommon import gmTools
     18  from Gnumed.pycommon import gmNetworkTools
     19
     20  from Gnumed.business import gmPraxis
     21
     22  from Gnumed.wxpython import gmGuiHelpers
     23  from Gnumed.wxpython import gmListWidgets
     24  from Gnumed.wxpython import gmEditArea
     25  from Gnumed.wxpython import gmAddressWidgets
     26
     27
     28  # constant defs
     29  _log = logging.getLogger('gm.ui')
     30
     31  #============================================================
     32 -def select_address(missing=None, person=None):
     33
     34          #--------------------------
     35          def calculate_tooltip(adr):
     36                  return '\n'.join(adr.format())
     37          #--------------------------
     38          addresses = person.get_addresses()
     39          if len(addresses) == 0:
     40                  return None
     41
     42          msg = _(
     43                  'There is no [%s] address registered with this patient.\n\n'
     44                  'Please select the address you would like to use instead:'
     45          ) % missing
     46          choices = [
     47                  [
     48                          a['l10n_address_type'],
     49                          '%s %s%s, %s %s, %s' % (
     50                                  a['street'],
     51                                  a['number'],
     52                                  gmTools.coalesce(a['subunit'], '', '/%s'),
     53                                  a['postcode'],
     54                                  a['urb'],
     55                                  a['l10n_country']
     56                          )
     57                  ]
     58          for a in addresses ]
     59
     60          return gmListWidgets.get_choices_from_list (
     61                  msg = msg,
     62                  caption = _('Selecting address by type'),
     63                  columns = [_('Type'), _('Address')],
     64                  choices = choices,
     65                  data = addresses,
     66                  single_selection = True,
     67                  list_tooltip_callback = calculate_tooltip
     68          )
     69
     70  #============================================================
     71 -class cPersonAddressesManagerPnl(gmListWidgets.cGenericListManagerPnl):
     72          """A list for managing a person's addresses.
     73
     74          Does NOT act on/listen to the current patient.
     75          """
     76 -        def __init__(self, *args, **kwargs):
     77
     78                  try:
     79                          self.__identity = kwargs['identity']
     80                          del kwargs['identity']
     81                  except KeyError:
     82                          self.__identity = None
     83
     84                  gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs)
     85
     86                  self.refresh_callback = self.refresh
     87                  self.new_callback = self._add_address
     88                  self.edit_callback = self._edit_address
     89                  self.delete_callback = self._del_address
     90
     91                  self.__init_ui()
     92                  self.refresh()
     93          #--------------------------------------------------------
     94          # external API
     95          #--------------------------------------------------------
     96 -        def refresh(self, *args, **kwargs):
     97                  if self.__identity is None:
     98                          self._LCTRL_items.set_string_items()
     99                          return
    100
    101                  adrs = self.__identity.get_addresses()
    102                  self._LCTRL_items.set_string_items (
    103                          items = [ [
    104                                          a['l10n_address_type'],
    105                                          a['street'],
    106                                          gmTools.coalesce(a['notes_street'], ''),
    107                                          a['number'],
    108                                          gmTools.coalesce(a['subunit'], ''),
    109                                          a['postcode'],
    110                                          a['urb'],
    111                                          gmTools.coalesce(a['suburb'], ''),
    112                                          a['l10n_region'],
    113                                          a['l10n_country'],
    114                                          gmTools.coalesce(a['notes_subunit'], '')
    115                                  ] for a in adrs
    116                          ]
    117                  )
    118                  self._LCTRL_items.set_column_widths()
    119                  self._LCTRL_items.set_data(data = adrs)
    120          #--------------------------------------------------------
    121          # internal helpers
    122          #--------------------------------------------------------
    123 -        def __init_ui(self):
    124                  self.__static_tooltip_part = _('List of addresses related to this person.')
    125                  self._LCTRL_items.item_tooltip_callback = self._calculate_tooltip
    126                  self._LCTRL_items.set_columns(columns = [
    127                          _('Type'),
    128                          _('Street'),
    129                          _('Street info'),
    130                          _('Number'),
    131                          _('Subunit'),
    132                          _('Postal code'),
    133                          _('Community'),
    134                          _('Suburb'),
    135                          _('Region'),
    136                          _('Country'),
    137                          _('Comment')
    138                  ])
    139
    140                  self.left_extra_button = (
    141                          _('Map'),
    142                          _('Show selected address on map'),
    143                          self._show_address_on_map
    144                  )
    145                  self.middle_extra_button = (
    146                          _('Distance'),
    147                          _('Show distance from your praxis'),
    148                          self._show_distance_on_map
    149                  )
    150
    151          #--------------------------------------------------------
    152 -        def _add_address(self):
    153                  ea = gmAddressWidgets.cAddressEAPnl(self, -1)
    154                  ea.address_holder = self.__identity
    155                  dlg = gmEditArea.cGenericEditAreaDlg2(self, -1, edit_area = ea)
    156                  dlg.SetTitle(_('Adding new address'))
    157                  if dlg.ShowModal() == wx.ID_OK:
    158                          return True
    159                  return False
    160          #--------------------------------------------------------
    161 -        def _edit_address(self, address):
    162                  ea = gmAddressWidgets.cAddressEAPnl(self, -1, address = address)
    163                  ea.address_holder = self.__identity
    164                  dlg = gmEditArea.cGenericEditAreaDlg2(self, -1, edit_area = ea)
    165                  dlg.SetTitle(_('Editing address'))
    166                  if dlg.ShowModal() == wx.ID_OK:
    167                          # did we add an entirely new address ?
    168                          # if so then unlink the old one as implied by "edit"
    169                          if ea.address['pk_address'] != address['pk_address']:
    170                                  self.__identity.unlink_address(address = address)
    171                          return True
    172                  return False
    173          #--------------------------------------------------------
    174 -        def _del_address(self, address):
    175                  go_ahead = gmGuiHelpers.gm_show_question (
    176                          _(      'Are you sure you want to remove this\n'
    177                                  "address from the patient's addresses ?\n"
    178                                  '\n'
    179                                  'The address itself will not be deleted\n'
    180                                  'but it will no longer be associated with\n'
    181                                  'this patient.'
    182                          ),
    183                          _('Removing address')
    184                  )
    185                  if not go_ahead:
    186                          return False
    187                  self.__identity.unlink_address(address = address)
    188                  return True
    189          #--------------------------------------------------------
    190 -        def _show_address_on_map(self, address):
    191                  if address is None:
    192                          return False
    193                  gmNetworkTools.open_url_in_browser(address.as_map_url, new = 2, autoraise = True)
    194
    195          #--------------------------------------------------------
    196 -        def _show_distance_on_map(self, address):
    197                  if address is None:
    198                          return False
    199                  praxis_branch = gmPraxis.gmCurrentPraxisBranch()
    200                  gmNetworkTools.open_url_in_browser(praxis_branch.get_distance2address_url(address), new = 2, autoraise = True)
    201
    202          #--------------------------------------------------------
    203 -        def _calculate_tooltip(self, address):
    204                  tt = '\n'.join(address.format())
    205                  tt += '\n'
    206                  tt += '%s\n' % (gmTools.u_box_horiz_single * 40)
    207                  tt += self.__static_tooltip_part
    208                  return tt
    209
    210          #--------------------------------------------------------
    211          # properties
    212          #--------------------------------------------------------
    213 -        def _get_identity(self):
    214                  return self.__identity
    215
    216 -        def _set_identity(self, identity):
    217                  self.__identity = identity
    218                  self.refresh()
    219
    220          identity = property(_get_identity, _set_identity)
    221
    222  #------------------------------------------------------------
    223  from Gnumed.wxGladeWidgets import wxgPersonContactsManagerPnl
    224
    225 -class cPersonContactsManagerPnl(wxgPersonContactsManagerPnl.wxgPersonContactsManagerPnl):
    226          """A panel for editing contact data for a person.
    227
    228          - provides access to:
    229            - addresses
    230            - communication paths
    231
    232          Does NOT act on/listen to the current patient.
    233          """
    234 -        def __init__(self, *args, **kwargs):
    235
    236                  wxgPersonContactsManagerPnl.wxgPersonContactsManagerPnl.__init__(self, *args, **kwargs)
    237
    238                  self.__identity = None
    239                  self.refresh()
    240          #--------------------------------------------------------
    241          # external API
    242          #--------------------------------------------------------
    243 -        def refresh(self):
    244                  self._PNL_addresses.identity = self.__identity
    245                  self._PNL_comms.channel_owner = self.__identity
    246          #--------------------------------------------------------
    247          # properties
    248          #--------------------------------------------------------
    249 -        def _get_identity(self):
    250                  return self.__identity
    251
    252 -        def _set_identity(self, identity):
    253                  self.__identity = identity
    254                  self.refresh()
    255
    256          identity = property(_get_identity, _set_identity)
    257
    258  #============================================================
    259  if __name__ == "__main__":
    260
    261          if len(sys.argv) < 2:
    262                  sys.exit()
    263
    264          if sys.argv[1] != 'test':
    265                  sys.exit()
    266
    267          from Gnumed.pycommon import gmI18N, gmPG2
    268
    269          gmI18N.activate_locale()
    270          gmI18N.install_domain(domain='gnumed')
    271          gmPG2.get_connection()
    272
    273          #--------------------------------------------------------
    274 -        def test_person_adrs_pnl():
    275                  app = wx.PyWidgetTester(size = (600, 400))
    276                  widget = cPersonAddressesManagerPnl(app.frame, -1)
    277                  widget.identity = activate_patient()
    278                  app.frame.Show(True)
    279                  app.MainLoop()
    280          #--------------------------------------------------------
    281 -        def test_pat_contacts_pnl():
    282                  app = wx.PyWidgetTester(size = (600, 400))
    283                  widget = cPersonContactsManagerPnl(app.frame, -1)
    284                  widget.identity = activate_patient()
    285                  app.frame.Show(True)
    286                  app.MainLoop()
    287          #--------------------------------------------------------
    288          #test_pat_contacts_pnl()
    289          #test_person_adrs_pnl()
    290
    291  #============================================================
    292