File: gmCodingWidgets.py

package info (click to toggle)
gnumed-client 1.4.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 164,192 kB
  • ctags: 168,531
  • sloc: python: 88,281; sh: 765; makefile: 37
file content (261 lines) | stat: -rw-r--r-- 7,333 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""GNUmed coding related widgets."""
#================================================================
__author__ = 'karsten.hilbert@gmx.net'
__license__ = 'GPL v2 or later (details at http://www.gnu.org)'

# stdlib
import logging, sys


# 3rd party
import wx


# GNUmed
if __name__ == '__main__':
	sys.path.insert(0, '../../')

from Gnumed.business import gmCoding

from Gnumed.pycommon import gmTools
from Gnumed.pycommon import gmMatchProvider

from Gnumed.wxpython import gmListWidgets
from Gnumed.wxpython import gmPhraseWheel


_log = logging.getLogger('gm.ui')

#================================================================
def browse_data_sources(parent=None):

	if parent is None:
		parent = wx.GetApp().GetTopWindow()
	#------------------------------------------------------------
	def refresh(lctrl):
		srcs = gmCoding.get_data_sources()
		items = [ [
			u'%s (%s): %s' % (
				s['name_short'],
				gmTools.coalesce(s['lang'], u'?'),
				s['version']
			),
			s['name_long'].split(u'\n')[0].split(u'\r')[0],
			s['source'].split(u'\n')[0].split(u'\r')[0],
			gmTools.coalesce(s['description'], u'').split(u'\n')[0].split(u'\r')[0],
			s['pk']
		] for s in srcs ]
		lctrl.set_string_items(items)
		lctrl.set_data(srcs)
	#------------------------------------------------------------
	gmListWidgets.get_choices_from_list (
		parent = parent,
		msg = _('Sources of reference data registered in GNUmed.'),
		caption = _('Showing data sources'),
		columns = [ _('System'), _('Name'), _('Source'), _('Description'), '#' ],
		single_selection = True,
		can_return_empty = False,
		ignore_OK_button = True,
		refresh_callback = refresh
#		edit_callback=None,
#		new_callback=None,
#		delete_callback=None,
#		left_extra_button=None,
#		middle_extra_button=None,
#		right_extra_button=None
	)

#----------------------------------------------------------------
class cDataSourcePhraseWheel(gmPhraseWheel.cPhraseWheel):

	def __init__(self, *args, **kwargs):

		gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)

		query = u"""
			SELECT DISTINCT ON (list_label)
				pk
					AS data,
				name_short || ' (' || version || ')'
					AS field_label,
				name_short || ' ' || version || ' (' || name_long || ')'
					AS list_label
			FROM
				ref.data_source
			WHERE
				name_short %(fragment_condition)s
					OR
				name_long %(fragment_condition)s
			ORDER BY list_label
			LIMIT 50
		"""
		mp = gmMatchProvider.cMatchProvider_SQL2(queries = query)
		mp.setThresholds(1, 2, 4)
#		mp.word_separators = '[ \t=+&:@]+'
		self.SetToolTipString(_('Select a data source / coding system.'))
		self.matcher = mp
		self.selection_only = True

#================================================================
def browse_coded_terms(parent=None, coding_systems=None, languages=None):

	if parent is None:
		parent = wx.GetApp().GetTopWindow()
	#------------------------------------------------------------
	def refresh(lctrl):
		coded_terms = gmCoding.get_coded_terms (
			coding_systems = coding_systems,
			languages = languages,
			order_by = u'term, coding_system, code'
		)
		items = [ [
			ct['term'],
			ct['code'],
			ct['coding_system'],
			gmTools.coalesce(ct['lang'], u''),
			ct['version'],
			ct['coding_system_long']
		] for ct in coded_terms ]
		lctrl.set_string_items(items)
		lctrl.set_data(coded_terms)
	#------------------------------------------------------------
	gmListWidgets.get_choices_from_list (
		parent = parent,
		msg = _('Coded terms known to GNUmed (may take a while to load).'),
		caption = _('Showing coded terms.'),
		columns = [ _('Term'), _('Code'), _('System'), _('Language'), _('Version'), _(u'Coding system details') ],
		single_selection = True,
		can_return_empty = True,
		ignore_OK_button = True,
		refresh_callback = refresh
#		edit_callback=None,
#		new_callback=None,
#		delete_callback=None,
#		left_extra_button=None,
#		middle_extra_button=None,
#		right_extra_button=None
	)

#================================================================

class cGenericCodesPhraseWheel(gmPhraseWheel.cMultiPhraseWheel):

	def __init__(self, *args, **kwargs):

		super(cGenericCodesPhraseWheel, self).__init__(*args, **kwargs)

		query = u"""
			SELECT
				-- DISTINCT ON (list_label)
				data,
				list_label,
				field_label
			FROM (

				SELECT
					pk_generic_code
						AS data,
					(code || ' (' || coding_system || '): ' || term || ' (' || version || coalesce(' - ' || lang, '') || ')')
						AS list_label,
					code AS
						field_label
				FROM
					ref.v_coded_terms
				WHERE
					term %(fragment_condition)s
						OR
					code %(fragment_condition)s
					%(ctxt_system)s
					%(ctxt_lang)s

			) AS applicable_codes
			ORDER BY list_label
			LIMIT 30
		"""
		ctxt = {
			'ctxt_system': {				# must be a TUPLE !
				'where_part': u'AND coding_system IN %(system)s',
				'placeholder': u'system'
			},
			'ctxt_lang': {
				'where_part': u'AND lang = %(lang)s',
				'placeholder': u'lang'
			}
		}

		mp = gmMatchProvider.cMatchProvider_SQL2(queries = query, context = ctxt)
		mp.setThresholds(2, 4, 5)
		mp.word_separators = '[ \t=+&/:-]+'
		#mp.print_queries = True

		self.phrase_separators = ';'
		self.selection_only = False			# not sure yet how this fares with multi-phrase input
		self.SetToolTipString(_('Select one or more codes that apply.'))
		self.matcher = mp

		self.add_callback_on_lose_focus(callback = self.__on_losing_focus)
	#------------------------------------------------------------
	def __on_losing_focus(self):
		self._adjust_data_after_text_update()
		if self.GetValue().strip() == u'':
			return

		if len(self.data) != len(self.displayed_strings):
			self.display_as_valid(valid = False, partially_invalid = True)
			return

		self.display_as_valid(valid = True)
	#------------------------------------------------------------
	def _get_data_tooltip(self):
		if len(self.data) == 0:
			return u''

		return u';\n'.join([ i['list_label'] for i in self.data.values() ]) + u';'
	#------------------------------------------------------------
	def generic_linked_codes2item_dict(self, codes):
		if len(codes) == 0:
			return u'', {}

		code_dict = {}
		val = u''
		for code in codes:
			list_label = u'%s (%s): %s (%s - %s)' % (
				code['code'],
				code['name_short'],
				code['term'],
				code['version'],
				code['lang']
			)
			field_label = code['code']
			code_dict[field_label] = {'data': code['pk_generic_code'], 'field_label': field_label, 'list_label': list_label}
			val += u'%s; ' % field_label

		return val.strip(), code_dict
#================================================================
# main
#----------------------------------------------------------------
if __name__ == '__main__':

	if len(sys.argv) < 2:
		sys.exit()

	if sys.argv[1] != 'test':
		sys.exit()

	from Gnumed.pycommon import gmI18N
	gmI18N.activate_locale()
	gmI18N.install_domain()
	from Gnumed.pycommon import gmPG2

	#--------------------------------------------------------
	def test_generic_codes_prw():
		gmPG2.get_connection()
		app = wx.PyWidgetTester(size = (500, 40))
		pw = cGenericCodesPhraseWheel(app.frame, -1)
		#pw.set_context(context = u'zip', val = u'04318')
		app.frame.Show(True)
		app.MainLoop()
	#--------------------------------------------------------
	test_generic_codes_prw()

#================================================================