PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 24ms RepoModel.GetById 36ms app.codeStats 0ms

/src/libtomahawk/playlist/albumview.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 228 lines | 165 code | 44 blank | 19 comment | 21 complexity | ab7eeb4ec18f3f88eedeebbde48d1d4d MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "albumview.h"
 20
 21#include <QHeaderView>
 22#include <QKeyEvent>
 23#include <QPainter>
 24#include <QScrollBar>
 25#include <qmath.h>
 26
 27#include "audio/audioengine.h"
 28#include "tomahawksettings.h"
 29#include "artist.h"
 30#include "albumitem.h"
 31#include "albumitemdelegate.h"
 32#include "albummodel.h"
 33#include "viewmanager.h"
 34#include "utils/logger.h"
 35#include "dynamic/widgets/LoadingSpinner.h"
 36
 37#define SCROLL_TIMEOUT 280
 38
 39using namespace Tomahawk;
 40
 41
 42AlbumView::AlbumView( QWidget* parent )
 43    : QListView( parent )
 44    , m_model( 0 )
 45    , m_proxyModel( 0 )
 46    , m_delegate( 0 )
 47    , m_loadingSpinner( new LoadingSpinner( this ) )
 48    , m_overlay( new OverlayWidget( this ) )
 49    , m_inited( false )
 50{
 51    setDragEnabled( true );
 52    setDropIndicatorShown( false );
 53    setDragDropOverwriteMode( false );
 54    setUniformItemSizes( true );
 55    setSpacing( 16 );
 56    setContentsMargins( 0, 0, 0, 0 );
 57    setMouseTracking( true );
 58
 59    setResizeMode( Adjust );
 60    setViewMode( IconMode );
 61    setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
 62
 63    setAutoFitItems( true );
 64    setProxyModel( new AlbumProxyModel( this ) );
 65
 66    connect( this, SIGNAL( doubleClicked( QModelIndex ) ), SLOT( onItemActivated( QModelIndex ) ) );
 67}
 68
 69
 70AlbumView::~AlbumView()
 71{
 72    qDebug() << Q_FUNC_INFO;
 73}
 74
 75
 76void
 77AlbumView::setProxyModel( AlbumProxyModel* model )
 78{
 79    m_proxyModel = model;
 80    m_delegate = new AlbumItemDelegate( this, m_proxyModel );
 81    connect( m_delegate, SIGNAL( updateIndex( QModelIndex ) ), this, SLOT( update( QModelIndex ) ) );
 82    setItemDelegate( m_delegate );
 83
 84    QListView::setModel( m_proxyModel );
 85}
 86
 87
 88void
 89AlbumView::setModel( QAbstractItemModel* model )
 90{
 91    Q_UNUSED( model );
 92    qDebug() << "Explicitly use setAlbumModel instead";
 93    Q_ASSERT( false );
 94}
 95
 96
 97void
 98AlbumView::setAlbumModel( AlbumModel* model )
 99{
100    m_model = model;
101
102    if ( m_proxyModel )
103    {
104        m_proxyModel->setSourceAlbumModel( m_model );
105        m_proxyModel->sort( 0 );
106    }
107
108    connect( m_proxyModel, SIGNAL( filterChanged( QString ) ), SLOT( onFilterChanged( QString ) ) );
109
110    connect( m_model, SIGNAL( itemCountChanged( unsigned int ) ), SLOT( onItemCountChanged( unsigned int ) ) );
111    connect( m_model, SIGNAL( loadingStarted() ), m_loadingSpinner, SLOT( fadeIn() ) );
112    connect( m_model, SIGNAL( loadingFinished() ), m_loadingSpinner, SLOT( fadeOut() ) );
113}
114
115
116void
117AlbumView::onItemActivated( const QModelIndex& index )
118{
119    AlbumItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( index ) );
120    if ( item )
121    {
122//        qDebug() << "Result activated:" << item->album()->tracks().first()->toString() << item->album()->tracks().first()->results().first()->url();
123//        APP->audioEngine()->playItem( item->album().data(), item->album()->tracks().first()->results().first() );
124
125        if ( !item->album().isNull() )
126            ViewManager::instance()->show( item->album() );
127        else if ( !item->artist().isNull() )
128            ViewManager::instance()->show( item->artist() );
129    }
130}
131
132
133void
134AlbumView::onItemCountChanged( unsigned int items )
135{
136    if ( items == 0 )
137    {
138        if ( m_model->collection().isNull() || ( !m_model->collection().isNull() && m_model->collection()->source()->isLocal() ) )
139            m_overlay->setText( tr( "After you have scanned your music collection you will find your latest album additions right here." ) );
140        else
141            m_overlay->setText( tr( "This collection doesn't have any recent albums." ) );
142
143        m_overlay->show();
144    }
145    else
146        m_overlay->hide();
147}
148
149
150void
151AlbumView::paintEvent( QPaintEvent* event )
152{
153    if ( !autoFitItems() || m_inited || !m_proxyModel->rowCount() )
154        QListView::paintEvent( event );
155}
156
157
158void
159AlbumView::resizeEvent( QResizeEvent* event )
160{
161    if ( autoFitItems() )
162    {
163#ifdef Q_WS_X11
164        int scrollbar = verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0;
165#else
166        int scrollbar = verticalScrollBar()->rect().width();
167#endif
168        int rectWidth = contentsRect().width() - scrollbar - 16 - 3;
169        QSize itemSize = m_proxyModel->data( QModelIndex(), Qt::SizeHintRole ).toSize();
170
171        int itemsPerRow = qFloor( rectWidth / ( itemSize.width() + 16 ) );
172        int rightSpacing = rectWidth - ( itemsPerRow * ( itemSize.width() + 16 ) );
173        int newSpacing = 16 + floor( rightSpacing / ( itemsPerRow + 1 ) );
174
175        if ( itemsPerRow < 1 )
176            setSpacing( 16 );
177        else
178            setSpacing( newSpacing );
179
180        if ( !m_inited )
181        {
182            m_inited = true;
183            repaint();
184        }
185    }
186
187    QListView::resizeEvent( event );
188}
189
190
191void
192AlbumView::onFilterChanged( const QString& )
193{
194    if ( selectedIndexes().count() )
195        scrollTo( selectedIndexes().at( 0 ), QAbstractItemView::PositionAtCenter );
196}
197
198
199void
200AlbumView::startDrag( Qt::DropActions supportedActions )
201{
202    QList<QPersistentModelIndex> pindexes;
203    QModelIndexList indexes;
204    foreach( const QModelIndex& idx, selectedIndexes() )
205    {
206        if ( ( m_proxyModel->flags( idx ) & Qt::ItemIsDragEnabled ) )
207        {
208            indexes << idx;
209            pindexes << idx;
210        }
211    }
212
213    if ( indexes.count() == 0 )
214        return;
215
216    qDebug() << "Dragging" << indexes.count() << "indexes";
217    QMimeData* data = m_proxyModel->mimeData( indexes );
218    if ( !data )
219        return;
220
221    QDrag* drag = new QDrag( this );
222    drag->setMimeData( data );
223    const QPixmap p = TomahawkUtils::createDragPixmap( TomahawkUtils::MediaTypeAlbum, indexes.count() );
224    drag->setPixmap( p );
225    drag->setHotSpot( QPoint( -20, -20 ) );
226
227    /* Qt::DropAction action = */ drag->exec( supportedActions, Qt::CopyAction );
228}