-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayoutA.py
182 lines (145 loc) · 6.48 KB
/
layoutA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from PySide6 import QtWidgets, QtCore, QtGui
from components.titlebar import TitleBar
from components.songinfo import SongInfoWidget
from components.playerctrl import PlayerControlWidget
from qframelesswindow import TitleBarBase
import os
import requests
from urllib.parse import urlparse, unquote
import hashlib
class LayoutA(TitleBarBase):
def __init__(self, parent):
super(LayoutA, self).__init__(parent)
self.parent = parent
self.maxBtn.hide()
self.closeBtn.hide()
self.minBtn.hide()
self.setFixedHeight(120)
# Main layout
self.background_widget = QtWidgets.QWidget(self)
self.background_widget.setObjectName("background-widget")
self.main_layout = QtWidgets.QHBoxLayout(self)
self.main_layout.setContentsMargins(
10, 10, 10, 10
) # Required ... Does horrible things when turned off
# Cover image label
self.cover_image = QtWidgets.QLabel()
self.cover_image.setFixedSize(80, 80)
self.cover_image.setObjectName("cover-image")
# Right-side layout
self.right_layout = QtWidgets.QVBoxLayout()
self.right_layout.setContentsMargins(5, 5, 5, 5)
# TitleBar widget
self.title_bar = TitleBar(self.parent, "Song Title")
self.title_bar.setObjectName("title-bar")
self.right_layout.addWidget(self.title_bar)
# Info and control layout
self.info_control_layout = QtWidgets.QHBoxLayout()
self.info_control_layout.setContentsMargins(0, 0, 0, 0)
# SongInfoWidget
self.song_info = SongInfoWidget("Song Title", "Author Name")
self.song_info.setObjectName("song-info")
self.info_control_layout.addWidget(self.song_info)
# PlayerControlWidget
self.player_control = PlayerControlWidget()
self.player_control.setObjectName("player-control")
self.info_control_layout.addWidget(self.player_control)
self.right_layout.addStretch()
# Add info and control layout to right-side layout
self.right_layout.addLayout(self.info_control_layout)
# Add cover image and right-side layout to main layout
self.main_layout.addWidget(self.cover_image)
self.main_layout.addLayout(self.right_layout)
# Set main layout
# self.setLayout(self.main_layout)
self.background_widget.setLayout(self.main_layout)
self.setLayout(QtWidgets.QHBoxLayout(self))
self.layout().addWidget(self.background_widget)
def assign_actions(self, action_worker):
self.action_worker = action_worker
self.player_control.play_button.clicked.connect(
lambda: self.action_worker.playpause()
)
self.player_control.next_button.clicked.connect(
lambda: self.action_worker.next()
)
self.player_control.prev_button.clicked.connect(
lambda: self.action_worker.previous()
)
def get_coverimage_url(self, url, cache_dir="cache"):
try:
parsed_url = urlparse(url)
# If it's already a file URL or local path
if parsed_url.scheme == "file" or not parsed_url.scheme:
path = unquote(parsed_url.path)
if os.name == "nt" and path.startswith("/"):
path = path[1:]
return path
# For http/https URLs, download and cache
elif parsed_url.scheme in ["http", "https"]:
# Create cache directory if it doesn't exist
os.makedirs(cache_dir, exist_ok=True)
# Create a unique filename using URL hash
url_hash = hashlib.md5(url.encode()).hexdigest()
# Get file extension from URL or default to .jpg
ext = os.path.splitext(parsed_url.path)[1]
if not ext:
ext = ".jpg"
cache_path = os.path.join(cache_dir, f"{url_hash}{ext}")
# If not already cached, download the file
if not os.path.exists(cache_path):
response = requests.get(url, stream=True)
response.raise_for_status() # Raise exception for bad status codes
with open(cache_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return cache_path
else:
raise ValueError(f"URL scheme '{parsed_url.scheme}' is not supported")
except Exception as e:
print(f"Error processing URL for cover image {url}: {str(e)}")
return None
def setCoverImage(self, path):
path = self.get_coverimage_url(path)
pixmap = QtGui.QPixmap(path)
if not pixmap.isNull():
label_size = self.cover_image.size()
# Scale the pixmap to cover the label while maintaining aspect ratio
scaled_pixmap = pixmap.scaled(
label_size,
QtCore.Qt.KeepAspectRatioByExpanding,
QtCore.Qt.SmoothTransformation,
)
# Calculate the cropping rectangle
x_offset = (scaled_pixmap.width() - label_size.width()) // 2
y_offset = (scaled_pixmap.height() - label_size.height()) // 2
rect = QtCore.QRect(
x_offset, y_offset, label_size.width(), label_size.height()
)
# Crop the scaled pixmap to fit the label
cropped_pixmap = scaled_pixmap.copy(rect)
# Create a rounded mask
mask = QtGui.QPixmap(cropped_pixmap.size())
mask.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(mask)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
path = QtGui.QPainterPath()
path.addRoundedRect(
0, 0, cropped_pixmap.width(), cropped_pixmap.height(), 4, 4
)
painter.setClipPath(path)
painter.drawPixmap(0, 0, cropped_pixmap)
painter.end()
# Set the resulting pixmap on the label
self.cover_image.setPixmap(mask)
def setSongInfo(self, title, author):
self.song_info.setInfo(title, author)
def setSource(self, source):
self.title_bar.setSource(source)
def setPlayPauseStatus(self, status):
self.player_control.setPlayPauseStatus(status)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
mainWidget = LayoutA(QtWidgets.QWidget())
mainWidget.show()
app.exec()