From f1bd808d334774fa644e446cd500943114ab7861 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Fri, 10 Dec 2010 21:26:33 -0500 Subject: [PATCH] Rework source tree to use two enums instead of raw ints that drove me crazy... --- src/sourcetree/sourcesmodel.cpp | 36 +++++++++++++++++++++++-------- src/sourcetree/sourcesmodel.h | 13 +++++++++-- src/sourcetree/sourcetreeitem.cpp | 15 +++++++------ src/sourcetree/sourcetreeitem.h | 16 ++++++++------ src/sourcetree/sourcetreeview.cpp | 30 +++++++++++++++++--------- 5 files changed, 75 insertions(+), 35 deletions(-) diff --git a/src/sourcetree/sourcesmodel.cpp b/src/sourcetree/sourcesmodel.cpp index 17bbae89d..3c2858fcb 100644 --- a/src/sourcetree/sourcesmodel.cpp +++ b/src/sourcetree/sourcesmodel.cpp @@ -50,7 +50,7 @@ SourcesModel::flags( const QModelIndex& index ) const if ( index.isValid() ) { - if ( indexType( index ) == 1 ) + if ( indexType( index ) == PlaylistSource ) { playlist_ptr playlist = indexToPlaylist( index ); if ( !playlist.isNull() && playlist->author()->isLocal() ) @@ -166,14 +166,14 @@ SourcesModel::onItemOffline( const QModelIndex& idx ) } -int +SourcesModel::SourceType SourcesModel::indexType( const QModelIndex& index ) { if ( !index.isValid() ) - return -1; + return Invalid; QModelIndex idx = index.model()->index( index.row(), 0, index.parent() ); - return idx.data( Qt::UserRole + 1 ).toInt(); + return static_cast( idx.data( SourceTreeItem::Type ).toInt() ); } @@ -184,10 +184,10 @@ SourcesModel::indexToPlaylist( const QModelIndex& index ) if ( !index.isValid() ) return res; - if ( indexType( index ) == 1 ) + if ( indexType( index ) == PlaylistSource ) { QModelIndex idx = index.model()->index( index.row(), 0, index.parent() ); - qlonglong pptr = idx.data( Qt::UserRole + 3 ).toLongLong(); + qlonglong pptr = idx.data( SourceTreeItem::PlaylistPointer ).toLongLong(); playlist_ptr* playlist = reinterpret_cast(pptr); if ( playlist ) return *playlist; @@ -196,6 +196,24 @@ SourcesModel::indexToPlaylist( const QModelIndex& index ) return res; } +dynplaylist_ptr SourcesModel::indexToDynamicPlaylist(const QModelIndex& index) +{ + dynplaylist_ptr res; + if ( !index.isValid() ) + return res; + + if ( indexType( index ) == DynamicPlaylistSource ) + { + QModelIndex idx = index.model()->index( index.row(), 0, index.parent() ); + qlonglong pptr = idx.data( SourceTreeItem::DynamicPlaylistPointer ).toLongLong(); + dynplaylist_ptr* playlist = reinterpret_cast(pptr); + if ( playlist ) + return *playlist; + } + + return res; +} + SourceTreeItem* SourcesModel::indexToTreeItem( const QModelIndex& index ) @@ -204,10 +222,10 @@ SourcesModel::indexToTreeItem( const QModelIndex& index ) return 0; int type = indexType( index ); - if ( type == 0 || type == 1 ) + if ( type == CollectionSource || type == PlaylistSource || type == DynamicPlaylistSource ) { QModelIndex idx = index.model()->index( index.row(), 0, index.parent() ); - qlonglong pptr = idx.data( Qt::UserRole + 2 ).toLongLong(); + qlonglong pptr = idx.data( SourceTreeItem::SourceItemPointer ).toLongLong(); SourceTreeItem* item = reinterpret_cast(pptr); if ( item ) return item; @@ -225,7 +243,7 @@ SourcesModel::setData( const QModelIndex& index, const QVariant& value, int role if ( !index.isValid() ) return false; - if ( indexType( index ) == 1 ) + if ( indexType( index ) == PlaylistSource ) { playlist_ptr playlist = indexToPlaylist( index ); if ( !playlist.isNull() ) diff --git a/src/sourcetree/sourcesmodel.h b/src/sourcetree/sourcesmodel.h index e39f3b11c..816385bd8 100644 --- a/src/sourcetree/sourcesmodel.h +++ b/src/sourcetree/sourcesmodel.h @@ -12,7 +12,15 @@ class SourcesModel : public QStandardItemModel { Q_OBJECT -public: +public: + enum SourceType { + Invalid = -1, + + CollectionSource = 0, + PlaylistSource = 1, + DynamicPlaylistSource = 2 + }; + explicit SourcesModel( QObject* parent = 0 ); virtual QStringList mimeTypes() const; @@ -23,8 +31,9 @@ public: bool appendItem( const Tomahawk::source_ptr& source ); bool removeItem( const Tomahawk::source_ptr& source ); - static int indexType( const QModelIndex& index ); + static SourceType indexType( const QModelIndex& index ); static Tomahawk::playlist_ptr indexToPlaylist( const QModelIndex& index ); + static Tomahawk::dynplaylist_ptr indexToDynamicPlaylist( const QModelIndex& index ); static SourceTreeItem* indexToTreeItem( const QModelIndex& index ); signals: diff --git a/src/sourcetree/sourcetreeitem.cpp b/src/sourcetree/sourcetreeitem.cpp index d4a7df2d8..f412ae11b 100644 --- a/src/sourcetree/sourcetreeitem.cpp +++ b/src/sourcetree/sourcetreeitem.cpp @@ -6,6 +6,7 @@ #include "tomahawk/collection.h" #include "tomahawk/playlist.h" #include "tomahawk/tomahawkapp.h" +#include "sourcesmodel.h" using namespace Tomahawk; @@ -24,8 +25,8 @@ SourceTreeItem::SourceTreeItem( const source_ptr& source, QObject* parent ) { QStandardItem* item = new QStandardItem( "" ); item->setEditable( false ); - item->setData( 0, Qt::UserRole + 1 ); - item->setData( (qlonglong)this, Qt::UserRole + 2 ); + item->setData( SourcesModel::CollectionSource, Type ); + item->setData( (qlonglong)this, SourceItemPointer ); m_columns << item; if ( !source.isNull() ) @@ -123,9 +124,9 @@ SourceTreeItem::playlistLoaded( PlaylistRevision revision, bool dynamic ) QStandardItem* pi = item->child( i ); qlonglong piptr = pi->data( PlaylistPointer ).toLongLong(); playlist_ptr* pl = reinterpret_cast(piptr); - int type = pi->data( Type ).toInt(); + SourcesModel::SourceType type = static_cast( pi->data( Type ).toInt() ); - if ( type == 1 && ptr == qlonglong( pl->data() ) ) + if ( ( type == SourcesModel::PlaylistSource || type == SourcesModel::DynamicPlaylistSource ) && ptr == qlonglong( pl->data() ) ) { //qDebug() << "Found playlist!"; pi->setEnabled( true ); @@ -169,7 +170,7 @@ SourceTreeItem::playlistsAdded( const QList< playlist_ptr >& playlists, bool dyn subitem->setEditable( false ); subitem->setEnabled( false ); subitem->setData( ptr, PlaylistPointer ); - subitem->setData( 1, Type ); + subitem->setData( dynamic ? SourcesModel::DynamicPlaylistSource : SourcesModel::DynamicPlaylistSource, Type ); subitem->setData( (qlonglong)this, SourceItemPointer ); m_columns.at( 0 )->appendRow( subitem ); @@ -196,9 +197,9 @@ SourceTreeItem::playlistsDeleted( const QList< playlist_ptr >& playlists, bool d QStandardItem* pi = item->child( i ); qlonglong piptr = pi->data( PlaylistPointer ).toLongLong(); playlist_ptr* pl = reinterpret_cast(piptr); - int type = pi->data( Type ).toInt(); + SourcesModel::SourceType type = static_cast( pi->data( Type ).toInt() ); - if ( type == 1 && ptr == qlonglong( pl->data() ) ) + if ( ( type == SourcesModel::PlaylistSource || type == SourcesModel::DynamicPlaylistSource ) && ptr == qlonglong( pl->data() ) ) { if( dynamic ) m_dynplaylists.removeAll( p.staticCast() ); diff --git a/src/sourcetree/sourcetreeitem.h b/src/sourcetree/sourcetreeitem.h index 434ae4bce..4117a6a1a 100644 --- a/src/sourcetree/sourcetreeitem.h +++ b/src/sourcetree/sourcetreeitem.h @@ -11,14 +11,16 @@ class SourceTreeItem : public QObject { Q_OBJECT - -enum PlaylistItemType { - Type = Qt::UserRole + 1, - SourceItemPointer = Qt::UserRole + 2, - PlaylistPointer = Qt::UserRole + 3 -}; - public: + + enum PlaylistItemType { + Type = Qt::UserRole + 1, /// Value is SourcesModel::SourceType + SourceItemPointer = Qt::UserRole + 2, /// value is the sourcetreeritem of the collection itself. + PlaylistPointer = Qt::UserRole + 3, /// Value is the playlist_ptr.data() + DynamicPlaylistPointer = Qt::UserRole + 4 /// Value is the playlist_ptr.data() + }; + + explicit SourceTreeItem( const Tomahawk::source_ptr& source, QObject* parent ); virtual ~SourceTreeItem(); diff --git a/src/sourcetree/sourcetreeview.cpp b/src/sourcetree/sourcetreeview.cpp index 6c0d1bb73..310818e64 100644 --- a/src/sourcetree/sourcetreeview.cpp +++ b/src/sourcetree/sourcetreeview.cpp @@ -87,8 +87,8 @@ SourceTreeView::setupMenus() m_deletePlaylistAction = m_playlistMenu.addAction( tr( "&Delete Playlist" ) ); bool readonly = true; - int type = SourcesModel::indexType( m_contextMenuIndex ); - if ( type == 1 ) + SourcesModel::SourceType type = SourcesModel::indexType( m_contextMenuIndex ); + if ( type == SourcesModel::PlaylistSource ) { playlist_ptr playlist = SourcesModel::indexToPlaylist( m_contextMenuIndex ); if ( !playlist.isNull() ) @@ -120,8 +120,8 @@ SourceTreeView::onItemActivated( const QModelIndex& index ) if ( !index.isValid() ) return; - int type = SourcesModel::indexType( index ); - if ( type == 0 ) + SourcesModel::SourceType type = SourcesModel::indexType( index ); + if ( type == SourcesModel::CollectionSource ) { SourceTreeItem* item = SourcesModel::indexToTreeItem( index ); if ( item ) @@ -139,13 +139,23 @@ SourceTreeView::onItemActivated( const QModelIndex& index ) } } } - else if ( type == 1 ) + else if ( type == SourcesModel::PlaylistSource ) { playlist_ptr playlist = SourcesModel::indexToPlaylist( index ); if ( !playlist.isNull() ) { qDebug() << "Playlist activated:" << playlist->title(); + APP->playlistManager()->show( playlist ); + } + } + else if ( type == SourcesModel::DynamicPlaylistSource ) + { + dynplaylist_ptr playlist = SourcesModel::indexToDynamicPlaylist( index ); + if ( !playlist.isNull() ) + { + qDebug() << "Dynamic Playlist activated:" << playlist->title(); + APP->playlistManager()->show( playlist ); } } @@ -174,8 +184,8 @@ SourceTreeView::deletePlaylist() if ( !idx.isValid() ) return; - int type = SourcesModel::indexType( idx ); - if ( type == 1 ) + SourcesModel::SourceType type = SourcesModel::indexType( idx ); + if ( type == SourcesModel::PlaylistSource ) { playlist_ptr playlist = SourcesModel::indexToPlaylist( idx ); if ( !playlist.isNull() ) @@ -240,7 +250,7 @@ SourceTreeView::dragMoveEvent( QDragMoveEvent* event ) const QRect rect = visualRect( index ); m_dropRect = rect; - if ( SourcesModel::indexType( index ) == 1 ) + if ( SourcesModel::indexType( index ) == SourcesModel::PlaylistSource ) { playlist_ptr playlist = SourcesModel::indexToPlaylist( index ); if ( !playlist.isNull() && playlist->author()->isLocal() ) @@ -277,7 +287,7 @@ SourceTreeView::dropEvent( QDropEvent* event ) if ( index.isValid() ) { - if ( SourcesModel::indexType( index ) == 1 ) + if ( SourcesModel::indexType( index ) == SourcesModel::PlaylistSource ) { playlist_ptr playlist = SourcesModel::indexToPlaylist( index ); if ( !playlist.isNull() && playlist->author()->isLocal() ) @@ -364,7 +374,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co { o.state = QStyle::State_Enabled; - if ( SourcesModel::indexType( index ) == 1 && + if ( SourcesModel::indexType( index ) == SourcesModel::PlaylistSource && ( option.state & QStyle::State_Selected ) == QStyle::State_Selected ) { o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) );