void TreeModel::getCover( const QModelIndex& index ) { TreeModelItem* item = itemFromIndex( index ); if ( !item->cover.isNull() ) return; Tomahawk::InfoSystem::InfoStringHash trackInfo; Tomahawk::InfoSystem::InfoRequestData requestData; if ( !item->artist().isNull() ) { trackInfo["artist"] = item->artist()->name(); requestData.type = Tomahawk::InfoSystem::InfoArtistImages; } else if ( !item->album().isNull() ) { trackInfo["artist"] = item->album()->artist()->name(); trackInfo["album"] = item->album()->name(); requestData.type = Tomahawk::InfoSystem::InfoAlbumCoverArt; } else return; trackInfo["pptr"] = QString::number( (qlonglong)item ); m_coverHash.insert( (qlonglong)item, index ); requestData.caller = m_infoId; requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo ); requestData.customData = QVariantMap(); Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData ); }
void TreeModel::getCover( const QModelIndex& index ) { TreeModelItem* item = itemFromIndex( index ); if ( !item->artist().isNull() && !item->artist()->infoLoaded() ) item->artist()->cover( QSize( 0, 0 ) ); else if ( !item->album().isNull() && !item->album()->infoLoaded() ) item->album()->cover( QSize( 0, 0 ) ); }
void ArtistView::onItemActivated( const QModelIndex& index ) { TreeModelItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( index ) ); if ( item ) { if ( !item->artist().isNull() ) ViewManager::instance()->show( item->artist() ); else if ( !item->album().isNull() ) ViewManager::instance()->show( item->album() ); else if ( !item->result().isNull() ) AudioEngine::instance()->playItem( 0, item->result() ); } }
void ArtistView::onScrollTimeout() { qDebug() << Q_FUNC_INFO; if ( m_timer.isActive() ) m_timer.stop(); QModelIndex left = indexAt( viewport()->rect().topLeft() ); while ( left.isValid() && left.parent().isValid() ) left = left.parent(); QModelIndex right = indexAt( viewport()->rect().bottomLeft() ); while ( right.isValid() && right.parent().isValid() ) right = right.parent(); int max = m_proxyModel->trackCount(); if ( right.isValid() ) max = right.row() + 1; for ( int i = left.row(); i < max; i++ ) { TreeModelItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( m_proxyModel->index( i, 0 ) ) ); Tomahawk::InfoSystem::InfoCriteriaHash trackInfo; trackInfo["artist"] = item->artist()->name(); trackInfo["pptr"] = QString::number( (qlonglong)item ); Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( s_tmInfoIdentifier, Tomahawk::InfoSystem::InfoArtistImages, QVariant::fromValue< Tomahawk::InfoSystem::InfoCriteriaHash >( trackInfo ), Tomahawk::InfoSystem::InfoCustomData() ); } }
void ArtistView::onItemActivated( const QModelIndex& index ) { TreeModelItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( index ) ); if ( item ) { if ( !item->artist().isNull() ) ViewManager::instance()->show( item->artist() ); else if ( !item->album().isNull() ) ViewManager::instance()->show( item->album(), m_model->mode() ); else if ( !item->result().isNull() && item->result()->isOnline() ) { m_model->setCurrentItem( item->index ); AudioEngine::instance()->playItem( m_proxyModel->playlistInterface(), item->result() ); } } }
bool TreeModel::hasChildren( const QModelIndex& parent ) const { TreeModelItem* parentItem = itemFromIndex( parent ); if ( !parentItem ) return false; if ( parentItem == m_rootItem ) return true; return ( !parentItem->artist().isNull() || !parentItem->album().isNull() ); }
void TreeModel::fetchMore( const QModelIndex& parent ) { TreeModelItem* parentItem = itemFromIndex( parent ); if ( !parentItem || parentItem->fetchingMore ) return; parentItem->fetchingMore = true; if ( !parentItem->artist().isNull() ) { tDebug() << Q_FUNC_INFO << "Loading Artist:" << parentItem->artist()->name(); fetchAlbums( parentItem->artist() ); } else if ( !parentItem->album().isNull() ) { tDebug() << Q_FUNC_INFO << "Loading Album:" << parentItem->album()->name(); addTracks( parentItem->album(), parent ); } else Q_ASSERT( false ); }
void TreeProxyModel::onRowsInserted( const QModelIndex& parent, int /* start */, int /* end */ ) { if ( m_filter.isEmpty() ) return; if ( sender() != m_model ) return; TreeModelItem* pi = m_model->itemFromIndex( m_model->index( parent.row(), 0, parent.parent() ) ); if ( pi->artist().isNull() ) return; DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( m_model->collection() ); cmd->setArtist( pi->artist() ); cmd->setFilter( m_filter ); connect( cmd, SIGNAL( albums( QList<Tomahawk::album_ptr>, QVariant ) ), SLOT( onFilterAlbums( QList<Tomahawk::album_ptr> ) ) ); Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) ); }
void ArtistView::currentChanged( const QModelIndex& current, const QModelIndex& previous ) { QTreeView::currentChanged( current, previous ); if ( !m_updateContextView ) return; TreeModelItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( current ) ); if ( item ) { if ( !item->result().isNull() ) ViewManager::instance()->context()->setQuery( item->result()->toQuery() ); else if ( !item->artist().isNull() ) ViewManager::instance()->context()->setArtist( item->artist() ); else if ( !item->album().isNull() ) ViewManager::instance()->context()->setAlbum( item->album() ); else if ( !item->query().isNull() ) ViewManager::instance()->context()->setQuery( item->query() ); } }
void ArtistView::onCustomContextMenu( const QPoint& pos ) { m_contextMenu->clear(); QModelIndex idx = indexAt( pos ); idx = idx.sibling( idx.row(), 0 ); m_contextMenuIndex = idx; if ( !idx.isValid() ) return; QList<query_ptr> queries; QList<artist_ptr> artists; QList<album_ptr> albums; foreach ( const QModelIndex& index, selectedIndexes() ) { if ( index.column() || selectedIndexes().contains( index.parent() ) ) continue; TreeModelItem* item = m_proxyModel->itemFromIndex( m_proxyModel->mapToSource( index ) ); if ( item && !item->result().isNull() ) queries << item->result()->toQuery(); else if ( item && !item->query().isNull() ) queries << item->query(); if ( item && !item->artist().isNull() ) artists << item->artist(); if ( item && !item->album().isNull() ) albums << item->album(); } m_contextMenu->setQueries( queries ); m_contextMenu->setArtists( artists ); m_contextMenu->setAlbums( albums ); m_contextMenu->exec( mapToGlobal( pos ) ); }
Qt::ItemFlags TreeModel::flags( const QModelIndex& index ) const { Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index ); if ( index.isValid() && index.column() == 0 ) { TreeModelItem* item = itemFromIndex( index ); if ( item && !item->result().isNull() ) return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags; if ( item && ( !item->album().isNull() || !item->artist().isNull() ) ) return Qt::ItemIsDragEnabled | defaultFlags; } return defaultFlags; }
bool TreeModel::canFetchMore( const QModelIndex& parent ) const { TreeModelItem* parentItem = itemFromIndex( parent ); if ( parentItem->fetchingMore ) return false; if ( !parentItem->artist().isNull() ) { return true; } else if ( !parentItem->album().isNull() ) { return true; } return false; }
void TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { TreeModelItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); if ( !item ) return; QString text; if ( !item->artist().isNull() ) { text = item->artist()->name(); } else if ( !item->album().isNull() ) { text = item->album()->name(); } else if ( !item->result().isNull() || !item->query().isNull() ) { float opacity = item->result().isNull() ? 0.0 : item->result()->score(); opacity = qMax( (float)0.3, opacity ); QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity ); { QStyleOptionViewItemV4 o = option; initStyleOption( &o, QModelIndex() ); painter->save(); o.palette.setColor( QPalette::Text, textColor ); if ( o.state & QStyle::State_Selected && o.state & QStyle::State_Active ) { o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) ); } if ( item->isPlaying() ) { o.palette.setColor( QPalette::Highlight, o.palette.color( QPalette::Mid ) ); if ( o.state & QStyle::State_Selected ) o.palette.setColor( QPalette::Text, textColor ); o.state |= QStyle::State_Selected; } int oldX = 0; if ( m_view->header()->visualIndex( index.column() ) == 0 ) { oldX = o.rect.x(); o.rect.setX( 0 ); } qApp->style()->drawControl( QStyle::CE_ItemViewItem, &o, painter ); if ( oldX > 0 ) o.rect.setX( oldX ); { QRect r = o.rect.adjusted( 3, 0, 0, 0 ); // Paint Now Playing Speaker Icon if ( item->isPlaying() && m_view->header()->visualIndex( index.column() ) == 0 ) { r.adjust( 0, 0, 0, -3 ); painter->drawPixmap( r.adjusted( 3, 1, 18 - r.width(), 1 ), m_nowPlayingIcon ); r.adjust( 25, 0, 0, 3 ); } painter->setPen( o.palette.text().color() ); QTextOption to( Qt::AlignVCenter ); QString text = painter->fontMetrics().elidedText( index.data().toString(), Qt::ElideRight, r.width() - 3 ); painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, to ); } painter->restore(); } return; } else return; if ( text.trimmed().isEmpty() ) text = tr( "Unknown" ); QStyleOptionViewItemV4 opt = option; initStyleOption( &opt, QModelIndex() ); qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); if ( option.state & QStyle::State_Selected ) { opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); } if ( index.column() > 0 ) return; painter->save(); painter->setRenderHint( QPainter::Antialiasing ); painter->setPen( opt.palette.color( QPalette::Text ) ); QRect r = option.rect.adjusted( 4, 4, -option.rect.width() + option.rect.height() - 4, -4 ); // painter->drawPixmap( r, QPixmap( RESPATH "images/cover-shadow.png" ) ); QPixmap cover; if ( !item->album().isNull() ) { cover = item->album()->cover( r.size(), false ); if ( cover.isNull() ) cover = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultAlbumCover, TomahawkUtils::ScaledCover, r.size() ); } else if ( !item->artist().isNull() ) { cover = item->artist()->cover( r.size(), false ); if ( cover.isNull() ) cover = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultArtistImage, TomahawkUtils::ScaledCover, r.size() ); } painter->drawPixmap( r, cover ); QTextOption to; to.setAlignment( Qt::AlignVCenter ); r = option.rect.adjusted( option.rect.height(), 6, -4, -option.rect.height() + 22 ); text = painter->fontMetrics().elidedText( text, Qt::ElideRight, r.width() ); painter->drawText( r, text, to ); painter->restore(); }
QMimeData* TreeModel::mimeData( const QModelIndexList &indexes ) const { qDebug() << Q_FUNC_INFO; QByteArray resultData; QDataStream resultStream( &resultData, QIODevice::WriteOnly ); // lets try with artist only bool fail = false; foreach ( const QModelIndex& i, indexes) { if ( i.column() > 0 || indexes.contains( i.parent() ) ) continue; TreeModelItem* item = itemFromIndex( i ); if ( !item ) continue; if ( !item->artist().isNull() ) { const artist_ptr& artist = item->artist(); resultStream << artist->name(); } else { fail = true; break; } } if ( !fail ) { QMimeData* mimeData = new QMimeData(); mimeData->setData( "application/tomahawk.metadata.artist", resultData ); return mimeData; } // lets try with album only fail = false; resultData.clear(); foreach ( const QModelIndex& i, indexes ) { if ( i.column() > 0 || indexes.contains( i.parent() ) ) continue; TreeModelItem* item = itemFromIndex( i ); if ( !item ) continue; if ( !item->album().isNull() ) { const album_ptr& album = item->album(); resultStream << album->artist()->name(); resultStream << album->name(); } else { fail = true; break; } } if ( !fail ) { QMimeData* mimeData = new QMimeData(); mimeData->setData( "application/tomahawk.metadata.album", resultData ); return mimeData; } // lets try with tracks only fail = false; resultData.clear(); foreach ( const QModelIndex& i, indexes ) { if ( i.column() > 0 || indexes.contains( i.parent() ) ) continue; TreeModelItem* item = itemFromIndex( i ); if ( !item ) continue; if ( !item->result().isNull() ) { const result_ptr& result = item->result(); resultStream << qlonglong( &result ); } else { fail = true; break; } } if ( !fail ) { QMimeData* mimeData = new QMimeData(); mimeData->setData( "application/tomahawk.result.list", resultData ); return mimeData; } // Ok... we have to use mixed resultData.clear(); foreach ( const QModelIndex& i, indexes ) { if ( i.column() > 0 || indexes.contains( i.parent() ) ) continue; TreeModelItem* item = itemFromIndex( i ); if ( !item ) continue; if ( !item->artist().isNull() ) { const artist_ptr& artist = item->artist(); resultStream << QString( "application/tomahawk.metadata.artist" ) << artist->name(); } else if ( !item->album().isNull() ) { const album_ptr& album = item->album(); resultStream << QString( "application/tomahawk.metadata.album" ) << album->artist()->name() << album->name(); } else if ( !item->result().isNull() ) { const result_ptr& result = item->result(); resultStream << QString( "application/tomahawk.result.list" ) << qlonglong( &result ); } } QMimeData* mimeData = new QMimeData(); mimeData->setData( "application/tomahawk.mixed", resultData ); return mimeData; }
QVariant TreeModel::data( const QModelIndex& index, int role ) const { TreeModelItem* entry = itemFromIndex( index ); if ( !entry ) return QVariant(); if ( role == Qt::SizeHintRole ) { if ( !entry->result().isNull() || !entry->query().isNull() ) { return QSize( 128, 20 ); } else if ( !entry->album().isNull() ) { return QSize( 128, 32 ); } else if ( !entry->artist().isNull() ) { return QSize( 128, 44 ); } return QSize( 128, 0 ); } if ( role != Qt::DisplayRole ) // && role != Qt::ToolTipRole ) return QVariant(); if ( !entry->artist().isNull() && index.column() == Name ) { return entry->artist()->name(); } else if ( !entry->album().isNull() && index.column() == Name ) { return entry->album()->name(); } else if ( !entry->result().isNull() ) { const result_ptr& result = entry->result(); unsigned int discnumber = 0; if ( !entry->query().isNull() ) discnumber = entry->query()->discnumber(); if ( discnumber == 0 ) discnumber = result->discnumber(); unsigned int albumpos = 0; if ( !entry->query().isNull() ) albumpos = entry->query()->albumpos(); if ( albumpos == 0 ) albumpos = result->albumpos(); switch( index.column() ) { case Name: return QString( "%1%2%3" ).arg( discnumber > 0 ? QString( "%1." ).arg( discnumber ) : QString() ) .arg( albumpos > 0 ? QString( "%1. ").arg( albumpos ) : QString() ) .arg( result->track() ); case Duration: return TomahawkUtils::timeToString( result->duration() ); case Bitrate: if ( result->bitrate() > 0 ) return result->bitrate(); break; case Age: return TomahawkUtils::ageToString( QDateTime::fromTime_t( result->modificationTime() ) ); case Year: if ( result->year() != 0 ) return result->year(); break; case Filesize: return TomahawkUtils::filesizeToString( result->size() ); case Origin: return result->friendlySource(); case AlbumPosition: return result->albumpos(); case Composer: if ( !result->composer().isNull() ) return result->composer()->name(); break; default: return QVariant(); } } else if ( !entry->query().isNull() ) { const query_ptr& query = entry->query(); switch( index.column() ) { case Name: return QString( "%1%2%3" ).arg( query->discnumber() > 0 ? QString( "%1." ).arg( query->discnumber() ) : QString() ) .arg( query->albumpos() > 0 ? QString( "%1. ").arg( query->albumpos() ) : QString() ) .arg( query->track() ); case AlbumPosition: return entry->query()->albumpos(); default: return QVariant(); } } return QVariant(); }