forked from arton0306/LetsKTV
/
PlayListTableModel.cpp
95 lines (84 loc) · 2.38 KB
/
PlayListTableModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <algorithm>
#include "PlayListTableModel.h"
#include "debug.h"
PlayListTableModel::PlayListTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
bool PlayListTableModel::isEmpty() const
{
return 0 == rowCount( QModelIndex() );
}
int PlayListTableModel::rowCount( const QModelIndex &aParent ) const
{
Q_UNUSED( aParent );
return mPlayList.size();
}
int PlayListTableModel::columnCount( const QModelIndex &aParent ) const
{
Q_UNUSED( aParent );
return TITLE_COUNT;
}
QVariant PlayListTableModel::headerData( int aSection, Qt::Orientation aOrientation, int aRole ) const
{
if ( aOrientation == Qt::Horizontal )
{
if ( aRole == Qt::DisplayRole )
{
return getTableHeaderText( aSection );
}
}
return QAbstractTableModel::headerData( aSection, aOrientation, aRole );
}
QVariant PlayListTableModel::data( const QModelIndex & aIndex, int aRole ) const
{
if ( !aIndex.isValid() ) return QVariant();
if ( aRole == Qt::DisplayRole )
{
switch ( aIndex.column() )
{
case SINGER: return mPlayList[aIndex.row()]->getSinger();
case SONGNAME: return mPlayList[aIndex.row()]->getSongName();
default:
DEBUG() << "getdata column error";
return QVariant();
}
}
return QVariant();
}
QString const & PlayListTableModel::getTableHeaderText( int aSection )
{
static bool isInit = false;
static QList<QString> tableHeaders;
if ( !isInit )
{
// the order must be the same with PlayListTableTitleType ( can be refactor to map )
tableHeaders.push_back( "歌手" );
tableHeaders.push_back( "歌名" );
isInit = true;
}
return tableHeaders.at( aSection );
}
void PlayListTableModel::addSong( Song const & aSong )
{
beginInsertRows( QModelIndex(), mPlayList.size(), mPlayList.size() );
mPlayList.push_back( &aSong );
endInsertRows();
}
void PlayListTableModel::removeFrontSong()
{
beginRemoveRows( QModelIndex(), 0, 0 );
mPlayList.pop_front();
endRemoveRows();
}
Song const & PlayListTableModel::front() const
{
DEBUG() << "Now PlayList size: " << mPlayList.size() << " get front";
return *mPlayList[0];
}
void PlayListTableModel::shufflePlayList()
{
beginResetModel();
random_shuffle( mPlayList.begin(), mPlayList.end() );
endResetModel();
}