/src/contents/code/ui/songwidgets.py
http://mpdplasma.googlecode.com/ · Python · 104 lines · 70 code · 8 blank · 26 comment · 5 complexity · 201a44633e5dd621bee3c86a0d76b87a MD5 · raw file
- # -*- coding: utf-8 -*-
- #-------------------------------------------------------------------------------
- # Copyright 2009 E. A. Graham Jr. <txcrackers@gmail.com>.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # under the License.
- #-------------------------------------------------------------------------------
- from PyQt4.QtGui import *
- from PyKDE4.kdeui import KIcon
- import os
- import datetime
- NOCOVER = KIcon('audio-x-generic')
- #===============================================================================
- # List and tree widget items formatted in various ways for the forms to display
- # stuff with.
- #===============================================================================
- class FullTreeWidget(QListWidgetItem):
- '''Song, album, cover in a tree widget item'''
- # Used in CurrentPlaylistForm
- def __init__(self,musicPath,song):
- QListWidgetItem.__init__(self)
- self.song = song
- self.setIcon(songIcon(musicPath,song))
- self.setText(songTitle(song) + '\n' + song.get('artist','?'))
- album = song.get('album', '')
- playtime = str(datetime.timedelta(0, int(song.get('time', '0'))))
- self.setToolTip("Album:\t %s\nTime:\t %s\nFile:\t %s" % (album, playtime, song['file']))
- self.color = self.backgroundColor()
- def currentlyPlaying(self,flag):
- font = self.font()
- if flag:
- font.setBold(True)
- self.color.setAlpha(50)
- else:
- font.setBold(False)
- self.color.setAlpha(0)
- self.setFont(font)
- self.setBackgroundColor(self.color)
- class LongSongWidget(QTreeWidgetItem):
- '''Lays out a song in a three-column tree widget: artist, title, album'''
- # Used in PlaylistForm
- def __init__(self,song):
- QTreeWidgetItem.__init__(self)
- self.song = song
- self.setText(0,song.get('artist','?'))
- self.setText(1,songTitle(song))
- self.setText(2,song.get('album',''))
- class SongAndTrackWidget(QTreeWidgetItem):
- '''Track + Title'''
- def __init__(self,song):
- QTreeWidgetItem.__init__(self)
- self.song = song
- self.setText(1,song.get('track','#') + ' - ' + songTitle(song))
- class AlbumWidget(QTreeWidgetItem):
- '''Cover, album name, and year'''
- # Used in LibraryForm
- def __init__(self,musicPath,song):
- QTreeWidgetItem.__init__(self)
- self.setIcon(0,songIcon(musicPath,song))
- text = song.get('album','Unknown')
- self.album = text
- if song.get('date',None) != None:
- text += ' (' + str(song['date']) + ')'
- self.setText(1,text)
- class ArtistWidget(QTreeWidgetItem):
- '''Just the artist, but set up to span all the columns'''
- def __init__(self,artist):
- QTreeWidgetItem.__init__(self,[artist])
- self.setFirstColumnSpanned(True)
- self.artist = artist
- def songTitle(song):
- return song.get('title',song.get('name',song['file']))
- def songIcon(musicPath,song):
- '''Find the cover image for the current song, if possible and return as icon'''
- try:
- x = song['file'].rfind('/')
- dir = song['file'][:x]
- iconname = musicPath + '/' + dir + '/folder.jpg'
- if os.path.exists(iconname):
- return KIcon(iconname)
- except Exception:
- pass
- return NOCOVER