Skip to content

Commit

Permalink
feat: added cover editing to metadata editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzheremi2 committed Jan 5, 2025
1 parent 1780b60 commit 5933e56
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 74 deletions.
92 changes: 46 additions & 46 deletions chronograph/ui/SongCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion chronograph/utils/file_mutagen_id3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
54 changes: 27 additions & 27 deletions data/gtk/ui/SongCard.blp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5933e56

Please sign in to comment.