fix album art scaling on album art change with QGraphicsPixmapItem

This commit is contained in:
billypom on debian 2024-02-28 19:26:21 -05:00
parent 8b77bd7fee
commit 6cb1f79bf5
3 changed files with 39 additions and 9 deletions

37
main.py
View File

@ -1,8 +1,8 @@
import DBA
from ui import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QHeaderView
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QHeaderView, QGraphicsPixmapItem
import qdarktheme
from PyQt5.QtCore import QUrl, QTimer, QEvent, Qt, QRect
from PyQt5.QtCore import QUrl, QTimer, QEvent, Qt
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QAudioProbe
from PyQt5.QtGui import QPixmap
from utils import scan_for_music
@ -76,7 +76,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.actionClearDatabase.triggered.connect(self.clear_database) # Clear database
## tableView
# self.tableView.clicked.connect(self.set_clicked_cell_filepath)
self.tableView.doubleClicked.connect(self.play_audio_file) # Double click to play song
self.tableView.doubleClicked.connect(self.play_audio_file) # Listens for the double click event, and plays the song
self.tableView.enterKey.connect(self.play_audio_file) # Press Enter to play song
self.tableView.playPauseSignal.connect(self.on_play_clicked) # Spacebar toggle playpause signal
self.tableView.viewport().installEventFilter(self) # for drag & drop functionality
@ -124,7 +124,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
super().closeEvent(event)
def play_audio_file(self):
"""Start playback of selected track & moves playback slider"""
"""Start playback of tableView.current_song_filepath track & moves playback slider"""
self.current_song_metadata = self.tableView.get_current_song_metadata() # get metadata
self.current_song_album_art = self.tableView.get_current_song_album_art()
url = QUrl.fromLocalFile(self.tableView.get_current_song_filepath()) # read the file
@ -149,17 +149,36 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
def load_album_art(self, album_art_data):
"""Sets the album art for the currently playing track"""
if self.current_song_album_art:
# Clear the scene
self.album_art_scene.clear()
# Reset the scene
self.albumGraphicsView.setScene(None)
# Create pixmap for album art
pixmap = QPixmap()
pixmap.loadFromData(self.current_song_album_art)
self.album_art_scene.addPixmap(pixmap)
# Reset the scene
self.albumGraphicsView.setScene(None)
# Create a QGraphicsPixmapItem for more control over pic
pixmapItem = QGraphicsPixmapItem(pixmap)
pixmapItem.setTransformationMode(Qt.SmoothTransformation) # For better quality scaling
# Add pixmap item to the scene
self.album_art_scene.addItem(pixmapItem)
# Set the scene
self.albumGraphicsView.setScene(self.album_art_scene)
# Put artwork in the scene, fit to graphics view widget
self.albumGraphicsView.fitInView(self.album_art_scene.sceneRect(), Qt.KeepAspectRatio)
# Adjust the album art scaling
self.adjustPixmapScaling(pixmapItem)
def adjustPixmapScaling(self, pixmapItem):
"""Adjust the scaling of the pixmap item to fit the QGraphicsView, maintaining aspect ratio"""
viewWidth = self.albumGraphicsView.width()
viewHeight = self.albumGraphicsView.height()
pixmapSize = pixmapItem.pixmap().size()
# Calculate scaling factor while maintaining aspect ratio
scaleX = viewWidth / pixmapSize.width()
scaleY = viewHeight / pixmapSize.height()
scaleFactor = min(scaleX, scaleY)
# Apply scaling to the pixmap item
pixmapItem.setScale(scaleFactor)
def update_audio_visualization(self):
"""Handles upading points on the pyqtgraph visual"""

2
ui.py
View File

@ -32,10 +32,12 @@ class Ui_MainWindow(object):
sizePolicy.setHeightForWidth(self.albumGraphicsView.sizePolicy().hasHeightForWidth())
self.albumGraphicsView.setSizePolicy(sizePolicy)
self.albumGraphicsView.setMinimumSize(QtCore.QSize(200, 200))
self.albumGraphicsView.setMaximumSize(QtCore.QSize(16777215, 16777215))
self.albumGraphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.albumGraphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.albumGraphicsView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
self.albumGraphicsView.setInteractive(False)
self.albumGraphicsView.setResizeAnchor(QtWidgets.QGraphicsView.AnchorViewCenter)
self.albumGraphicsView.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
self.albumGraphicsView.setObjectName("albumGraphicsView")
self.vlayoutAlbumArt.addWidget(self.albumGraphicsView)

9
ui.ui
View File

@ -39,6 +39,12 @@
<height>200</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
@ -51,6 +57,9 @@
<property name="interactive">
<bool>false</bool>
</property>
<property name="resizeAnchor">
<enum>QGraphicsView::AnchorViewCenter</enum>
</property>
<property name="viewportUpdateMode">
<enum>QGraphicsView::FullViewportUpdate</enum>
</property>