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

Source Code for Module Gnumed.wxpython.gmPatPicWidgets

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