def __update_metadata(self): track = get_current_track() if self.__get_status() == "Stopped": self.__metadata = { "mpris:trackid": GLib.Variant("o", "/org/mpris/MediaPlayer2/TrackList/NoTrack") } else: self.__metadata["mpris:trackid"] = self.__track_id track_number = track.number if track_number is None: track_number = 1 self.__metadata["xesam:trackNumber"] = GLib.Variant( "i", track_number) self.__metadata["xesam:title"] = GLib.Variant("s", track.name) self.__metadata["xesam:album"] = GLib.Variant("s", track.book.name) self.__metadata["xesam:artist"] = GLib.Variant( "s", track.book.author) self.__metadata["mpris:length"] = GLib.Variant( "x", track.length * 1000 * 1000) self.__metadata["xesam:url"] = GLib.Variant( "s", "file:///" + track.file) cover_path = "/tmp/cozy_mpris.jpg" pixbuf = artwork_cache.get_cover_pixbuf(track.book) if pixbuf is not None: pixbuf.savev(cover_path, "jpeg", ["quality"], ["90"]) if cover_path is not None: self.__metadata["mpris:artUrl"] = GLib.Variant( "s", "file://" + cover_path)
def __init__(self, book, on_click, scale): super().__init__(on_click, book) self.set_tooltip_text(_("Play this book")) pixbuf = artwork_cache.get_cover_pixbuf(book, scale, BOOK_ICON_SIZE) surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale, None) img = Gtk.Image.new_from_surface(surface) img.set_size_request(BOOK_ICON_SIZE, BOOK_ICON_SIZE) title_label = Gtk.Label() title_label.set_text( tools.shorten_string(self.book.name, MAX_BOOK_LENGTH)) title_label.set_halign(Gtk.Align.START) title_label.props.margin = 4 title_label.props.hexpand = True title_label.props.hexpand_set = True title_label.set_margin_right(5) title_label.props.width_request = 100 title_label.props.xalign = 0.0 title_label.set_line_wrap(True) self.box.add(img) self.box.add(title_label) self.add(self.box) self.show_all()
def set_book(self, book): """ Display the given book in the book overview. """ if self.book and self.book.id == book.id: self.update_time() return self.book = db.Book.get_by_id(book.id) if self.ui.is_playing and self.ui.titlebar.current_book and self.book.id == self.ui.titlebar.current_book.id: self.play_book_button.set_image(self.pause_img) else: self.play_book_button.set_image(self.play_img) self.name_label.set_text(book.name) self.author_label.set_text(book.author) self.update_offline_status() pixbuf = artwork_cache.get_cover_pixbuf( book, self.ui.window.get_scale_factor(), 250) if pixbuf: surface = Gdk.cairo_surface_create_from_pixbuf( pixbuf, self.ui.window.get_scale_factor(), None) self.cover_img.set_from_surface(surface) else: self.cover_img.set_from_icon_name("book-open-variant-symbolic", Gtk.IconSize.DIALOG) self.cover_img.props.pixel_size = 250 self.duration = db.get_book_duration(book) self.speed = self.book.playback_speed self.total_label.set_text( tools.seconds_to_human_readable(self.duration / self.speed)) self.last_played_label.set_text( tools.past_date_to_human_readable(book.last_played)) self.published_label.set_visible(False) self.published_text.set_visible(False) # track list # This box contains all track content self.track_box = Gtk.Box() self.track_box.set_orientation(Gtk.Orientation.VERTICAL) self.track_box.set_halign(Gtk.Align.START) self.track_box.set_valign(Gtk.Align.START) self.track_box.props.margin = 8 for track in db.tracks(book): self.track_box.add(TrackElement(track, self)) tools.remove_all_children(self.track_list_container) self.track_box.show_all() self.track_list_container.add(self.track_box) self._mark_current_track() self.update_time()
def set_book(self, book): if self.book is not None and self.book.id == book.id: self.update_time() return self.book = book if self.ui.is_playing and self.ui.titlebar.current_book is not None and self.book.id == self.ui.titlebar.current_book.id: self.play_book_button.set_image(self.pause_img) else: self.play_book_button.set_image(self.play_img) self.name_label.set_text(book.name) self.author_label.set_text(book.author) pixbuf = artwork_cache.get_cover_pixbuf( book, self.ui.window.get_scale_factor(), 250) surface = Gdk.cairo_surface_create_from_pixbuf( pixbuf, self.ui.window.get_scale_factor(), None) self.cover_img.set_from_surface(surface) self.duration = db.get_book_duration(book) self.speed = db.Book.select().where( db.Book.id == self.book.id).get().playback_speed self.total_label.set_text( tools.seconds_to_human_readable(self.duration / self.speed)) self.last_played_label.set_text( tools.past_date_to_human_readable(book.last_played)) self.published_label.set_visible(False) self.published_text.set_visible(False) # track list # This box contains all track content self.track_box = Gtk.Box() self.track_box.set_orientation(Gtk.Orientation.VERTICAL) self.track_box.set_halign(Gtk.Align.START) self.track_box.set_valign(Gtk.Align.START) self.track_box.props.margin = 8 count = 0 for track in db.tracks(book): self.track_box.add(TrackElement(track, self.ui, self)) count += 1 tools.remove_all_children(self.track_list_container) self.track_box.show_all() self.track_list_container.add(self.track_box) self._mark_current_track() self.update_time()
def update_track_ui(self): # set data of new stream in ui track = player.get_current_track() if track is None: return self.title_label.set_text(track.book.name) self.subtitle_label.set_text(track.name) self.block_ui_buttons(False) self.progress_scale.set_sensitive(True) self.progress_scale.set_visible(True) # only change cover when book has changed if self.current_book is not track.book: self.current_book = track.book if tools.is_elementary(): size = 28 else: size = 40 self.set_title_cover( artwork_cache.get_cover_pixbuf( track.book, self.ui.window.get_scale_factor(), size), size) self.current_remaining = db.get_book_remaining(self.current_book, False) self.current_elapsed = db.get_book_progress(self.current_book, False) self.__update_progress_scale_range() if tools.get_glib_settings().get_boolean("titlebar-remaining-time"): self.progress_scale.set_value(self.current_elapsed / self.ui.speed.get_speed()) else: self.progress_scale.set_value(0) self.update_ui_time(None) self.current_label.set_visible(True) self.remaining_label.set_visible(True)
def __update_track_ui(self): # set data of new stream in ui track = player.get_current_track() self.title_label.set_text(track.book.name) self.subtitle_label.set_text(track.name) self.block_ui_buttons(False, True) self.progress_scale.set_sensitive(True) self.progress_scale.set_visible(True) # only change cover when book has changed if self.current_book is not track.book: self.current_book = track.book if self.is_elementary: size = 28 else: size = 40 self.set_title_cover( artwork_cache.get_cover_pixbuf(track.book, size)) total = player.get_current_track().length self.progress_scale.set_range(0, total) self.progress_scale.set_value(int(track.position / 1000000000)) self.__update_ui_time(None)
def __init__(self, book, size, bordered=False, square=False): """ :param size: the size for the longer side of the image :param bordered: should there be a border around the album art? :param square: should the widget be always a square? """ super().__init__() self.book = book self.selected = False self.signal_ids = [] self.play_signal_ids = [] # scale the book cover to a fix size. pixbuf = artwork_cache.get_cover_pixbuf(book, size) # box is the main container for the album art self.set_halign(Gtk.Align.CENTER) self.set_valign(Gtk.Align.CENTER) # connect mouse events to the event box self.signal_ids.append( self.connect("enter-notify-event", self._on_enter_notify)) self.signal_ids.append( self.connect("leave-notify-event", self._on_leave_notify)) # img contains the album art img = Gtk.Image() img.set_halign(Gtk.Align.CENTER) img.set_valign(Gtk.Align.CENTER) if bordered: img.get_style_context().add_class("bordered") if square: img.set_size_request(size, size) img.set_from_pixbuf(pixbuf) self.play_box = Gtk.EventBox() # we want to change the mouse cursor if the user is hovering over the play button self.play_signal_ids.append( self.play_box.connect("enter-notify-event", self._on_play_enter_notify)) self.play_signal_ids.append( self.play_box.connect("leave-notify-event", self._on_play_leave_notify)) # on click we want to play the audio book self.play_signal_ids.append( self.play_box.connect("button-press-event", self._on_play_button_press)) self.play_box.set_property("halign", Gtk.Align.CENTER) self.play_box.set_property("valign", Gtk.Align.CENTER) self.play_box.set_tooltip_text(_("Play this book")) # play_color is an overlay for the play button # with this it should be visible on any album art color play_image = GdkPixbuf.Pixbuf.new_from_resource( "/de/geigi/cozy/play_background.svg") if square: play_image = play_image.scale_simple(size - 10, size - 10, GdkPixbuf.InterpType.BILINEAR) self.play_button = Gtk.Image.new_from_pixbuf(play_image) self.play_button.set_property("halign", Gtk.Align.CENTER) self.play_button.set_property("valign", Gtk.Align.CENTER) # this is the main overlay for the album art # we need to create field for the overlays # to change the opacity of them on mouse over/leave events self.overlay = Gtk.Overlay.new() # this is the play symbol overlay self.play_overlay = Gtk.Overlay.new() # this is for the play button animation self.play_revealer = Gtk.Revealer() self.play_revealer.set_transition_type( Gtk.RevealerTransitionType.CROSSFADE) self.play_revealer.set_transition_duration(300) self.play_revealer.add(self.play_overlay) # this grid has a background color to act as a visible overlay color = Gtk.Grid() color.set_property("halign", Gtk.Align.CENTER) color.set_property("valign", Gtk.Align.CENTER) # assemble play overlay self.play_box.add(self.play_button) self.play_overlay.add(self.play_box) # assemble overlay with album art self.overlay.add(img) self.overlay.add_overlay(self.play_revealer) # assemble overlay color color.add(self.overlay) self.add(color)
def __init__(self, book, size, scale, bordered=False, square=False): """ :param size: the size for the longer side of the image :param bordered: should there be a border around the album art? :param square: should the widget be always a square? """ super().__init__() self.props.height_request = size self.book = book self.selected = False self.signal_ids = [] self.play_signal_ids = [] # the event box is used for mouse enter and leave signals self.event_box = Gtk.EventBox() self.event_box.set_property("halign", Gtk.Align.CENTER) self.event_box.set_property("valign", Gtk.Align.CENTER) # scale the book cover to a fix size. pixbuf = artwork_cache.get_cover_pixbuf(book, scale, size) # box is the main container for the album art self.set_halign(Gtk.Align.CENTER) self.set_valign(Gtk.Align.CENTER) # img contains the album art img = Gtk.Image() img.set_halign(Gtk.Align.CENTER) img.set_valign(Gtk.Align.CENTER) if pixbuf: if bordered: img.get_style_context().add_class("bordered") surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale, None) img.set_from_surface(surface) else: img.set_from_icon_name("book-open-variant-symbolic", Gtk.IconSize.DIALOG) img.props.pixel_size = size self.play_box = Gtk.EventBox() # on click we want to play the audio book self.play_signal_ids.append( self.play_box.connect("button-press-event", self._on_play_button_press)) self.play_box.set_property("halign", Gtk.Align.CENTER) self.play_box.set_property("valign", Gtk.Align.CENTER) self.play_box.set_tooltip_text(_("Play this book")) # play_color is an overlay for the play button # with this it should be visible on any album art color play_image = GdkPixbuf.Pixbuf.new_from_resource( "/de/geigi/cozy/play_background.svg") if square: play_image = play_image.scale_simple(size - 10, size - 10, GdkPixbuf.InterpType.BILINEAR) if size < 100: self.icon_size = Gtk.IconSize.LARGE_TOOLBAR else: self.icon_size = Gtk.IconSize.DIALOG self.play_button = Gtk.Image.new_from_icon_name( "media-playback-start-symbolic", self.icon_size) self.play_button.set_property("halign", Gtk.Align.CENTER) self.play_button.set_property("valign", Gtk.Align.CENTER) self.play_button.get_style_context().add_class("white") # this is the main overlay for the album art # we need to create field for the overlays # to change the opacity of them on mouse over/leave events self.overlay = Gtk.Overlay.new() # this is the play symbol overlay self.play_overlay = Gtk.Overlay.new() # this is for the play button animation self.play_revealer = Gtk.Revealer() self.play_revealer.set_transition_type( Gtk.RevealerTransitionType.CROSSFADE) self.play_revealer.set_transition_duration(300) self.play_revealer.add(self.play_overlay) self.play_revealer.add_events(Gdk.EventMask.ENTER_NOTIFY_MASK) self.play_revealer.add_events(Gdk.EventMask.LEAVE_NOTIFY_MASK) # this grid has a background color to act as a visible overlay color = Gtk.Grid() color.set_property("halign", Gtk.Align.CENTER) color.set_property("valign", Gtk.Align.CENTER) if square: self.set_size_request(size, size) smaller_width = size - pixbuf.get_width() if smaller_width > 1: self.event_box.set_margin_left(smaller_width / 2) # assemble play overlay self.play_box.add(self.play_button) self.play_overlay.add(self.play_box) # assemble overlay with album art self.overlay.add(img) self.overlay.add_overlay(self.play_revealer) # assemble overlay color color.add(self.overlay) self.event_box.add(color) self.add(self.event_box) # connect signals self.play_signal_ids.append( self.play_box.connect("enter-notify-event", self._on_play_enter_notify)) self.play_signal_ids.append( self.play_box.connect("leave-notify-event", self._on_play_leave_notify)) # connect mouse events to the event box self.signal_ids.append( self.event_box.connect("enter-notify-event", self._on_enter_notify)) self.signal_ids.append( self.event_box.connect("leave-notify-event", self._on_leave_notify))