diff --git a/backend_xmmsclient++/playlistmodel.cpp b/backend_xmmsclient++/playlistmodel.cpp index b95f033..ec09107 100644 --- a/backend_xmmsclient++/playlistmodel.cpp +++ b/backend_xmmsclient++/playlistmodel.cpp @@ -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)); diff --git a/src/playlist/playlistview.cpp b/src/playlist/playlistview.cpp index 3a25d91..415c3dc 100644 --- a/src/playlist/playlistview.cpp +++ b/src/playlist/playlistview.cpp @@ -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) { diff --git a/src/playlist/playlistview.h b/src/playlist/playlistview.h index d49b931..02e065d 100644 --- a/src/playlist/playlistview.h +++ b/src/playlist/playlistview.h @@ -26,6 +26,7 @@ class PlaylistModel; #include #include #include +#include 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 m_entry_info; + + QSet m_old_selection; + int m_old_current_index; }; diff --git a/src/playlist/playlistwidget.cpp b/src/playlist/playlistwidget.cpp index 5032bca..3715694 100644 --- a/src/playlist/playlistwidget.cpp +++ b/src/playlist/playlistwidget.cpp @@ -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