aboutsummaryrefslogtreecommitdiffstats
path: root/CollectionModel.cpp
blob: dcad43815041e81c6f47c9a1ec76c7ab39621c93 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2008 Jason A. Donenfeld <Jason@zx2c4.com>
 */

#include "CollectionModel.h"
#include "Song.h"
#include "PlayerWindow.h"
#include "Credentials.h"

#include <QFont>
#include <QApplication>
#include <QPalette>
#include <QMimeData>
#include <QUrl>

CollectionModel::CollectionModel(QList<Song*> songs, Credentials* credentials, QObject* parent)
	: QAbstractItemModel(parent)
	, m_songs(songs)
	, m_position(-1)
	, m_fakeBoldState(false)
	, m_credentials(credentials)
{}

QVariant CollectionModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())
		return QVariant();

	int row = index.row();
	if(role == Qt::DisplayRole) {
		switch(index.column()) {
			case 0:
				if(m_songs[row]->track() == 0)
					return QVariant();
				else
					return QVariant(m_songs[row]->track());
			case 1:
				return QVariant(m_songs[row]->title());
			case 2:
				return QVariant(m_songs[row]->album());
			case 3:
				return QVariant(m_songs[row]->artist());
			default:
				return QVariant();
		}
	}
	if (row != m_position && !m_fakeBoldState)
		return QVariant();
	if(role == Qt::FontRole) {
		QFont font;
		font.setBold(true);
		return font;
	}
	if (role == Qt::BackgroundRole)
		return QApplication::palette().alternateBase();
	
	return QVariant();
}
QVariant CollectionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
		switch(section) {
			case 0:
				return tr("#");
			case 1:
				return tr("Title");
			case 2:
				return tr("Album");
			case 3:
				return tr("Artist");
			default:
				return QVariant();
		}
	return QVariant();
}

int CollectionModel::rowCount(const QModelIndex &parent) const
{
	if (parent.isValid())
		return 0;
	return m_songs.size();
}

int CollectionModel::columnCount(const QModelIndex&) const
{
	return 4;
}

QModelIndex CollectionModel::index(int row, int column, const QModelIndex& parent) const
{
	if (parent.isValid())
		return QModelIndex();
	if(!hasIndex(row, column, parent))
		return QModelIndex();

	return createIndex(row, column);
}
QModelIndex CollectionModel::parent(const QModelIndex&) const
{
	return QModelIndex();
}
Qt::ItemFlags CollectionModel::flags(const QModelIndex &index) const
{
	if(!index.isValid())
		return 0;
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
QStringList CollectionModel::mimeTypes() const
{
	QStringList ret;
	ret << "text/uri-list";
	return ret;
}
QMimeData* CollectionModel::mimeData(const QModelIndexList& indexes) const
{
	QMimeData* ret = new QMimeData();
	QStringList urls;
	Q_FOREACH(QModelIndex index, indexes)
	{
		if(index.column() == 1)
			urls << m_credentials->songUrl(m_songs[index.row()]->sha1()).toString();
	};
	ret->setData("text/uri-list", urls.join("\n").toUtf8());
	return ret;
}
Song* CollectionModel::song(const QModelIndex& index) const
{
	if (!index.isValid())
		return 0;
	return m_songs[index.row()];
}