Skip to content

Commit

Permalink
History: show track count and duration in sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Feb 11, 2024
1 parent 40be3fe commit 65c355b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/library/trackset/playlistfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ QList<BasePlaylistFeature::IdAndLabel> PlaylistFeature::createPlaylistLabels() {
" ON PlaylistTracks.playlist_id = Playlists.id "
"LEFT JOIN library "
" ON PlaylistTracks.track_id = library.id "
" WHERE Playlists.hidden = 0 "
" GROUP BY Playlists.id");
" WHERE Playlists.hidden = %1 "
" GROUP BY Playlists.id")
.arg(QString::number(
PlaylistDAO::HiddenType::PLHT_NOT_HIDDEN));
queryString.append(
mixxx::DbConnection::collateLexicographically(
" ORDER BY sort_name"));
Expand Down
68 changes: 59 additions & 9 deletions src/library/trackset/setlogfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

namespace {
constexpr int kNumToplevelHistoryEntries = 5;

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));
}

} // namespace

using namespace mixxx::library::prefs;
Expand Down Expand Up @@ -215,12 +227,40 @@ void SetlogFeature::onRightClickChild(const QPoint& globalPos, const QModelIndex
QModelIndex SetlogFeature::constructChildModel(int selectedId) {
// qDebug() << "SetlogFeature::constructChildModel() id:" << selectedId;
// Setup the sidebar playlist model
QSqlTableModel playlistTableModel(this,
m_pLibrary->trackCollectionManager()->internalCollection()->database());
playlistTableModel.setTable("Playlists");
playlistTableModel.setFilter("hidden=" + QString::number(PlaylistDAO::PLHT_SET_LOG));
playlistTableModel.setSort(
playlistTableModel.fieldIndex("id"), Qt::DescendingOrder);
QSqlDatabase database =
m_pLibrary->trackCollectionManager()->internalCollection()->database();

QString queryString = QStringLiteral(
"CREATE TEMPORARY VIEW IF NOT EXISTS SetlogCountsDurations "
"AS SELECT "
" Playlists.id AS id, "
" Playlists.name AS name, "
" Playlists.date_created AS date_created, "
" 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 = %1 "
" GROUP BY Playlists.id")
.arg(QString::number(PlaylistDAO::HiddenType::PLHT_SET_LOG));
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("SetlogCountsDurations");
playlistTableModel.setSort(playlistTableModel.fieldIndex("id"), Qt::DescendingOrder);
playlistTableModel.select();
while (playlistTableModel.canFetchMore()) {
playlistTableModel.fetchMore();
Expand All @@ -229,6 +269,8 @@ QModelIndex SetlogFeature::constructChildModel(int selectedId) {
int nameColumn = record.indexOf("name");
int idColumn = record.indexOf("id");
int createdColumn = record.indexOf("date_created");
int countColumn = record.indexOf("count");
int durationColumn = record.indexOf("durationSeconds");

// Nice to have: restore previous expanded/collapsed state of YEAR items
clearChildModel();
Expand All @@ -250,8 +292,16 @@ QModelIndex SetlogFeature::constructChildModel(int selectedId) {
playlistTableModel
.data(playlistTableModel.index(row, createdColumn))
.toDateTime();
int count = playlistTableModel
.data(playlistTableModel.index(row, countColumn))
.toInt();
int duration =
playlistTableModel
.data(playlistTableModel.index(row, durationColumn))
.toInt();
QString label = createPlaylistLabel(name, count, duration);

// Create the TreeItem whose parent is the invisible root item
// Create the TreeItem whose parent is the invisible root item.
// Show only [kNumToplevelHistoryEntries] recent playlists at the top level
// before grouping them by year.
if (row >= kNumToplevelHistoryEntries) {
Expand All @@ -273,12 +323,12 @@ QModelIndex SetlogFeature::constructChildModel(int selectedId) {
itemList.push_back(std::move(pNewGroupItem));
}

TreeItem* pItem = pGroupItem->appendChild(name, id);
TreeItem* pItem = pGroupItem->appendChild(label, id);
pItem->setBold(m_playlistIdsOfSelectedTrack.contains(id));
decorateChild(pItem, id);
} else {
// add most recent top-level playlist
auto pItem = std::make_unique<TreeItem>(name, id);
auto pItem = std::make_unique<TreeItem>(label, id);
pItem->setBold(m_playlistIdsOfSelectedTrack.contains(id));
decorateChild(pItem.get(), id);

Expand Down

0 comments on commit 65c355b

Please sign in to comment.