-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbrowsetablemodel.cpp
535 lines (480 loc) · 19 KB
/
browsetablemodel.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include "library/browse/browsetablemodel.h"
#include <QMessageBox>
#include <QMetaType>
#include <QStringList>
#include <QTableView>
#include <QUrl>
#include <QtConcurrentRun>
#include <QtSql>
#include "control/controlobject.h"
#include "library/browse/browsetablemodel.h"
#include "library/browse/browsethread.h"
#include "library/previewbuttondelegate.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "mixer/playerinfo.h"
#include "mixer/playermanager.h"
#include "moc_browsetablemodel.cpp"
#include "track/track.h"
#include "widget/wlibrarytableview.h"
namespace {
/// Helper to insert values into a QList with specific indices.
///
/// *For legacy code only - Do not use for new code!*
template<typename T>
void listAppendOrReplaceAt(QList<T>* pList, int index, const T& value) {
VERIFY_OR_DEBUG_ASSERT(index <= pList->size()) {
qWarning() << "listAppendOrReplaceAt: Padding list with"
<< (index - pList->size()) << "default elements";
while (index > pList->size()) {
pList->append(T());
}
}
VERIFY_OR_DEBUG_ASSERT(index == pList->size()) {
pList->replace(index, value);
return;
}
pList->append(value);
}
} // anonymous namespace
BrowseTableModel::BrowseTableModel(QObject* parent,
TrackCollectionManager* pTrackCollectionManager,
RecordingManager* pRecordingManager)
: TrackModel(pTrackCollectionManager->internalCollection()->database(),
"mixxx.db.model.browse"),
QStandardItemModel(parent),
m_pTrackCollectionManager(pTrackCollectionManager),
m_pRecordingManager(pRecordingManager),
m_previewDeckGroup(PlayerManager::groupForPreviewDeck(0)) {
QStringList headerLabels;
/// The order of the columns appended here must exactly match the ordering
/// of the enum that is used for indexing.
listAppendOrReplaceAt(&headerLabels, COLUMN_PREVIEW, tr("Preview"));
listAppendOrReplaceAt(&headerLabels, COLUMN_FILENAME, tr("Filename"));
listAppendOrReplaceAt(&headerLabels, COLUMN_ARTIST, tr("Artist"));
listAppendOrReplaceAt(&headerLabels, COLUMN_TITLE, tr("Title"));
listAppendOrReplaceAt(&headerLabels, COLUMN_ALBUM, tr("Album"));
listAppendOrReplaceAt(&headerLabels, COLUMN_TRACK_NUMBER, tr("Track #"));
listAppendOrReplaceAt(&headerLabels, COLUMN_YEAR, tr("Year"));
listAppendOrReplaceAt(&headerLabels, COLUMN_GENRE, tr("Genre"));
listAppendOrReplaceAt(&headerLabels, COLUMN_COMPOSER, tr("Composer"));
listAppendOrReplaceAt(&headerLabels, COLUMN_COMMENT, tr("Comment"));
listAppendOrReplaceAt(&headerLabels, COLUMN_DURATION, tr("Duration"));
listAppendOrReplaceAt(&headerLabels, COLUMN_BPM, tr("BPM"));
listAppendOrReplaceAt(&headerLabels, COLUMN_KEY, tr("Key"));
listAppendOrReplaceAt(&headerLabels, COLUMN_TYPE, tr("Type"));
listAppendOrReplaceAt(&headerLabels, COLUMN_BITRATE, tr("Bitrate"));
listAppendOrReplaceAt(&headerLabels, COLUMN_NATIVELOCATION, tr("Location"));
listAppendOrReplaceAt(&headerLabels, COLUMN_ALBUMARTIST, tr("Album Artist"));
listAppendOrReplaceAt(&headerLabels, COLUMN_GROUPING, tr("Grouping"));
listAppendOrReplaceAt(&headerLabels, COLUMN_FILE_MODIFIED_TIME, tr("File Modified"));
listAppendOrReplaceAt(&headerLabels, COLUMN_FILE_CREATION_TIME, tr("File Created"));
listAppendOrReplaceAt(&headerLabels, COLUMN_REPLAYGAIN, tr("ReplayGain"));
addSearchColumn(COLUMN_FILENAME);
addSearchColumn(COLUMN_ARTIST);
addSearchColumn(COLUMN_ALBUM);
addSearchColumn(COLUMN_TITLE);
addSearchColumn(COLUMN_GENRE);
addSearchColumn(COLUMN_COMPOSER);
addSearchColumn(COLUMN_KEY);
addSearchColumn(COLUMN_COMMENT);
addSearchColumn(COLUMN_ALBUMARTIST);
addSearchColumn(COLUMN_GROUPING);
addSearchColumn(COLUMN_FILE_MODIFIED_TIME);
addSearchColumn(COLUMN_FILE_CREATION_TIME);
setDefaultSort(COLUMN_FILENAME, Qt::AscendingOrder);
for (int i = 0; i < static_cast<int>(TrackModel::SortColumnId::IdMax); ++i) {
m_columnIndexBySortColumnId[i] = -1;
}
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Filename)] = COLUMN_FILENAME;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Artist)] = COLUMN_ARTIST;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Title)] = COLUMN_TITLE;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Album)] = COLUMN_ALBUM;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::AlbumArtist)] =
COLUMN_ALBUMARTIST;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Year)] = COLUMN_YEAR;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Genre)] = COLUMN_GENRE;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Composer)] = COLUMN_COMPOSER;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Grouping)] = COLUMN_GROUPING;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::TrackNumber)] =
COLUMN_TRACK_NUMBER;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::FileType)] = COLUMN_TYPE;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::NativeLocation)] =
COLUMN_NATIVELOCATION;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Comment)] = COLUMN_COMMENT;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Duration)] = COLUMN_DURATION;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::BitRate)] = COLUMN_BITRATE;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Bpm)] = COLUMN_BPM;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::ReplayGain)] =
COLUMN_REPLAYGAIN;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Key)] = COLUMN_KEY;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Preview)] = COLUMN_PREVIEW;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::Grouping)] = COLUMN_GROUPING;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::FileModifiedTime)] =
COLUMN_FILE_MODIFIED_TIME;
m_columnIndexBySortColumnId[static_cast<int>(
TrackModel::SortColumnId::FileCreationTime)] =
COLUMN_FILE_CREATION_TIME;
m_sortColumnIdByColumnIndex.clear();
for (int i = static_cast<int>(TrackModel::SortColumnId::IdMin);
i < static_cast<int>(TrackModel::SortColumnId::IdMax);
++i) {
TrackModel::SortColumnId sortColumn = static_cast<TrackModel::SortColumnId>(i);
int columnIndex = m_columnIndexBySortColumnId[static_cast<int>(sortColumn)];
if (columnIndex >= 0) {
m_sortColumnIdByColumnIndex.insert(columnIndex, sortColumn);
}
}
setHorizontalHeaderLabels(headerLabels);
// register the QList<T> as a metatype since we use QueuedConnection below
qRegisterMetaType<QList<QList<QStandardItem*>>>(
"QList< QList<QStandardItem*>>");
qRegisterMetaType<BrowseTableModel*>("BrowseTableModel*");
m_pBrowseThread = BrowseThread::getInstanceRef();
connect(m_pBrowseThread.data(),
&BrowseThread::clearModel,
this,
&BrowseTableModel::slotClear,
Qt::QueuedConnection);
connect(m_pBrowseThread.data(),
&BrowseThread::rowsAppended,
this,
&BrowseTableModel::slotInsert,
Qt::QueuedConnection);
connect(&PlayerInfo::instance(),
&PlayerInfo::trackChanged,
this,
&BrowseTableModel::trackChanged);
trackChanged(m_previewDeckGroup,
PlayerInfo::instance().getTrackInfo(m_previewDeckGroup),
TrackPointer());
}
BrowseTableModel::~BrowseTableModel() {
}
int BrowseTableModel::columnIndexFromSortColumnId(TrackModel::SortColumnId column) const {
if (column < TrackModel::SortColumnId::IdMin ||
column >= TrackModel::SortColumnId::IdMax) {
return -1;
}
return m_columnIndexBySortColumnId[static_cast<int>(column)];
}
TrackModel::SortColumnId BrowseTableModel::sortColumnIdFromColumnIndex(int index) const {
return m_sortColumnIdByColumnIndex.value(index, TrackModel::SortColumnId::Invalid);
}
const QList<int>& BrowseTableModel::searchColumns() const {
return m_searchColumns;
}
void BrowseTableModel::addSearchColumn(int index) {
m_searchColumns.push_back(index);
}
void BrowseTableModel::setPath(mixxx::FileAccess path) {
if (path.info().hasLocation()) {
m_currentDirectory = path.info().locationPath();
} else {
m_currentDirectory = QString();
}
m_pBrowseThread->executePopulation(std::move(path), this);
}
TrackPointer BrowseTableModel::getTrack(const QModelIndex& index) const {
return getTrackByRef(TrackRef::fromFilePath(getTrackLocation(index)));
}
TrackPointer BrowseTableModel::getTrackByRef(const TrackRef& trackRef) const {
if (m_pRecordingManager->getRecordingLocation() == trackRef.getLocation()) {
QMessageBox::critical(nullptr,
tr("Mixxx Library"),
tr("Could not load the following file because it is in use by "
"Mixxx or another application.") +
"\n" + trackRef.getLocation());
return TrackPointer();
}
// NOTE(uklotzde, 2015-12-08): Accessing tracks from the browse view
// will implicitly add them to the library. Is this really what we
// want here??
// NOTE(rryan, 2015-12-27): This was intentional at the time since
// some people use Browse instead of the library and we want to let
// them edit the tracks in a way that persists across sessions
// and we didn't want to edit the files on disk by default
// unless the user opts in to that.
return m_pTrackCollectionManager->getOrAddTrack(trackRef);
}
QString BrowseTableModel::getTrackLocation(const QModelIndex& index) const {
int row = index.row();
QModelIndex index2 = this->index(row, COLUMN_NATIVELOCATION);
QString nativeLocation = data(index2).toString();
QString location = QDir::fromNativeSeparators(nativeLocation);
return location;
}
TrackId BrowseTableModel::getTrackId(const QModelIndex& index) const {
TrackPointer pTrack = getTrack(index);
if (pTrack) {
return pTrack->getId();
} else {
qWarning()
<< "Track is not available in library"
<< getTrackUrl(index);
return TrackId();
}
}
QUrl BrowseTableModel::getTrackUrl(const QModelIndex& index) const {
const QString trackLocation = getTrackLocation(index);
DEBUG_ASSERT(trackLocation.trimmed() == trackLocation);
if (trackLocation.isEmpty()) {
return {};
}
return QUrl::fromLocalFile(trackLocation);
}
CoverInfo BrowseTableModel::getCoverInfo(const QModelIndex& index) const {
TrackPointer pTrack = getTrack(index);
if (pTrack) {
return CoverInfo(pTrack->getCoverInfo(), getTrackLocation(index));
} else {
qWarning()
<< "Track is not available in library"
<< getTrackUrl(index);
return CoverInfo();
}
}
const QVector<int> BrowseTableModel::getTrackRows(TrackId trackId) const {
Q_UNUSED(trackId);
// We can't implement this as it stands.
return QVector<int>();
}
void BrowseTableModel::search(const QString& searchText, const QString& extraFilter) {
Q_UNUSED(extraFilter);
Q_UNUSED(searchText);
}
const QString BrowseTableModel::currentSearch() const {
return QString("");
}
bool BrowseTableModel::isColumnInternal(int) {
return false;
}
bool BrowseTableModel::isColumnHiddenByDefault(int column) {
if (column == COLUMN_COMPOSER ||
column == COLUMN_TRACK_NUMBER ||
column == COLUMN_YEAR ||
column == COLUMN_GROUPING ||
column == COLUMN_NATIVELOCATION ||
column == COLUMN_ALBUMARTIST ||
column == COLUMN_FILE_CREATION_TIME ||
column == COLUMN_REPLAYGAIN) {
return true;
}
return false;
}
void BrowseTableModel::moveTrack(const QModelIndex&, const QModelIndex&) {
}
void BrowseTableModel::removeTracks(const QModelIndexList&) {
}
QMimeData* BrowseTableModel::mimeData(const QModelIndexList& indexes) const {
QMimeData* mimeData = new QMimeData();
QList<QUrl> urls;
// Ok, so the list of indexes we're given contains separates indexes for
// each column, so even if only one row is selected, we'll have like 7
// indexes. We need to only count each row once:
QList<int> rows;
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
if (!rows.contains(index.row())) {
rows.push_back(index.row());
QUrl url = getTrackUrl(index);
if (!url.isValid()) {
qDebug() << "ERROR invalid url" << url;
continue;
}
urls.append(url);
}
}
}
mimeData->setUrls(urls);
return mimeData;
}
void BrowseTableModel::slotClear(BrowseTableModel* caller_object) {
if (caller_object == this) {
removeRows(0, rowCount());
}
}
void BrowseTableModel::slotInsert(const QList<QList<QStandardItem*>>& rows,
BrowseTableModel* caller_object) {
// There exists more than one BrowseTableModel in Mixxx We only want to
// receive items here, this object has 'ordered' by the BrowserThread
// (singleton)
if (caller_object == this) {
//qDebug() << "BrowseTableModel::slotInsert";
for (int i = 0; i < rows.size(); ++i) {
appendRow(rows.at(i));
}
}
emit restoreModelState();
}
TrackModel::Capabilities BrowseTableModel::getCapabilities() const {
return Capability::AddToTrackSet |
Capability::AddToAutoDJ |
Capability::LoadToDeck |
Capability::LoadToPreviewDeck |
Capability::LoadToSampler |
Capability::RemoveFromDisk;
}
QString BrowseTableModel::modelKey(bool noSearch) const {
// Browse feature does currently not support searching.
Q_UNUSED(noSearch);
return QStringLiteral("browse:") + m_currentDirectory;
}
Qt::ItemFlags BrowseTableModel::flags(const QModelIndex& index) const {
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
// Enable dragging songs from this data model to elsewhere (like the
// waveform widget to load a track into a Player).
defaultFlags |= Qt::ItemIsDragEnabled;
int column = index.column();
switch (column) {
case COLUMN_FILENAME:
case COLUMN_BITRATE:
case COLUMN_DURATION:
case COLUMN_TYPE:
case COLUMN_FILE_MODIFIED_TIME:
case COLUMN_FILE_CREATION_TIME:
case COLUMN_REPLAYGAIN:
// read-only
return defaultFlags;
default:
// editable
return defaultFlags | Qt::ItemIsEditable;
}
}
bool BrowseTableModel::setData(
const QModelIndex& index,
const QVariant& value,
int role) {
Q_UNUSED(role);
QStandardItem* item = itemFromIndex(index);
DEBUG_ASSERT(nullptr != item);
TrackPointer pTrack(getTrack(index));
if (!pTrack) {
qWarning() << "BrowseTableModel::setData():"
<< "Failed to resolve track"
<< getTrackLocation(index);
// restore previous item content
item->setText(index.data().toString());
item->setToolTip(item->text());
return false;
}
// check if one the item were edited
int col = index.column();
switch (col) {
case COLUMN_ARTIST:
pTrack->setArtist(value.toString());
break;
case COLUMN_TITLE:
pTrack->setTitle(value.toString());
break;
case COLUMN_ALBUM:
pTrack->setAlbum(value.toString());
break;
case COLUMN_BPM:
pTrack->trySetBpm(value.toDouble());
break;
case COLUMN_KEY:
pTrack->setKeyText(value.toString());
break;
case COLUMN_TRACK_NUMBER:
pTrack->setTrackNumber(value.toString());
break;
case COLUMN_COMMENT:
pTrack->setComment(value.toString());
break;
case COLUMN_GENRE:
m_pTrackCollectionManager->updateTrackGenre(pTrack.get(), value.toString());
break;
case COLUMN_COMPOSER:
pTrack->setComposer(value.toString());
break;
case COLUMN_YEAR:
pTrack->setYear(value.toString());
break;
case COLUMN_ALBUMARTIST:
pTrack->setAlbumArtist(value.toString());
break;
case COLUMN_GROUPING:
pTrack->setGrouping(value.toString());
break;
default:
qWarning() << "BrowseTableModel::setData():"
<< "No tagger column";
// restore previous item context
item->setText(index.data().toString());
item->setToolTip(item->text());
return false;
}
item->setText(value.toString());
item->setToolTip(item->text());
return true;
}
void BrowseTableModel::trackChanged(
const QString& group, TrackPointer pNewTrack, TrackPointer pOldTrack) {
Q_UNUSED(pOldTrack);
if (group == m_previewDeckGroup) {
for (int row = 0; row < rowCount(); ++row) {
QModelIndex i = index(row, COLUMN_PREVIEW);
if (i.data().toBool()) {
QStandardItem* item = itemFromIndex(i);
item->setText("0");
}
}
if (pNewTrack) {
QString trackLocation = pNewTrack->getLocation();
for (int row = 0; row < rowCount(); ++row) {
QModelIndex i = index(row, COLUMN_PREVIEW);
QString location = getTrackLocation(i);
if (location == trackLocation) {
QStandardItem* item = itemFromIndex(i);
item->setText("1");
break;
}
}
}
}
}
bool BrowseTableModel::isColumnSortable(int column) const {
return COLUMN_PREVIEW != column;
}
QAbstractItemDelegate* BrowseTableModel::delegateForColumn(const int i, QObject* pParent) {
WLibraryTableView* pTableView = qobject_cast<WLibraryTableView*>(pParent);
DEBUG_ASSERT(pTableView);
if (PlayerManager::numPreviewDecks() > 0 && i == COLUMN_PREVIEW) {
return new PreviewButtonDelegate(pTableView, i);
}
return nullptr;
}
bool BrowseTableModel::updateTrackGenre(
Track* pTrack,
const QString& genre) const {
return m_pTrackCollectionManager->updateTrackGenre(pTrack, genre);
}
#if defined(__EXTRA_METADATA__)
bool BrowseTableModel::updateTrackMood(
Track* pTrack,
const QString& mood) const {
return m_pTrackCollectionManager->updateTrackMood(pTrack, mood);
}
#endif // __EXTRA_METADATA__