1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-30 01:00:13 +02:00

Rework source tree to use two enums instead of raw ints that drove me crazy...

This commit is contained in:
Leo Franchi
2010-12-10 21:26:33 -05:00
parent d4e95e05d4
commit f1bd808d33
5 changed files with 75 additions and 35 deletions

View File

@@ -50,7 +50,7 @@ SourcesModel::flags( const QModelIndex& index ) const
if ( index.isValid() ) if ( index.isValid() )
{ {
if ( indexType( index ) == 1 ) if ( indexType( index ) == PlaylistSource )
{ {
playlist_ptr playlist = indexToPlaylist( index ); playlist_ptr playlist = indexToPlaylist( index );
if ( !playlist.isNull() && playlist->author()->isLocal() ) if ( !playlist.isNull() && playlist->author()->isLocal() )
@@ -166,14 +166,14 @@ SourcesModel::onItemOffline( const QModelIndex& idx )
} }
int SourcesModel::SourceType
SourcesModel::indexType( const QModelIndex& index ) SourcesModel::indexType( const QModelIndex& index )
{ {
if ( !index.isValid() ) if ( !index.isValid() )
return -1; return Invalid;
QModelIndex idx = index.model()->index( index.row(), 0, index.parent() ); QModelIndex idx = index.model()->index( index.row(), 0, index.parent() );
return idx.data( Qt::UserRole + 1 ).toInt(); return static_cast<SourcesModel::SourceType>( idx.data( SourceTreeItem::Type ).toInt() );
} }
@@ -184,10 +184,10 @@ SourcesModel::indexToPlaylist( const QModelIndex& index )
if ( !index.isValid() ) if ( !index.isValid() )
return res; return res;
if ( indexType( index ) == 1 ) if ( indexType( index ) == PlaylistSource )
{ {
QModelIndex idx = index.model()->index( index.row(), 0, index.parent() ); 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<playlist_ptr*>(pptr); playlist_ptr* playlist = reinterpret_cast<playlist_ptr*>(pptr);
if ( playlist ) if ( playlist )
return *playlist; return *playlist;
@@ -196,6 +196,24 @@ SourcesModel::indexToPlaylist( const QModelIndex& index )
return res; 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<dynplaylist_ptr*>(pptr);
if ( playlist )
return *playlist;
}
return res;
}
SourceTreeItem* SourceTreeItem*
SourcesModel::indexToTreeItem( const QModelIndex& index ) SourcesModel::indexToTreeItem( const QModelIndex& index )
@@ -204,10 +222,10 @@ SourcesModel::indexToTreeItem( const QModelIndex& index )
return 0; return 0;
int type = indexType( index ); 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() ); 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<SourceTreeItem*>(pptr); SourceTreeItem* item = reinterpret_cast<SourceTreeItem*>(pptr);
if ( item ) if ( item )
return item; return item;
@@ -225,7 +243,7 @@ SourcesModel::setData( const QModelIndex& index, const QVariant& value, int role
if ( !index.isValid() ) if ( !index.isValid() )
return false; return false;
if ( indexType( index ) == 1 ) if ( indexType( index ) == PlaylistSource )
{ {
playlist_ptr playlist = indexToPlaylist( index ); playlist_ptr playlist = indexToPlaylist( index );
if ( !playlist.isNull() ) if ( !playlist.isNull() )

View File

@@ -12,7 +12,15 @@ class SourcesModel : public QStandardItemModel
{ {
Q_OBJECT Q_OBJECT
public: public:
enum SourceType {
Invalid = -1,
CollectionSource = 0,
PlaylistSource = 1,
DynamicPlaylistSource = 2
};
explicit SourcesModel( QObject* parent = 0 ); explicit SourcesModel( QObject* parent = 0 );
virtual QStringList mimeTypes() const; virtual QStringList mimeTypes() const;
@@ -23,8 +31,9 @@ public:
bool appendItem( const Tomahawk::source_ptr& source ); bool appendItem( const Tomahawk::source_ptr& source );
bool removeItem( 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::playlist_ptr indexToPlaylist( const QModelIndex& index );
static Tomahawk::dynplaylist_ptr indexToDynamicPlaylist( const QModelIndex& index );
static SourceTreeItem* indexToTreeItem( const QModelIndex& index ); static SourceTreeItem* indexToTreeItem( const QModelIndex& index );
signals: signals:

View File

@@ -6,6 +6,7 @@
#include "tomahawk/collection.h" #include "tomahawk/collection.h"
#include "tomahawk/playlist.h" #include "tomahawk/playlist.h"
#include "tomahawk/tomahawkapp.h" #include "tomahawk/tomahawkapp.h"
#include "sourcesmodel.h"
using namespace Tomahawk; using namespace Tomahawk;
@@ -24,8 +25,8 @@ SourceTreeItem::SourceTreeItem( const source_ptr& source, QObject* parent )
{ {
QStandardItem* item = new QStandardItem( "" ); QStandardItem* item = new QStandardItem( "" );
item->setEditable( false ); item->setEditable( false );
item->setData( 0, Qt::UserRole + 1 ); item->setData( SourcesModel::CollectionSource, Type );
item->setData( (qlonglong)this, Qt::UserRole + 2 ); item->setData( (qlonglong)this, SourceItemPointer );
m_columns << item; m_columns << item;
if ( !source.isNull() ) if ( !source.isNull() )
@@ -123,9 +124,9 @@ SourceTreeItem::playlistLoaded( PlaylistRevision revision, bool dynamic )
QStandardItem* pi = item->child( i ); QStandardItem* pi = item->child( i );
qlonglong piptr = pi->data( PlaylistPointer ).toLongLong(); qlonglong piptr = pi->data( PlaylistPointer ).toLongLong();
playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr); playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr);
int type = pi->data( Type ).toInt(); SourcesModel::SourceType type = static_cast<SourcesModel::SourceType>( 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!"; //qDebug() << "Found playlist!";
pi->setEnabled( true ); pi->setEnabled( true );
@@ -169,7 +170,7 @@ SourceTreeItem::playlistsAdded( const QList< playlist_ptr >& playlists, bool dyn
subitem->setEditable( false ); subitem->setEditable( false );
subitem->setEnabled( false ); subitem->setEnabled( false );
subitem->setData( ptr, PlaylistPointer ); subitem->setData( ptr, PlaylistPointer );
subitem->setData( 1, Type ); subitem->setData( dynamic ? SourcesModel::DynamicPlaylistSource : SourcesModel::DynamicPlaylistSource, Type );
subitem->setData( (qlonglong)this, SourceItemPointer ); subitem->setData( (qlonglong)this, SourceItemPointer );
m_columns.at( 0 )->appendRow( subitem ); m_columns.at( 0 )->appendRow( subitem );
@@ -196,9 +197,9 @@ SourceTreeItem::playlistsDeleted( const QList< playlist_ptr >& playlists, bool d
QStandardItem* pi = item->child( i ); QStandardItem* pi = item->child( i );
qlonglong piptr = pi->data( PlaylistPointer ).toLongLong(); qlonglong piptr = pi->data( PlaylistPointer ).toLongLong();
playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr); playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr);
int type = pi->data( Type ).toInt(); SourcesModel::SourceType type = static_cast<SourcesModel::SourceType>( pi->data( Type ).toInt() );
if ( type == 1 && ptr == qlonglong( pl->data() ) ) if ( ( type == SourcesModel::PlaylistSource || type == SourcesModel::DynamicPlaylistSource ) && ptr == qlonglong( pl->data() ) )
{ {
if( dynamic ) if( dynamic )
m_dynplaylists.removeAll( p.staticCast<Tomahawk::DynamicPlaylist>() ); m_dynplaylists.removeAll( p.staticCast<Tomahawk::DynamicPlaylist>() );

View File

@@ -11,14 +11,16 @@
class SourceTreeItem : public QObject class SourceTreeItem : public QObject
{ {
Q_OBJECT Q_OBJECT
enum PlaylistItemType {
Type = Qt::UserRole + 1,
SourceItemPointer = Qt::UserRole + 2,
PlaylistPointer = Qt::UserRole + 3
};
public: 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 ); explicit SourceTreeItem( const Tomahawk::source_ptr& source, QObject* parent );
virtual ~SourceTreeItem(); virtual ~SourceTreeItem();

View File

@@ -87,8 +87,8 @@ SourceTreeView::setupMenus()
m_deletePlaylistAction = m_playlistMenu.addAction( tr( "&Delete Playlist" ) ); m_deletePlaylistAction = m_playlistMenu.addAction( tr( "&Delete Playlist" ) );
bool readonly = true; bool readonly = true;
int type = SourcesModel::indexType( m_contextMenuIndex ); SourcesModel::SourceType type = SourcesModel::indexType( m_contextMenuIndex );
if ( type == 1 ) if ( type == SourcesModel::PlaylistSource )
{ {
playlist_ptr playlist = SourcesModel::indexToPlaylist( m_contextMenuIndex ); playlist_ptr playlist = SourcesModel::indexToPlaylist( m_contextMenuIndex );
if ( !playlist.isNull() ) if ( !playlist.isNull() )
@@ -120,8 +120,8 @@ SourceTreeView::onItemActivated( const QModelIndex& index )
if ( !index.isValid() ) if ( !index.isValid() )
return; return;
int type = SourcesModel::indexType( index ); SourcesModel::SourceType type = SourcesModel::indexType( index );
if ( type == 0 ) if ( type == SourcesModel::CollectionSource )
{ {
SourceTreeItem* item = SourcesModel::indexToTreeItem( index ); SourceTreeItem* item = SourcesModel::indexToTreeItem( index );
if ( item ) 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 ); playlist_ptr playlist = SourcesModel::indexToPlaylist( index );
if ( !playlist.isNull() ) if ( !playlist.isNull() )
{ {
qDebug() << "Playlist activated:" << playlist->title(); 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 ); APP->playlistManager()->show( playlist );
} }
} }
@@ -174,8 +184,8 @@ SourceTreeView::deletePlaylist()
if ( !idx.isValid() ) if ( !idx.isValid() )
return; return;
int type = SourcesModel::indexType( idx ); SourcesModel::SourceType type = SourcesModel::indexType( idx );
if ( type == 1 ) if ( type == SourcesModel::PlaylistSource )
{ {
playlist_ptr playlist = SourcesModel::indexToPlaylist( idx ); playlist_ptr playlist = SourcesModel::indexToPlaylist( idx );
if ( !playlist.isNull() ) if ( !playlist.isNull() )
@@ -240,7 +250,7 @@ SourceTreeView::dragMoveEvent( QDragMoveEvent* event )
const QRect rect = visualRect( index ); const QRect rect = visualRect( index );
m_dropRect = rect; m_dropRect = rect;
if ( SourcesModel::indexType( index ) == 1 ) if ( SourcesModel::indexType( index ) == SourcesModel::PlaylistSource )
{ {
playlist_ptr playlist = SourcesModel::indexToPlaylist( index ); playlist_ptr playlist = SourcesModel::indexToPlaylist( index );
if ( !playlist.isNull() && playlist->author()->isLocal() ) if ( !playlist.isNull() && playlist->author()->isLocal() )
@@ -277,7 +287,7 @@ SourceTreeView::dropEvent( QDropEvent* event )
if ( index.isValid() ) if ( index.isValid() )
{ {
if ( SourcesModel::indexType( index ) == 1 ) if ( SourcesModel::indexType( index ) == SourcesModel::PlaylistSource )
{ {
playlist_ptr playlist = SourcesModel::indexToPlaylist( index ); playlist_ptr playlist = SourcesModel::indexToPlaylist( index );
if ( !playlist.isNull() && playlist->author()->isLocal() ) if ( !playlist.isNull() && playlist->author()->isLocal() )
@@ -364,7 +374,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
{ {
o.state = QStyle::State_Enabled; o.state = QStyle::State_Enabled;
if ( SourcesModel::indexType( index ) == 1 && if ( SourcesModel::indexType( index ) == SourcesModel::PlaylistSource &&
( option.state & QStyle::State_Selected ) == QStyle::State_Selected ) ( option.state & QStyle::State_Selected ) == QStyle::State_Selected )
{ {
o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) ); o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) );