diff --git a/chronograph/ui/SongCard.py b/chronograph/ui/SongCard.py index d19446f..82d81a6 100644 --- a/chronograph/ui/SongCard.py +++ b/chronograph/ui/SongCard.py @@ -2,7 +2,7 @@ import pathlib from typing import Union -from gi.repository import Adw, Gdk, GObject, Gtk # type: ignore +from gi.repository import Adw, Gdk, Gio, GObject, Gtk # type: ignore from chronograph import shared from chronograph.ui.BoxDialog import BoxDialog @@ -40,8 +40,8 @@ class SongCard(Gtk.Box): # Metadata editor metadata_editor: Adw.Dialog = Gtk.Template.Child() - # metadata_editor_cover_button: Gtk.MenuButton = Gtk.Template.Child() - # metadata_editor_cover_image: Gtk.Image = Gtk.Template.Child() + metadata_editor_cover_button: Gtk.MenuButton = Gtk.Template.Child() + metadata_editor_cover_image: Gtk.Image = Gtk.Template.Child() metadata_editor_title_row: Adw.EntryRow = Gtk.Template.Child() metadata_editor_artist_row: Adw.EntryRow = Gtk.Template.Child() metadata_editor_album_row: Adw.EntryRow = Gtk.Template.Child() @@ -82,20 +82,22 @@ def __init__(self, file: Union[FileID3, FileVorbis]) -> None: self.play_button.connect("clicked", self.on_play_button_clicked) self.bind_props() self.invalidate_cover(self.cover_img) - # TODO Until better times - # self.metadata_editor_cover_button.install_action( - # "card.change", None, self.metadata_change_cover - # ) - # self.metadata_editor_cover_button.install_action( - # "card.remove", None, self.metadata_remove_cover - # ) + + actions: Gio.SimpleActionGroup = Gio.SimpleActionGroup.new() + change_action: Gio.SimpleAction = Gio.SimpleAction.new("change", None) + change_action.connect("activate", self.metadata_change_cover) + remove_action: Gio.SimpleAction = Gio.SimpleAction.new("remove", None) + remove_action.connect("activate", self.metadata_remove_cover) + actions.add_action(change_action) + actions.add_action(remove_action) + self.metadata_editor_cover_button.insert_action_group("card", actions) def open_metadata_editor(self, *_args) -> None: - # TODO Until better times - # if (texture := self._file.get_cover_texture()) != "icon": - # self.metadata_editor_cover_image.set_from_paintable(texture) - # else: - # self.metadata_editor_cover_image.set_from_icon_name("note-placeholder") + + if (texture := self._file.get_cover_texture()) != "icon": + self.metadata_editor_cover_image.set_from_paintable(texture) + else: + self.metadata_editor_cover_image.set_from_icon_name("note-placeholder") ( self.metadata_editor_title_row.set_text(self.title) if self.title != "Unknоwn" @@ -114,17 +116,17 @@ def open_metadata_editor(self, *_args) -> None: self.metadata_editor.present(shared.win) def metadata_editor_save(self, *_args) -> None: - # TODO Until better times - # if ( - # self._file._cover_updated - # and self._mde_new_cover_path != "" - # and self._mde_new_cover_path is not None - # ): - # self._file.set_cover(self._mde_new_cover_path) - # self._file._cover_updated = False - # elif (self._file._cover_updated) and (self._mde_new_cover_path is None): - # self._file.set_cover(None) - # self._file._cover_updated = False + + if ( + self._file._cover_updated + and self._mde_new_cover_path != "" + and self._mde_new_cover_path is not None + ): + self._file.set_cover(self._mde_new_cover_path) + self._file._cover_updated = False + elif (self._file._cover_updated) and (self._mde_new_cover_path is None): + self._file.set_cover(None) + self._file._cover_updated = False if (title_data := self.metadata_editor_title_row.get_text()) != self.title: self._file.set_str_data("TIT2", title_data) @@ -134,30 +136,28 @@ def metadata_editor_save(self, *_args) -> None: self._file.set_str_data("TALB", album_data) self._file.save() - # TODO Until better times - # self.invalidate_cover(self.cover_img) + self.invalidate_cover(self.cover_img) self.invalidate_update("title") self.invalidate_update("artist") self.metadata_editor.close() - # TODO Until better times - # def metadata_change_cover(self, *_args) -> None: - # dialog = Gtk.FileDialog( - # default_filter=Gtk.FileFilter(mime_types=["image/png", "image/jpeg"]) - # ) - # dialog.open(shared.win, None, self.on_metadata_change_cover) - - # def on_metadata_change_cover(self, file_dialog: Gtk.FileDialog, result) -> None: - # self._mde_new_cover_path = file_dialog.open_finish(result).get_path() - # self._file._cover_updated = True - # self.metadata_editor_cover_image.set_from_paintable( - # Gdk.Texture.new_from_filename(self._mde_new_cover_path) - # ) - - # def metadata_remove_cover(self, *_args) -> None: - # self._mde_new_cover_path = None - # self._file._cover_updated = True - # self.metadata_editor_cover_image.set_from_icon_name("note-placeholder") + def metadata_change_cover(self, *_args) -> None: + dialog = Gtk.FileDialog( + default_filter=Gtk.FileFilter(mime_types=["image/png", "image/jpeg"]) + ) + dialog.open(shared.win, None, self.on_metadata_change_cover) + + def on_metadata_change_cover(self, file_dialog: Gtk.FileDialog, result) -> None: + self._mde_new_cover_path = file_dialog.open_finish(result).get_path() + self._file._cover_updated = True + self.metadata_editor_cover_image.set_from_paintable( + Gdk.Texture.new_from_filename(self._mde_new_cover_path) + ) + + def metadata_remove_cover(self, *_args) -> None: + self._mde_new_cover_path = None + self._file._cover_updated = True + self.metadata_editor_cover_image.set_from_icon_name("note-placeholder") def on_metadata_editor_close(self, *_args) -> None: self._file._cover_updated = False diff --git a/chronograph/utils/file_mutagen_id3.py b/chronograph/utils/file_mutagen_id3.py index e649039..f9d9a73 100644 --- a/chronograph/utils/file_mutagen_id3.py +++ b/chronograph/utils/file_mutagen_id3.py @@ -67,7 +67,6 @@ def set_cover(self, img_path: Union[str, None]) -> None: img_path : str | None path to image or None if cover should be deleted """ - print("Test") if img_path is not None: self._cover = open(img_path, "rb").read() if self._mutagen_file.tags: diff --git a/data/gtk/ui/SongCard.blp b/data/gtk/ui/SongCard.blp index d47a819..0c9e17e 100644 --- a/data/gtk/ui/SongCard.blp +++ b/data/gtk/ui/SongCard.blp @@ -38,33 +38,33 @@ Adw.Dialog metadata_editor { spacing: 8; hexpand: true; - // Adw.Clamp { - // orientation: horizontal; - // maximum-size: 160; - - // MenuButton metadata_editor_cover_button { - // tooltip-text: _("Change cover"); - // overflow: hidden; - // has-frame: false; - // menu-model: cover_action; - - // Box { - // orientation: vertical; - - // Image metadata_editor_cover_image { - // width-request: 160; - // height-request: 160; - // hexpand: true; - // vexpand: true; - // overflow: hidden; - // icon-name: "note-placeholder"; - // pixel-size: 160; - - // styles ["rounded"] - // } - // } - // } - // } + Adw.Clamp { + orientation: horizontal; + maximum-size: 160; + + MenuButton metadata_editor_cover_button { + tooltip-text: _("Change cover"); + overflow: hidden; + has-frame: false; + menu-model: cover_action; + + Box { + orientation: vertical; + + Image metadata_editor_cover_image { + width-request: 160; + height-request: 160; + hexpand: true; + vexpand: true; + overflow: hidden; + icon-name: "note-placeholder"; + pixel-size: 160; + + styles ["rounded"] + } + } + } + } ListBox metadata_editor_props { selection-mode: none;