summaryrefslogtreecommitdiff
path: root/plugins/Library.py
blob: 101fce4684672f4f8498b7b31620e4959c24ca4e (plain)
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
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant

from clPlugin import Plugin
from misc import ORGNAME, APPNAME

class pluginLibrary(Plugin):
    o=None
    DEFAULTS = {'modes' : 'artist\n'\
                          'artist/album\n'\
                          'artist/date/album\n'\
                          'genre\n'\
                          'genre/artist\n'\
                          'genre/artist/album\n'}

    def __init__(self, winMain):
        Plugin.__init__(self, winMain, 'Library')
        self.settings = QtCore.QSettings(ORGNAME, APPNAME)
    def _load(self):
        self.o = LibraryWidget(self)
        self.mpclient.add_listener('onReady', self.o.fill_library)
        self.mpclient.add_listener('onDisconnect', self.o.fill_library)
        self.mpclient.add_listener('onUpdateDBFinish', self.o.fill_library)
    def _unload(self):
        self.o = None

    def getInfo(self):
        return "List showing all the songs allowing filtering and grouping."

    def _getDockWidget(self):
        return self._createDock(self.o)

    class SettingsWidgetLibrary(Plugin.SettingsWidget):
        modes = None
        def __init__(self, plugin):
            Plugin.SettingsWidget.__init__(self, plugin)
            self.setLayout(QtGui.QVBoxLayout())

            self.modes = QtGui.QTextEdit()
            self.modes.insertPlainText(self.settings.value(self.plugin.getName() + '/modes').toString())
            self.layout().addWidget(self.modes)

        def save_settings(self):
            self.settings.setValue(self.plugin.getName() + '/modes', QVariant(self.modes.toPlainText()))
            self.plugin.o.refresh_modes()

    def get_settings_widget(self):
        return self.SettingsWidgetLibrary(self)

class LibraryWidget(QtGui.QWidget):
    library     = None
    search_txt  = None
    modes       = None
    settings    = None
    plugin      = None

    def __init__(self, plugin):
        QtGui.QWidget.__init__(self)
        self.plugin = plugin
        self.settings = QtCore.QSettings(ORGNAME, APPNAME)
        self.settings.beginGroup(self.plugin.getName())

        self.modes = QtGui.QComboBox()
        self.refresh_modes()
        self.connect(self.modes, QtCore.SIGNAL('activated(int)'), self.modes_activated)

        self.search_txt = QtGui.QLineEdit()
        self.connect(self.search_txt, QtCore.SIGNAL('textChanged(const QString&)'),
                                self.filter_changed)
        self.connect(self.search_txt, QtCore.SIGNAL('returnPressed()'), self.add_filtered)

        #construct the library
        self.library = QtGui.QTreeWidget()
        self.library.setColumnCount(1)
        self.library.setAlternatingRowColors(True)
        self.library.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
        self.library.headerItem().setHidden(True)
        self.fill_library()
        self.connect(self.library, QtCore.SIGNAL('itemActivated(QTreeWidgetItem*, int)'), self.add_selection)

        self.setLayout(QtGui.QVBoxLayout())
        self.layout().setSpacing(2)
        self.layout().setMargin(0)
        self.layout().addWidget(self.modes)
        self.layout().addWidget(self.search_txt)
        self.layout().addWidget(self.library)

    def refresh_modes(self):
        self.modes.clear()
        for mode in self.settings.value('/modes').toString().split('\n'):
            self.modes.addItem(mode)
        self.modes.setCurrentIndex(self.settings.value('current_mode').toInt()[0])


    def fill_library(self, params = None):
        self.library.clear()

        #build a tree from library
        tree = [{},self.library.invisibleRootItem()]
        for song in self.plugin.mpclient.listLibrary():
            cur_item = tree
            for part in str(self.modes.currentText()).split('/'):
                tag = song.getTag(part)
                if isinstance(tag, list):
                    tag = tag[0]            #FIXME hack to make songs with multiple genres work.
                if not tag:
                    tag = 'Unknown'
                if tag in cur_item[0]:
                    cur_item = cur_item[0][tag]
                else:
                    it = QtGui.QTreeWidgetItem([tag])
                    cur_item[1].addChild(it)
                    cur_item[0][tag] = [{}, it]
                    cur_item = cur_item[0][tag]
            it = QtGui.QTreeWidgetItem(['%02d %s'%(song.getTrack() if song.getTrack() else 0,
                                                   song.getTitle() if song.getTitle() else song.getFilepath())], 1000)
            it.setData(0, QtCore.Qt.UserRole, QVariant(song))
            cur_item[1].addChild(it)

        self.library.sortItems(0, QtCore.Qt.AscendingOrder)

    def filter_changed(self, text):
        items = self.library.findItems(text, QtCore.Qt.MatchContains|QtCore.Qt.MatchRecursive)
        for i in range(self.library.topLevelItemCount()):
            self.library.topLevelItem(i).setHidden(True)
        for item in items:
            while item.parent():
                item = item.parent()
            item.setHidden(False)
        self.filtered_items = items

    def add_filtered(self):
        self.library.clearSelection()
        for item in self.filtered_items:
            item.setSelected(True)
        self.add_selection()
        self.library.clearSelection()
        self.search_txt.clear()

    def add_selection(self):
        paths = []
        for item in self.library.selectedItems():
            self.item_to_playlist(item, paths)
        self.plugin.mpclient.addToPlaylist(paths)

    def item_to_playlist(self, item, add_queue):
        if item.type() == 1000:
            add_queue.append(item.data(0, QtCore.Qt.UserRole).toPyObject().getFilepath())
        else:
            for i in range(item.childCount()):
                self.item_to_playlist(item.child(i), add_queue)

    def modes_activated(self):
        self.settings.setValue('current_mode', QVariant(self.modes.currentIndex()))
        self.fill_library()