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
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
39 """A patient picture control ready for display.
40 with popup menu to import/export
41 remove or Acquire from a device
42 """
55
56
57
59
60 self.Bind(wx.EVT_RIGHT_UP, self._on_RightClick_photo)
61
62
63 gmDispatcher.connect(receiver=self._on_post_patient_selection, signal = 'post_patient_selection')
64
67
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
76
78 """Import an existing photo."""
79
80
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
90
91
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:
121 return
122
123 self.__import_pic_into_db(fname=fnames[0])
124 self.__reload_photo()
125
126
127
129
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
163
164
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
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
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 Exception:
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
208
209 if __name__ == "__main__":
210 app = wx.PyWidgetTester(size = (200, 200))
211 app.SetWidget(cPatientPicture, -1)
212 app.MainLoop()
213
214