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
8 import logging, sys
9
10
11
12 import wx
13
14
15
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
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
61
62
63
64
65
66 )
67
68
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
101
102
103
104
105
106 )
107
108
109
111
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': {
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
159
160 self.phrase_separators = ';'
161 self.selection_only = False
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
177
183
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
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
227
228 test_generic_codes_prw()
229
230
231