implemented playlist contents interactive dragging

This commit is contained in:
Brian Hrebec 2010-05-25 20:13:14 -05:00
parent b4e5959f2d
commit 714d629752
4 changed files with 113 additions and 3 deletions

View file

@ -214,8 +214,8 @@ PlaylistModel::handle_change (const Xmms::Dict &chg)
m_plist.insert (npos, id);
endInsertRows ();
if (pos < npos && pos)
pos --;
//if (pos < npos && pos)
// pos --;
emit entryMoved (index (pos, 0), index (npos, 0));

View file

@ -123,7 +123,7 @@ PlaylistView::PlaylistView (QWidget *parent) : QListView (parent)
setSelectionMode (QAbstractItemView::ExtendedSelection);
setUniformItemSizes(true);
setDragEnabled(true);
//setDragEnabled(true);
setAcceptDrops(true);
// TODO make sure delegate gets deleted
@ -142,6 +142,7 @@ PlaylistView::PlaylistView (QWidget *parent) : QListView (parent)
connect (client->xplayback (), SIGNAL(playbackStatusChanged(Xmms::Playback::Status)),
this, SLOT(handleStatus(Xmms::Playback::Status)));
}
void
@ -312,6 +313,104 @@ PlaylistView::mouseDoubleClickEvent (QMouseEvent *event)
}
}
void
PlaylistView::mouseMoveEvent (QMouseEvent *event)
{
if (event->buttons () != Qt::LeftButton)
return;
QModelIndex mouseIndex = indexAt (event->pos ());
QModelIndexList sel = selectedIndexes ();
if (mouseIndex.row () < 0 || sel.empty())
return;
if (mouseIndex != currentIndex () && m_old_selection.empty()) {
m_old_current_index = currentIndex ().row();
qSort(sel);
int diff = mouseIndex.row () - currentIndex ().row ();
if (!(sel.first().row () + diff >= 0) ||
!(sel.last ().row () + diff < model ()->rowCount ()))
return;
if (diff < 0 ) {
// move selection up
for (int i = 0; i < sel.size (); i++) {
int row = sel[i].row();
int nrow = row + diff;
App->client ()->playlist ()->moveEntry (row, nrow);
m_old_selection.insert(row);
}
} else {
// move selection down
for (int i = sel.size () - 1; i >= 0; i--) {
int row = sel[i].row();
int nrow = row + diff;
App->client ()->playlist ()->moveEntry (row, nrow);
m_old_selection.insert(row);
}
}
setSelectionMode (QAbstractItemView::NoSelection);
setCurrentIndex(mouseIndex);
setSelectionMode (QAbstractItemView::ExtendedSelection);
}
/*
QRect r = visualRect(currentIndex ());
int y = event->pos ().y();
if (!(y >= r.y () &&
y <= r.y () + r.height ())) {
m_old_selection.clear();
m_old_current_index = currentIndex ().row();
QModelIndexList sel = selectedIndexes ();
qSort(sel);
if (y < r.y ()) {
// move selection up
for (int i = 0; i < sel.size (); i++) {
int row = sel[i].row();
//qDebug() << "moving " << row << "to" << row - 1;
App->client ()->playlist ()->moveEntry (row, row - 1);
m_old_selection.insert(row);
}
} else {
// move selection down
for (int i = sel.size () - 1; i >= 0; i--) {
int row = sel[i].row();
//qDebug() << "moving " << row << "to" << row + 1;
App->client ()->playlist ()->moveEntry (row, row + 1);
m_old_selection.insert(row);
}
}
}
*/
}
void
PlaylistView::entryMoved(QModelIndex a, QModelIndex b)
{
if (m_old_selection.contains (a.row())) {
selectionModel ()->select (b, QItemSelectionModel::Select);
m_old_selection.remove (a.row());
if (m_old_current_index == a.row ()) {
setSelectionMode (QAbstractItemView::NoSelection);
setCurrentIndex (b);
setSelectionMode (QAbstractItemView::ExtendedSelection);
}
}
}
void
PlaylistView::showEntryInfo (void)
{

View file

@ -26,6 +26,7 @@ class PlaylistModel;
#include <QAbstractItemDelegate>
#include <QListView>
#include <QPointer>
#include <QSet>
class QWidget;
class QModelIndex;
@ -69,9 +70,11 @@ class PlaylistView : public QListView {
void settingsChanged (void);
void setPixmaps (Skin *skin);
void currentPosChanged (QModelIndex);
void entryMoved(QModelIndex, QModelIndex);
protected:
void mouseDoubleClickEvent (QMouseEvent *event);
void mouseMoveEvent (QMouseEvent *event);
protected slots:
void selectionChanged (const QItemSelection &, const QItemSelection &);
@ -85,6 +88,9 @@ class PlaylistView : public QListView {
QColor m_color_normal;
QColor m_color_normal_bg;
QPointer<EntryInfo> m_entry_info;
QSet<int> m_old_selection;
int m_old_current_index;
};

View file

@ -196,6 +196,11 @@ PlaylistWidget::PlaylistWidget (PlaylistWindow *parent) : QWidget (parent)
// TODO: creation of Playlistmodel should be done elsewhere
m_view->setModel (App->client ()->active_playlist ());
connect (App->client ()->active_playlist (),
SIGNAL(entryMoved (QModelIndex, QModelIndex)),
m_view,
SLOT (entryMoved (QModelIndex, QModelIndex)));
/*
* This is a hack to make PlaylistScrollBar work with PlaylistView.
* It is necessery because of limitations and at least one Bug in the