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

# Source Code for Module Gnumed.wxpython.gmCodingWidgets

      1  """GNUmed coding related widgets."""
      2  #================================================================
      3  __version__ = '$Revision: 1.4 $'
      4  __author__ = 'karsten.hilbert@gmx.net'
      5  __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
      6
      7  # stdlib
      8  import logging, sys
      9
     10
     11  # 3rd party
     12  import wx
     13
     14
     15  # GNUmed
     16  if __name__ == '__main__':
     17          sys.path.insert(0, '../../')
     18
     19  from Gnumed.business import gmCoding
     20  from Gnumed.pycommon import gmTools
     21  from Gnumed.pycommon import gmMatchProvider
     22  from Gnumed.wxpython import gmListWidgets
     23  from Gnumed.wxpython import gmPhraseWheel
     24
     25
     26  _log = logging.getLogger('gm.ui')
     27  _log.info(__version__)
     28
     29  #================================================================
     30 -def browse_data_sources(parent=None):
     31
     32          if parent is None:
     33                  parent = wx.GetApp().GetTopWindow()
     34          #------------------------------------------------------------
     35          def refresh(lctrl):
     36                  srcs = gmCoding.get_data_sources()
     37                  items = [ [
     38                          u'%s (%s): %s' % (
     39                                  s['name_short'],
     40                                  gmTools.coalesce(s['lang'], u'?'),
     41                                  s['version']
     42                          ),
     43                          s['name_long'].split(u'\n')[0].split(u'\r')[0],
     44                          s['source'].split(u'\n')[0].split(u'\r')[0],
     45                          gmTools.coalesce(s['description'], u'').split(u'\n')[0].split(u'\r')[0],
     46                          s['pk']
     47                  ] for s in srcs ]
     48                  lctrl.set_string_items(items)
     49                  lctrl.set_data(srcs)
     50          #------------------------------------------------------------
     51          gmListWidgets.get_choices_from_list (
     52                  parent = parent,
     53                  msg = _('Sources of reference data registered in GNUmed.'),
     54                  caption = _('Showing data sources'),
     55                  columns = [ _('System'), _('Name'), _('Source'), _('Description'), '#' ],
     56                  single_selection = True,
     57                  can_return_empty = False,
     58                  ignore_OK_button = True,
     59                  refresh_callback = refresh
     60  #               edit_callback=None,
     61  #               new_callback=None,
     62  #               delete_callback=None,
     63  #               left_extra_button=None,
     64  #               middle_extra_button=None,
     65  #               right_extra_button=None
     66          )
     67
     68  #================================================================
     69 -def browse_coded_terms(parent=None, coding_systems=None, languages=None):
     70
     71          if parent is None:
     72                  parent = wx.GetApp().GetTopWindow()
     73          #------------------------------------------------------------
     74          def refresh(lctrl):
     75                  coded_terms = gmCoding.get_coded_terms (
     76                          coding_systems = coding_systems,
     77                          languages = languages,
     78                          order_by = u'term, coding_system, code'
     79                  )
     80                  items = [ [
     81                          ct['term'],
     82                          ct['code'],
     83                          ct['coding_system'],
     84                          gmTools.coalesce(ct['lang'], u''),
     85                          ct['version'],
     86                          ct['coding_system_long']
     87                  ] for ct in coded_terms ]
     88                  lctrl.set_string_items(items)
     89                  lctrl.set_data(coded_terms)
     90          #------------------------------------------------------------
     91          gmListWidgets.get_choices_from_list (
     92                  parent = parent,
     93                  msg = _('Coded terms known to GNUmed (may take a while to load).'),
     94                  caption = _('Showing coded terms.'),
     95                  columns = [ _('Term'), _('Code'), _('System'), _('Language'), _('Version'), _(u'Coding system details') ],
     96                  single_selection = True,
     97                  can_return_empty = True,
     98                  ignore_OK_button = True,
     99                  refresh_callback = refresh
    100  #               edit_callback=None,
    101  #               new_callback=None,
    102  #               delete_callback=None,
    103  #               left_extra_button=None,
    104  #               middle_extra_button=None,
    105  #               right_extra_button=None
    106          )
    107
    108  #================================================================
    109
    110 -class cGenericCodesPhraseWheel(gmPhraseWheel.cMultiPhraseWheel):
    111
    112 -        def __init__(self, *args, **kwargs):
    113
    114                  super(cGenericCodesPhraseWheel, self).__init__(*args, **kwargs)
    115
    116                  query = u"""
    117                          SELECT
    118                                  -- DISTINCT ON (list_label)
    119                                  data,
    120                                  list_label,
    121                                  field_label
    122                          FROM (
    123
    124                                  SELECT
    125                                          pk_generic_code
    126                                                  AS data,
    127                                          (code || ' (' || coding_system || '): ' || term || ' (' || version || coalesce(' - ' || lang, '') || ')')
    128                                                  AS list_label,
    129                                          code AS
    130                                                  field_label
    131                                  FROM
    132                                          ref.v_coded_terms
    133                                  WHERE
    134                                          term %(fragment_condition)s
    135                                                  OR
    136                                          code %(fragment_condition)s
    137                                          %(ctxt_system)s
    138                                          %(ctxt_lang)s
    139
    140                          ) AS applicable_codes
    141                          ORDER BY list_label
    142                          LIMIT 30
    143                  """
    144                  ctxt = {
    145                          'ctxt_system': {                                # must be a TUPLE !
    146                                  'where_part': u'AND coding_system IN %(system)s',
    147                                  'placeholder': u'system'
    148                          },
    149                          'ctxt_lang': {
    150                                  'where_part': u'AND lang = %(lang)s',
    151                                  'placeholder': u'lang'
    152                          }
    153                  }
    154
    155                  mp = gmMatchProvider.cMatchProvider_SQL2(queries = query, context = ctxt)
    156                  mp.setThresholds(2, 4, 5)
    157                  mp.word_separators = '[ \t=+&/:-]+'
    158                  #mp.print_queries = True
    159
    160                  self.phrase_separators = ';'
    161                  self.selection_only = False                     # not sure yet how this fares with multi-phrase input
    162                  self.SetToolTipString(_('Select one or more codes that apply.'))
    163                  self.matcher = mp
    164
    165                  self.add_callback_on_lose_focus(callback = self.__on_losing_focus)
    166          #------------------------------------------------------------
    167 -        def __on_losing_focus(self):
    168                  self._adjust_data_after_text_update()
    169                  if self.GetValue().strip() == u'':
    170                          return
    171
    172                  if len(self.data) != len(self.displayed_strings):
    173                          self.display_as_valid(valid = False, partially_invalid = True)
    174                          return
    175
    176                  self.display_as_valid(valid = True)
    177          #------------------------------------------------------------
    178 -        def _get_data_tooltip(self):
    179                  if len(self.data) == 0:
    180                          return u''
    181
    182                  return u';\n'.join([ i['list_label'] for i in self.data.values() ]) + u';'
    183          #------------------------------------------------------------
    184 -        def generic_linked_codes2item_dict(self, codes):
    185                  if len(codes) == 0:
    186                          return u'', {}
    187
    188                  code_dict = {}
    189                  val = u''
    190                  for code in codes:
    191                          list_label = u'%s (%s): %s (%s - %s)' % (
    192                                  code['code'],
    193                                  code['name_short'],
    194                                  code['term'],
    195                                  code['version'],
    196                                  code['lang']
    197                          )
    198                          field_label = code['code']
    199                          code_dict[field_label] = {'data': code['pk_generic_code'], 'field_label': field_label, 'list_label': list_label}
    200                          val += u'%s; ' % field_label
    201
    202                  return val.strip(), code_dict
    203  #================================================================
    204  # main
    205  #----------------------------------------------------------------
    206  if __name__ == '__main__':
    207
    208          if len(sys.argv) < 2:
    209                  sys.exit()
    210
    211          if sys.argv[1] != 'test':
    212                  sys.exit()
    213
    214          from Gnumed.pycommon import gmI18N
    215          gmI18N.activate_locale()
    216          gmI18N.install_domain()
    217          from Gnumed.pycommon import gmPG2
    218
    219          #--------------------------------------------------------
    220 -        def test_generic_codes_prw():
    221                  gmPG2.get_connection()
    222                  app = wx.PyWidgetTester(size = (500, 40))
    223                  pw = cGenericCodesPhraseWheel(app.frame, -1)
    224                  #pw.set_context(context = u'zip', val = u'04318')
    225                  app.frame.Show(True)
    226                  app.MainLoop()
    227          #--------------------------------------------------------
    228          test_generic_codes_prw()
    229
    230  #================================================================
    231