Skip to content

Commit

Permalink
Merge branch '2.2' of [email protected]:mixxxdj/mixxx.git
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/library/baseplaylistfeature.cpp
#	src/library/playlistfeature.cpp
  • Loading branch information
uklotzde committed Jul 31, 2019
2 parents 6ae9e79 + 0295027 commit 8deb742
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 77 deletions.
90 changes: 42 additions & 48 deletions src/library/baseplaylistfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,28 +618,27 @@ void BasePlaylistFeature::htmlLinkClicked(const QUrl& link) {
* This method queries the database and does dynamic insertion
*/
QModelIndex BasePlaylistFeature::constructChildModel(int selected_id) {
buildPlaylistList();
QList<TreeItem*> data_list;
int selected_row = -1;

int row = 0;
for (auto it = m_playlistList.constBegin();
it != m_playlistList.constEnd(); ++it, ++row) {
int playlist_id = it->first;
QString playlist_name = it->second;
for (const IdAndLabel& idAndLabel : createPlaylistLabels()) {
int playlistId = idAndLabel.id;
QString playlistLabel = idAndLabel.label;

if (selected_id == playlist_id) {
if (selected_id == playlistId) {
// save index for selection
selected_row = row;
m_childModel.index(selected_row, 0);
}

// Create the TreeItem whose parent is the invisible root item
TreeItem* item = new TreeItem(this, playlist_name, playlist_id);
item->setBold(m_playlistsSelectedTrackIsIn.contains(playlist_id));
TreeItem* item = new TreeItem(this, playlistLabel, playlistId);
item->setBold(m_playlistsSelectedTrackIsIn.contains(playlistId));

decorateChild(item, playlist_id);
decorateChild(item, playlistId);
data_list.append(item);

++row;
}

// Append all the newly created TreeItems in a dynamic way to the childmodel
Expand All @@ -650,22 +649,20 @@ QModelIndex BasePlaylistFeature::constructChildModel(int selected_id) {
return m_childModel.index(selected_row, 0);
}

void BasePlaylistFeature::updateChildModel(int selected_id) {
buildPlaylistList();
void BasePlaylistFeature::updateChildModel(int playlistId) {
QString playlistLabel = fetchPlaylistLabel(playlistId);

int row = 0;
for (auto it = m_playlistList.constBegin();
it != m_playlistList.constEnd(); ++it, ++row) {
int playlist_id = it->first;
QString playlist_name = it->second;

if (selected_id == playlist_id) {
TreeItem* item = m_childModel.getItem(indexFromPlaylistId(playlist_id));
item->setLabel(playlist_name);
item->setData(playlist_id);
decorateChild(item, playlist_id);
}
QVariant variantId = QVariant(playlistId);

for (int row = 0; row < m_childModel.rowCount(); ++row) {
QModelIndex index = m_childModel.index(row, 0);
TreeItem* pTreeItem = m_childModel.getItem(index);
DEBUG_ASSERT(pTreeItem != nullptr);
if (!pTreeItem->hasChildren() && // leaf node
pTreeItem->getData() == variantId) {
pTreeItem->setLabel(playlistLabel);
decorateChild(pTreeItem, playlistId);
}
}
}

Expand All @@ -675,18 +672,18 @@ void BasePlaylistFeature::updateChildModel(int selected_id) {
* Clears the child model dynamically, but the invisible root item remains
*/
void BasePlaylistFeature::clearChildModel() {
m_childModel.removeRows(0, m_playlistList.size());
m_childModel.removeRows(0, m_childModel.rowCount());
}

QModelIndex BasePlaylistFeature::indexFromPlaylistId(int playlistId) {
int row = 0;
for (auto it = m_playlistList.constBegin();
it != m_playlistList.constEnd(); ++it, ++row) {
int current_id = it->first;
QString playlist_name = it->second;

if (playlistId == current_id) {
return m_childModel.index(row, 0);
QVariant variantId = QVariant(playlistId);
for (int row = 0; row < m_childModel.rowCount(); ++row) {
QModelIndex index = m_childModel.index(row, 0);
TreeItem* pTreeItem = m_childModel.getItem(index);
DEBUG_ASSERT(pTreeItem != nullptr);
if (!pTreeItem->hasChildren() && // leaf node
pTreeItem->getData() == variantId) {
return index;
}
}
return QModelIndex();
Expand All @@ -700,24 +697,21 @@ void BasePlaylistFeature::slotTrackSelected(TrackPointer pTrack) {
}
m_playlistDao.getPlaylistsTrackIsIn(trackId, &m_playlistsSelectedTrackIsIn);

TreeItem* rootItem = m_childModel.getRootItem();
if (rootItem == nullptr) {
return;
}

// Set all playlists the track is in bold (or if there is no track selected,
// clear all the bolding).
int row = 0;
for (auto it = m_playlistList.constBegin();
it != m_playlistList.constEnd(); ++it, ++row) {
TreeItem* playlist = rootItem->child(row);
if (playlist == nullptr) {
continue;
for (int row = 0; row < m_childModel.rowCount(); ++row) {
QModelIndex index = m_childModel.index(row, 0);
TreeItem* pTreeItem = m_childModel.getItem(index);
DEBUG_ASSERT(pTreeItem != nullptr);
if (!pTreeItem->hasChildren()) { // leaf node
bool ok;
int playlistId = pTreeItem->getData().toInt(&ok);
VERIFY_OR_DEBUG_ASSERT(ok) {
continue;
}
bool shouldBold = m_playlistsSelectedTrackIsIn.contains(playlistId);
pTreeItem->setBold(shouldBold);
}

int playlistId = it->first;
bool shouldBold = m_playlistsSelectedTrackIsIn.contains(playlistId);
playlist->setBold(shouldBold);
}

m_childModel.triggerRepaint();
Expand Down
13 changes: 10 additions & 3 deletions src/library/baseplaylistfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ class BasePlaylistFeature : public LibraryFeature {
void slotAnalyzePlaylist();

protected:

struct IdAndLabel {
int id;
QString label;
};


virtual QModelIndex constructChildModel(int selected_id);
virtual void updateChildModel(int selected_id);
virtual void clearChildModel();
virtual void buildPlaylistList() = 0;
virtual void decorateChild(TreeItem *pChild, int playlist_id) = 0;
virtual QList<IdAndLabel> createPlaylistLabels() = 0;
virtual QString fetchPlaylistLabel(int playlistId) = 0;
virtual void decorateChild(TreeItem *pChild, int playlistId) = 0;
virtual void addToAutoDJ(bool bTop);

int playlistIdFromIndex(QModelIndex index);
Expand All @@ -95,7 +103,6 @@ class BasePlaylistFeature : public LibraryFeature {
QAction *m_pExportTrackFilesAction;
QAction *m_pDuplicatePlaylistAction;
QAction *m_pAnalyzePlaylistAction;
QList<QPair<int, QString> > m_playlistList;
QModelIndex m_lastRightClickedIndex;
TreeItemModel m_childModel;
TrackPointer m_pSelectedTrack;
Expand Down
66 changes: 54 additions & 12 deletions src/library/playlistfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
#include "util/dnd.h"
#include "util/duration.h"

namespace {

QString createPlaylistLabel(
QString name,
int count,
int duration) {
return QString("%1 (%2) %3").arg(name, QString::number(count),
mixxx::Duration::formatSeconds(duration));
}

} // anonymous namespace


PlaylistFeature::PlaylistFeature(QObject* parent,
TrackCollection* pTrackCollection,
UserSettingsPointer pConfig)
Expand Down Expand Up @@ -125,9 +138,8 @@ bool PlaylistFeature::dragMoveAcceptChild(const QModelIndex& index, QUrl url) {
return !locked && formatSupported;
}

void PlaylistFeature::buildPlaylistList() {
m_playlistList.clear();

QList<BasePlaylistFeature::IdAndLabel> PlaylistFeature::createPlaylistLabels() {
QList<BasePlaylistFeature::IdAndLabel> playlistLabels;
QString queryString = QString(
"CREATE TEMPORARY VIEW IF NOT EXISTS PlaylistsCountsDurations "
"AS SELECT "
Expand Down Expand Up @@ -163,21 +175,51 @@ void PlaylistFeature::buildPlaylistList() {

for (int row = 0; row < playlistTableModel.rowCount(); ++row) {
int id = playlistTableModel.data(
playlistTableModel.index(row, idColumn)).toInt();
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
QSqlTableModel playlistTableModel(this, m_pTrackCollection->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(row, nameColumn)).toString();
playlistTableModel.index(0, nameColumn)).toString();
int count = playlistTableModel.data(
playlistTableModel.index(row, countColumn)).toInt();
playlistTableModel.index(0, countColumn)).toInt();
int duration = playlistTableModel.data(
playlistTableModel.index(row, durationColumn)).toInt();
m_playlistList.append(qMakePair(id, QString("%1 (%2) %3")
.arg(name, QString::number(count),
mixxx::Duration::formatTime(duration, mixxx::Duration::Precision::SECONDS))));
playlistTableModel.index(0, durationColumn)).toInt();
return createPlaylistLabel(name, count, duration);
}
return QString();
}

void PlaylistFeature::decorateChild(TreeItem* item, int playlist_id) {
if (m_playlistDao.isPlaylistLocked(playlist_id)) {
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());
Expand Down
5 changes: 3 additions & 2 deletions src/library/playlistfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class PlaylistFeature : public BasePlaylistFeature {
void slotPlaylistTableRenamed(int playlistId, QString a_strName);

protected:
void buildPlaylistList();
void decorateChild(TreeItem *pChild, int playlist_id);
QList<BasePlaylistFeature::IdAndLabel> createPlaylistLabels() override;
QString fetchPlaylistLabel(int playlistId) override;
void decorateChild(TreeItem *pChild, int playlist_id) override;

private:
QString getRootViewHtml() const;
Expand Down
40 changes: 32 additions & 8 deletions src/library/setlogfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SetlogFeature::SetlogFeature(QObject* parent,
connect(m_pGetNewPlaylist, SIGNAL(triggered()), this, SLOT(slotGetNewPlaylist()));

// initialized in a new generic slot(get new history playlist purpose)
emit(slotGetNewPlaylist());
slotGetNewPlaylist();
}

SetlogFeature::~SetlogFeature() {
Expand Down Expand Up @@ -119,9 +119,8 @@ void SetlogFeature::onRightClickChild(const QPoint& globalPos, QModelIndex index
menu.exec(globalPos);
}


void SetlogFeature::buildPlaylistList() {
m_playlistList.clear();
QList<BasePlaylistFeature::IdAndLabel> SetlogFeature::createPlaylistLabels() {
QList<BasePlaylistFeature::IdAndLabel> playlistLabels;
// Setup the sidebar playlist model
QSqlTableModel playlistTableModel(this, m_pTrackCollection->database());
playlistTableModel.setTable("Playlists");
Expand All @@ -141,14 +140,39 @@ void SetlogFeature::buildPlaylistList() {
playlistTableModel.index(row, idColumn)).toInt();
QString name = playlistTableModel.data(
playlistTableModel.index(row, nameColumn)).toString();
m_playlistList.append(qMakePair(id, name));
BasePlaylistFeature::IdAndLabel idAndLabel;
idAndLabel.id = id;
idAndLabel.label = name;
playlistLabels.append(idAndLabel);
}
return playlistLabels;
}

QString SetlogFeature::fetchPlaylistLabel(int playlistId) {
// Setup the sidebar playlist model
QSqlTableModel playlistTableModel(this, m_pTrackCollection->database());
playlistTableModel.setTable("Playlists");
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");

DEBUG_ASSERT(playlistTableModel.rowCount() <= 1);
if (playlistTableModel.rowCount() > 0) {
return playlistTableModel.data(
playlistTableModel.index(0, nameColumn)).toString();
}
return QString();
}

void SetlogFeature::decorateChild(TreeItem* item, int playlist_id) {
if (playlist_id == m_playlistId) {
void SetlogFeature::decorateChild(TreeItem* item, int playlistId) {
if (playlistId == m_playlistId) {
item->setIcon(QIcon(":/images/library/ic_library_history_current.svg"));
} else if (m_playlistDao.isPlaylistLocked(playlist_id)) {
} else if (m_playlistDao.isPlaylistLocked(playlistId)) {
item->setIcon(QIcon(":/images/library/ic_library_locked.svg"));
} else {
item->setIcon(QIcon());
Expand Down
5 changes: 3 additions & 2 deletions src/library/setlogfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class SetlogFeature : public BasePlaylistFeature {
void slotGetNewPlaylist();

protected:
void buildPlaylistList();
void decorateChild(TreeItem *pChild, int playlist_id);
QList<BasePlaylistFeature::IdAndLabel> createPlaylistLabels() override;
QString fetchPlaylistLabel(int playlistId) override;
void decorateChild(TreeItem *pChild, int playlistId) override;

private slots:
void slotPlayingTrackChanged(TrackPointer currentPlayingTrack);
Expand Down
4 changes: 3 additions & 1 deletion src/library/treeitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ int TreeItem::parentRow() const {

TreeItem* TreeItem::child(int row) const {
DEBUG_ASSERT(row >= 0);
DEBUG_ASSERT(row < m_children.size());
VERIFY_OR_DEBUG_ASSERT(row < m_children.size()) {
return nullptr;
}
return m_children[row];
}

Expand Down
5 changes: 4 additions & 1 deletion src/library/treeitemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ TreeItem* TreeItemModel::setRootItem(std::unique_ptr<TreeItem> pRootItem) {
* Before you can resize the data model dynamically by using 'insertRows' and 'removeRows'
* make sure you have initialized
*/
void TreeItemModel::insertTreeItemRows(QList<TreeItem*>& rows, int position, const QModelIndex& parent) {
void TreeItemModel::insertTreeItemRows(
QList<TreeItem*>& rows,
int position,
const QModelIndex& parent) {
if (rows.isEmpty()) {
return;
}
Expand Down

0 comments on commit 8deb742

Please sign in to comment.