-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.cpp
289 lines (240 loc) · 6.98 KB
/
playlist.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "playlist.h"
#include <QPalette>
#include <QIcon>
#include <QMimeData>
#include <QTableView>
#include <algorithm>
#define DRAG_DROP_MIME_TYPE "application/x-foxboxplaylistindexnumbers"
Playlist::Playlist()
{}
int Playlist::columnCount(const QModelIndex&) const
{
return 2;
}
int Playlist::rowCount(const QModelIndex&) const
{
return _songList.count();
}
QVariant Playlist::data(const QModelIndex &index, int role) const
{
if (index.row() > _songList.count())
{
return QVariant::Invalid;
}
switch (role)
{
case Qt::DisplayRole: {
switch (index.column())
{
case 0:
return _songList.at(index.row())->songName();
case 1:
return _songList.at(index.row())->path();
}
break;
}
case Qt::DecorationRole: {
if (index.column() == 0) {
return QIcon(_songList.at(index.row())->valid()
? ":/res/sound.png"
: ":/res/unknown.png");
}
break;
}
case Qt::ForegroundRole: {
if (index.row() == _currentIndex)
{
QPalette pal;
return pal.brush(QPalette::BrightText);
}
break;
}
}
return QVariant::Invalid;
}
QVariant Playlist::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
{
return QVariant::Invalid;
}
if (orientation == Qt::Vertical)
{
return QVariant(section + 1);
}
switch (section)
{
case 0: return QVariant("Name");
case 1: return QVariant("Path");
}
return QVariant::Invalid;
}
Qt::ItemFlags Playlist::flags(const QModelIndex& index) const
{
Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
if (index.isValid())
{
return Qt::ItemIsDragEnabled | defaultFlags;
}
return Qt::ItemIsDropEnabled | defaultFlags;
}
Qt::DropActions Playlist::supportedDropActions() const
{
return Qt::MoveAction;
}
QStringList Playlist::mimeTypes() const
{
return QAbstractItemModel::mimeTypes() << DRAG_DROP_MIME_TYPE;
}
QMimeData* Playlist::mimeData(const QModelIndexList &indexes) const
{
QMimeData* mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
for (auto &index : indexes)
{
if (!(index.isValid() && index.column() == 0))
{
continue;
}
stream << index.row();
}
mimeData->setData(DRAG_DROP_MIME_TYPE, encodedData);
return mimeData;
}
bool Playlist::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
{
if (!canDropMimeData(data, action, row, column, parent))
{
return false;
}
if (action == Qt::IgnoreAction)
{
return true;
}
int beginRow;
if (row != -1)
{
beginRow = row;
}
else
{
beginRow = rowCount();
}
QByteArray encodedData = data->data(DRAG_DROP_MIME_TYPE);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QList<int> sourceRows;
int rows = 0;
while (!stream.atEnd())
{
int rowNumber;
stream >> rowNumber;
sourceRows << rowNumber;
++rows;
}
// all of this that follows is probably just some darn workaround/hack
// around something that Qt provides but I'm too lazy to implement for now.
// works good enough for me, so ehhh ¯\_(ツ)_/¯
// sort the row numbers
std::sort(sourceRows.begin(), sourceRows.end(),
[](const int a, const int b) { return a < b; });
// get the song objects for the row numbers
QList<Song*> selectedSongs;
for (auto sourceRow : sourceRows)
{
selectedSongs << _songList.at(sourceRow);
// edge cases: beginRow is set to sourceRow, which means that we want to
// insert the songs after it. increment it
if (beginRow == sourceRow)
{
beginRow++;
}
}
// make sure beginRow (drop target row) is not out of bounds
// this means that the dropped songs should come after the last song in the
// playlist
bool shouldAppend = false;
if (beginRow >= _songList.count())
{
beginRow = _songList.count() - 1;
shouldAppend = true;
}
// get the song object for the target row
Song* targetSong = _songList.at(beginRow);
// get the current playing song object
Song* currentSongObj = currentSong();
// remove all songs from their previous positions from back to front
for (auto it = sourceRows.rbegin(); it != sourceRows.rend(); it++)
{
_songList.removeAt(*it);
};
// figure out the new target row
// this is perfectly fine, since we create a new Song object is for each
// loaded file. we are comparing pointers here, not file paths!
int newSourceRow = _songList.indexOf(targetSong);
if (shouldAppend)
{
newSourceRow++;
}
// insert the new songs at their desired position
for (auto it = selectedSongs.rbegin(); it != selectedSongs.rend(); it++)
{
_songList.insert(newSourceRow, *it);
}
// and select them ;-)
auto tview = qobject_cast<QTableView*>(this->parent());
tview->selectionModel()->clearSelection();
auto topLeft = index(newSourceRow, 0);
auto bottomRight = index(newSourceRow + selectedSongs.count() - 1, columnCount() - 1);
auto selection = QItemSelection(topLeft, bottomRight);
tview->selectionModel()->select(selection, QItemSelectionModel::Select);
// finally, reassign the current song index to the song that's actually
// playing
_currentIndex = _songList.indexOf(currentSongObj);
return true;
}
void Playlist::append(QString path)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_songList.append(new Song(path));
endInsertRows();
}
void Playlist::clear()
{
auto oldRowCount = rowCount();
beginRemoveRows(QModelIndex(), 0, oldRowCount);
for (int i = 0; i < _songList.count(); i++)
{
_songList.at(i)->deleteLater();
}
_songList.clear();
endRemoveRows();
// For some reason this "fixes" the display of the vertical header data...
emit(headerDataChanged(Qt::Vertical, 0, oldRowCount));
}
Song* Playlist::currentSong()
{
return at(_currentIndex);
}
Song* Playlist::at(int index)
{
return _songList.at(index);
}
void Playlist::setCurrentIndex(int currentIndex)
{
// TODO: boundary check of currentIndex
if (rowCount() == 0)
{
_currentIndex = 0;
return;
}
auto oldIndex = _currentIndex;
_currentIndex = currentIndex;
auto topLeft = index(oldIndex, 0);
auto bottomRight = index(currentIndex, columnCount());
// horrible(?) workaround to make the dataChanged emit thingy work from
// another thread:
qRegisterMetaType<QVector<int> >("QVector<int>");
emit(dataChanged(topLeft, bottomRight, { Qt::BackgroundRole }));
emit(currentIndexChanged(oldIndex, _currentIndex));
}