| Home | Trees | Indices | Help |
|
|---|
|
|
1 """GNUmed data mining related widgets."""
2
3 #================================================================
4 __author__ = 'karsten.hilbert@gmx.net'
5 __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
6
7
8 # stdlib
9 import sys
10 import os
11 import fileinput
12 import logging
13
14
15 # 3rd party
16 import wx
17
18
19 # GNUmed
20 if __name__ == '__main__':
21 sys.path.insert(0, '../../')
22 from Gnumed.pycommon import gmDispatcher
23 from Gnumed.pycommon import gmMimeLib
24 from Gnumed.pycommon import gmTools
25 from Gnumed.pycommon import gmPG2
26 from Gnumed.pycommon import gmMatchProvider
27 from Gnumed.pycommon import gmI18N
28 from Gnumed.pycommon import gmNetworkTools
29
30 from Gnumed.business import gmPerson
31 from Gnumed.business import gmDataMining
32 from Gnumed.business import gmPersonSearch
33
34 from Gnumed.wxpython import gmGuiHelpers
35 from Gnumed.wxpython import gmListWidgets
36
37
38 _log = logging.getLogger('gm.ui')
39 #================================================================
41
43 """<patient_key> must index or name a column in self.__data"""
44 try:
45 self.patient_key = kwargs['patient_key']
46 del kwargs['patient_key']
47 except KeyError:
48 self.patient_key = None
49
50 gmListWidgets.cReportListCtrl.__init__(self, *args, **kwargs)
51
52 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self._on_list_item_activated, self)
53 #------------------------------------------------------------
55 if self.patient_key is not None:
56 try:
57 data[self.patient_key]
58 return self.patient_key
59 except (KeyError, IndexError, TypeError):
60 _log.error('misconfigured identifier column <%s>', self.patient_key)
61
62 _log.debug('identifier column not configured, trying to detect')
63
64 if data.has_key('pk_patient'):
65 return u'pk_patient'
66
67 if data.has_key('fk_patient'):
68 return u'fk_patient'
69
70 if data.has_key('pk_identity'):
71 return u'pk_identity'
72
73 if data.has_key('fk_identity'):
74 return u'fk_identity'
75
76 if data.has_key('id_identity'):
77 return u'id_identity'
78
79 return gmListWidgets.get_choices_from_list (
80 parent = self,
81 msg = _(
82 'The report result list does not contain any columns\n'
83 'named "%s", "pk_patient", or "pk_identity".\n'
84 '\n'
85 'Select the column which contains patient IDs:\n'
86 ) % self.patient_key,
87 caption = _('Choose column from query results ...'),
88 choices = data.keys(),
89 columns = [_('Column name')],
90 single_selection = True
91 )
92 #------------------------------------------------------------
93 # event handling
94 #------------------------------------------------------------
96 data = self.get_selected_item_data(only_one = True)
97 pk_pat_col = self.__get_patient_pk_column(data = data)
98
99 if pk_pat_col is None:
100 gmDispatcher.send(signal = 'statustext', msg = _('List not known to be patient-related.'))
101 return
102
103 pat_data = data[pk_pat_col]
104 try:
105 pat_pk = int(pat_data)
106 pat = gmPerson.cIdentity(aPK_obj = pat_pk)
107 except (ValueError, TypeError):
108 searcher = gmPersonSearch.cPatientSearcher_SQL()
109 idents = searcher.get_identities(pat_data)
110 if len(idents) == 0:
111 gmDispatcher.send(signal = 'statustext', msg = _('No matching patient found.'))
112 return
113 if len(idents) == 1:
114 pat = idents[0]
115 else:
116 from Gnumed.wxpython import gmPatSearchWidgets
117 dlg = gmPatSearchWidgets.cSelectPersonFromListDlg(parent=wx.GetTopLevelParent(self), id=-1)
118 dlg.set_persons(persons=idents)
119 result = dlg.ShowModal()
120 if result == wx.ID_CANCEL:
121 dlg.Destroy()
122 return
123 pat = dlg.get_selected_person()
124 dlg.Destroy()
125
126 from Gnumed.wxpython import gmPatSearchWidgets
127 gmPatSearchWidgets.set_active_patient(patient = pat)
128
129 #================================================================
130 from Gnumed.wxGladeWidgets import wxgPatientListingPnl
131
133
135
136 try:
137 button_defs = kwargs['button_defs'][:5]
138 del kwargs['button_defs']
139 except KeyError:
140 button_defs = []
141
142 try:
143 msg = kwargs['message']
144 del kwargs['message']
145 except KeyError:
146 msg = None
147
148 wxgPatientListingPnl.wxgPatientListingPnl.__init__(self, *args, **kwargs)
149
150 if msg is not None:
151 self._lbl_msg.SetLabel(msg)
152
153 buttons = [self._BTN_1, self._BTN_2, self._BTN_3, self._BTN_4, self._BTN_5]
154 for idx in range(len(button_defs)):
155 button_def = button_defs[idx]
156 if button_def['label'].strip() == u'':
157 continue
158 buttons[idx].SetLabel(button_def['label'])
159 buttons[idx].SetToolTipString(button_def['tooltip'])
160 buttons[idx].Enable(True)
161
162 self.Fit()
163 #------------------------------------------------------------
164 # event handling
165 #------------------------------------------------------------
167 event.Skip()
168 #------------------------------------------------------------
170 event.Skip()
171 #------------------------------------------------------------
173 event.Skip()
174 #------------------------------------------------------------
176 event.Skip()
177 #------------------------------------------------------------
179 event.Skip()
180
181 #================================================================
182 from Gnumed.wxGladeWidgets import wxgDataMiningPnl
183
185
187 wxgDataMiningPnl.wxgDataMiningPnl.__init__(self, *args, **kwargs)
188
189 self.__init_ui()
190
191 # make me a file drop target
192 dt = gmGuiHelpers.cFileDropTarget(self)
193 self.SetDropTarget(dt)
194 #--------------------------------------------------------
196 mp = gmMatchProvider.cMatchProvider_SQL2 (
197 queries = [u"""
198 SELECT DISTINCT ON (label)
199 cmd,
200 label
201 FROM cfg.report_query
202 WHERE
203 label %(fragment_condition)s
204 OR
205 cmd %(fragment_condition)s
206 """]
207 )
208 mp.setThresholds(2,3,5)
209 self._PRW_report_name.matcher = mp
210 self._PRW_report_name.add_callback_on_selection(callback = self._on_report_selected)
211 self._PRW_report_name.add_callback_on_lose_focus(callback = self._auto_load_report)
212 #--------------------------------------------------------
214 if self._TCTRL_query.GetValue() == u'':
215 if self._PRW_report_name.GetData() is not None:
216 self._TCTRL_query.SetValue(self._PRW_report_name.GetData())
217 self._BTN_run.SetFocus()
218 #--------------------------------------------------------
222 #--------------------------------------------------------
223 # file drop target API
224 #--------------------------------------------------------
226 # act on first file only
227 fname = filenames[0]
228 _log.debug('importing SQL from <%s>', fname)
229 # act on text files only
230 mime_type = gmMimeLib.guess_mimetype(fname)
231 _log.debug('mime type: %s', mime_type)
232 if not mime_type.startswith('text/'):
233 _log.debug('not a text file')
234 gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. Not a text file.') % fname, beep = True)
235 return False
236 # # act on "small" files only
237 # stat_val = os.stat(fname)
238 # if stat_val.st_size > 5000:
239 # gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. File too big (> 2000 bytes).') % fname, beep = True)
240 # return False
241 # all checks passed
242 for line in fileinput.input(fname):
243 self._TCTRL_query.AppendText(line)
244 #--------------------------------------------------------
245 # notebook plugin API
246 #--------------------------------------------------------
249 #--------------------------------------------------------
250 # event handlers
251 #--------------------------------------------------------
309 #--------------------------------------------------------
313 #--------------------------------------------------------
325 #--------------------------------------------------------
330 #--------------------------------------------------------
346 #--------------------------------------------------------
396 #--------------------------------------------------------
528 #================================================================
529 # main
530 #----------------------------------------------------------------
531 if __name__ == '__main__':
532 from Gnumed.pycommon import gmI18N, gmDateTime
533
534 gmI18N.activate_locale()
535 gmI18N.install_domain()
536 gmDateTime.init()
537
538 #------------------------------------------------------------
540 app = wx.PyWidgetTester(size = (400, 500))
541 lst = cPatientListingCtrl(app.frame, patient_key = 0)
542 lst.set_columns(['name', 'comment'])
543 lst.set_string_items([
544 ['Kirk', 'Kirk by name'],
545 ['#12', 'Kirk by ID'],
546 ['unknown', 'unknown patient']
547 ])
548 # app.SetWidget(cPatientListingCtrl, patient_key = 0)
549 app.frame.Show()
550 app.MainLoop()
551 #------------------------------------------------------------
552
553 test_pat_list_ctrl()
554
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Fri Jul 12 03:56:53 2013 | http://epydoc.sourceforge.net |