PageRenderTime 143ms CodeModel.GetById 40ms app.highlight 45ms RepoModel.GetById 36ms app.codeStats 0ms

/src/libtomahawk/jobview/JobStatusView.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 234 lines | 176 code | 40 blank | 18 comment | 23 complexity | 0c2d3acfacdd3da8d2e3abf684422429 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2011, Leo Franchi <lfranchi@kde.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "JobStatusView.h"
 21
 22#include "Pipeline.h"
 23#include "AclJobItem.h"
 24#include "JobStatusModel.h"
 25#include "JobStatusItem.h"
 26#include "JobStatusDelegate.h"
 27#include "utils/Logger.h"
 28#include "Source.h"
 29#include "IndexingJobItem.h"
 30#include "PipelineStatusItem.h"
 31#include "ScannerStatusItem.h"
 32#include "TransferStatusItem.h"
 33#include "LatchedStatusItem.h"
 34
 35#include <QHeaderView>
 36#include <QVBoxLayout>
 37#include <QListView>
 38#include <QAbstractItemModel>
 39
 40using namespace Tomahawk;
 41
 42
 43JobStatusView* JobStatusView::s_instance = 0;
 44QList< QPointer< JobStatusItem > > s_jobItems;
 45
 46void
 47JobStatusView::addJob( JobStatusItem* item )
 48{
 49    if ( s_instance == 0 || s_instance->model() == 0 )
 50    {
 51        s_jobItems.append( QPointer<JobStatusItem>( item ) );
 52    }
 53    else
 54    {
 55        s_instance->model()->addJob( item );
 56    }
 57}
 58
 59
 60void
 61JobStatusView::addJob( const QPointer<JobStatusItem>& item )
 62{
 63    if ( s_instance == 0 || s_instance->model() == 0 )
 64    {
 65        s_jobItems.append( item );
 66    }
 67    else
 68    {
 69        s_instance->model()->addJob( item.data() );
 70    }
 71}
 72
 73
 74JobStatusView::JobStatusView( AnimatedSplitter* parent )
 75    : AnimatedWidget( parent )
 76    , m_model( 0 )
 77    , m_parent( parent )
 78    , m_cachedHeight( -1 )
 79{
 80    s_instance = this;
 81
 82    setHiddenSize( QSize( 0, 0 ) );
 83    setLayout( new QVBoxLayout() );
 84    m_view = new QListView( this );
 85
 86    layout()->setMargin( 0 );
 87    layout()->addWidget( m_view );
 88
 89    m_view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
 90    m_view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
 91    m_view->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Ignored );
 92
 93    m_view->setFrameShape( QFrame::NoFrame );
 94    m_view->setAttribute( Qt::WA_MacShowFocusRect, 0 );
 95    m_view->setUniformItemSizes( false );
 96
 97    new IndexStatusManager( this );
 98    new PipelineStatusManager( this );
 99    new ScannerStatusManager( this );
