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
):
9 DEFAULTS
= {'modes' : 'artist\n'\
11 'artist/date/album\n'\
14 'genre/artist/album\n'}
16 def __init__(self
, winMain
):
17 Plugin
.__init
__(self
, winMain
, 'Library')
18 self
.settings
= QtCore
.QSettings(ORGNAME
, APPNAME
)
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
)
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
):
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
):
57 def __init__(self
, plugin
):
58 QtGui
.QWidget
.__init
__(self
)
60 self
.settings
= QtCore
.QSettings(ORGNAME
, APPNAME
)
61 self
.settings
.beginGroup(self
.plugin
.getName())
63 self
.modes
= QtGui
.QComboBox()
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&)'),
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)
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
):
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):
98 #build a tree from library
99 tree
= [{},self
.library
.invisibleRootItem()]
100 for song
in self
.plugin
.monty
.listLibrary():
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.
108 if tag
in cur_item
[0]:
109 cur_item
= cur_item
[0][tag
]
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)
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)
137 self
.library
.clearSelection()
138 self
.search_txt
.clear()
140 def add_selection(self
):
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())
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()))