Fixing the rowCount and colCount methods
[soundwave.git] / playlist_model.cpp
blobccfe52f9fa172598704b2eba560a04ea5c006841
1 #include "playlist_model.h"
3 //=========================================================
4 PlaylistModel::PlaylistModel( QObject *parent )
5 : QAbstractItemModel( parent )
9 //=========================================================
10 QVariant PlaylistModel::data( const QModelIndex &index, int role ) const
12 // invalid index position, so we return nothing
13 if( !index.isValid() || index.column() > 0 || index.row() >= items.size())
14 return QVariant();
16 // return the title of the song for the display role
17 switch( role ){
18 case Qt::DisplayRole:
19 return items[ index.row()].name;
22 // for unhandled roles, we just return NULL data
23 return QVariant();
25 //=========================================================
26 QModelIndex PlaylistModel::index( int row, int col, const QModelIndex &parent ) const
28 // return invalid index for invalid input parameters
29 if( col != 0 || row < 0 || parent.isValid())
30 return QModelIndex();
32 // row cannot be larger then size of the playlist
33 if( row >= items.size())
34 return QModelIndex();
36 // ok, this is a valid row,col so we return a valid index for it
37 return createIndex( row, col );
39 //=========================================================
40 QModelIndex PlaylistModel::parent( const QModelIndex &index ) const
42 // No parents for any index - flat list
43 return QModelIndex();
45 //=========================================================
46 int PlaylistModel::rowCount( const QModelIndex & parent) const
48 if (parent.isValid())
49 return 0;
50 else
51 // Number of Rows is equal to the number of items in the list
52 return items.size();
54 //=========================================================
55 int PlaylistModel::colCount( const QModelIndex & parent) const
58 if (parent.isValid())
59 return 0;
60 else
61 // PlayList always has one column
62 return 1;