summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-09-12 20:12:53 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-09-12 20:12:53 +0200
commitbe7260ad246f033479b0a5e4a84bc94ac2dba604 (patch)
tree38c185969d5a0e7708b70b49b0d76beafed6c3a6
parent1961ac0e94e9fe9fb73552a798e3f46b9715c9ee (diff)
Lyrics: allow dragging of items.
though nowhere to drop them now
-rw-r--r--nephilim/common.py3
-rw-r--r--nephilim/plugins/Library.py72
2 files changed, 54 insertions, 21 deletions
diff --git a/nephilim/common.py b/nephilim/common.py
index a04cc42..5bf562b 100644
--- a/nephilim/common.py
+++ b/nephilim/common.py
@@ -28,6 +28,9 @@ appIcon = ':icons/nephilim_small.png'
APPNAME = 'nephilim'
ORGNAME = 'nephilim'
+# custom mimetypes used for drag&drop
+MIMETYPES = {'songs' : 'application/x-mpd-songlist', 'plistsongs' : 'application/x-mpd-playlistsonglist'}
+
def sec2min(secs):
"""Converts seconds to min:sec."""
min=int(secs/60)
diff --git a/nephilim/plugins/Library.py b/nephilim/plugins/Library.py
index 89573e6..edc59d6 100644
--- a/nephilim/plugins/Library.py
+++ b/nephilim/plugins/Library.py
@@ -19,6 +19,7 @@ from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant
from ..plugin import Plugin
+from ..common import MIMETYPES
class Library(Plugin):
# public, const
@@ -96,6 +97,7 @@ class LibraryWidget(QtGui.QWidget):
filtered_items = None
filter = None
+
class LibrarySongItem(QtGui.QStandardItem):
# public
"Song path"
@@ -128,6 +130,31 @@ class LibraryWidget(QtGui.QWidget):
self.sort(0, QtCore.Qt.AscendingOrder)
+ def walk_tree(self, indices):
+ """Returns a generator over all songs that are children of indices."""
+ for index in indices:
+ if self.hasChildren(index):
+ for song in self.walk_tree([self.index(i, 0, index) for i in range(self.rowCount(index))]):
+ yield song
+ else:
+ yield self.itemFromIndex(index).path
+
+ def flags(self, index):
+ return (QtCore.Qt.ItemIsDragEnabled if index.isValid() else 0) | QtGui.QStandardItemModel.flags(self, index)
+
+ def mimeTypes(self):
+ return MIMETYPES['songs']
+
+ def mimeData(self, indices):
+ data = SongsMimeData()
+
+ songs = []
+ for song in self.walk_tree(indices):
+ songs.append(song)
+
+ data.set_songs(songs)
+ return data
+
class LibraryView(QtGui.QTreeView):
def __init__(self):
QtGui.QTreeView.__init__(self)
@@ -136,13 +163,7 @@ class LibraryWidget(QtGui.QWidget):
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setUniformRowHeights(True)
self.setHeaderHidden(True)
-
- def selectedItems(self):
- ret = []
- for index in self.selectedIndexes():
- ret.append(self.model().itemFromIndex(index))
-
- return ret
+ self.setDragEnabled(True)
def __init__(self, plugin):
QtGui.QWidget.__init__(self)
@@ -166,7 +187,7 @@ class LibraryWidget(QtGui.QWidget):
self.library_view = self.LibraryView()
self.library_view.setModel(self.library_model)
- self.library_view.activated.connect(self.add_selection)
+ self.library_view.activated.connect(lambda : self.add_indices(self.library_view.selectedIndexes()))
self.setLayout(QtGui.QVBoxLayout())
self.layout().setSpacing(2)
@@ -228,23 +249,32 @@ class LibraryWidget(QtGui.QWidget):
self.filtered_items = filtered_items
self.filter = text
+ @QtCore.pyqtSlot()
def add_filtered(self):
- self.add_items(self.filtered_items)
+ self.add_indices([self.library_model.indexFromItem(index) for index in self.filtered_items])
self.search_txt.clear()
- def add_selection(self):
- self.add_items(self.library_view.selectedItems())
-
- def add_items(self, items):
+ def add_indices(self, indices):
paths = []
- for item in items:
- self.item_to_playlist(item, paths)
+ for song in self.library_model.walk_tree(indices):
+ paths.append(song)
self.plugin.mpclient.add(paths)
- def item_to_playlist(self, item, add_queue):
- if isinstance(item, self.LibrarySongItem):
- add_queue.append(item.path)
- else:
- for i in range(item.rowCount()):
- self.item_to_playlist(item.child(i), add_queue)
+class SongsMimeData(QtCore.QMimeData):
+ # private
+ __songs = None
+
+ def set_songs(self, songs):
+ self.__songs = songs
+
+ def songs(self):
+ return self.__songs
+
+ def formats(self):
+ types = QtCore.QMimeData.formats(self)
+ return types if not self.__songs else types + [MIMETYPES['songs']]
+ def hasFormat(self, format):
+ if format == MIMETYPES['songs'] and self.__songs:
+ return True
+ return QtCore.QMimeData.hasFormat(self, format)