| Home | Trees | Indices | Help |
|
|---|
|
|
1 """GNUmed data mining related widgets.
2 """
3 #================================================================
4 # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/wxpython/gmDataMiningWidgets.py,v $
5 # $Id: gmDataMiningWidgets.py,v 1.14 2009-07-30 12:03:34 ncq Exp $
6 __version__ = '$Revision: 1.14 $'
7 __author__ = 'karsten.hilbert@gmx.net'
8 __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
9
10
11 # stdlib
12 import sys, os, fileinput, 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, gmMimeLib, gmTools, gmPG2, gmMatchProvider, gmI18N, gmNetworkTools
23 from Gnumed.business import gmPerson, gmDataMining, gmPersonSearch
24 from Gnumed.wxpython import gmGuiHelpers, gmListWidgets
25 from Gnumed.wxGladeWidgets import wxgPatientListingPnl, wxgDataMiningPnl
26
27
28 _log = logging.getLogger('gm.ui')
29 _log.info(__version__)
30 #================================================================
32
34 """<patient_key> must index or name a column in self.__data"""
35 try:
36 self.patient_key = kwargs['patient_key']
37 del kwargs['patient_key']
38 except KeyError:
39 self.patient_key = None
40
41 gmListWidgets.cReportListCtrl.__init__(self, *args, **kwargs)
42
43 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self._on_list_item_activated, self)
44 #------------------------------------------------------------
45 # event handling
46 #------------------------------------------------------------
48 if self.patient_key is None:
49 gmDispatcher.send(signal = 'statustext', msg = _('List not known to be patient-related.'))
50 return
51 data = self.get_selected_item_data(only_one=True)
52 try:
53 pat_data = data[self.patient_key]
54 except (KeyError, IndexError, TypeError):
55 gmGuiHelpers.gm_show_info (
56 _(
57 'Cannot activate patient.\n\n'
58 'The row does not contain a column\n'
59 'named or indexed "%s".\n\n'
60 ) % self.patient_key,
61 _('activating patient from list')
62 )
63 return
64 try:
65 pat_pk = int(pat_data)
66 pat = gmPerson.cIdentity(aPK_obj = pat_pk)
67 except (ValueError, TypeError):
68 searcher = gmPersonSearch.cPatientSearcher_SQL()
69 idents = searcher.get_identities(pat_data)
70 if len(idents) == 0:
71 gmDispatcher.send(signal = 'statustext', msg = _('No matching patient found.'))
72 return
73 if len(idents) == 1:
74 pat = idents[0]
75 else:
76 from Gnumed.wxpython import gmPatSearchWidgets
77 dlg = gmPatSearchWidgets.cSelectPersonFromListDlg(parent=wx.GetTopLevelParent(self), id=-1)
78 dlg.set_persons(persons=idents)
79 result = dlg.ShowModal()
80 if result == wx.ID_CANCEL:
81 dlg.Destroy()
82 return
83 pat = dlg.get_selected_person()
84 dlg.Destroy()
85
86 from Gnumed.wxpython import gmPatSearchWidgets
87 gmPatSearchWidgets.set_active_patient(patient = pat)
88 #================================================================
90
92
93 try:
94 button_defs = kwargs['button_defs'][:5]
95 del kwargs['button_defs']
96 except KeyError:
97 button_defs = []
98
99 try:
100 msg = kwargs['message']
101 del kwargs['message']
102 except KeyError:
103 msg = None
104
105 wxgPatientListingPnl.wxgPatientListingPnl.__init__(self, *args, **kwargs)
106
107 if msg is not None:
108 self._lbl_msg.SetLabel(msg)
109
110 buttons = [self._BTN_1, self._BTN_2, self._BTN_3, self._BTN_4, self._BTN_5]
111 for idx in range(len(button_defs)):
112 button_def = button_defs[idx]
113 if button_def['label'].strip() == u'':
114 continue
115 buttons[idx].SetLabel(button_def['label'])
116 buttons[idx].SetToolTipString(button_def['tooltip'])
117 buttons[idx].Enable(True)
118
119 self.Fit()
120 #------------------------------------------------------------
121 # event handling
122 #------------------------------------------------------------
125 #------------------------------------------------------------
128 #------------------------------------------------------------
131 #------------------------------------------------------------
134 #------------------------------------------------------------
137 #================================================================
139
141 wxgDataMiningPnl.wxgDataMiningPnl.__init__(self, *args, **kwargs)
142
143 self.__init_ui()
144
145 # make me a file drop target
146 dt = gmGuiHelpers.cFileDropTarget(self)
147 self.SetDropTarget(dt)
148 #--------------------------------------------------------
150 mp = gmMatchProvider.cMatchProvider_SQL2 (
151 queries = [u"""
152 SELECT DISTINCT ON (label)
153 cmd,
154 label
155 FROM cfg.report_query
156 WHERE
157 label %(fragment_condition)s
158 OR
159 cmd %(fragment_condition)s
160 """]
161 )
162 mp.setThresholds(2,3,5)
163 self._PRW_report_name.matcher = mp
164 self._PRW_report_name.add_callback_on_selection(callback = self._on_report_selected)
165 self._PRW_report_name.add_callback_on_lose_focus(callback = self._auto_load_report)
166 #--------------------------------------------------------
168 if self._TCTRL_query.GetValue() == u'':
169 if self._PRW_report_name.GetData() is not None:
170 self._TCTRL_query.SetValue(self._PRW_report_name.GetData())
171 self._BTN_run.SetFocus()
172 #--------------------------------------------------------
176 #--------------------------------------------------------
177 # file drop target API
178 #--------------------------------------------------------
180 # act on first file only
181 fname = filenames[0]
182 _log.debug('importing SQL from <%s>', fname)
183 # act on text files only
184 mime_type = gmMimeLib.guess_mimetype(fname)
185 _log.debug('mime type: %s', mime_type)
186 if not mime_type.startswith('text/'):
187 _log.debug('not a text file')
188 gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. Not a text file.') % fname, beep = True)
189 return False
190 # act on "small" files only
191 stat_val = os.stat(fname)
192 if stat_val.st_size > 2000:
193 gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. File too big (> 2000 bytes).') % fname, beep = True)
194 return False
195 # all checks passed
196 for line in fileinput.input(fname):
197 self._TCTRL_query.AppendText(line)
198 #--------------------------------------------------------
199 # notebook plugin API
200 #--------------------------------------------------------
203 #--------------------------------------------------------
204 # event handlers
205 #--------------------------------------------------------
207 data = self._LCTRL_result.get_selected_item_data()
208
209 try:
210 pk_pat = data['pk_patient']
211 except KeyError:
212 gmGuiHelpers.gm_show_warning (
213 _(
214 'Cannot activate patient.\n\n'
215 'The report result list does not contain\n'
216 'a column named "pk_patient".\n\n'
217 'You may want to use the SQL "AS" column alias\n'
218 'syntax to make your query return such a column.\n'
219 ),
220 _('Activating patient from report result')
221 )
222 return
223
224 try:
225 pat = gmPerson.cPatient(aPK_obj = pk_pat)
226 except StandardError:
227 gmGuiHelpers.gm_show_warning (
228 _(
229 'Cannot activate patient.\n'
230 '\n'
231 'There does not seem to exist a patient\n'
232 'with an internal ID of [%s].\n'
233 ) % pk_pat,
234 _('Activating patient from report result')
235 )
236 return
237
238 from Gnumed.wxpython import gmPatSearchWidgets
239 gmPatSearchWidgets.set_active_patient(patient = pat)
240 #--------------------------------------------------------
295 #--------------------------------------------------------
299 #--------------------------------------------------------
311 #--------------------------------------------------------
317 #--------------------------------------------------------
333 #--------------------------------------------------------
383 #--------------------------------------------------------
518 #================================================================
519 # main
520 #----------------------------------------------------------------
521 if __name__ == '__main__':
522 from Gnumed.pycommon import gmI18N, gmDateTime
523
524 gmI18N.activate_locale()
525 gmI18N.install_domain()
526 gmDateTime.init()
527
528 #------------------------------------------------------------
530 app = wx.PyWidgetTester(size = (400, 500))
531 lst = cPatientListingCtrl(app.frame, patient_key = 0)
532 lst.set_columns(['name', 'comment'])
533 lst.set_string_items([
534 ['Kirk', 'Kirk by name'],
535 ['#12', 'Kirk by ID'],
536 ['unknown', 'unknown patient']
537 ])
538 # app.SetWidget(cPatientListingCtrl, patient_key = 0)
539 app.frame.Show()
540 app.MainLoop()
541 #------------------------------------------------------------
542
543 test_pat_list_ctrl()
544
545 #================================================================
546
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Jun 25 03:58:51 2012 | http://epydoc.sourceforge.net |