Package Gnumed :: Package wxpython :: Module gmPatPicWidgets
[frames] | no frames]

Source Code for Module Gnumed.wxpython.gmPatPicWidgets

  1  """GNUmed patient picture widget.""" 
  2   
  3  #===================================================================== 
  4  __author__  = "R.Terry <rterry@gnumed.net>,\ 
  5                             I.Haywood <i.haywood@ugrad.unimelb.edu.au>,\ 
  6                             K.Hilbert <Karsten.Hilbert@gmx.net>" 
  7  __license__ = "GPL v2 or later" 
  8   
  9  # standard lib 
 10  import sys, os, os.path, logging 
 11   
 12   
 13  # 3rd party 
 14  import wx, wx.lib.imagebrowser 
 15   
 16   
 17  # GNUmed 
 18  from Gnumed.pycommon import gmDispatcher 
 19  from Gnumed.pycommon import gmTools 
 20  from Gnumed.pycommon import gmI18N 
 21  from Gnumed.pycommon import gmDateTime 
 22   
 23  from Gnumed.business import gmDocuments 
 24  from Gnumed.business import gmPerson 
 25  from Gnumed.business import gmPraxis 
 26   
 27  from Gnumed.wxpython import gmGuiHelpers 
 28   
 29   
 30  _log = logging.getLogger('gm.ui') 
 31   
 32   
 33  ID_AcquirePhoto = wx.NewId() 
 34  ID_ImportPhoto = wx.NewId() 
 35  ID_Refresh = wx.NewId() 
 36   
 37  #===================================================================== 
