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
10 import sys, os, os.path, logging
11
12
13
14 import wx, wx.lib.imagebrowser
15
16
17
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
32 """A patient picture control ready for display.
33 with popup menu to import/export
34 remove or Acquire from a device
35 """
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
50
52
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
59 gmDispatcher.connect(receiver=self._on_post_patient_selection, signal = u'post_patient_selection')
60
63
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
72
74 """Import an existing photo."""
75
76
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
86
87
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:
117 return
118
119 self.__import_pic_into_db(fname=fnames[0])
120 self.__reload_photo()
121
122
123
125
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
151
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
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
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
195
196 if __name__ == "__main__":
197 app = wx.PyWidgetTester(size = (200, 200))
198 app.SetWidget(cPatientPicture, -1)
199 app.MainLoop()
200
201