Skip to content

Commit 2095280

Browse files
committed
Crates/DragDrop: Implement dropping crates and folders onto the tree view
1 parent d64cda2 commit 2095280

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

src/library/trackset/crate/cratefeature.cpp

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,25 @@ void CrateFeature::updateTreeItemForCrateSummary(
276276
pTreeItem->setUrl(CrateURLs::toUrl(crateSummary.getId()));
277277
}
278278

279+
bool CrateFeature::dropAccept(const QList<QUrl>& urls, QObject* pSource) {
280+
Q_UNUSED(pSource);
281+
QList<CrateId> crateIds = CrateURLs::parseCrateUrls(urls);
282+
if (crateIds.isEmpty()) {
283+
return false;
284+
}
285+
return moveToParent(CrateId(), crateIds);
286+
}
287+
279288
bool CrateFeature::dropAcceptChild(
280289
const QModelIndex& index, const QList<QUrl>& urls, QObject* pSource) {
281-
CrateId crateId(crateIdFromIndex(index));
282-
VERIFY_OR_DEBUG_ASSERT(crateId.isValid()) {
290+
CrateId targetCrateId(crateIdFromIndex(index));
291+
VERIFY_OR_DEBUG_ASSERT(targetCrateId.isValid()) {
283292
return false;
284293
}
294+
295+
bool movedTracks = false;
296+
bool movedCrates = false;
297+
285298
// If a track is dropped onto a crate's name, but the track isn't in the
286299
// library, then add the track to the library before adding it to the
287300
// playlist.
@@ -292,12 +305,22 @@ bool CrateFeature::dropAcceptChild(
292305
DragAndDropHelper::supportedTracksFromUrls(urls, false, true);
293306
const QList<TrackId> trackIds =
294307
m_pLibrary->trackCollectionManager()->resolveTrackIds(fileInfos, pSource);
295-
if (trackIds.isEmpty()) {
296-
return false;
308+
if (!trackIds.isEmpty()) {
309+
m_pTrackCollection->addCrateTracks(targetCrateId, trackIds);
310+
movedTracks = true;
297311
}
298312

299-
m_pTrackCollection->addCrateTracks(crateId, trackIds);
300-
return true;
313+
const QList<CrateId> crateIds = CrateURLs::parseCrateUrls(urls);
314+
if (!crateIds.isEmpty()) {
315+
moveToParent(targetCrateId, crateIds);
316+
movedCrates = true;
317+
}
318+
319+
return movedTracks || movedCrates;
320+
}
321+
322+
bool CrateFeature::dragMoveAccept(const QList<QUrl>& urls) {
323+
return !CrateURLs::parseCrateUrls(urls).isEmpty();
301324
}
302325

303326
bool CrateFeature::dragMoveAcceptChild(const QModelIndex& index, const QList<QUrl>& urls) {
@@ -310,7 +333,8 @@ bool CrateFeature::dragMoveAcceptChild(const QModelIndex& index, const QList<QUr
310333
crate.isLocked()) {
311334
return false;
312335
}
313-
return DragAndDropHelper::urlsContainSupportedTrackFiles(urls, true);
336+
return DragAndDropHelper::urlsContainSupportedTrackFiles(urls, true) ||
337+
!CrateURLs::parseCrateUrls(urls).isEmpty();
314338
}
315339

316340
void CrateFeature::bindLibraryWidget(
@@ -490,6 +514,32 @@ void CrateFeature::createNewCrate(CrateId parentId, bool selectAfterCreation) {
490514
}
491515
}
492516

517+
bool CrateFeature::moveToParent(CrateId destinationId, const QList<CrateId>& cratesToMove) {
518+
// Note: An "invalid"/NULL destination is not actually invalid
519+
// for this function, but instead represents the root folder.
520+
bool success = false;
521+
for (CrateId crateToMoveId : cratesToMove) {
522+
success |= moveToParent(destinationId, crateToMoveId, false);
523+
}
524+
return success;
525+
}
526+
527+
bool CrateFeature::moveToParent(CrateId destinationId, CrateId crateToMoveId, bool selectAfterMove) {
528+
// Note: An "invalid"/NULL destination is not actually invalid
529+
// for this function, but instead represents the root folder.
530+
Crate crate;
531+
if (m_pTrackCollection->crates().readCrateById(crateToMoveId, &crate)) {
532+
crate.setParentId(destinationId);
533+
const bool success = m_pTrackCollection->updateCrate(crate);
534+
if (success && selectAfterMove) {
535+
// Scroll to new location of the selected crate/folder
536+
m_pSidebarWidget->selectChildIndex(indexFromCrateId(crateToMoveId), false);
537+
}
538+
return success;
539+
}
540+
return false;
541+
}
542+
493543
void CrateFeature::deleteItem(const QModelIndex& index) {
494544
m_lastRightClickedIndex = index;
495545
slotDeleteCrate();

src/library/trackset/crate/cratefeature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ class CrateFeature : public BaseTrackSetFeature {
3030

3131
QVariant title() override;
3232

33+
bool dropAccept(const QList<QUrl>& urls, QObject* pSource) override;
3334
bool dropAcceptChild(const QModelIndex& index,
3435
const QList<QUrl>& urls,
3536
QObject* pSource) override;
37+
bool dragMoveAccept(const QList<QUrl>& url) override;
3638
bool dragMoveAcceptChild(const QModelIndex& index, const QList<QUrl>& urls) override;
3739

3840
void bindLibraryWidget(WLibrary* libraryWidget,
@@ -107,6 +109,8 @@ class CrateFeature : public BaseTrackSetFeature {
107109

108110
// TreeItem actions
109111
void createNewCrate(CrateId parentId, bool selectAfterCreation);
112+
bool moveToParent(CrateId destinationId, CrateId itemToMoveId, bool selectAfterMove);
113+
bool moveToParent(CrateId destinationId, const QList<CrateId>& itemsToMove);
110114

111115
QString formatRootViewHtml() const;
112116

0 commit comments

Comments
 (0)