38 -class cPatientPicture(wx.StaticBitmap):
39 """A patient picture control ready for display. 40 with popup menu to import/export 41 remove or Acquire from a device 42 """
43 - def __init__(self, *args, **kwargs):
44 45 wx.StaticBitmap.__init__(self, *args, **kwargs) 46 47 paths = gmTools.gmPaths(app_name = 'gnumed', wx = wx) 48 self.__fallback_pic_name = os.path.join(paths.system_app_data_dir, 'bitmaps', 'empty-face-in-bust.png') 49 self.__desired_width = 50 50 self.__desired_height = 54 51 self.__pat = gmPerson.gmCurrentPatient() 52 53 self.__init_ui() 54 self.__register_events()
55 #----------------------------------------------------------------- 56 # event handling 57 #-----------------------------------------------------------------
58 - def __register_events(self):
59 # wxPython events 60 self.Bind(wx.EVT_RIGHT_UP, self._on_RightClick_photo) 61 62 # dispatcher signals 63 gmDispatcher.connect(receiver=self._on_post_patient_selection, signal = 'post_patient_selection')
64 #-----------------------------------------------------------------
65 - def _on_post_patient_selection(self):
66 self.__reload_photo()
67 #-----------------------------------------------------------------
68 - def _on_RightClick_photo(self, event):
69 if not self.__pat.connected: 70 gmDispatcher.send(signal='statustext', msg=_('No active patient.')) 71 return False 72 self.PopupMenu(self.__photo_menu, event.GetPosition())
73 #-----------------------------------------------------------------
74 - def _on_refresh_from_backend(self, evt):
75 self.__reload_photo()
76 #-----------------------------------------------------------------
77 - def _on_ImportPhoto(self, event):
78 """Import an existing photo.""" 79 80 # get from file system 81 imp_dlg = wx.lib.imagebrowser.ImageDialog(parent = self, set_dir = os.path.expanduser('~')) 82 imp_dlg.Centre() 83 if imp_dlg.ShowModal() != wx.ID_OK: 84 return 85 86 self.__import_pic_into_db(fname = imp_dlg.GetFile()) 87 self.__reload_photo()
88 #-----------------------------------------------------------------
89 - def _on_AcquirePhoto(self, event):
90 91 # get from image source 92 from Gnumed.pycommon import gmScanBackend 93 94 try: 95 fnames = gmScanBackend.acquire_pages_into_files ( 96 delay = 5, 97 calling_window = self 98 ) 99 except OSError: 100 _log.exception('problem acquiring image from source') 101 gmGuiHelpers.gm_show_error ( 102 aMessage = _( 103 'No image could be acquired from the source.\n\n' 104 'This may mean the scanner driver is not properly installed.\n\n' 105 'On Windows you must install the TWAIN Python module\n' 106 'while on Linux and MacOSX it is recommended to install\n' 107 'the XSane package.' 108 ), 109 aTitle = _('Acquiring photo') 110 ) 111 return 112 113 if fnames is False: 114 gmGuiHelpers.gm_show_error ( 115 aMessage = _('Patient photo could not be acquired from source.'), 116 aTitle = _('Acquiring photo') 117 ) 118 return 119 120 if len(fnames) == 0: # no pages scanned 121 return 122 123 self.__import_pic_into_db(fname=fnames[0]) 124 self.__reload_photo()
125 #----------------------------------------------------------------- 126 # internal API 127 #-----------------------------------------------------------------
128 - def __init_ui(self):
129 # pre-make context menu 130 self.__photo_menu = wx.Menu() 131 item = self.__photo_menu.Append(-1, _('Refresh from database')) 132 self.Bind(wx.EVT_MENU, self._on_refresh_from_backend, item) 133 134 self.__photo_menu.AppendSeparator() 135 136 item = self.__photo_menu.Append(-1, _("Acquire from imaging device")) 137 self.Bind(wx.EVT_MENU, self._on_AcquirePhoto, item) 138 item = self.__photo_menu.Append(-1, _("Import from file")) 139 self.Bind(wx.EVT_MENU, self._on_ImportPhoto, item) 140 141 self.__set_pic_from_file()
142 143 #-----------------------------------------------------------------
144 - def __import_pic_into_db(self, fname=None):
145 146 docs = gmDocuments.search_for_documents(patient_id = self.__pat.ID, type_id = gmDocuments.MUGSHOT) 147 if len(docs) == 0: 148 emr = self.__pat.emr 149 epi = emr.add_episode(episode_name = 'administrative') 150 enc = emr.active_encounter 151 doc = gmDocuments.create_document ( 152 document_type = gmDocuments.MUGSHOT, 153 episode = epi['pk_episode'], 154 encounter = enc['pk_encounter'] 155 ) 156 doc['pk_org_unit'] = gmPraxis.gmCurrentPraxisBranch()['pk_org_unit'] 157 doc.save() 158 else: 159 doc = docs[0] 160 161 obj = doc.add_part(file = fname) 162 return True
163 164 #-----------------------------------------------------------------
165 - def __reload_photo(self):
166 """(Re)fetch patient picture from DB.""" 167 168 doc_folder = self.__pat.get_document_folder() 169 photo = doc_folder.get_latest_mugshot() 170 171 if photo is None: 172 fname = None 173 self.SetToolTip (_( 174 'Patient picture.\n' 175 '\n' 176 'Right-click for context menu.' 177 )) 178 # gmDispatcher.send(signal='statustext', msg=_('Cannot get most recent patient photo from database.')) 179 else: 180 fname = photo.save_to_file() 181 self.SetToolTip (_( 182 'Patient picture (%s).\n' 183 '\n' 184 'Right-click for context menu.' 185 ) % gmDateTime.pydt_strftime(photo['date_generated'], '%b %Y')) 186 187 return self.__set_pic_from_file(fname)
188 #-----------------------------------------------------------------
189 - def __set_pic_from_file(self, fname=None):
190 if fname is None: 191 fname = self.__fallback_pic_name 192 try: 193 img_data = wx.Image(fname, wx.BITMAP_TYPE_ANY) 194 img_data.Rescale(self.__desired_width, self.__desired_height) 195 bmp_data = wx.Bitmap(img_data) 196 except: 197 _log.exception('cannot set patient picture from [%s]', fname) 198 gmDispatcher.send(signal='statustext', msg=_('Cannot set patient picture from [%s].') % fname) 199 return False 200 del img_data 201 self.SetBitmap(bmp_data) 202 self.__pic_name = fname 203 204 return True
205 206 #==================================================== 207 # main 208 #---------------------------------------------------- 209 if __name__ == "__main__": 210 app = wx.PyWidgetTester(size = (200, 200)) 211 app.SetWidget(cPatientPicture, -1) 212 app.MainLoop() 213 #==================================================== 214