forked from trav/lintunes
- Multi-select Get Info keeps the art square; a pasted cover goes into every selected track (shared cover shown, else "mixed artwork"). - Paste Artwork button + Ctrl+V anywhere outside a text field; a caption says what happened. Clipboard reads that don't decode are retried and never staged; every attempt is logged at INFO. - Album art search queries Deezer alongside iTunes and ranks by album match (iTunes has no copy of Digable Planets' Reachin' at all). - embed_artwork shared by Get Info and Download Album Art; single-track Get Info now invalidates the MPRIS art cache.
128 lines
5.0 KiB
Python
128 lines
5.0 KiB
Python
"""Confirmation dialog for downloaded album art.
|
|
|
|
Shows one search result (iTunes or Deezer) at a time (300px preview + "Artist — Album"),
|
|
with Next Result cycling through the other matches. Nothing is written until
|
|
the user accepts; the caller then embeds ``selected_image``/``selected_mime``.
|
|
"""
|
|
from PyQt6.QtWidgets import (
|
|
QDialog, QDialogButtonBox, QLabel, QPushButton, QVBoxLayout,
|
|
)
|
|
from PyQt6.QtCore import Qt
|
|
from PyQt6.QtGui import QPixmap
|
|
|
|
from lintunes.art_search import AlbumArtFetcher
|
|
|
|
PREVIEW_PX = 300
|
|
|
|
|
|
class AlbumArtDialog(QDialog):
|
|
def __init__(self, candidates, track_count: int, album_count: int = 0,
|
|
parent=None):
|
|
super().__init__(parent)
|
|
self.setWindowTitle("Download Album Art")
|
|
self._candidates = candidates
|
|
self._index = 0
|
|
self._fetcher = AlbumArtFetcher()
|
|
self._fetcher.image_finished.connect(self._on_image)
|
|
self.selected_image: bytes | None = None
|
|
self.selected_mime = "image/jpeg"
|
|
# True when the user chose the whole-album button instead of just the
|
|
# selected tracks.
|
|
self.apply_to_album = False
|
|
self._image: bytes | None = None
|
|
self._mime = "image/jpeg"
|
|
|
|
layout = QVBoxLayout(self)
|
|
self._preview = QLabel()
|
|
self._preview.setFixedSize(PREVIEW_PX, PREVIEW_PX)
|
|
self._preview.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
self._preview.setStyleSheet("border: 1px solid palette(mid);")
|
|
layout.addWidget(self._preview, alignment=Qt.AlignmentFlag.AlignHCenter)
|
|
|
|
self._title = QLabel()
|
|
self._title.setAlignment(Qt.AlignmentFlag.AlignHCenter)
|
|
self._title.setWordWrap(True)
|
|
layout.addWidget(self._title)
|
|
|
|
noun = "track" if track_count == 1 else "tracks"
|
|
layout.addWidget(QLabel(f"The artwork will be embedded in "
|
|
f"{track_count} {noun}."),
|
|
alignment=Qt.AlignmentFlag.AlignHCenter)
|
|
|
|
buttons = QDialogButtonBox()
|
|
self._use_btn = QPushButton("Use This Artwork")
|
|
self._use_btn.setDefault(True)
|
|
buttons.addButton(self._use_btn, QDialogButtonBox.ButtonRole.AcceptRole)
|
|
# Offered when the library's album holds more songs than the
|
|
# selection, so one download can cover the whole album.
|
|
self._album_btn = None
|
|
if album_count > track_count:
|
|
self._album_btn = QPushButton(
|
|
f"Use for All {album_count} Songs in Album")
|
|
buttons.addButton(self._album_btn,
|
|
QDialogButtonBox.ButtonRole.ActionRole)
|
|
self._album_btn.clicked.connect(self._accept_album)
|
|
self._next_btn = QPushButton("Next Result")
|
|
self._next_btn.setEnabled(len(candidates) > 1)
|
|
buttons.addButton(self._next_btn, QDialogButtonBox.ButtonRole.ActionRole)
|
|
buttons.addButton(QDialogButtonBox.StandardButton.Cancel)
|
|
buttons.accepted.connect(self._accept_current)
|
|
buttons.rejected.connect(self.reject)
|
|
self._next_btn.clicked.connect(self._next_candidate)
|
|
layout.addWidget(buttons)
|
|
|
|
self._load_current()
|
|
|
|
# ---- candidate cycling ----
|
|
|
|
def _load_current(self):
|
|
candidate = self._candidates[self._index]
|
|
n = len(self._candidates)
|
|
counter = f" ({self._index + 1}/{n})" if n > 1 else ""
|
|
self._title.setText(
|
|
f"<b>{candidate.artist} — {candidate.album}</b>{counter}"
|
|
f"<br><small>via {candidate.source}</small>")
|
|
self._image = None
|
|
self._use_btn.setEnabled(False)
|
|
if self._album_btn is not None:
|
|
self._album_btn.setEnabled(False)
|
|
self._preview.setText("Loading…")
|
|
self._preview.setPixmap(QPixmap())
|
|
self._fetcher.fetch(candidate)
|
|
|
|
def _next_candidate(self):
|
|
self._index = (self._index + 1) % len(self._candidates)
|
|
self._load_current()
|
|
|
|
def _on_image(self, result: dict):
|
|
# Ignore a stale download finishing after Next Result moved on.
|
|
if result.get("candidate") is not self._candidates[self._index]:
|
|
if "candidate" in result:
|
|
return
|
|
if "error" in result:
|
|
self._preview.setText(f"Couldn't load image:\n{result['error']}")
|
|
return
|
|
pixmap = QPixmap()
|
|
if not pixmap.loadFromData(result["image"]):
|
|
self._preview.setText("Couldn't decode image")
|
|
return
|
|
self._image = result["image"]
|
|
self._mime = result["mime"]
|
|
self._preview.setPixmap(pixmap.scaled(
|
|
self._preview.size(), Qt.AspectRatioMode.KeepAspectRatio,
|
|
Qt.TransformationMode.SmoothTransformation))
|
|
self._use_btn.setEnabled(True)
|
|
if self._album_btn is not None:
|
|
self._album_btn.setEnabled(True)
|
|
|
|
def _accept_current(self):
|
|
if self._image is None:
|
|
return
|
|
self.selected_image = self._image
|
|
self.selected_mime = self._mime
|
|
self.accept()
|
|
|
|
def _accept_album(self):
|
|
self.apply_to_album = True
|
|
self._accept_current()
|