Skip to content

Commit

Permalink
gui: draw vip tag if a song needs vip privilege (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven authored Feb 20, 2024
1 parent e172227 commit 7307151
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
33 changes: 31 additions & 2 deletions feeluown/gui/widgets/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

from feeluown.utils import aio
from feeluown.utils.dispatch import Signal
from feeluown.library import ModelState
from feeluown.library import ModelState, ModelFlags, MediaFlags

from feeluown.gui.mimedata import ModelMimeData
from feeluown.gui.helpers import ItemViewNoScrollMixin, ReaderFetchMoreMixin
from feeluown.gui.helpers import (
ItemViewNoScrollMixin, ReaderFetchMoreMixin, painter_save
)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -468,6 +470,9 @@ def paint(self, painter, option, index):
painter.setRenderHint(QPainter.Antialiasing)
hovered = index.row() == self.row_hovered

if index.column() == Column.song:
self.paint_vip_tag(painter, option, index)

# Draw play button on Column.index when the row is hovered.
if hovered and index.column() == Column.index:
painter.save()
Expand Down Expand Up @@ -509,6 +514,30 @@ def paint(self, painter, option, index):
painter.drawRect(option.rect)
painter.restore()

def paint_vip_tag(self, painter, option, index):
song = index.data(Qt.UserRole)
if ModelFlags.normal in song.meta.flags and MediaFlags.vip in song.media_flags:
with painter_save(painter):
fm = option.fontMetrics
title = index.data(Qt.DisplayRole)
title_rect = fm.boundingRect(title)
if title_rect.width() < option.rect.width():
# Tested on (KDE and macOS):
# when font size is 7px, text width~>16 & height~>10
font = option.font
font.setPixelSize(7)
painter.setFont(font)
text_width, text_height = 16, 10
y = option.rect.y() + (option.rect.height() - text_height) // 2
# NOTE(cosven): On macOS, the acture width of text is large than
# title_rect.width(), which is also true on KDE. This is decided
# by QStyle. +10px works well on macOS, and it also works well
# on KDE (actually, from local test, 7px is enough for KDE).
x = option.rect.x() + title_rect.width() + 10
text_rect = QRect(x, y, text_width, text_height)
painter.drawRoundedRect(text_rect, 3, 3)
painter.drawText(text_rect, Qt.AlignCenter, 'VIP')

def sizeHint(self, option, index):
"""set proper width for each column
Expand Down
2 changes: 1 addition & 1 deletion feeluown/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
LyricModel, VideoModel, BriefVideoModel, \
ArtistModel, AlbumModel, PlaylistModel, BriefPlaylistModel, \
fmt_artists_names, AlbumType, SimpleSearchResult, \
get_modelcls_by_type, \
get_modelcls_by_type, MediaFlags, \
V2SupportedModelTypes
from .excs import NoUserLoggedIn, ModelNotFound, \
ProviderAlreadyExists, ResourceNotFound, MediaNotFound
Expand Down
9 changes: 9 additions & 0 deletions feeluown/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class ModelType(IntEnum):
none = 128


class MediaFlags(IntFlag):
not_sure = 0b10000000 # 不确定
not_exists = 0b00 # 不存在相关资源
sample = 0b01 # 存在试听片段
free = 0b10 # 存在免费的完整资源
vip = 0b100 # 存在资源,不免费,vip 可用
pay = 0b1000 # 存在资源,不免费,付费可用


class SearchType(Enum):
pl = 'playlist'
al = 'album'
Expand Down
3 changes: 2 additions & 1 deletion feeluown/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
pydantic_version = 1

from feeluown.utils.utils import elfhash
from .base import ModelType, ModelFlags, AlbumType
from .base import ModelType, ModelFlags, AlbumType, MediaFlags
from .base import SearchType # noqa
from .model_state import ModelState

Expand Down Expand Up @@ -297,6 +297,7 @@ class SongModel(BriefSongModel, BaseNormalModel):
# can be fetched in get_song_detail API. So one IO request can be saved
# to fetch a image url of the song.
pic_url: str = ''
media_flags: MediaFlags = MediaFlags.not_sure

def model_post_init(self, _):
super().model_post_init(_)
Expand Down

0 comments on commit 7307151

Please sign in to comment.