From 125962a7dae2dba54ae62a1b385164fc060a2606 Mon Sep 17 00:00:00 2001 From: Sean Davis Date: Tue, 16 Jul 2013 05:24:21 -0400 Subject: [PATCH] Do not show stock browser if no faces directory. Show error message if camera fails to init. --- mugshot/CameraMugshotDialog.py | 53 ++++++++++++++++++++++++++++------ mugshot/MugshotWindow.py | 4 +++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/mugshot/CameraMugshotDialog.py b/mugshot/CameraMugshotDialog.py index 256b6b6..34a725e 100644 --- a/mugshot/CameraMugshotDialog.py +++ b/mugshot/CameraMugshotDialog.py @@ -39,19 +39,24 @@ class CameraMugshotDialog(CameraDialog): # Pack the video widget into the dialog. vbox = builder.get_object('camera_box') self.video_window = Gtk.DrawingArea() - self.draw_handler = self.video_window.connect('draw', self.on_draw) self.video_window.connect("realize",self.__on_video_window_realized) vbox.pack_start(self.video_window, True, True, 0) self.video_window.show() # Prepare the camerabin element. self.camerabin = Gst.ElementFactory.make("camerabin", "camera-source") - bus = self.camerabin.get_bus() - bus.add_signal_watch() - bus.enable_sync_message_emission() - bus.connect("message", self._on_message) - bus.connect("sync-message::element", self._on_sync_message) - self.realized = False + if self.camerabin: + bus = self.camerabin.get_bus() + bus.add_signal_watch() + bus.enable_sync_message_emission() + bus.connect("message", self._on_message) + bus.connect("sync-message::element", self._on_sync_message) + self.realized = False + self.draw_handler = self.video_window.connect('draw', self.on_draw) + # If the camera fails to load, show an error on the screen. + else: + self.draw_handler = self.video_window.connect('draw', self.on_failed_draw) + self.realized = True # Essential widgets self.record_button = builder.get_object('camera_record') @@ -62,6 +67,34 @@ class CameraMugshotDialog(CameraDialog): self.show_all() + def on_failed_draw(self, widget, ctx): + """Display a message that the camera failed to load.""" + # Get the height and width of the drawing area. + alloc = widget.get_allocation() + height = alloc.height + width = alloc.width + + # Make the background black. + ctx.set_source_rgb(0,0,0) + ctx.paint() + + # Set the font details. + font_size = 20 + font_color = (255,255,255) + font_name = "Sans" + + # Draw the message to the drawing area. + ctx.set_source_rgb(*font_color) + ctx.select_font_face(font_name, cairo.FONT_SLANT_NORMAL, + cairo.FONT_WEIGHT_NORMAL) + ctx.set_font_size(font_size) + ctx.move_to(10,(height-font_size)/2) + # Translators: This string is split for use with cairo. The complete string is "Sorry, but your camera failed to initialize." + ctx.show_text(_("Sorry, but your camera")) + ctx.move_to(10,(height-font_size)/2+font_size) + # Translators: This string is split for use with cairo. The complete string is "Sorry, but your camera failed to initialize." + ctx.show_text(_("failed to initialize.")) + def on_draw(self, widget, ctx): """Display a message that the camera is initializing on first draw. Afterwards, blank the drawing area to clear the message.""" @@ -103,13 +136,15 @@ class CameraMugshotDialog(CameraDialog): if not self.realized: print _("Cannot display web cam output. Ignoring play command") else: - self.camerabin.set_state(Gst.State.PLAYING) + if self.camerabin: + self.camerabin.set_state(Gst.State.PLAYING) def pause(self): """Pause the camera output. It will cause the image to "freeze". Use play() to start the camera playing again. Note that calling pause before play may cause errors on certain camera.""" - self.camerabin.set_state(Gst.State.PAUSED) + if self.camerabin: + self.camerabin.set_state(Gst.State.PAUSED) def take_picture(self, filename): """take_picture - grab a frame from the webcam and save it to diff --git a/mugshot/MugshotWindow.py b/mugshot/MugshotWindow.py index 259f5c9..be794d5 100644 --- a/mugshot/MugshotWindow.py +++ b/mugshot/MugshotWindow.py @@ -40,6 +40,7 @@ home = os.path.expanduser('~') libreoffice_prefs = os.path.join(home, '.config', 'libreoffice', '4', 'user', 'registrymodifications.xcu') pidgin_prefs = os.path.join(home, '.purple', 'prefs.xml') +faces_dir = '/usr/share/pixmaps/faces/' def which(command): '''Use the system command which to get the absolute path for the given @@ -109,6 +110,9 @@ class MugshotWindow(Window): self.image_menu = builder.get_object('image_menu') self.image_menu.attach_to_widget(self.image_button, detach_cb) self.image_from_camera = builder.get_object('image_from_camera') + image_from_browse = builder.get_object('image_from_browse') + image_from_browse.set_visible( os.path.exists(faces_dir) and \ + len(os.listdir(faces_dir)) > 0 ) # Entry widgets (chfn) self.first_name_entry = builder.get_object('first_name')