100    new TransferStatusManager( this );
101    new LatchedStatusManager( this );
102
103    setMouseTracking( true );
104    m_view->setMouseTracking( true );
105}
106
107
108void
109JobStatusView::setModel( JobStatusSortModel* m )
110{
111    m_model = m;
112    m_view->setModel( m );
113    m_view->setItemDelegate( new JobStatusDelegate( m_view ) );
114
115    connect( m_view->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( checkCount() ) );
116    connect( m_view->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( checkCount() ) );
117    connect( m_view->model(), SIGNAL( modelReset() ), this, SLOT( checkCount() ) );
118    connect( m_view->model(), SIGNAL( customDelegateJobInserted( int, JobStatusItem* ) ), this, SLOT( customDelegateJobInserted( int, JobStatusItem* ) ) );
119    connect( m_view->model(), SIGNAL( customDelegateJobRemoved( int ) ), this, SLOT( customDelegateJobRemoved( int ) ) );
120    connect( m_view->model(), SIGNAL( refreshDelegates() ), this, SLOT( refreshDelegates() ) );
121    connect( m_view, SIGNAL( activated( QModelIndex ) ), this, SLOT( onItemActivated( QModelIndex ) ) );
122
123    foreach ( const QPointer<JobStatusItem> item, s_jobItems )
124    {
125        if ( !item.isNull() )
126        {
127            m_model->addJob( item.data() );
128        }
129    }
130    s_jobItems.clear();
131}
132
133
134void
135JobStatusView::customDelegateJobInserted( int row, JobStatusItem* item )
136{
137    if ( !item )
138        return;
139
140    item->createDelegate( m_view );
141    m_view->setItemDelegateForRow( row, item->customDelegate() );
142    ACLJobDelegate* delegate = qobject_cast< ACLJobDelegate* >( item->customDelegate() );
143    if ( delegate )
144    {
145        connect( delegate, SIGNAL( update( const QModelIndex& ) ), m_view, SLOT( update( const QModelIndex & ) ) );
146        connect( delegate, SIGNAL( aclResult( Tomahawk::ACLStatus::Type ) ), item, SLOT( aclResult( Tomahawk::ACLStatus::Type ) ) );
147        delegate->emitSizeHintChanged( m_model->index( row, 0 ) );
148    }
149    else
150        tLog() << Q_FUNC_INFO << "delegate was not properly found!";
151
152    checkCount();
153}
154
155
156void
157JobStatusView::customDelegateJobRemoved( int row )
158{
159    Q_UNUSED( row );
160    checkCount();
161}
162
163
164void
165JobStatusView::refreshDelegates()
166{
167    int count = m_model->rowCount();
168    for ( int i = 0; i < count; i++ )
169    {
170        QModelIndex index = m_model->index( i, 0 );
171        QVariant itemVar = index.data( JobStatusModel::JobDataRole );
172        if ( !itemVar.canConvert< JobStatusItem* >() || !itemVar.value< JobStatusItem* >() )
173        {
174            tLog() << Q_FUNC_INFO << "unable to fetch JobStatusItem* at row" << i;
175            continue;
176        }
177        JobStatusItem* item = itemVar.value< JobStatusItem* >();
178        if ( item->hasCustomDelegate() )
179            m_view->setItemDelegateForRow( i, item->customDelegate() );
180        else
181            m_view->setItemDelegateForRow( i, m_view->itemDelegate() );
182    }
183
184    checkCount();
185}
186
187
188void
189JobStatusView::onItemActivated( const QModelIndex& index )
190{
191    QVariant itemVar = index.data( JobStatusModel::JobDataRole );
192    if ( !itemVar.canConvert< JobStatusItem* >() || !itemVar.value< JobStatusItem* >() )
193    {
194        tLog() << Q_FUNC_INFO << "unable to fetch JobStatusItem*";
195        return;
196    }
197
198    JobStatusItem* item = itemVar.value< JobStatusItem* >();
199    item->activated();
200}
201
202
203void
204JobStatusView::checkCount()
205{
206    m_cachedHeight = -1;
207    if ( m_view->model()->rowCount() == 0 && !isHidden() )
208        emit hideWidget();
209    else
210        emit sizeHintChanged( sizeHint() );
211}
212
213
214QSize
215JobStatusView::sizeHint() const
216{
217    if ( m_cachedHeight >= 0 )
218        return QSize( 0, m_cachedHeight );
219
220    unsigned int y = 0;
221    y += m_view->contentsMargins().top() + m_view->contentsMargins().bottom();
222
223    if ( m_view->model()->rowCount() )
224    {
225        for ( int i = 0; i < m_view->model()->rowCount(); i++ )
226        {
227            y += m_view->sizeHintForRow( i );
228        }
229        y += 2; // some padding
230    }
231
232    m_cachedHeight = y;
233    return QSize( 0, y );
234}