def _create_preview(self): width = style.zoom(320) height = style.zoom(240) box = hippo.CanvasBox() if len(self._metadata.get('preview', '')) > 4: if self._metadata['preview'][1:4] == 'PNG': preview_data = self._metadata['preview'] else: # TODO: We are close to be able to drop this. import base64 preview_data = base64.b64decode(self._metadata['preview']) png_file = StringIO.StringIO(preview_data) try: surface = cairo.ImageSurface.create_from_png(png_file) has_preview = True except Exception: logging.exception('Error while loading the preview') has_preview = False else: has_preview = False if has_preview: preview_box = hippo.CanvasImage( image=surface, border=style.LINE_WIDTH, border_color=style.COLOR_BUTTON_GREY.get_int(), xalign=hippo.ALIGNMENT_CENTER, yalign=hippo.ALIGNMENT_CENTER, scale_width=width, scale_height=height) else: preview_box = hippo.CanvasText( text=_('No preview'), font_desc=style.FONT_NORMAL.get_pango_desc(), xalign=hippo.ALIGNMENT_CENTER, yalign=hippo.ALIGNMENT_CENTER, border=style.LINE_WIDTH, border_color=style.COLOR_BUTTON_GREY.get_int(), color=style.COLOR_BUTTON_GREY.get_int(), box_width=width, box_height=height) preview_box.connect_after('button-release-event', self._preview_box_button_release_event_cb) box.append(preview_box) return box
def make_audiobox(self, obj, property, width): image_file = os.path.join(Globals.pwd, theme.AUDIO_CHOOSE) if not os.path.exists(image_file): logging.debug('cannot find %s' % image_file) return hippo.CanvasBox() surface = cairo.ImageSurface.create_from_png(image_file) preview_sound = hippo.CanvasImage(image=surface, xalign=hippo.ALIGNMENT_START, yalign=hippo.ALIGNMENT_CENTER) preview_sound.connect('button-press-event', self.__do_clicked_preview_sound, obj, property) if hasattr(obj, property) and getattr(obj, property) != None: sound_name = getattr(obj, property) else: sound_name = _('Click to choose a sound') choose_sound = hippo.CanvasText(text=sound_name, xalign=hippo.ALIGNMENT_START) choose_sound.connect('button-press-event', self.__do_clicked_choose_sound, obj, property) sound_box = RoundBox() sound_box.props.padding = 2 sound_box.props.spacing = 10 sound_box.props.box_width = width sound_box.props.border = theme.BORDER_WIDTH_CONTROL / 2 sound_box.props.border_color = theme.COLOR_DARK_GREEN.get_int() sound_box.props.background_color = theme.COLOR_WHITE.get_int() sound_box.props.orientation = hippo.ORIENTATION_HORIZONTAL sound_box.props.xalign = hippo.ALIGNMENT_START sound_box.set_clickable(True) sound_box.append(preview_sound) sound_box.append(choose_sound) deglitch_box = hippo.CanvasBox(xalign=hippo.ALIGNMENT_START, box_width=width) deglitch_box.append(sound_box) return deglitch_box
class Page(hippo.CanvasBox): def __init__(self, **kwargs): hippo.CanvasBox.__init__(self, **kwargs) self.__alternate_color_listrows = False self.__color_listrow = theme.COLOR_LIST_ROW_ALT.get_int() def append(self, item, *args, **kwargs): hippo.CanvasBox.append(self, item, *args, **kwargs) @property def color_listrow(self): if not self.__alternate_color_listrows: return theme.COLOR_LIST_ROW.get_int() if self.__color_listrow == theme.COLOR_LIST_ROW_ALT.get_int(): self.__color_listrow = theme.COLOR_LIST_ROW.get_int() else: self.__color_listrow = theme.COLOR_LIST_ROW_ALT.get_int() return self.__color_listrow def make_listrow(self, contents=None, *args): list_row = RoundBox() list_row.props.border = 0 # properties not being set properly by constructor list_row.props.padding = theme.DEFAULT_PADDING #list_row.props.padding_right=0 list_row.props.background_color = self.color_listrow if contents is not None: list_row.append(contents, *args) return list_row def make_audiobox(self, obj, property, width): image_file = os.path.join(Globals.pwd, theme.AUDIO_CHOOSE) if not os.path.exists(image_file): logging.debug('cannot find %s' % image_file) return hippo.CanvasBox() surface = cairo.ImageSurface.create_from_png(image_file) preview_sound = hippo.CanvasImage(image=surface, xalign=hippo.ALIGNMENT_START, yalign=hippo.ALIGNMENT_CENTER) preview_sound.connect('button-press-event', self.__do_clicked_preview_sound, obj, property) if hasattr(obj, property) and getattr(obj, property) != None: sound_name = getattr(obj, property) else: sound_name = _('Click to choose a sound') choose_sound = hippo.CanvasText(text=sound_name, xalign=hippo.ALIGNMENT_START) choose_sound.connect('button-press-event', self.__do_clicked_choose_sound, obj, property) sound_box = RoundBox() sound_box.props.padding = 2 sound_box.props.spacing = 10 sound_box.props.box_width = width sound_box.props.border = theme.BORDER_WIDTH_CONTROL / 2 sound_box.props.border_color = theme.COLOR_DARK_GREEN.get_int() sound_box.props.background_color = theme.COLOR_WHITE.get_int() sound_box.props.orientation = hippo.ORIENTATION_HORIZONTAL sound_box.props.xalign = hippo.ALIGNMENT_START sound_box.set_clickable(True) sound_box.append(preview_sound) sound_box.append(choose_sound) deglitch_box = hippo.CanvasBox(xalign=hippo.ALIGNMENT_START, box_width=width) deglitch_box.append(sound_box) return deglitch_box def make_imagebox(self, obj, property=None, width=-1, height=-1, editable=True, padding=0): image = self.__get_property_value(obj, property) if image == '' or image == None: image = theme.IMAGE_CHOOSE file_name = os.path.join(Globals.pwd, image) logging.debug('make_imagebox(%r)' % file_name) # TODO -> handle landscape/portrait properly # load image - could be cleaner on the whole... :) try: if hasattr(obj, 'image_blob') and getattr( obj, 'image_blob') is not None: image_file = StringIO.StringIO(obj.image_blob) surface = cairo.ImageSurface.create_from_png(image_file) else: surface = cairo.ImageSurface.create_from_png(file_name) except Exception, e: logging.error('Error while loading image: %r' % e) # set border if editable: border_width = 0 else: border_width = theme.BORDER_WIDTH_IMAGE # the image itself cover_image = hippo.CanvasImage( image=surface, border=border_width, border_color=theme.COLOR_BLACK.get_int(), xalign=hippo.ALIGNMENT_CENTER, yalign=hippo.ALIGNMENT_CENTER, scale_width=width, scale_height=height) if editable: cover_image.set_clickable(True) cover_image.connect('button-press-event', self.__do_clicked_image, obj, 'image_blob') image_box = RoundBox() image_box.props.padding = 0 image_box.props.spacing = 0 image_box.props.border = theme.BORDER_WIDTH_CONTROL image_box.props.border_color = theme.COLOR_DARK_GREEN.get_int() image_box.append(cover_image) else: image_box = cover_image # Grrr... RoundedBoxes and CanvasImages expand their width to their parent # unless they're in a CanvasBox deglitch_box = hippo.CanvasBox(xalign=hippo.ALIGNMENT_CENTER, padding=padding) deglitch_box.append(image_box) return deglitch_box