-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
playlistfeature.cpp
405 lines (363 loc) · 15.7 KB
/
playlistfeature.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
#include "library/trackset/playlistfeature.h"
#include <QMenu>
#include <QtDebug>
#include "library/library.h"
#include "library/parser.h"
#include "library/playlisttablemodel.h"
#include "library/queryutil.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "library/treeitem.h"
#include "moc_playlistfeature.cpp"
#include "sources/soundsourceproxy.h"
#include "util/db/dbconnection.h"
#include "util/duration.h"
#include "widget/wlibrary.h"
#include "widget/wlibrarysidebar.h"
#include "widget/wtracktableview.h"
namespace {
QString createPlaylistLabel(
const QString& name,
int count,
int duration) {
return QStringLiteral("%1 (%2) %3")
.arg(name,
QString::number(count),
mixxx::Duration::formatTime(
duration, mixxx::Duration::Precision::SECONDS));
}
} // anonymous namespace
PlaylistFeature::PlaylistFeature(Library* pLibrary, UserSettingsPointer pConfig)
: BasePlaylistFeature(pLibrary,
pConfig,
new PlaylistTableModel(nullptr,
pLibrary->trackCollectionManager(),
"mixxx.db.model.playlist"),
QStringLiteral("PLAYLISTHOME"),
QStringLiteral("playlist")) {
// construct child model
std::unique_ptr<TreeItem> pRootItem = TreeItem::newRoot(this);
m_pSidebarModel->setRootItem(std::move(pRootItem));
constructChildModel(kInvalidPlaylistId);
m_pShufflePlaylistAction = new QAction(tr("Shuffle Playlist"), this);
connect(m_pShufflePlaylistAction,
&QAction::triggered,
this,
&PlaylistFeature::slotShufflePlaylist);
}
QVariant PlaylistFeature::title() {
return tr("Playlists");
}
void PlaylistFeature::onRightClick(const QPoint& globalPos) {
m_lastRightClickedIndex = QModelIndex();
QMenu menu(m_pSidebarWidget);
menu.addAction(m_pCreatePlaylistAction);
menu.addSeparator();
menu.addAction(m_pCreateImportPlaylistAction);
menu.exec(globalPos);
}
void PlaylistFeature::onRightClickChild(
const QPoint& globalPos, const QModelIndex& index) {
//Save the model index so we can get it in the action slots...
m_lastRightClickedIndex = index;
int playlistId = playlistIdFromIndex(index);
bool locked = m_playlistDao.isPlaylistLocked(playlistId);
m_pDeletePlaylistAction->setEnabled(!locked);
m_pRenamePlaylistAction->setEnabled(!locked);
m_pLockPlaylistAction->setText(locked ? tr("Unlock") : tr("Lock"));
QMenu menu(m_pSidebarWidget);
menu.addAction(m_pCreatePlaylistAction);
menu.addSeparator();
// TODO If playlist is selected and has more than one track selected
// show "Shuffle selected tracks", else show "Shuffle playlist"?
menu.addAction(m_pShufflePlaylistAction);
menu.addSeparator();
menu.addAction(m_pRenamePlaylistAction);
menu.addAction(m_pDuplicatePlaylistAction);
menu.addAction(m_pDeletePlaylistAction);
menu.addAction(m_pLockPlaylistAction);
menu.addSeparator();
menu.addAction(m_pAddToAutoDJAction);
menu.addAction(m_pAddToAutoDJTopAction);
menu.addAction(m_pAddToAutoDJReplaceAction);
menu.addSeparator();
menu.addAction(m_pAnalyzePlaylistAction);
menu.addSeparator();
menu.addAction(m_pImportPlaylistAction);
menu.addAction(m_pExportPlaylistAction);
menu.addAction(m_pExportTrackFilesAction);
menu.exec(globalPos);
}
bool PlaylistFeature::dropAcceptChild(
const QModelIndex& index, const QList<QUrl>& urls, QObject* pSource) {
int playlistId = playlistIdFromIndex(index);
VERIFY_OR_DEBUG_ASSERT(playlistId >= 0) {
return false;
}
// If a track is dropped onto a playlist's name, but the track isn't in the
// library, then add the track to the library before adding it to the
// playlist.
// pSource != nullptr it is a drop from inside Mixxx and indicates all
// tracks already in the DB
QList<TrackId> trackIds = m_pLibrary->trackCollectionManager()
->resolveTrackIdsFromUrls(urls, !pSource);
if (trackIds.isEmpty()) {
return false;
}
// Return whether appendTracksToPlaylist succeeded.
return m_playlistDao.appendTracksToPlaylist(trackIds, playlistId);
}
bool PlaylistFeature::dragMoveAcceptChild(const QModelIndex& index, const QUrl& url) {
int playlistId = playlistIdFromIndex(index);
bool locked = m_playlistDao.isPlaylistLocked(playlistId);
bool formatSupported = SoundSourceProxy::isUrlSupported(url) ||
Parser::isPlaylistFilenameSupported(url.toLocalFile());
return !locked && formatSupported;
}
QList<BasePlaylistFeature::IdAndLabel> PlaylistFeature::createPlaylistLabels() {
QSqlDatabase database =
m_pLibrary->trackCollectionManager()->internalCollection()->database();
QList<BasePlaylistFeature::IdAndLabel> playlistLabels;
QString queryString = QStringLiteral(
"CREATE TEMPORARY VIEW IF NOT EXISTS PlaylistsCountsDurations "
"AS SELECT "
" Playlists.id AS id, "
" Playlists.name AS name, "
" LOWER(Playlists.name) AS sort_name, "
" COUNT(case library.mixxx_deleted when 0 then 1 else null end) "
" AS count, "
" SUM(case library.mixxx_deleted "
" when 0 then library.duration else 0 end) AS durationSeconds "
"FROM Playlists "
"LEFT JOIN PlaylistTracks "
" ON PlaylistTracks.playlist_id = Playlists.id "
"LEFT JOIN library "
" ON PlaylistTracks.track_id = library.id "
" WHERE Playlists.hidden = 0 "
" GROUP BY Playlists.id");
queryString.append(
mixxx::DbConnection::collateLexicographically(
" ORDER BY sort_name"));
QSqlQuery query(database);
if (!query.exec(queryString)) {
LOG_FAILED_QUERY(query);
}
// Setup the sidebar playlist model
QSqlTableModel playlistTableModel(this, database);
playlistTableModel.setTable("PlaylistsCountsDurations");
playlistTableModel.select();
while (playlistTableModel.canFetchMore()) {
playlistTableModel.fetchMore();
}
QSqlRecord record = playlistTableModel.record();
int nameColumn = record.indexOf("name");
int idColumn = record.indexOf("id");
int countColumn = record.indexOf("count");
int durationColumn = record.indexOf("durationSeconds");
for (int row = 0; row < playlistTableModel.rowCount(); ++row) {
int id =
playlistTableModel
.data(playlistTableModel.index(row, idColumn))
.toInt();
QString name =
playlistTableModel
.data(playlistTableModel.index(row, nameColumn))
.toString();
int count =
playlistTableModel
.data(playlistTableModel.index(row, countColumn))
.toInt();
int duration =
playlistTableModel
.data(playlistTableModel.index(row, durationColumn))
.toInt();
BasePlaylistFeature::IdAndLabel idAndLabel;
idAndLabel.id = id;
idAndLabel.label = createPlaylistLabel(name, count, duration);
playlistLabels.append(idAndLabel);
}
return playlistLabels;
}
QString PlaylistFeature::fetchPlaylistLabel(int playlistId) {
// Setup the sidebar playlist model
QSqlDatabase database =
m_pLibrary->trackCollectionManager()->internalCollection()->database();
QSqlTableModel playlistTableModel(this, database);
playlistTableModel.setTable("PlaylistsCountsDurations");
QString filter = "id=" + QString::number(playlistId);
playlistTableModel.setFilter(filter);
playlistTableModel.select();
while (playlistTableModel.canFetchMore()) {
playlistTableModel.fetchMore();
}
QSqlRecord record = playlistTableModel.record();
int nameColumn = record.indexOf("name");
int countColumn = record.indexOf("count");
int durationColumn = record.indexOf("durationSeconds");
DEBUG_ASSERT(playlistTableModel.rowCount() <= 1);
if (playlistTableModel.rowCount() > 0) {
QString name =
playlistTableModel.data(playlistTableModel.index(0, nameColumn))
.toString();
int count = playlistTableModel
.data(playlistTableModel.index(0, countColumn))
.toInt();
int duration =
playlistTableModel
.data(playlistTableModel.index(0, durationColumn))
.toInt();
return createPlaylistLabel(name, count, duration);
}
return QString();
}
void PlaylistFeature::slotShufflePlaylist() {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
if (m_playlistDao.isPlaylistLocked(playlistId)) {
qDebug() << "Can't shuffle locked playlist" << playlistId
<< m_playlistDao.getPlaylistName(playlistId);
return;
}
// Shuffle all tracks
// If the playlist is loaded/visible shuffle only selected tracks
QModelIndexList selection;
if (isChildIndexSelectedInSidebar(m_lastRightClickedIndex) &&
m_pPlaylistTableModel->getPlaylist() == playlistId) {
if (m_pLibraryWidget) {
WTrackTableView* view = dynamic_cast<WTrackTableView*>(
m_pLibraryWidget->getActiveView());
if (view != nullptr) {
selection = view->selectionModel()->selectedIndexes();
}
}
m_pPlaylistTableModel->shuffleTracks(selection, QModelIndex());
} else {
// Create a temp model so we don't need to select the playlist
// in the persistent model in order to shuffle it
std::unique_ptr<PlaylistTableModel> pPlaylistTableModel =
std::make_unique<PlaylistTableModel>(this,
m_pLibrary->trackCollectionManager(),
"mixxx.db.model.playlist_shuffle");
pPlaylistTableModel->selectPlaylist(playlistId);
pPlaylistTableModel->setSort(
pPlaylistTableModel->fieldIndex(
ColumnCache::COLUMN_PLAYLISTTRACKSTABLE_POSITION),
Qt::AscendingOrder);
pPlaylistTableModel->select();
pPlaylistTableModel->shuffleTracks(selection, QModelIndex());
}
}
/// Purpose: When inserting or removing playlists,
/// we require the sidebar model not to reset.
/// This method queries the database and does dynamic insertion
/// @param selectedId entry which should be selected
QModelIndex PlaylistFeature::constructChildModel(int selectedId) {
std::vector<std::unique_ptr<TreeItem>> childrenToAdd;
int selectedRow = -1;
int row = 0;
const QList<IdAndLabel> playlistLabels = createPlaylistLabels();
for (const auto& idAndLabel : playlistLabels) {
int playlistId = idAndLabel.id;
QString playlistLabel = idAndLabel.label;
if (selectedId == playlistId) {
// save index for selection
selectedRow = row;
}
// Create the TreeItem whose parent is the invisible root item
auto pItem = std::make_unique<TreeItem>(playlistLabel, playlistId);
pItem->setBold(m_playlistIdsOfSelectedTrack.contains(playlistId));
decorateChild(pItem.get(), playlistId);
childrenToAdd.push_back(std::move(pItem));
++row;
}
// Append all the newly created TreeItems in a dynamic way to the childmodel
m_pSidebarModel->insertTreeItemRows(std::move(childrenToAdd), 0);
if (selectedRow == -1) {
return QModelIndex();
}
return m_pSidebarModel->index(selectedRow, 0);
}
void PlaylistFeature::decorateChild(TreeItem* item, int playlistId) {
if (m_playlistDao.isPlaylistLocked(playlistId)) {
item->setIcon(
QIcon(":/images/library/ic_library_locked_tracklist.svg"));
} else {
item->setIcon(QIcon());
}
}
void PlaylistFeature::slotPlaylistTableChanged(int playlistId) {
//qDebug() << "slotPlaylistTableChanged() playlistId:" << playlistId;
enum PlaylistDAO::HiddenType type = m_playlistDao.getHiddenType(playlistId);
if (type != PlaylistDAO::PLHT_NOT_HIDDEN && // not a regular playlist
type != PlaylistDAO::PLHT_UNKNOWN) { // not a deleted playlist
return;
}
// Store current selection
int selectedPlaylistId = kInvalidPlaylistId;
if (isChildIndexSelectedInSidebar(m_lastClickedIndex)) {
if (playlistId == playlistIdFromIndex(m_lastClickedIndex) &&
type == PlaylistDAO::PLHT_UNKNOWN) {
// if the selected playlist was deleted, find a sibling to select
selectedPlaylistId = getSiblingPlaylistIdOf(m_lastClickedIndex);
} else {
// just restore the current selection
selectedPlaylistId = playlistIdFromIndex(m_lastClickedIndex);
}
}
clearChildModel();
QModelIndex newIndex = constructChildModel(selectedPlaylistId);
if (selectedPlaylistId != kInvalidPlaylistId && newIndex.isValid()) {
// If a child index was selected and we got a new valid index select that.
// Else (root item was selected or for some reason no index could be created)
// there's nothing to do: either no child was selected earlier, or the root
// was selected and will remain selected after the child model was rebuilt.
activateChild(newIndex);
emit featureSelect(this, newIndex);
}
}
void PlaylistFeature::slotPlaylistContentOrLockChanged(const QSet<int>& playlistIds) {
// qDebug() << "slotPlaylistContentOrLockChanged() playlistId:" << playlistId;
QSet<int> idsToBeUpdated;
for (const auto playlistId : std::as_const(playlistIds)) {
if (m_playlistDao.getHiddenType(playlistId) == PlaylistDAO::PLHT_NOT_HIDDEN) {
idsToBeUpdated.insert(playlistId);
}
}
updateChildModel(idsToBeUpdated);
}
void PlaylistFeature::slotPlaylistTableRenamed(
int playlistId, const QString& newName) {
Q_UNUSED(newName);
// qDebug() << "slotPlaylistTableRenamed() playlistId:" << playlistId;
if (m_playlistDao.getHiddenType(playlistId) == PlaylistDAO::PLHT_NOT_HIDDEN) {
slotPlaylistTableChanged(playlistId);
}
}
QString PlaylistFeature::getRootViewHtml() const {
QString playlistsTitle = tr("Playlists");
QString playlistsSummary =
tr("Playlists are ordered lists of tracks that allow you to plan "
"your DJ sets.");
QString playlistsSummary2 =
tr("Some DJs construct playlists before they perform live, but "
"others prefer to build them on-the-fly.");
QString playlistsSummary3 =
tr("When using a playlist during a live DJ set, remember to always "
"pay close attention to how your audience reacts to the music "
"you've chosen to play.");
QString playlistsSummary4 =
tr("It may be necessary to skip some tracks in your prepared "
"playlist or add some different tracks in order to maintain the "
"energy of your audience.");
QString createPlaylistLink = tr("Create New Playlist");
QString html;
html.append(QStringLiteral("<h2>%1</h2>").arg(playlistsTitle));
html.append(QStringLiteral("<p>%1</p>").arg(playlistsSummary));
html.append(QStringLiteral("<p>%1</p>").arg(playlistsSummary2));
html.append(QStringLiteral("<p>%1<br>%2</p>").arg(playlistsSummary3, playlistsSummary4));
html.append(QStringLiteral("<a style=\"color:#0496FF;\" href=\"create\">%1</a>")
.arg(createPlaylistLink));
return html;
}