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