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

refactor the sourcetreeitem a bit so an item always exists properly as soon as it is constructed

that way the model's rowForItem() will work with any just-constructed SourceTreeItem

make the treeview open the playlists when you open the collection.
This commit is contained in:
Leo Franchi
2011-04-01 15:27:09 -04:00
parent ed27dcdeec
commit 2f7167037a
8 changed files with 40 additions and 77 deletions

View File

@@ -33,12 +33,9 @@ SourcesModel::SourcesModel( QObject* parent )
: QAbstractItemModel( parent )
{
m_rootItem = new SourceTreeItem( this, 0, Invalid );
<<<<<<< HEAD
=======
appendItem( source_ptr() );
>>>>>>> add supercollection back
onSourcesAdded( SourceList::instance()->sources() );
connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ) );
@@ -89,18 +86,7 @@ SourcesModel::rowCount( const QModelIndex& parent ) const
return itemFromIndex( parent )->children().count();
}
<<<<<<< HEAD
bool
SourcesModel::hasChildren( const QModelIndex& parent ) const
{
return rowCount( parent ) > 0;
}
QModelIndex
=======
QModelIndex
>>>>>>> add supercollection back
SourcesModel::parent( const QModelIndex& child ) const
{
// qDebug() << Q_FUNC_INFO << child;
@@ -112,13 +98,8 @@ SourcesModel::parent( const QModelIndex& child ) const
SourceTreeItem* parent = node->parent();
if( parent == m_rootItem )
return QModelIndex();
<<<<<<< HEAD
return createIndex( rowForItem( node ), 0, parent );
=======
return createIndex( rowForItem( parent ), 0, parent );
>>>>>>> add supercollection back
}
QModelIndex
@@ -169,9 +150,9 @@ SourcesModel::dropMimeData( const QMimeData* data, Qt::DropAction action, int ro
item = itemFromIndex( parent );
else
item = itemFromIndex( index( row, column, parent ) );
Q_ASSERT( item );
return item->dropMimeData( data, action );
}
@@ -201,16 +182,8 @@ SourcesModel::appendItem( const Tomahawk::source_ptr& source )
CollectionItem* item = new CollectionItem( this, m_rootItem, source );
connect( item, SIGNAL( updated() ), this, SLOT( collectionUpdated() ) );
m_rootItem->appendChild( item );
endInsertRows();
<<<<<<< HEAD
qDebug() << "Appending source item:" << item->source()->friendlyName();
=======
>>>>>>> add supercollection back
if ( !source.isNull() )
{
qDebug() << "Appending source item:" << item->source()->friendlyName();
@@ -250,7 +223,7 @@ SourcesModel::removeItem( const Tomahawk::source_ptr& source )
return false;
}
QModelIndex
QModelIndex
SourcesModel::indexFromPlaylist( const playlist_ptr& playlist )
{
QModelIndex idx;
@@ -260,7 +233,7 @@ SourcesModel::indexFromPlaylist( const playlist_ptr& playlist )
CollectionItem* col = qobject_cast< CollectionItem* >( m_rootItem->children().at( i ) );
if( col->source().isNull() )
continue; // skip super collection
// get the playlist item and look through its children
for( int k = 0; k < col->children().count(); k++ ) {
CategoryItem* cat = qobject_cast< CategoryItem* >( col->children().at( k ) );

View File

@@ -88,9 +88,6 @@ public:
QModelIndex indexFromPlaylist( const Tomahawk::playlist_ptr& playlist );
QModelIndex indexFromItem( SourceTreeItem* item ) const;
signals:
void clicked( const QModelIndex& );
private slots:
void onSourcesAdded( const QList<Tomahawk::source_ptr>& sources );
void onSourceAdded( const Tomahawk::source_ptr& source );

View File

@@ -34,6 +34,8 @@ SourcesProxyModel::SourcesProxyModel( SourcesModel* model, QObject* parent )
// setSortRole( SourcesModel::SortRole );
setSourceModel( model );
connect( model, SIGNAL( askForExpand( QModelIndex ) ), this, SLOT( askedToExpand( QModelIndex ) ) );
}
@@ -42,9 +44,6 @@ SourcesProxyModel::showOfflineSources()
{
m_filtered = false;
invalidateFilter();
// Q_ASSERT( qobject_cast<QTreeView*>( parent() ) );
// qobject_cast<QTreeView*>( parent() )->expandAll();
}
@@ -53,9 +52,6 @@ SourcesProxyModel::hideOfflineSources()
{
m_filtered = true;
invalidateFilter();
// Q_ASSERT( qobject_cast<QTreeView*>( parent() ) );
// qobject_cast<QTreeView*>( parent() )->expandAll();
}

View File

@@ -27,18 +27,25 @@
using namespace Tomahawk;
SourceTreeItem::SourceTreeItem( SourcesModel* model, SourceTreeItem* parent, SourcesModel::RowType thisType )
SourceTreeItem::SourceTreeItem( SourcesModel* model, SourceTreeItem* parent, SourcesModel::RowType thisType, int index )
: QObject()
, m_type( thisType )
, m_parent( parent )
, m_model( model )
{
// connect( this, SIGNAL( selectRequest() ), m_model, SLOT( onItemSelectRequest() ) );
{
connect( this, SIGNAL( beginChildRowsAdded( int,int ) ), m_model, SLOT( onItemRowsAddedBegin( int,int ) ) );
connect( this, SIGNAL( beginChildRowsRemoved( int,int ) ), m_model, SLOT( onItemRowsRemovedBegin( int,int ) ) );
connect( this, SIGNAL( childRowsAdded() ), m_model, SLOT( onItemRowsAddedDone() ) );
connect( this, SIGNAL( childRowsRemoved() ), m_model, SLOT( onItemRowsRemovedDone() ) );
if( !m_parent )
return;
// caller must call begin/endInsertRows
if( index < 0 )
m_parent->appendChild( this );
else
m_parent->insertChild( index, this );
}
@@ -61,8 +68,8 @@ CategoryItem::activate()
/// PlaylistItem
PlaylistItem::PlaylistItem( SourcesModel* mdl, SourceTreeItem* parent, const playlist_ptr& pl )
: SourceTreeItem( mdl, parent, SourcesModel::StaticPlaylist )
PlaylistItem::PlaylistItem( SourcesModel* mdl, SourceTreeItem* parent, const playlist_ptr& pl, int index )
: SourceTreeItem( mdl, parent, SourcesModel::StaticPlaylist, index )
, m_loaded( false )
, m_playlist( pl )
{
@@ -278,7 +285,6 @@ CategoryItem::CategoryItem( SourcesModel* model, SourceTreeItem* parent, Sources
// beginRowsAdded( 0, 0 );
if( m_showAdd ) {
m_addItem = new CategoryAddItem( model, this, m_category );
appendChild( m_addItem );
}
// endRowsAdded();
}
@@ -316,10 +322,8 @@ CollectionItem::CollectionItem( SourcesModel* mdl, SourceTreeItem* parent, cons
}
// create category item
m_playlists = new CategoryItem( model(), this, SourcesModel::PlaylistsCategory, source->isLocal() );
appendChild( m_playlists );
m_stations = new CategoryItem( model(), this, SourcesModel::StationsCategory, source->isLocal() );
appendChild( m_stations );
// ugh :( we're being added by the model, no need to notify for added rows now
m_playlists->blockSignals( true );
@@ -365,14 +369,18 @@ CollectionItem::onPlaylistsAdded( const QList< playlist_ptr >& playlists )
return;
QList< SourceTreeItem* > items;
int addOffset = playlists.first()->author()->isLocal() ? 1 : 0;
int from = m_playlists->children().count() - addOffset;
m_playlists->beginRowsAdded( from, from + playlists.count() - 1 );
foreach( const playlist_ptr& p, playlists )
{
PlaylistItem* plItem = new PlaylistItem( model(), m_playlists, p );
PlaylistItem* plItem = new PlaylistItem( model(), m_playlists, p, m_playlists->children().count() - addOffset );
qDebug() << "Playlist added:" << p->title() << p->creator() << p->info();
p->loadRevision();
items << plItem;
}
m_playlists->insertItems( items );
m_playlists->endRowsAdded();
}
void

View File

@@ -31,7 +31,7 @@ class SourceTreeItem : public QObject
Q_OBJECT
public:
SourceTreeItem() : m_type( SourcesModel::Invalid ), m_parent( 0 ), m_model( 0 ) {}
SourceTreeItem( SourcesModel* model, SourceTreeItem* parent, SourcesModel::RowType thisType );
SourceTreeItem( SourcesModel* model, SourceTreeItem* parent, SourcesModel::RowType thisType, int index = -1 ); // if index is -1, append at end of parent's child list
virtual ~SourceTreeItem();
// generic info used by the tree model
@@ -53,18 +53,17 @@ public:
virtual bool dropMimeData( const QMimeData* data, Qt::DropAction action ) { return false; }
/// don't call me unless you are a sourcetreeitem. i prefer this to making everyone a friend
void beginRowsAdded( int from, int num ) { emit beginChildRowsAdded( from, num ); }
void beginRowsAdded( int from, int to ) { emit beginChildRowsAdded( from, to ); }
void endRowsAdded() { emit childRowsAdded(); }
void beginRowsRemoved( int from, int num ) { emit beginChildRowsRemoved( from, num ); }
void beginRowsRemoved( int from, int to ) { emit beginChildRowsRemoved( from, to ); }
void endRowsRemoved() { emit childRowsRemoved(); }
signals:
void updated();
void selectRequest();
void beginChildRowsAdded( int fromRow, int num );
void beginChildRowsAdded( int fromRow, int toRow );
void childRowsAdded();
void beginChildRowsRemoved( int fromRow, int num );
void beginChildRowsRemoved( int fromRow, int toRow );
void childRowsRemoved();
private:
SourcesModel::RowType m_type;
@@ -147,7 +146,7 @@ class PlaylistItem : public SourceTreeItem
{
Q_OBJECT
public:
PlaylistItem( SourcesModel* model, SourceTreeItem* parent, const Tomahawk::playlist_ptr& pl );
PlaylistItem( SourcesModel* model, SourceTreeItem* parent, const Tomahawk::playlist_ptr& pl, int index = -1 );
virtual QString text() const;
virtual Tomahawk::playlist_ptr playlist() const;

View File

@@ -77,10 +77,13 @@ SourceTreeView::SourceTreeView( QWidget* parent )
setAllColumnsShowFocus( true );
setUniformRowHeights( false );
setIndentation( 16 );
setAnimated( true );
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
// setAnimated( true );
setItemDelegate( new SourceDelegate( this ) );
@@ -237,7 +240,7 @@ SourceTreeView::onItemExpanded( const QModelIndex& idx )
// make sure to expand children nodes for collections
if( idx.data( SourcesModel::SourceTreeItemTypeRole ) == SourcesModel::Collection ) {
for( int i = 0; i < model()->rowCount( idx ); i++ ) {
expand( model()->index( i, 0, idx ) );
setExpanded( model()->index( i, 0, idx ), true );
}
}
}
@@ -266,20 +269,6 @@ SourceTreeView::deletePlaylist()
playlist_ptr playlist = item->playlist();
Playlist::remove( playlist );
}
// SourcesModel::SourceType type = SourcesModel::indexType( idx );
// if ( type == SourcesModel::PlaylistSource )
// {
// playlist_ptr playlist = SourcesModel::indexToPlaylist( idx );
// if ( !playlist.isNull() )
// {
// qDebug() << "Playlist about to be deleted:" << playlist->title();
// Playlist::remove( playlist );
// }
// } else if( type == SourcesModel::DynamicPlaylistSource ) {
// dynplaylist_ptr playlist = SourcesModel::indexToDynamicPlaylist( idx );
// if( !playlist.isNull() )
// DynamicPlaylist::remove( playlist );
// }
}

View File

@@ -37,6 +37,7 @@ Q_OBJECT
public:
explicit SourceTreeView( QWidget* parent = 0 );
public slots:
void showOfflineSources();
void hideOfflineSources();