1 """GNUmed coding related widgets."""
2
3 __author__ = 'karsten.hilbert@gmx.net'
4 __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
5
6
7 import logging, sys
8
9
10
11 import wx
12
13
14
15 if __name__ == '__main__':
16 sys.path.insert(0, '../../')
17
18 from Gnumed.business import gmCoding
19
20 from Gnumed.pycommon import gmTools
21 from Gnumed.pycommon import gmMatchProvider
22
23 from Gnumed.wxpython import gmListWidgets
24 from Gnumed.wxpython import gmPhraseWheel
25
26
27 _log = logging.getLogger('gm.ui')
28
29
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
72
73 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
74
75 query = """
76 SELECT DISTINCT ON (list_label)
77 pk
78 AS data,
79 name_short || ' (' || version || ')'
80 AS field_label,
81 name_short || ' ' || version || ' (' || name_long || ')'
82 AS list_label
83 FROM
84 ref.data_source
85 WHERE
86 name_short %(fragment_condition)s
87 OR
88 name_long %(fragment_condition)s
89 ORDER BY list_label
90 LIMIT 50
91 """
92 mp = gmMatchProvider.cMatchProvider_SQL2(queries = query)
93 mp.setThresholds(1, 2, 4)
94
95 self.SetToolTip(_('Select a data source / coding system.'))
96 self.matcher = mp
97 self.selection_only = True
98
99
101
102 if parent is None:
103 parent = wx.GetApp().GetTopWindow()
104
105 def refresh(lctrl):
106 coded_terms = gmCoding.get_coded_terms (
107 coding_systems = coding_systems,
108 languages = languages,
109 order_by = 'term, coding_system, code'
110 )
111 items = [ [
112 ct['term'],
113 ct['code'],
114 ct['coding_system'],
115 gmTools.coalesce(ct['lang'], ''),
116 ct['version'],
117 ct['coding_system_long']
118 ] for ct in coded_terms ]
119 lctrl.set_string_items(items)
120 lctrl.set_data(coded_terms)
121
122 gmListWidgets.get_choices_from_list (
123 parent = parent,
124 msg = _('Coded terms known to GNUmed (may take a while to load).'),
125 caption = _('Showing coded terms.'),
126 columns = [ _('Term'), _('Code'), _('System'), _('Language'), _('Version'), _('Coding system details') ],
127 single_selection = True,
128 can_return_empty = True,
129 ignore_OK_button = True,
130 refresh_callback = refresh
131
132
133
134
135
136
137 )
138
139
140
142
144
145 super(cGenericCodesPhraseWheel, self).__init__(*args, **kwargs)
146
147 query = """
148 SELECT
149 -- DISTINCT ON (list_label)
150 data,
151 list_label,
152 field_label
153 FROM (
154
155 SELECT
156 pk_generic_code
157 AS data,
158 (code || ' (' || coding_system || '): ' || term || ' (' || version || coalesce(' - ' || lang, '') || ')')
159 AS list_label,
160 code AS
161 field_label
162 FROM
163 ref.v_coded_terms
164 WHERE
165 term %(fragment_condition)s
166 OR
167 code %(fragment_condition)s
168 %(ctxt_system)s
169 %(ctxt_lang)s
170
171 ) AS applicable_codes
172 ORDER BY list_label
173 LIMIT 30
174 """
175 ctxt = {
176 'ctxt_system': {
177 'where_part': 'AND coding_system IN %(system)s',
178 'placeholder': 'system'
179 },
180 'ctxt_lang': {
181 'where_part': 'AND lang = %(lang)s',
182 'placeholder': 'lang'
183 }
184 }
185
186 mp = gmMatchProvider.cMatchProvider_SQL2(queries = query, context = ctxt)
187 mp.setThresholds(2, 4, 5)
188 mp.word_separators = '[ \t=+&/:-]+'
189
190
191 self.phrase_separators = ';'
192 self.selection_only = False
193 self.SetToolTip(_('Select one or more codes that apply.'))
194 self.matcher = mp
195
196 self.add_callback_on_lose_focus(callback = self.__on_losing_focus)
197
208
214
216 if len(codes) == 0:
217 return '', {}
218
219 code_dict = {}
220 val = ''
221 for code in codes:
222 list_label = '%s (%s): %s (%s - %s)' % (
223 code['code'],
224 code['name_short'],
225 code['term'],
226 code['version'],
227 code['lang']
228 )
229 field_label = code['code']
230 code_dict[field_label] = {'data': code['pk_generic_code'], 'field_label': field_label, 'list_label': list_label}
231 val += '%s; ' % field_label
232
233 return val.strip(), code_dict
234
235
236
237 if __name__ == '__main__':
238
239 if len(sys.argv) < 2:
240 sys.exit()
241
242 if sys.argv[1] != 'test':
243 sys.exit()
244
245 from Gnumed.pycommon import gmI18N
246 gmI18N.activate_locale()
247 gmI18N.install_domain()
248 from Gnumed.pycommon import gmPG2
249
250
258
259 test_generic_codes_prw()
260
261
262