Tomahawk::result_ptr PlayableProxyModelPlaylistInterface::currentItem() const { if ( m_proxyModel.isNull() ) return Tomahawk::result_ptr(); PlayableProxyModel* proxyModel = m_proxyModel.data(); PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( proxyModel->currentIndex() ) ); if ( item && !item->query().isNull() && item->query()->playable() ) return item->query()->results().at( 0 ); return Tomahawk::result_ptr(); }
QList< Tomahawk::query_ptr > PlayableProxyModelPlaylistInterface::tracks() { if ( m_proxyModel.isNull() ) return QList< Tomahawk::query_ptr >(); PlayableProxyModel* proxyModel = m_proxyModel.data(); QList<Tomahawk::query_ptr> queries; for ( int i = 0; i < proxyModel->rowCount( QModelIndex() ); i++ ) { PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( proxyModel->index( i, 0 ) ) ); if ( item ) queries << item->query(); } return queries; }
void OverlayWidget::onViewChanged() { if ( !m_itemView ) return; PlayableProxyModel* model = qobject_cast<PlayableProxyModel*>( m_itemView->model() ); if ( !model ) return; if ( m_text.isEmpty() || model->rowCount( QModelIndex() ) || model->isLoading() ) { hide(); } else { show(); } }
Tomahawk::result_ptr PlayableProxyModelPlaylistInterface::siblingItem( int itemsAway, bool readOnly ) { qDebug() << Q_FUNC_INFO; if ( m_proxyModel.isNull() ) return Tomahawk::result_ptr(); PlayableProxyModel* proxyModel = m_proxyModel.data(); QModelIndex idx = proxyModel->index( 0, 0 ); if ( proxyModel->rowCount() ) { if ( m_shuffled ) { // random mode is enabled // TODO come up with a clever random logic, that keeps track of previously played items idx = proxyModel->index( qrand() % proxyModel->rowCount(), 0 ); } else if ( proxyModel->currentIndex().isValid() ) { idx = proxyModel->currentIndex(); // random mode is disabled if ( m_repeatMode != PlaylistModes::RepeatOne ) { // keep progressing through the playlist normally idx = proxyModel->index( idx.row() + itemsAway, 0 ); } } } if ( !idx.isValid() && m_repeatMode == PlaylistModes::RepeatAll ) { // repeat all tracks if ( itemsAway > 0 ) { // reset to first item idx = proxyModel->index( 0, 0 ); } else { // reset to last item idx = proxyModel->index( proxyModel->rowCount() - 1, 0 ); } } // Try to find the next available PlaylistItem (with results) while ( idx.isValid() ) { PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) ); if ( item && item->query()->playable() ) { qDebug() << "Next PlaylistItem found:" << item->query()->toString() << item->query()->results().at( 0 )->url(); if ( !readOnly ) proxyModel->setCurrentIndex( idx ); return item->query()->results().at( 0 ); } idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0 ); } if ( !readOnly ) proxyModel->setCurrentIndex( QModelIndex() ); return Tomahawk::result_ptr(); }
qint64 PlayableProxyModelPlaylistInterface::siblingIndex( int itemsAway, qint64 rootIndex ) const { if ( m_proxyModel.isNull() ) return -1; PlayableProxyModel* proxyModel = m_proxyModel.data(); while ( m_shuffleHistory.count() && m_shuffleHistory.count() >= proxyModel->rowCount() ) { m_shuffleHistory.removeFirst(); } QModelIndex idx = QModelIndex(); if ( proxyModel->rowCount() ) { if ( m_shuffled ) { if ( itemsAway < 0 ) { if ( m_shuffleHistory.count() > 1 ) { if ( proxyModel->itemFromQuery( m_shuffleHistory.at( m_shuffleHistory.count() - 2 ) ) ) idx = proxyModel->mapFromSource( proxyModel->itemFromQuery( m_shuffleHistory.at( m_shuffleHistory.count() - 2 ) )->index ); } else return -1; } else { // random mode is enabled if ( m_shuffleCache.isValid() ) { idx = m_shuffleCache; } else { int safetyCounter = 0; PlayableItem* item = 0; do { safetyCounter++; idx = proxyModel->index( qrand() % proxyModel->rowCount(), 0 ); item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) ); } while ( safetyCounter < proxyModel->rowCount() && ( !item || !item->query()->playable() || m_shuffleHistory.contains( item->query() ) ) ); if ( item && item->query()->playable() ) { m_shuffleCache = idx; tDebug( LOGVERBOSE ) << "Next shuffled PlaylistItem cached:" << item->query()->toString() << item->query()->results().at( 0 )->url() << "- after" << safetyCounter << "tries to find a track"; } else { tDebug() << Q_FUNC_INFO << "Error finding next shuffled playable track"; } } } } else { if ( m_repeatMode == PlaylistModes::RepeatOne ) { idx = proxyModel->currentIndex(); } else { // random mode is disabled if ( rootIndex == -1 ) { idx = proxyModel->currentIndex(); } else { PlayableItem* pitem = reinterpret_cast<PlayableItem*>( (void*)rootIndex ); if ( !pitem || !pitem->index.isValid() ) return -1; idx = proxyModel->mapFromSource( pitem->index ); } idx = proxyModel->index( idx.row() + itemsAway, 0 ); } } } if ( !idx.isValid() && m_repeatMode == PlaylistModes::RepeatAll ) { // repeat all tracks if ( itemsAway > 0 ) { // reset to first item idx = proxyModel->index( 0, 0 ); } else { // reset to last item idx = proxyModel->index( proxyModel->rowCount() - 1, 0 ); } } while ( idx.isValid() ) { PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) ); if ( item ) { return (qint64)( item->index.internalPointer() ); } idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0 ); } return -1; }
CollectionViewPage::CollectionViewPage( const Tomahawk::collection_ptr& collection, QWidget* parent ) : QWidget( parent ) , m_header( new FilterHeader( this ) ) , m_columnView( new ColumnView() ) , m_trackView( new TrackView() ) , m_albumView( new GridView() ) , m_model( 0 ) , m_flatModel( 0 ) , m_albumModel( 0 ) , m_playlistInterface( new MetaPlaylistInterface() ) { qRegisterMetaType< CollectionViewPageMode >( "CollectionViewPageMode" ); m_header->setBackground( ImageRegistry::instance()->pixmap( RESPATH "images/collection_background.png", QSize( 0, 0 ) ), true ); setPixmap( TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultCollection, TomahawkUtils::Original, QSize( 256, 256 ) ) ); m_columnView->proxyModel()->setStyle( PlayableProxyModel::SingleColumn ); PlayableProxyModel* trackViewProxyModel = m_trackView->proxyModel(); if ( collection->backendType() == Collection::ScriptCollectionType ) { m_trackView->proxyModel()->setStyle( PlayableProxyModel::Locker ); } else { m_trackView->proxyModel()->setStyle( PlayableProxyModel::Collection ); } // mapSourceColumnToColumn depends on the just set PlayableProxyModel::PlayableItemStyle m_trackView->setColumnHidden( trackViewProxyModel->mapSourceColumnToColumn( PlayableModel::Composer ), true ); m_trackView->setColumnHidden( trackViewProxyModel->mapSourceColumnToColumn( PlayableModel::Origin ), true ); m_trackView->setColumnHidden( trackViewProxyModel->mapSourceColumnToColumn( PlayableModel::Score ), true ); m_trackView->setColumnHidden( trackViewProxyModel->mapSourceColumnToColumn( PlayableModel::Bitrate ), true ); m_trackView->setGuid( QString( "trackview/flat" ) ); m_trackView->setSortingEnabled( true ); m_trackView->sortByColumn( 0, Qt::AscendingOrder ); { m_albumView->setAutoResize( false ); m_albumView->setAutoFitItems( true ); m_albumView->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); m_albumView->setItemWidth( TomahawkUtils::DpiScaler::scaledX( this, 170 ) ); m_albumView->delegate()->setWordWrapping( true ); m_albumView->setEmptyTip( tr( "Sorry, there are no albums in this collection!" ) ); TomahawkStyle::stylePageFrame( m_albumView ); m_albumView->setStyleSheet( QString( "QListView { background-color: %1; }" ).arg( TomahawkStyle::PAGE_BACKGROUND.name() ) ); } m_stack = new QStackedWidget(); setLayout( new QVBoxLayout() ); TomahawkUtils::unmarginLayout( layout() ); { m_header->ui->anchor1Label->setText( tr( "Artists" ) ); m_header->ui->anchor2Label->setText( tr( "Albums" ) ); m_header->ui->anchor3Label->setText( tr( "Songs" ) ); if ( collection->browseCapabilities().contains( Collection::CapabilityBrowseArtists ) ) m_header->ui->anchor1Label->show(); if ( collection->browseCapabilities().contains( Collection::CapabilityBrowseAlbums ) ) m_header->ui->anchor2Label->show(); if ( collection->browseCapabilities().contains( Collection::CapabilityBrowseTracks ) ) m_header->ui->anchor3Label->show(); const float lowOpacity = 0.8; m_header->ui->anchor1Label->setOpacity( 1 ); m_header->ui->anchor2Label->setOpacity( lowOpacity ); m_header->ui->anchor3Label->setOpacity( lowOpacity ); QFontMetrics fm( m_header->ui->anchor1Label->font() ); m_header->ui->anchor1Label->setFixedWidth( fm.width( m_header->ui->anchor1Label->text() ) + 16 ); m_header->ui->anchor2Label->setFixedWidth( fm.width( m_header->ui->anchor2Label->text() ) + 16 ); m_header->ui->anchor3Label->setFixedWidth( fm.width( m_header->ui->anchor3Label->text() ) + 16 ); NewClosure( m_header->ui->anchor1Label, SIGNAL( clicked() ), const_cast< CollectionViewPage* >( this ), SLOT( setCurrentMode( CollectionViewPageMode ) ), CollectionViewPage::Columns )->setAutoDelete( false ); NewClosure( m_header->ui->anchor2Label, SIGNAL( clicked() ), const_cast< CollectionViewPage* >( this ), SLOT( setCurrentMode( CollectionViewPageMode ) ), CollectionViewPage::Albums )->setAutoDelete( false ); NewClosure( m_header->ui->anchor3Label, SIGNAL( clicked() ), const_cast< CollectionViewPage* >( this ), SLOT( setCurrentMode( CollectionViewPageMode ) ), CollectionViewPage::Flat )->setAutoDelete( false ); } if ( collection->backendType() == Collection::ScriptCollectionType ) { m_downloadAllButton = m_header->addButton( tr( "Download All" ) ); connect( m_downloadAllButton, SIGNAL( clicked() ), SLOT( onDownloadAll() ) ); connect( DownloadManager::instance(), SIGNAL( stateChanged( DownloadManager::DownloadManagerState, DownloadManager::DownloadManagerState ) ), SLOT( onDownloadManagerStateChanged( DownloadManager::DownloadManagerState, DownloadManager::DownloadManagerState ) ) ); m_header->setRefreshVisible( true ); connect( m_header, SIGNAL( refresh() ), SLOT( onCollectionChanged() ) ); } layout()->addWidget( m_header ); layout()->addWidget( m_stack ); m_stack->addWidget( m_columnView ); m_stack->addWidget( m_albumView ); m_stack->addWidget( m_trackView ); connect( m_header, SIGNAL( filterTextChanged( QString ) ), SLOT( setFilter( QString ) ) ); m_playlistInterface->addChildInterface( m_trackView->playlistInterface() ); m_playlistInterface->addChildInterface( m_albumView->playlistInterface() ); m_playlistInterface->addChildInterface( m_columnView->proxyModel()->playlistInterface() ); loadCollection( collection ); }