From 062cf35324c1a0860ce2b4059977d72689b19daa Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Fri, 22 Apr 2011 16:07:44 -0400 Subject: [PATCH] sort playlists by date added allow users to drag tracks onto the New Playlist item to seed a new plsylist --- src/sourcetree/items/categoryitems.cpp | 44 +++++++++++++++++++++++++ src/sourcetree/items/categoryitems.h | 4 +++ src/sourcetree/items/collectionitem.cpp | 12 +++++++ src/sourcetree/items/collectionitem.h | 1 + src/sourcetree/items/playlistitems.cpp | 14 ++++++++ src/sourcetree/items/playlistitems.h | 2 ++ src/sourcetree/items/sourcetreeitem.h | 1 + src/sourcetree/sourcesmodel.cpp | 2 ++ src/sourcetree/sourcesmodel.h | 6 ++-- src/sourcetree/sourcesproxymodel.cpp | 2 +- src/sourcetree/sourcetreeview.cpp | 5 ++- 11 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/sourcetree/items/categoryitems.cpp b/src/sourcetree/items/categoryitems.cpp index 1106ead7c..cfa9457e5 100644 --- a/src/sourcetree/items/categoryitems.cpp +++ b/src/sourcetree/items/categoryitems.cpp @@ -21,6 +21,9 @@ #include "widgets/newplaylistwidget.h" #include "viewmanager.h" #include "viewpage.h" +#include "sourcelist.h" + +#include using namespace Tomahawk; @@ -89,6 +92,47 @@ CategoryAddItem::icon() const return QIcon( RESPATH "images/add.png" ); } +bool +CategoryAddItem::willAcceptDrag( const QMimeData* data ) const +{ + if( m_categoryType == SourcesModel::PlaylistsCategory && data->hasFormat( "application/tomahawk.query.list" ) ) { + return true; + } + return false; +} + +bool +CategoryAddItem::dropMimeData( const QMimeData* data, Qt::DropAction action ) +{ + // Create a new playlist seeded with these items + if( m_categoryType == SourcesModel::PlaylistsCategory && data->hasFormat( "application/tomahawk.query.list" ) ) { + QByteArray itemData = data->data( "application/tomahawk.query.list" ); + QDataStream stream( &itemData, QIODevice::ReadOnly ); + QList< Tomahawk::query_ptr > queries; + + while ( !stream.atEnd() ) + { + qlonglong qptr; + stream >> qptr; + + Tomahawk::query_ptr* query = reinterpret_cast(qptr); + if ( query && !query->isNull() ) + { + qDebug() << "Dropped query item:" << query->data()->artist() << "-" << query->data()->track(); + queries << *query; + } + } + + playlist_ptr newpl = Playlist::create( SourceList::instance()->getLocal(), uuid(), "New Playlist", "", SourceList::instance()->getLocal()->friendlyName(), false ); + newpl->addEntries( queries, newpl->currentrevision() ); + ViewManager::instance()->show( newpl ); + + return true; + } + return false; +} + + /// CategoryItem CategoryItem::CategoryItem( SourcesModel* model, SourceTreeItem* parent, SourcesModel::CategoryType category, bool showAddItem ) diff --git a/src/sourcetree/items/categoryitems.h b/src/sourcetree/items/categoryitems.h index 05fa7a8dd..1f16d5f83 100644 --- a/src/sourcetree/items/categoryitems.h +++ b/src/sourcetree/items/categoryitems.h @@ -31,6 +31,10 @@ public: virtual void activate(); virtual QIcon icon() const; + virtual bool willAcceptDrag(const QMimeData* data) const; + virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action); + + private: SourcesModel::CategoryType m_categoryType; }; diff --git a/src/sourcetree/items/collectionitem.cpp b/src/sourcetree/items/collectionitem.cpp index a2097def1..f478b0a56 100644 --- a/src/sourcetree/items/collectionitem.cpp +++ b/src/sourcetree/items/collectionitem.cpp @@ -89,6 +89,18 @@ CollectionItem::text() const return m_source.isNull() ? tr( "Super Collection" ) : m_source->friendlyName(); } +int +CollectionItem::peerSortValue() const +{ + if( m_source.isNull() ) + return -1; + if( m_source->isLocal() ) + return 0; + + return 1; +} + + void CollectionItem::activate() { diff --git a/src/sourcetree/items/collectionitem.h b/src/sourcetree/items/collectionitem.h index 37886f397..1cb99e0cb 100644 --- a/src/sourcetree/items/collectionitem.h +++ b/src/sourcetree/items/collectionitem.h @@ -30,6 +30,7 @@ public: virtual QString text() const; virtual void activate(); virtual QIcon icon() const; + virtual int peerSortValue() const; Tomahawk::source_ptr source() const; diff --git a/src/sourcetree/items/playlistitems.cpp b/src/sourcetree/items/playlistitems.cpp index 787822b7a..ce5de4277 100644 --- a/src/sourcetree/items/playlistitems.cpp +++ b/src/sourcetree/items/playlistitems.cpp @@ -66,6 +66,13 @@ PlaylistItem::onPlaylistChanged() emit updated(); } +int +PlaylistItem::peerSortValue() const +{ + return m_playlist->createdOn(); +} + + Qt::ItemFlags PlaylistItem::flags() const { @@ -187,6 +194,13 @@ DynamicPlaylistItem::onDynamicPlaylistLoaded( DynamicPlaylistRevision revision ) emit updated(); } +int +DynamicPlaylistItem::peerSortValue() const +{ + return m_dynplaylist->createdOn(); +} + + void DynamicPlaylistItem::checkReparentHackNeeded( const DynamicPlaylistRevision& revision ) { diff --git a/src/sourcetree/items/playlistitems.h b/src/sourcetree/items/playlistitems.h index 0e884f377..e7242945e 100644 --- a/src/sourcetree/items/playlistitems.h +++ b/src/sourcetree/items/playlistitems.h @@ -34,6 +34,7 @@ public: virtual bool dropMimeData( const QMimeData* data, Qt::DropAction action ); virtual QIcon icon() const; virtual bool setData(const QVariant& v, bool role); + virtual int peerSortValue() const; protected: void setLoaded( bool loaded ); @@ -59,6 +60,7 @@ public: Tomahawk::dynplaylist_ptr dynPlaylist() const; virtual bool willAcceptDrag( const QMimeData* data ) const; virtual void activate(); + virtual int peerSortValue() const; private slots: void onDynamicPlaylistLoaded( Tomahawk::DynamicPlaylistRevision revision ); diff --git a/src/sourcetree/items/sourcetreeitem.h b/src/sourcetree/items/sourcetreeitem.h index 6122d9f07..bfeb09147 100644 --- a/src/sourcetree/items/sourcetreeitem.h +++ b/src/sourcetree/items/sourcetreeitem.h @@ -52,6 +52,7 @@ public: virtual bool willAcceptDrag( const QMimeData* data ) const { return false; } virtual bool dropMimeData( const QMimeData* data, Qt::DropAction action ) { return false; } virtual bool setData( const QVariant& v, bool role ) { return false; } + virtual int peerSortValue() const { return 0; } // How to sort relative to peers in the tree. /// don't call me unless you are a sourcetreeitem. i prefer this to making everyone a friend void beginRowsAdded( int from, int to ) { emit beginChildRowsAdded( from, to ); } diff --git a/src/sourcetree/sourcesmodel.cpp b/src/sourcetree/sourcesmodel.cpp index d68e21c24..a95d8e12d 100644 --- a/src/sourcetree/sourcesmodel.cpp +++ b/src/sourcetree/sourcesmodel.cpp @@ -77,6 +77,8 @@ SourcesModel::data( const QModelIndex& index, int role ) const return itemFromIndex( index )->text(); case Qt::DecorationRole: return itemFromIndex( index )->icon(); + case SourcesModel::SortRole: + return itemFromIndex( index )->peerSortValue(); } return QVariant(); } diff --git a/src/sourcetree/sourcesmodel.h b/src/sourcetree/sourcesmodel.h index dba7d6dae..982803edd 100644 --- a/src/sourcetree/sourcesmodel.h +++ b/src/sourcetree/sourcesmodel.h @@ -59,10 +59,12 @@ public: }; enum Roles { - SourceTreeItemRole = Qt::UserRole + 10, - SourceTreeItemTypeRole = Qt::UserRole + 11 + SourceTreeItemRole = Qt::UserRole + 10, + SourceTreeItemTypeRole = Qt::UserRole + 11, + SortRole = Qt::UserRole + 12 }; + SourcesModel( QObject* parent = 0 ); virtual ~SourcesModel(); diff --git a/src/sourcetree/sourcesproxymodel.cpp b/src/sourcetree/sourcesproxymodel.cpp index 589d6474e..b778d5c31 100644 --- a/src/sourcetree/sourcesproxymodel.cpp +++ b/src/sourcetree/sourcesproxymodel.cpp @@ -31,7 +31,7 @@ SourcesProxyModel::SourcesProxyModel( SourcesModel* model, QObject* parent ) , m_filtered( false ) { setDynamicSortFilter( true ); -// setSortRole( SourcesModel::SortRole ); + setSortRole( SourcesModel::SortRole ); setSourceModel( model ); diff --git a/src/sourcetree/sourcetreeview.cpp b/src/sourcetree/sourcetreeview.cpp index 5a7da0e73..3ec62c6c4 100644 --- a/src/sourcetree/sourcetreeview.cpp +++ b/src/sourcetree/sourcetreeview.cpp @@ -78,9 +78,8 @@ SourceTreeView::SourceTreeView( QWidget* parent ) setAllColumnsShowFocus( true ); setUniformRowHeights( false ); setIndentation( 16 ); - -// setSortingEnabled( true ); -// sortByColumn( 1, Qt::AscendingOrder ); + setSortingEnabled( true ); + sortByColumn( 1, Qt::AscendingOrder ); // TODO animation conflicts with the expanding-playlists-when-collection-is-null // so investigate