AlbumCover: refresh has to take two parameters.
[nephilim.git] / plugins / Library.py
blob64f68a3ac3c24702e053c97b476581ce46f826ba
1 from PyQt4 import QtGui, QtCore
2 from PyQt4.QtCore import QVariant
4 from clPlugin import Plugin
5 from misc import ORGNAME, APPNAME
7 class pluginLibrary(Plugin):
8 o=None
9 DEFAULTS = {'modes' : 'artist\n'\
10 'artist/album\n'\
11 'artist/date/album\n'\
12 'genre\n'\
13 'genre/artist\n'\
14 'genre/artist/album\n'}
16 def __init__(self, winMain):
17 Plugin.__init__(self, winMain, 'Library')
18 self.settings = QtCore.QSettings(ORGNAME, APPNAME)
19 def _load(self):
20 self.o = LibraryWidget(self)
21 self.monty.add_listener('onReady', self.o.fill_library)
22 self.monty.add_listener('onDisconnect', self.o.fill_library)
23 self.monty.add_listener('onUpdateDBFinish', self.o.fill_library)
24 def _unload(self):
25 self.o = None
27 def getInfo(self):
28 return "List showing all the songs allowing filtering and grouping."
30 def _getDockWidget(self):
31 return self._createDock(self.o)
33 class SettingsWidgetLibrary(Plugin.SettingsWidget):
34 modes = None
35 def __init__(self, plugin):
36 Plugin.SettingsWidget.__init__(self, plugin)
37 self.setLayout(QtGui.QVBoxLayout())
39 self.modes = QtGui.QTextEdit()
40 self.modes.insertPlainText(self.settings.value(self.plugin.getName() + '/modes').toString())
41 self.layout().addWidget(self.modes)
43 def save_settings(self):
44 self.settings.setValue(self.plugin.getName() + '/modes', QVariant(self.modes.toPlainText()))
45 self.plugin.o.refresh_modes()
47 def get_settings_widget(self):
48 return self.SettingsWidgetLibrary(self)
50 class LibraryWidget(QtGui.QWidget):
51 library = None
52 search_txt = None
53 modes = None
54 settings = None
55 plugin = None
57 def __init__(self, plugin):
58 QtGui.QWidget.__init__(self)
59 self.plugin = plugin
60 self.settings = QtCore.QSettings(ORGNAME, APPNAME)
61 self.settings.beginGroup(self.plugin.getName())
63 self.modes = QtGui.QComboBox()
64 self.refresh_modes()
65 self.connect(self.modes, QtCore.SIGNAL('activated(int)'), self.modes_activated)
67 self.search_txt = QtGui.QLineEdit()
68 self.connect(self.search_txt, QtCore.SIGNAL('textChanged(const QString&)'),
69 self.filter_changed)
70 self.connect(self.search_txt, QtCore.SIGNAL('returnPressed()'), self.add_filtered)
72 #construct the library
73 self.library = QtGui.QTreeWidget()
74 self.library.setColumnCount(1)
75 self.library.setAlternatingRowColors(True)
76 self.library.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
77 self.library.headerItem().setHidden(True)
78 self.fill_library()
79 self.connect(self.library, QtCore.SIGNAL('itemActivated(QTreeWidgetItem*, int)'), self.add_selection)
81 self.setLayout(QtGui.QVBoxLayout())
82 self.layout().setSpacing(2)
83 self.layout().setMargin(0)
84 self.layout().addWidget(self.modes)
85 self.layout().addWidget(self.search_txt)
86 self.layout().addWidget(self.library)
88 def refresh_modes(self):
89 self.modes.clear()
90 for mode in self.settings.value('/modes').toString().split('\n'):
91 self.modes.addItem(mode)
92 self.modes.setCurrentIndex(self.settings.value('current_mode').toInt()[0])
95 def fill_library(self, params = None):
96 self.library.clear()
98 #build a tree from library
99 tree = [{},self.library.invisibleRootItem()]
100 for song in self.plugin.monty.listLibrary():
101 cur_item = tree
102 for part in str(self.modes.currentText()).split('/'):
103 tag = song.getTag(part)
104 if isinstance(tag, list):
105 tag = tag[0] #FIXME hack to make songs with multiple genres work.
106 if not tag:
107 tag = 'Unknown'
108 if tag in cur_item[0]:
109 cur_item = cur_item[0][tag]
110 else:
111 it = QtGui.QTreeWidgetItem([tag])
112 cur_item[1].addChild(it)
113 cur_item[0][tag] = [{}, it]
114 cur_item = cur_item[0][tag]
115 it = QtGui.QTreeWidgetItem(['%02d %s'%(song.getTrack() if song.getTrack() else 0,
116 song.getTitle() if song.getTitle() else song.getFilepath())], 1000)
117 it.setData(0, QtCore.Qt.UserRole, QVariant(song))
118 cur_item[1].addChild(it)
120 self.library.sortItems(0, QtCore.Qt.AscendingOrder)
122 def filter_changed(self, text):
123 items = self.library.findItems(text, QtCore.Qt.MatchContains|QtCore.Qt.MatchRecursive)
124 for i in range(self.library.topLevelItemCount()):
125 self.library.topLevelItem(i).setHidden(True)
126 for item in items:
127 while item.parent():
128 item = item.parent()
129 item.setHidden(False)
130 self.filtered_items = items
132 def add_filtered(self):
133 self.library.clearSelection()
134 for item in self.filtered_items:
135 item.setSelected(True)
136 self.add_selection()
137 self.library.clearSelection()
138 self.search_txt.clear()
140 def add_selection(self):
141 paths = []
142 for item in self.library.selectedItems():
143 self.item_to_playlist(item, paths)
144 self.plugin.monty.addToPlaylist(paths)
146 def item_to_playlist(self, item, add_queue):
147 if item.type() == 1000:
148 add_queue.append(item.data(0, QtCore.Qt.UserRole).toPyObject().getFilepath())
149 else:
150 for i in range(item.childCount()):
151 self.item_to_playlist(item.child(i), add_queue)
153 def modes_activated(self):
154 self.settings.setValue('current_mode', QVariant(self.modes.currentIndex()))
155 self.fill_library()