fix album art scaling on album art change with QGraphicsPixmapItem
This commit is contained in:
parent
8b77bd7fee
commit
6cb1f79bf5
37
main.py
37
main.py
@ -1,8 +1,8 @@
|
|||||||
import DBA
|
import DBA
|
||||||
from ui import Ui_MainWindow
|
from ui import Ui_MainWindow
|
||||||
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QHeaderView
|
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QHeaderView, QGraphicsPixmapItem
|
||||||
import qdarktheme
|
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.QtMultimedia import QMediaPlayer, QMediaContent, QAudioProbe
|
||||||
from PyQt5.QtGui import QPixmap
|
from PyQt5.QtGui import QPixmap
|
||||||
from utils import scan_for_music
|
from utils import scan_for_music
|
||||||
@ -76,7 +76,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
|
|||||||
self.actionClearDatabase.triggered.connect(self.clear_database) # Clear database
|
self.actionClearDatabase.triggered.connect(self.clear_database) # Clear database
|
||||||
## tableView
|
## tableView
|
||||||
# self.tableView.clicked.connect(self.set_clicked_cell_filepath)
|
# 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.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.playPauseSignal.connect(self.on_play_clicked) # Spacebar toggle playpause signal
|
||||||
self.tableView.viewport().installEventFilter(self) # for drag & drop functionality
|
self.tableView.viewport().installEventFilter(self) # for drag & drop functionality
|
||||||
@ -124,7 +124,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
|
|||||||
super().closeEvent(event)
|
super().closeEvent(event)
|
||||||
|
|
||||||
def play_audio_file(self):
|
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_metadata = self.tableView.get_current_song_metadata() # get metadata
|
||||||
self.current_song_album_art = self.tableView.get_current_song_album_art()
|
self.current_song_album_art = self.tableView.get_current_song_album_art()
|
||||||
url = QUrl.fromLocalFile(self.tableView.get_current_song_filepath()) # read the file
|
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):
|
def load_album_art(self, album_art_data):
|
||||||
"""Sets the album art for the currently playing track"""
|
"""Sets the album art for the currently playing track"""
|
||||||
if self.current_song_album_art:
|
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
|
# Create pixmap for album art
|
||||||
pixmap = QPixmap()
|
pixmap = QPixmap()
|
||||||
pixmap.loadFromData(self.current_song_album_art)
|
pixmap.loadFromData(self.current_song_album_art)
|
||||||
self.album_art_scene.addPixmap(pixmap)
|
# Create a QGraphicsPixmapItem for more control over pic
|
||||||
# Reset the scene
|
pixmapItem = QGraphicsPixmapItem(pixmap)
|
||||||
self.albumGraphicsView.setScene(None)
|
pixmapItem.setTransformationMode(Qt.SmoothTransformation) # For better quality scaling
|
||||||
|
# Add pixmap item to the scene
|
||||||
|
self.album_art_scene.addItem(pixmapItem)
|
||||||
# Set the scene
|
# Set the scene
|
||||||
self.albumGraphicsView.setScene(self.album_art_scene)
|
self.albumGraphicsView.setScene(self.album_art_scene)
|
||||||
# Put artwork in the scene, fit to graphics view widget
|
# Adjust the album art scaling
|
||||||
self.albumGraphicsView.fitInView(self.album_art_scene.sceneRect(), Qt.KeepAspectRatio)
|
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):
|
def update_audio_visualization(self):
|
||||||
"""Handles upading points on the pyqtgraph visual"""
|
"""Handles upading points on the pyqtgraph visual"""
|
||||||
|
|||||||
2
ui.py
2
ui.py
@ -32,10 +32,12 @@ class Ui_MainWindow(object):
|
|||||||
sizePolicy.setHeightForWidth(self.albumGraphicsView.sizePolicy().hasHeightForWidth())
|
sizePolicy.setHeightForWidth(self.albumGraphicsView.sizePolicy().hasHeightForWidth())
|
||||||
self.albumGraphicsView.setSizePolicy(sizePolicy)
|
self.albumGraphicsView.setSizePolicy(sizePolicy)
|
||||||
self.albumGraphicsView.setMinimumSize(QtCore.QSize(200, 200))
|
self.albumGraphicsView.setMinimumSize(QtCore.QSize(200, 200))
|
||||||
|
self.albumGraphicsView.setMaximumSize(QtCore.QSize(16777215, 16777215))
|
||||||
self.albumGraphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
|
self.albumGraphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
|
||||||
self.albumGraphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
|
self.albumGraphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
|
||||||
self.albumGraphicsView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
|
self.albumGraphicsView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
|
||||||
self.albumGraphicsView.setInteractive(False)
|
self.albumGraphicsView.setInteractive(False)
|
||||||
|
self.albumGraphicsView.setResizeAnchor(QtWidgets.QGraphicsView.AnchorViewCenter)
|
||||||
self.albumGraphicsView.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
|
self.albumGraphicsView.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
|
||||||
self.albumGraphicsView.setObjectName("albumGraphicsView")
|
self.albumGraphicsView.setObjectName("albumGraphicsView")
|
||||||
self.vlayoutAlbumArt.addWidget(self.albumGraphicsView)
|
self.vlayoutAlbumArt.addWidget(self.albumGraphicsView)
|
||||||
|
|||||||
9
ui.ui
9
ui.ui
@ -39,6 +39,12 @@
|
|||||||
<height>200</height>
|
<height>200</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="maximumSize">
|
||||||
|
<size>
|
||||||
|
<width>16777215</width>
|
||||||
|
<height>16777215</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
<property name="verticalScrollBarPolicy">
|
<property name="verticalScrollBarPolicy">
|
||||||
<enum>Qt::ScrollBarAlwaysOff</enum>
|
<enum>Qt::ScrollBarAlwaysOff</enum>
|
||||||
</property>
|
</property>
|
||||||
@ -51,6 +57,9 @@
|
|||||||
<property name="interactive">
|
<property name="interactive">
|
||||||
<bool>false</bool>
|
<bool>false</bool>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="resizeAnchor">
|
||||||
|
<enum>QGraphicsView::AnchorViewCenter</enum>
|
||||||
|
</property>
|
||||||
<property name="viewportUpdateMode">
|
<property name="viewportUpdateMode">
|
||||||
<enum>QGraphicsView::FullViewportUpdate</enum>
|
<enum>QGraphicsView::FullViewportUpdate</enum>
|
||||||
</property>
|
</property>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user