fix a crash of my own making when recieving an invalid index
[amarok.git] / src / playlist / PlaylistGraphicsView.cpp
blob551d33ef28519a7ee010884471cd36fbb72e12bc
1 /***************************************************************************
2 * copyright : (C) 2007 Ian Monroe <ian@monroe.nu> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License version 2 *
6 * as published by the Free Software Foundation. *
7 ***************************************************************************/
9 #include "debug.h"
10 #include "PlaylistModel.h"
11 #include "PlaylistGraphicsItem.h"
12 #include "PlaylistGraphicsView.h"
14 #include <QModelIndex>
15 #include <QGraphicsScene>
17 PlaylistNS::GraphicsView::GraphicsView( QWidget* parent, PlaylistNS::Model* model )
18 : QGraphicsView( parent )
19 , m_model( model )
21 DEBUG_BLOCK
22 setScene( new QGraphicsScene() );
23 rowsInserted( QModelIndex(), 0, m_model->rowCount() - 1);
24 connect( m_model, SIGNAL( modelReset() ), this, SLOT( modelReset() ) );
25 connect( m_model, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), this, SLOT( rowsInserted( const QModelIndex &, int, int ) ) );
26 connect( m_model, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ), this, SLOT( rowsRemoved( const QModelIndex&, int, int ) ) );
27 connect( m_model, SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ), this, SLOT( dataChanged( const QModelIndex& ) ) );
28 show();
31 void
32 PlaylistNS::GraphicsView::rowsInserted( const QModelIndex& parent, int start, int end )
34 Q_UNUSED( parent );
35 for( int i = start; i <= end; i++ )
37 PlaylistNS::GraphicsItem* item = new PlaylistNS::GraphicsItem();
38 item->setPos( 0.0, PlaylistNS::GraphicsItem::height() * i );
39 scene()->addItem( item );
40 m_tracks.insert( i, item );
44 void
45 PlaylistNS::GraphicsView::rowsRemoved(const QModelIndex& parent, int start, int end )
47 Q_UNUSED( parent );
48 for( int i = end; i >= start; i-- )
49 delete m_tracks.takeAt( i );
52 void
53 PlaylistNS::GraphicsView::modelReset()
55 foreach( PlaylistNS::GraphicsItem* it, m_tracks )
57 delete it;
59 m_tracks.clear();
63 void
64 PlaylistNS::GraphicsView::dataChanged(const QModelIndex & index)
66 DEBUG_BLOCK
67 if ( !index.isValid() )
68 return;
70 if ( m_tracks.count() > index.row() )
71 m_tracks[ index.row() ]->refresh();
74 #include "PlaylistGraphicsView.moc"