/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

  1. # -*- coding: utf-8 -*-
  2. #-------------------------------------------------------------------------------
  3. # Copyright 2009 E. A. Graham Jr. <txcrackers@gmail.com>.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # under the License.
  17. #-------------------------------------------------------------------------------
  18. from PyQt4.QtGui import *
  19. from PyKDE4.kdeui import KIcon
  20. import os
  21. import datetime
  22. NOCOVER = KIcon('audio-x-generic')
  23. #===============================================================================
  24. # List and tree widget items formatted in various ways for the forms to display
  25. # stuff with.
  26. #===============================================================================
  27. class FullTreeWidget(QListWidgetItem):
  28. '''Song, album, cover in a tree widget item'''
  29. # Used in CurrentPlaylistForm
  30. def __init__(self,musicPath,song):
  31. QListWidgetItem.__init__(self)
  32. self.song = song
  33. self.setIcon(songIcon(musicPath,song))
  34. self.setText(songTitle(song) + '\n' + song.get('artist','?'))
  35. album = song.get('album', '')
  36. playtime = str(datetime.timedelta(0, int(song.get('time', '0'))))
  37. self.setToolTip("Album:\t %s\nTime:\t %s\nFile:\t %s" % (album, playtime, song['file']))
  38. self.color = self.backgroundColor()
  39. def currentlyPlaying(self,flag):
  40. font = self.font()
  41. if flag:
  42. font.setBold(True)
  43. self.color.setAlpha(50)
  44. else:
  45. font.setBold(False)
  46. self.color.setAlpha(0)
  47. self.setFont(font)
  48. self.setBackgroundColor(self.color)
  49. class LongSongWidget(QTreeWidgetItem):
  50. '''Lays out a song in a three-column tree widget: artist, title, album'''
  51. # Used in PlaylistForm
  52. def __init__(self,song):
  53. QTreeWidgetItem.__init__(self)
  54. self.song = song
  55. self.setText(0,song.get('artist','?'))
  56. self.setText(1,songTitle(song))
  57. self.setText(2,song.get('album',''))
  58. class SongAndTrackWidget(QTreeWidgetItem):
  59. '''Track + Title'''
  60. def __init__(self,song):
  61. QTreeWidgetItem.__init__(self)
  62. self.song = song
  63. self.setText(1,song.get('track','#') + ' - ' + songTitle(song))
  64. class AlbumWidget(QTreeWidgetItem):
  65. '''Cover, album name, and year'''
  66. # Used in LibraryForm
  67. def __init__(self,musicPath,song):
  68. QTreeWidgetItem.__init__(self)
  69. self.setIcon(0,songIcon(musicPath,song))
  70. text = song.get('album','Unknown')
  71. self.album = text
  72. if song.get('date',None) != None:
  73. text += ' (' + str(song['date']) + ')'
  74. self.setText(1,text)
  75. class ArtistWidget(QTreeWidgetItem):
  76. '''Just the artist, but set up to span all the columns'''
  77. def __init__(self,artist):
  78. QTreeWidgetItem.__init__(self,[artist])
  79. self.setFirstColumnSpanned(True)
  80. self.artist = artist
  81. def songTitle(song):
  82. return song.get('title',song.get('name',song['file']))
  83. def songIcon(musicPath,song):
  84. '''Find the cover image for the current song, if possible and return as icon'''
  85. try:
  86. x = song['file'].rfind('/')
  87. dir = song['file'][:x]
  88. iconname = musicPath + '/' + dir + '/folder.jpg'
  89. if os.path.exists(iconname):
  90. return KIcon(iconname)
  91. except Exception:
  92. pass
  93. return NOCOVER