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 #================================================================41148 149 #================================================================ 150 from Gnumed.wxGladeWidgets import wxgPatientListingPnl 15143 """<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.data is None: 56 return None 57 58 if len(self.data) == 0: 59 return None 60 61 if data is None: 62 data = self.get_selected_item_data(only_one = True) 63 64 if data is None: 65 data = self.get_item_data(item_idx = 0) 66 67 if data is None: 68 return None 69 70 if self.patient_key is not None: 71 try: 72 data[self.patient_key] 73 return self.patient_key 74 except (KeyError, IndexError, TypeError): 75 # programming error 76 _log.error('misconfigured identifier column <%s>', self.patient_key) 77 78 _log.debug('identifier column not configured, trying to detect') 79 80 if data.has_key('pk_patient'): 81 return u'pk_patient' 82 83 if data.has_key('fk_patient'): 84 return u'fk_patient' 85 86 if data.has_key('pk_identity'): 87 return u'pk_identity' 88 89 if data.has_key('fk_identity'): 90 return u'fk_identity' 91 92 if data.has_key('id_identity'): 93 return u'id_identity' 94 95 return gmListWidgets.get_choices_from_list ( 96 parent = self, 97 msg = _( 98 'The report result list does not contain any of the following columns:\n' 99 '\n' 100 ' <%s> / pk_patient / fk_patient\n' 101 ' pk_identity / fk_identity / id_identity\n' 102 '\n' 103 'Select the column which contains patient IDs:\n' 104 ) % self.patient_key, 105 caption = _('Choose column from query results ...'), 106 choices = data.keys(), 107 columns = [_('Column name')], 108 single_selection = True 109 )110 111 patient_pk_data_key = property(__get_patient_pk_data_key, lambda x:x) 112 #------------------------------------------------------------ 113 # event handling 114 #------------------------------------------------------------116 data = self.get_selected_item_data(only_one = True) 117 pk_pat_col = self.__get_patient_pk_data_key(data = data) 118 119 if pk_pat_col is None: 120 gmDispatcher.send(signal = 'statustext', msg = _('List not known to be patient-related.')) 121 return 122 123 pat_data = data[pk_pat_col] 124 try: 125 pat_pk = int(pat_data) 126 pat = gmPerson.cIdentity(aPK_obj = pat_pk) 127 except (ValueError, TypeError): 128 searcher = gmPersonSearch.cPatientSearcher_SQL() 129 idents = searcher.get_identities(pat_data) 130 if len(idents) == 0: 131 gmDispatcher.send(signal = 'statustext', msg = _('No matching patient found.')) 132 return 133 if len(idents) == 1: 134 pat = idents[0] 135 else: 136 from Gnumed.wxpython import gmPatSearchWidgets 137 dlg = gmPatSearchWidgets.cSelectPersonFromListDlg(parent=wx.GetTopLevelParent(self), id=-1) 138 dlg.set_persons(persons=idents) 139 result = dlg.ShowModal() 140 if result == wx.ID_CANCEL: 141 dlg.Destroy() 142 return 143 pat = dlg.get_selected_person() 144 dlg.Destroy() 145 146 from Gnumed.wxpython import gmPatSearchWidgets 147 gmPatSearchWidgets.set_active_patient(patient = pat)153200 201 #================================================================ 202 from Gnumed.wxGladeWidgets import wxgDataMiningPnl 203155 156 try: 157 button_defs = kwargs['button_defs'][:5] 158 del kwargs['button_defs'] 159 except KeyError: 160 button_defs = [] 161 162 try: 163 msg = kwargs['message'] 164 del kwargs['message'] 165 except KeyError: 166 msg = None 167 168 wxgPatientListingPnl.wxgPatientListingPnl.__init__(self, *args, **kwargs) 169 170 if msg is not None: 171 self._lbl_msg.SetLabel(msg) 172 173 buttons = [self._BTN_1, self._BTN_2, self._BTN_3, self._BTN_4, self._BTN_5] 174 for idx in range(len(button_defs)): 175 button_def = button_defs[idx] 176 if button_def['label'].strip() == u'': 177 continue 178 buttons[idx].SetLabel(button_def['label']) 179 buttons[idx].SetToolTipString(button_def['tooltip']) 180 buttons[idx].Enable(True) 181 182 self.Fit()183 #------------------------------------------------------------ 184 # event handling 185 #------------------------------------------------------------187 event.Skip()188 #------------------------------------------------------------190 event.Skip()191 #------------------------------------------------------------193 event.Skip()194 #------------------------------------------------------------196 event.Skip()197 #------------------------------------------------------------199 event.Skip()205585 #================================================================ 586 # main 587 #---------------------------------------------------------------- 588 if __name__ == '__main__': 589 from Gnumed.pycommon import gmI18N, gmDateTime 590 591 gmI18N.activate_locale() 592 gmI18N.install_domain() 593 gmDateTime.init() 594 595 #------------------------------------------------------------207 wxgDataMiningPnl.wxgDataMiningPnl.__init__(self, *args, **kwargs) 208 209 self.__init_ui() 210 211 # make me a file drop target 212 dt = gmGuiHelpers.cFileDropTarget(self) 213 self.SetDropTarget(dt)214 #--------------------------------------------------------216 mp = gmMatchProvider.cMatchProvider_SQL2 ( 217 queries = [u""" 218 SELECT DISTINCT ON (label) 219 cmd, 220 label 221 FROM cfg.report_query 222 WHERE 223 label %(fragment_condition)s 224 OR 225 cmd %(fragment_condition)s 226 """] 227 ) 228 mp.setThresholds(2,3,5) 229 self._PRW_report_name.matcher = mp 230 self._PRW_report_name.add_callback_on_selection(callback = self._on_report_selected) 231 self._PRW_report_name.add_callback_on_lose_focus(callback = self._auto_load_report)232 #--------------------------------------------------------234 if self._TCTRL_query.GetValue() == u'': 235 if self._PRW_report_name.GetData() is not None: 236 self._TCTRL_query.SetValue(self._PRW_report_name.GetData()) 237 self._BTN_run.SetFocus()238 #-------------------------------------------------------- 242 #-------------------------------------------------------- 243 # file drop target API 244 #--------------------------------------------------------246 # act on first file only 247 fname = filenames[0] 248 _log.debug('importing SQL from <%s>', fname) 249 # act on text files only 250 mime_type = gmMimeLib.guess_mimetype(fname) 251 _log.debug('mime type: %s', mime_type) 252 if not mime_type.startswith('text/'): 253 _log.debug('not a text file') 254 gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. Not a text file.') % fname, beep = True) 255 return False 256 # # act on "small" files only 257 # stat_val = os.stat(fname) 258 # if stat_val.st_size > 5000: 259 # gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. File too big (> 2000 bytes).') % fname, beep = True) 260 # return False 261 # all checks passed 262 for line in fileinput.input(fname): 263 self._TCTRL_query.AppendText(line)264 #-------------------------------------------------------- 265 # notebook plugin API 266 #-------------------------------------------------------- 269 #-------------------------------------------------------- 270 # event handlers 271 #-------------------------------------------------------- 329 #-------------------------------------------------------- 333 #-------------------------------------------------------- 345 #-------------------------------------------------------- 350 #-------------------------------------------------------- 366 #-------------------------------------------------------- 414 415 #-------------------------------------------------------- 450 451 #--------------------------------------------------------597 app = wx.PyWidgetTester(size = (400, 500)) 598 lst = cPatientListingCtrl(app.frame, patient_key = 0) 599 lst.set_columns(['name', 'comment']) 600 lst.set_string_items([ 601 ['Kirk', 'Kirk by name'], 602 ['#12', 'Kirk by ID'], 603 ['unknown', 'unknown patient'] 604 ]) 605 # app.SetWidget(cPatientListingCtrl, patient_key = 0) 606 app.frame.Show() 607 app.MainLoop()608 #------------------------------------------------------------ 609 610 test_pat_list_ctrl() 611
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Oct 5 03:57:27 2013 | http://epydoc.sourceforge.net |